Skip to content

Commit

Permalink
Merge pull request #256 from emqx/fix-unicode-map-key
Browse files Browse the repository at this point in the history
fix: bad pp unicode map keys
  • Loading branch information
zhongwencool authored May 26, 2023
2 parents ea29e39 + 8afbabd commit 7d70326
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/hocon_pp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,13 @@ gen_map_field(K, V, Opts, NL) ->

%% maybe quote key
maybe_quote(K) when is_atom(K) -> atom_to_list(K);
maybe_quote(K) ->
case re:run(K, "[^A-Za-z_]") of
nomatch -> K;
_ -> io_lib:format("~0p", [unicode:characters_to_list(K, utf8)])
maybe_quote(K0) ->
case re:run(K0, "[^A-Za-z_]") of
nomatch ->
K0;
_ ->
K1 = unicode:characters_to_list(K0, utf8),
<<"\"", (format_escape_sequences(K1))/binary, "\"">>
end.

bin(IoData) ->
Expand Down
9 changes: 7 additions & 2 deletions test/hocon_pp_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,10 @@ utf8_test() ->
?assertThrow({invalid_utf8, _}, hocon_pp:do(InvalidUtf8, #{})),
Utf8 = #{<<"test">> => <<"测试-专用"/utf8>>},
PP = hocon_pp:do(Utf8, #{}),
{ok, Conf2} = hocon:binary(PP),
?assertEqual(Utf8, Conf2).
{ok, Conf} = hocon:binary(PP),
?assertEqual(Utf8, Conf),
%% support utf8 key
Utf81 = #{<<"测试-test-专用"/utf8>> => <<"测试-专用"/utf8>>},
PP1 = hocon_pp:do(Utf81, #{}),
{ok, Conf1} = hocon:binary(PP1),
?assertEqual(Utf81, Conf1).

0 comments on commit 7d70326

Please sign in to comment.