diff --git a/src/hocon_pp.erl b/src/hocon_pp.erl index 290ee3e..dfb9ec6 100644 --- a/src/hocon_pp.erl +++ b/src/hocon_pp.erl @@ -112,7 +112,17 @@ 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(Value, Opts) -> + throw(#{ + reason => unsupported_value, + value => Value, + options => Opts + }). gen_list(L, Opts) -> case is_oneliner(L) of diff --git a/test/hocon_pp_tests.erl b/test/hocon_pp_tests.erl index 202f3c9..18b2466 100644 --- a/test/hocon_pp_tests.erl +++ b/test/hocon_pp_tests.erl @@ -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 + ).