Skip to content

Commit

Permalink
fix: compute values only for maps and not when serializing raw config
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesmg committed Jan 22, 2025
1 parent dea6db6 commit 1cf5e0d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/hocon_tconf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ map_one_field_non_hidden(FieldType, FieldSchema, FieldValue0, Opts) ->
{Acc, FieldValue}
end.

maybe_computed(FieldSchema, #{} = CheckedValue, Opts) ->
maybe_computed(_FieldSchema, CheckedValue, #{make_serializable := true}) ->
CheckedValue;
maybe_computed(FieldSchema, #{} = CheckedValue, #{format := map} = Opts) ->
case field_schema(FieldSchema, computed) of
Fn when is_function(Fn, 2) ->
Computed = Fn(CheckedValue, Opts),
Expand Down
23 changes: 23 additions & 0 deletions test/hocon_tconf_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2760,4 +2760,27 @@ computed_fields_test() ->
?assertEqual(0, counters:get(Counter, 2)),
%% Bag is called
?assertEqual(1, counters:get(Counter, 3)),

%% Computed values shouldn't show up in raw configurations to be serialized.
AssertNoComputed = fun
Rec(M) when is_map(M) ->
case is_map_key(?COMPUTED, M) of
true ->
error({should_not_have_computed, M});
false ->
maps:foreach(fun(_K, V) -> Rec(V) end, M)
end;
Rec(Xs) when is_list(Xs) ->
lists:foreach(Rec, Xs);
Rec(_X) ->
ok
end,
Res3 = #{} = hocon_tconf:check_plain(Sc, Data, #{make_serializable => true}),
AssertNoComputed(Res3),
%% Computed values shouldn't show up when handling rich maps
BinaryHocon = hocon_pp:do(Data, #{}),
{ok, RichmapData} = hocon:binary(BinaryHocon, #{format => richmap}),
Res4 = #{} = hocon_tconf:check_plain(Sc, RichmapData, #{format => richmap}),
AssertNoComputed(Res4),

ok.

0 comments on commit 1cf5e0d

Please sign in to comment.