Skip to content

Commit

Permalink
fix(decoder): missing string plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
williamthome committed Dec 5, 2023
1 parent b75ab32 commit 80203e0
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions src/euneus_decoder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
-compile({ inline, plugins/3 }).
-compile({ inline, string/6 }).
-compile({ inline, string/7 }).
-compile({ inline, normalize_string/3 }).
-compile({ inline, number/6 }).
-compile({ inline, number_exp_cont/7 }).
-compile({ inline, number_exp_sign/6 }).
Expand Down Expand Up @@ -478,35 +479,9 @@ value(Data, Opts, Input, Pos, Buffer) ->
string(Data, Opts, Input, Pos, Buffer, Len) ->
case Data of
<<$"/integer, Rest/bitstring>> ->
Last = binary_part(Input, Pos, Len),
Value =
case Buffer of
[?key | _] ->
case plugins(Opts#opts.plugins, Last, Opts) of
next ->
case Opts of
#opts{keys = undefined} ->
Last;
#opts{keys = Normalize} ->
Normalize(Last, Opts)
end;
{halt, Term} ->
Term
end;
[_|_] ->
case plugins(Opts#opts.plugins, Last, Opts) of
next ->
case Opts of
#opts{values = undefined} ->
Last;
#opts{values = Normalize} ->
Normalize(Last, Opts)
end;
{halt, Term} ->
Term
end
end,
continue(Rest, Opts, Input, Pos + Len + 1, Buffer, Value);
String0 = binary_part(Input, Pos, Len),
String = normalize_string(String0, Opts, Buffer),
continue(Rest, Opts, Input, Pos + Len + 1, Buffer, String);
<<$\\/integer, Rest/bitstring>> ->
Part = binary_part(Input, Pos, Len),
escape(Rest, Opts, Input, Pos + Len, Buffer, Part);
Expand All @@ -530,7 +505,8 @@ string(Data, Opts, Input, Pos, Buffer, Acc, Len) ->
case Data of
<<$"/integer, Rest/bitstring>> ->
Last = binary_part(Input, Pos, Len),
String = iolist_to_binary([Acc | Last]),
String0 = iolist_to_binary([Acc | Last]),
String = normalize_string(String0, Opts, Buffer),
continue(Rest, Opts, Input, Pos + Len + 1, Buffer, String);
<<$\\/integer, Rest/bitstring>> ->
Part = binary_part(Input, Pos, Len),
Expand All @@ -551,6 +527,29 @@ string(Data, Opts, Input, Pos, Buffer, Acc, Len) ->
throw_eof(Opts, Input, Pos, Buffer)
end.

normalize_string(String, Opts, Buffer) ->
case plugins(Opts#opts.plugins, String, Opts) of
next ->
case Buffer of
[?key | _] ->
case Opts of
#opts{keys = undefined} ->
String;
#opts{keys = Normalize} ->
Normalize(String, Opts)
end;
[_|_] ->
case Opts of
#opts{values = undefined} ->
String;
#opts{values = Normalize} ->
Normalize(String, Opts)
end
end;
{halt, Term} ->
Term
end.

number(Data, Opts, Input, Pos, Buffer, Len) ->
case Data of
<<$0/integer, Rest/bitstring>> ->
Expand Down Expand Up @@ -1953,6 +1952,18 @@ decode_test() ->
, <<"{\"foo\": \"1\"}">>
, #{ keys => to_atom, values => to_integer }
},
{ {ok, #{keya => <<"valuea">>}}
, <<"{\"key\\u0061\": \"value\\u0061\"}">>
, #{keys => to_atom}
},
{ {ok, #{<<"keya">> => valuea}}
, <<"{\"key\\u0061\": \"value\\u0061\"}">>
, #{values => to_atom}
},
{ {ok, #{keya => valuea}}
, <<"{\"key\\u0061\": \"value\\u0061\"}">>
, #{values => to_atom, keys => to_atom}
},
{{error, not_an_iodata}, error, #{}}
]].

Expand Down

0 comments on commit 80203e0

Please sign in to comment.