Skip to content

Commit

Permalink
feat: pp supports wrapped value
Browse files Browse the repository at this point in the history
  • Loading branch information
lafirest committed Dec 13, 2023
1 parent 30e3990 commit 1143017
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/hocon_pp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ gen(M, Opts) when is_map(M) ->
true -> "";
false -> ?NL
end,
[gen_map(M, Opts), NL].
[gen_map(M, Opts), NL];
gen(F, #{lazy_evaluator := Evaluator} = Opts) when is_function(F, 0) ->
%% a lazy value, e.g. secret data
Value = Evaluator(F),
gen(Value, Opts).

gen_list(L, Opts) ->
case is_oneliner(L) of
Expand Down
26 changes: 26 additions & 0 deletions test/hocon_pp_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,29 @@ utf8_test() ->
PP1 = hocon_pp:do(Utf81, #{}),
{ok, Conf1} = hocon:binary(PP1),
?assertEqual(Utf81, Conf1).

wrap_value_test() ->
RawConf =
#{
atom_key => #{atom_key => fun() -> atom_value end},
<<"binary_key">> => fun() ->
#{
atom_key1 => <<"binary_value">>,
atom_key2 => fun() -> '42wierd_atom_value' end,
atom_key3 => ''
}
end
},
PP = hocon_pp:do(RawConf, #{lazy_evaluator => fun(F) -> F() end}),
{ok, RawConf2} = hocon:binary(iolist_to_binary(PP)),
?assertEqual(
#{
<<"atom_key">> => #{<<"atom_key">> => <<"atom_value">>},
<<"binary_key">> => #{
<<"atom_key1">> => <<"binary_value">>,
<<"atom_key2">> => <<"42wierd_atom_value">>,
<<"atom_key3">> => <<"">>
}
},
RawConf2
).

0 comments on commit 1143017

Please sign in to comment.