Skip to content

Commit

Permalink
feat(hocon_schema): add hocon_schema:map_translate/3
Browse files Browse the repository at this point in the history
  • Loading branch information
terry-xiaoyu committed Jun 23, 2021
1 parent b60c66a commit b0a0acb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/hocon_schema.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@

-export([map/2, map/3, map/4]).
-export([translate/3]).
-export([nest/1]).
-export([generate/2, generate/3]).
-export([generate/2, generate/3, map_translate/3]).
-export([check/2, check/3, check_plain/2, check_plain/3, check_plain/4]).
-export([deep_get/2, deep_get/3, deep_get/4, deep_put/3]).
-export([richmap_to_map/1, atom_key_map/1, get_value/2]).
-export([richmap_to_map/1, get_value/2]).
-export([find_struct/2]).

-include("hoconsc.hrl").

-ifdef(TEST).
-export([nest/1]).
-endif.

-export_type([ name/0
, typefunc/0
, translationfunc/0
Expand Down Expand Up @@ -165,9 +168,14 @@ generate(Schema, Conf) ->
generate(Schema, Conf, #{}).

generate(Schema, Conf, Opts) ->
{Mapped, _NewConf} = map_translate(Schema, Conf, Opts),
Mapped.

-spec(map_translate(schema(), hocon:config(), opts()) -> [proplists:property()]).
map_translate(Schema, Conf, Opts) ->
{Mapped, NewConf} = map(Schema, Conf, all, Opts),
Translated = translate(Schema, NewConf, Mapped),
nest(Translated).
{nest(Translated), NewConf}.

%% @private returns a nested proplist with atom keys
-spec(nest([proplists:property()]) -> [proplists:property()]).
Expand Down
3 changes: 3 additions & 0 deletions test/hocon_schema_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ generate_compatibility_test() ->

[{app_foo, C0}] = cuttlefish_generator:map({Translations, Mappings, []}, Conf),
[{app_foo, C1}] = hocon_schema:generate(demo_schema, Hocon),
{[{app_foo, C1}], Conf1} = hocon_schema:map_translate(demo_schema, Hocon, #{}),
?assertMatch(#{<<"foo">> := #{<<"max">> := 2, <<"min">> := 1, <<"setting">> := "val"}},
hocon_schema:richmap_to_map(Conf1)),
?assertEqual(lists:ukeysort(1, C0), lists:ukeysort(1, C1)).

deep_get_test_() ->
Expand Down

0 comments on commit b0a0acb

Please sign in to comment.