Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed yaml files that uses an empty list being decoded to "<<>>" #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 16 additions & 53 deletions src/nklib_yaml.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@

decode(Term) ->
Fun = fun() ->
Objs = yamerl_constr:string(Term),
parse_decoded_list(Objs, [])
case yamerl:decode(Term, [{map_node_format, map}, str_node_as_binary]) of
[[_|_]=Res|[]] ->
% If the response is a list of a single list, then return the inner list
Res;
Res ->
Res
end
end,
case nklib_util:do_try(Fun) of
{exception, {error, {Error, Trace}}} ->
Expand All @@ -63,57 +68,6 @@ decode(Term) ->
end.


%% @private
parse_decoded_list([], Acc) ->
case Acc of
[List] when is_list(List) ->
List;
[Tuple] ->
[Tuple];
_ ->
lists:reverse(Acc)
end;

parse_decoded_list([Obj|Rest], Acc) ->
case Obj of
[Tuple|_] when is_tuple(Tuple) ->
Obj2 = parse_decoded_obj(Obj, []),
parse_decoded_list(Rest, [Obj2|Acc]);
[List|_] when is_list(List) ->
Obj2 = parse_decoded_list(Obj, []),
parse_decoded_list(Rest, [Obj2|Acc]);
_ when is_list(Obj) ->
parse_decoded_list(Rest, [to_bin(Obj)|Acc]);
_ when is_integer(Obj); is_float(Obj); is_atom(Obj) ->
parse_decoded_list(Rest, [Obj|Acc])
end.



%% @private
parse_decoded_obj([], Acc) ->
maps:from_list(Acc);

parse_decoded_obj([{Key, Val}|Rest], Acc) ->
Val2 = case Val of
[Tuple|_] when is_tuple(Tuple) ->
parse_decoded_obj(Val, []);
[List|_] when is_list(List) ->
parse_decoded_list(Val, []);
String when is_list(String) ->
to_bin(String);
_ ->
Val
end,
parse_decoded_obj(Rest, [{to_bin(Key), Val2}|Acc]).


%% @private
to_bin(List) ->
unicode:characters_to_binary(List).



%% ===================================================================
%% Tests
%% ===================================================================
Expand All @@ -124,6 +78,7 @@ test() ->
test_2(),
test_3(),
test_4(),
test_5(),
ok.


Expand Down Expand Up @@ -237,3 +192,11 @@ test_4() ->
Res1 = decode(Body2),
ok.


test_5() ->
Body = <<"
a: []
b: \"\"
">>,
[#{<<"a">> := [], <<"b">> := <<>>}] = decode(Body),
ok.