Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compute values only for maps and not when serializing raw config #305

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading