Skip to content

Commit

Permalink
Merge pull request #138 from zhongwencool/master
Browse files Browse the repository at this point in the history
feat(hoconsc): is_type/1 to check if input is a hocon type
  • Loading branch information
zhongwencool authored Sep 17, 2021
2 parents b644461 + 9689b89 commit e660671
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/hoconsc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
-export([ref/1, ref/2]).
-export([array/1, union/1, enum/1]).
-export([lazy/1, map/2]).
-export([is_schema/1]).

-include("hoconsc.hrl").

Expand Down Expand Up @@ -48,10 +49,25 @@ union(OfTypes) when is_list(OfTypes) -> ?UNION(OfTypes).
%% @doc make a enum type.
enum(OfSymbols) when is_list(OfSymbols) -> ?ENUM(OfSymbols).

%% @doc make a lazy type.
lazy(HintType) -> ?LAZY(HintType).

%% @doc make a map type.
map(Name, Type) -> ?MAP(Name, Type).

%% @doc Check Type is a hocon type.
is_schema(?UNION(Members)) -> lists:all(fun is_schema/1, Members);
is_schema(?ARRAY(ElemT)) -> is_schema(ElemT);
is_schema(?LAZY(HintT)) -> is_schema(HintT);
is_schema(?REF(_)) -> true;
is_schema(?R_REF(_, _)) -> true;
is_schema(?ENUM(_)) -> true;
is_schema(?MAP(_, _)) -> true;
is_schema(#{type := _}) -> true;
is_schema(Type) when ?IS_TYPEREFL(Type) -> true;
is_schema(Func) when is_function(Func) -> true;
is_schema(_) -> false.

assert_type(S) when is_function(S) -> error({expecting_type_but_got_schema, S});
assert_type(#{type := _} = S) -> error({expecting_type_but_got_schema, S});
assert_type(?UNION(Members)) -> lists:foreach(fun assert_type/1, Members);
Expand Down

0 comments on commit e660671

Please sign in to comment.