Skip to content

Commit fad34ad

Browse files
authored
Merge pull request #258 from emqx/serializable-options
feat: transform atom to binary if serializable=true
2 parents bd46e34 + 5988ee6 commit fad34ad

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/hocon_tconf.erl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ map_field(Type, Schema, Value0, Opts) ->
657657
eval_builtin_converter(PlainValue, Type, Opts) ->
658658
case is_make_serializable(Opts) of
659659
true ->
660-
ensure_bin_str(PlainValue);
660+
ensure_serializable(PlainValue);
661661
false ->
662662
hocon_schema_builtin:convert(PlainValue, Type)
663663
end.
@@ -1263,12 +1263,16 @@ eval_arity1_converter(F, V, Opts) ->
12631263
obfuscate_sensitive_values(#{obfuscate_sensitive_values := true}) -> true;
12641264
obfuscate_sensitive_values(_) -> false.
12651265

1266-
ensure_bin_str(Value) when is_list(Value) ->
1266+
ensure_serializable(undefined) ->
1267+
undefined;
1268+
ensure_serializable(Boolean) when is_boolean(Boolean) -> Boolean;
1269+
ensure_serializable(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8);
1270+
ensure_serializable(Value) when is_list(Value) ->
12671271
case io_lib:printable_unicode_list(Value) of
12681272
true -> unicode:characters_to_binary(Value, utf8);
12691273
false -> Value
12701274
end;
1271-
ensure_bin_str(Value) ->
1275+
ensure_serializable(Value) ->
12721276
Value.
12731277

12741278
check_indexed_array(List) ->

test/hocon_tconf_tests.erl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ default_value_test() ->
9393
}
9494
},
9595
Res
96+
),
97+
%% check foo and dummy is binary.
98+
?assertEqual(
99+
#{
100+
<<"bar">> =>
101+
#{
102+
<<"field1">> => <<"foo">>,
103+
<<"union_with_default">> => <<"dummy">>
104+
}
105+
},
106+
hocon_tconf:make_serializable(?MODULE, Res, #{})
96107
).
97108

98109
obfuscate_sensitive_values_test() ->
@@ -167,7 +178,7 @@ nest_ref_fill_default_test() ->
167178
#{
168179
<<"perf">> =>
169180
#{
170-
<<"route_lock_type">> => key,
181+
<<"route_lock_type">> => <<"key">>,
171182
<<"trie_compaction">> => true
172183
},
173184
<<"route_batch_clean">> => false

0 commit comments

Comments
 (0)