From ef9412b0c637494af45585208e439280e3a2f160 Mon Sep 17 00:00:00 2001 From: zhongwencool Date: Thu, 16 Sep 2021 15:10:02 +0800 Subject: [PATCH 1/2] feat(hoconsc): is_type/1 to check if input is a hocon type --- src/hoconsc.erl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/hoconsc.erl b/src/hoconsc.erl index e39baaf..8258535 100644 --- a/src/hoconsc.erl +++ b/src/hoconsc.erl @@ -21,6 +21,7 @@ -export([ref/1, ref/2]). -export([array/1, union/1, enum/1]). -export([lazy/1, map/2]). +-export([is_type/1]). -include("hoconsc.hrl"). @@ -48,10 +49,22 @@ 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_type(?UNION(Members)) -> lists:all(fun is_type/1, Members); +is_type(?ARRAY(ElemT)) -> is_type(ElemT); +is_type(?LAZY(HintT)) -> is_type(HintT); +is_type(?REF(_)) -> true; +is_type(?R_REF(_, _)) -> true; +is_type(#{type := _}) -> true; +is_type(Type) when ?IS_TYPEREFL(Type) -> true; +is_type(_) -> 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); From 441cc9e34314489c4876097fc1d8abf6752c3658 Mon Sep 17 00:00:00 2001 From: zhongwencool Date: Fri, 17 Sep 2021 15:31:11 +0800 Subject: [PATCH 2/2] fix: is_type/1 to is_schema/1 --- src/hoconsc.erl | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/hoconsc.erl b/src/hoconsc.erl index 8258535..1dd3b3d 100644 --- a/src/hoconsc.erl +++ b/src/hoconsc.erl @@ -21,7 +21,7 @@ -export([ref/1, ref/2]). -export([array/1, union/1, enum/1]). -export([lazy/1, map/2]). --export([is_type/1]). +-export([is_schema/1]). -include("hoconsc.hrl"). @@ -56,14 +56,17 @@ lazy(HintType) -> ?LAZY(HintType). map(Name, Type) -> ?MAP(Name, Type). %% @doc Check Type is a hocon type. -is_type(?UNION(Members)) -> lists:all(fun is_type/1, Members); -is_type(?ARRAY(ElemT)) -> is_type(ElemT); -is_type(?LAZY(HintT)) -> is_type(HintT); -is_type(?REF(_)) -> true; -is_type(?R_REF(_, _)) -> true; -is_type(#{type := _}) -> true; -is_type(Type) when ?IS_TYPEREFL(Type) -> true; -is_type(_) -> false. +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});