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

Fix error handler for maps and implemented converter of list fromatted configuration to record #120

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 67 additions & 1 deletion src/jsx_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
-module(jsx_config).

-export([parse_config/1]).
-export([config_to_list/1]).
-export([config_to_list/1, list_to_config/1]).
-export([extract_config/1, valid_flags/0]).

-ifdef(TEST).
Expand Down Expand Up @@ -115,6 +115,45 @@ parse_strict(_Strict, _Rest, _Config) ->
erlang:error(badarg).


-spec list_to_config(Config::proplists:proplist()) -> config().

list_to_config(Config) ->
CL=lists:foldl(
fun(Key, Acc) when Key==error_handler orelse
Key==incomplete_handler ->
case lists:keyfind(Key, 1, Config) of
false ->
[false|Acc];
{_, Val} ->
[Val|Acc]
end;
(StrictKey, Acc) when StrictKey==strict_comments orelse
StrictKey==strict_utf8 orelse
StrictKey==strict_single_quotes orelse
StrictKey==strict_commas orelse
StrictKey==strict_control_codes orelse
StrictKey==strict_escapes ->
Flag1=lists:member(StrictKey, Config) orelse
lists:member(strict, Config),
if Flag1 -> [ Flag1 |Acc];
true ->
case lists:keyfind(strict,1,Config) of
false ->
[ false | Acc ];
{_, StrictList } ->
[ lists:member(short_flag(StrictKey),StrictList) | Acc ]
end
end;
(Key, Acc) -> [lists:member(Key, Config)|Acc]
end, [config], record_info(fields, config)),
list_to_tuple(lists:reverse(CL)).

short_flag(strict_comments) -> comments;
short_flag(strict_utf8) -> utf8;
short_flag(strict_single_quotes) -> single_quotes;
short_flag(strict_commas) -> commas;
short_flag(strict_control_codes) -> control_codes;
short_flag(strict_escapes) -> escapes.

-spec config_to_list(Config::config()) -> proplists:proplist().

Expand Down Expand Up @@ -339,6 +378,33 @@ config_to_list_test_() ->
)}
].

repack_params_test_() ->
C1=#config{
strict_comments = true,
strict_utf8 = true,
strict_escapes = true,
strict_control_codes = true,
error_handler = fun fake_error_handler/3
},
C2=#config{escaped_strings = true, %will be strict flag
strict_comments=true,
strict_commas=true,
strict_utf8=true,
strict_single_quotes=true,
strict_escapes=true,
strict_control_codes=true,
error_handler = fun fake_error_handler/3
},
C1L=jsx_config:config_to_list(C1),
C2L=jsx_config:config_to_list(C2),
[
?_assertMatch(true,lists:member(strict,C2L)),
?_assertMatch({_,_},lists:keyfind(strict,1,C1L)),
{"With all strict",
?_assertEqual(C1,jsx_config:list_to_config(C1L))},
{"Partially strict",
?_assertEqual(C2,jsx_config:list_to_config(C2L))}
].

fake_error_handler(_, _, _) -> ok.

Expand Down
41 changes: 40 additions & 1 deletion src/jsx_parser.erl
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ when is_atom(Key); is_binary(Key); is_integer(Key) ->
Config
)
catch error:badarg ->
?error(object, [{string, Key}|Tokens], Handler, Stack, Config)
?error(object, [{key, Key}|Tokens], Handler, [object|Stack], Config)
end;
object([], Handler, Stack, Config) ->
incomplete(object, Handler, Stack, Config);
Expand Down Expand Up @@ -1210,5 +1210,44 @@ rogue_tuple_test_() ->
)}
].

bad_string_encoding_test_() ->
B64Encoder=fun(
[{Type, Str}|Tokens],
{parser, State, Handler, Stack}, Conf) when
Type==key orelse Type==string ->
Conf1=jsx_config:list_to_config(Conf),
resume(
[{Type, base64:encode(Str)}|Tokens],
State, Handler, Stack, Conf1)
end,
Config=[strict, {error_handler, B64Encoder}],
[
{"map string encoder",
?_assertEqual(
[start_object,
{key,<<"AAAAAE9Yn0hn29V+nKn4CA==">>},
{string,<<"AQEBAdf57B7ihdvsUCSUHA==">>},
end_object,end_json],
parse([start_object,
<<0,0,0,0,79,88,159,72,103,219,213,126,156,169,248,8>>,
<<1,1,1,1,215,249,236,30,226,133,219,236,80,36,148,28>>,
end_object,end_json],
Config
)
)},
{"array string encoder",
?_assertEqual(
[start_array,
{string,<<"DMF1ucDxtqgxw5niaXcmYQ==">>},
{string,<<"kutf/uauL+w61xx3dTFXjw==">>},
end_array,end_json],
parse([start_array,
<<12,193,117,185,192,241,182,168,49,195,153,226,105,119,38,97>>,
<<146,235,95,254,230,174,47,236,58,215,28,119,117,49,87,143>>,
end_array,end_json],
Config
)
)}
].

-endif.