Skip to content

Commit

Permalink
Merge pull request #258 from emqx/serializable-options
Browse files Browse the repository at this point in the history
feat: transform atom to binary if serializable=true
  • Loading branch information
zhongwencool authored Jun 10, 2023
2 parents bd46e34 + 5988ee6 commit fad34ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/hocon_tconf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ map_field(Type, Schema, Value0, Opts) ->
eval_builtin_converter(PlainValue, Type, Opts) ->
case is_make_serializable(Opts) of
true ->
ensure_bin_str(PlainValue);
ensure_serializable(PlainValue);
false ->
hocon_schema_builtin:convert(PlainValue, Type)
end.
Expand Down Expand Up @@ -1263,12 +1263,16 @@ eval_arity1_converter(F, V, Opts) ->
obfuscate_sensitive_values(#{obfuscate_sensitive_values := true}) -> true;
obfuscate_sensitive_values(_) -> false.

ensure_bin_str(Value) when is_list(Value) ->
ensure_serializable(undefined) ->
undefined;
ensure_serializable(Boolean) when is_boolean(Boolean) -> Boolean;
ensure_serializable(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8);
ensure_serializable(Value) when is_list(Value) ->
case io_lib:printable_unicode_list(Value) of
true -> unicode:characters_to_binary(Value, utf8);
false -> Value
end;
ensure_bin_str(Value) ->
ensure_serializable(Value) ->
Value.

check_indexed_array(List) ->
Expand Down
13 changes: 12 additions & 1 deletion test/hocon_tconf_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ default_value_test() ->
}
},
Res
),
%% check foo and dummy is binary.
?assertEqual(
#{
<<"bar">> =>
#{
<<"field1">> => <<"foo">>,
<<"union_with_default">> => <<"dummy">>
}
},
hocon_tconf:make_serializable(?MODULE, Res, #{})
).

obfuscate_sensitive_values_test() ->
Expand Down Expand Up @@ -167,7 +178,7 @@ nest_ref_fill_default_test() ->
#{
<<"perf">> =>
#{
<<"route_lock_type">> => key,
<<"route_lock_type">> => <<"key">>,
<<"trie_compaction">> => true
},
<<"route_batch_clean">> => false
Expand Down

0 comments on commit fad34ad

Please sign in to comment.