Skip to content

Commit

Permalink
Consider proplist a list of key-value pairs (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamthome authored Sep 7, 2024
1 parent 22ebd51 commit fc02384
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 42 deletions.
13 changes: 4 additions & 9 deletions src/euneus_encoder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -695,19 +695,14 @@ encoder(State) ->
encode_proplist(Proplist, State) ->
continue(proplists:to_map(Proplist), State).

is_proplist([]) ->
false;
is_proplist(List) ->
lists:all(fun is_proplist_prop/1, List).

% Must be the same types handled by key_to_binary/1.
is_proplist_prop({Key, _}) ->
% The key must be of the same type that is handled by key_to_binary/1.
is_proplist([{Key, _} | _]) ->
is_binary(Key) orelse
is_list(Key) orelse
is_atom(Key) orelse
is_integer(Key);
is_proplist_prop(Key) ->
is_atom(Key).
is_proplist(_List) ->
false.

encode_sort_keys_map(Map, #state{sort_keys = true} = State) ->
do_encode_map([
Expand Down
41 changes: 8 additions & 33 deletions test/euneus_encoder_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -225,45 +225,20 @@ sort_keys_test(Config) when is_list(Config) ->
)
].

-if(?OTP_RELEASE >= 26).
proplists_test(Config) when is_list(Config) ->
[
?assertEqual(<<"[]">>, encode([], #{proplists => true})),
?assertEqual(<<"[\"foo\",\"bar\"]">>, encode([foo, bar], #{proplists => true})),
?assertEqual(<<"{\"foo\":\"bar\"}">>, encode([{<<"foo">>, bar}], #{proplists => true})),
?assertEqual(<<"{\"foo\":\"bar\"}">>, encode([{"foo", bar}], #{proplists => true})),
?assertEqual(<<"{\"foo\":\"bar\"}">>, encode([{foo, bar}], #{proplists => true})),
?assertEqual(<<"{\"0\":0}">>, encode([{0, 0}], #{proplists => true})),
?assertEqual(<<"[]">>, encode([], #{proplists => {true, fun(_) -> false end}})),
?assertEqual(
<<"[null,{\"0\":0,\"foo\":\"bar\",\"baz\":true}]">>,
encode([null, [{foo, bar}, baz, {0, 0}]], #{proplists => true})
),
?assertEqual(
<<"[null,{\"foo\":\"bar\"}]">>,
encode([null, [{foo, bar}]], #{
proplists =>
{true, fun
([{_, _}]) -> true;
(_) -> false
end}
})
<<"{\"foo\":\"bar\"}">>,
encode([{foo, bar}], #{proplists => {true, fun([{_, _}]) -> true end}})
)
].
-else.
proplists_test(Config) when is_list(Config) ->
[
?assertEqual(<<"[]">>, encode([], #{proplists => true})),
?assertEqual(
<<"[null,{\"foo\":\"bar\",\"baz\":true,\"0\":0}]">>,
encode([null, [{foo, bar}, baz, {0, 0}]], #{proplists => true})
),
?assertEqual(
<<"[null,{\"foo\":\"bar\"}]">>,
encode([null, [{foo, bar}]], #{
proplists =>
{true, fun
([{_, _}]) -> true;
(_) -> false
end}
})
)
].
-endif.

escape_test(Config) when is_list(Config) ->
?assertEqual(<<"bar">>, encode(foo, #{escape => fun(_) -> <<"bar">> end})).
Expand Down

0 comments on commit fc02384

Please sign in to comment.