From 362f0b4aa9d982a33dcb3e7d8ced80ec755b64a1 Mon Sep 17 00:00:00 2001 From: cleverfox Date: Mon, 23 Jul 2018 16:29:35 +0300 Subject: [PATCH] Fix error handler and parser of list config to record --- src/jsx_config.erl | 68 +++++++++++++++++++++++++++++++++++++++++++++- src/jsx_parser.erl | 41 +++++++++++++++++++++++++++- 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/src/jsx_config.erl b/src/jsx_config.erl index 47cbcf72..0673df62 100644 --- a/src/jsx_config.erl +++ b/src/jsx_config.erl @@ -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). @@ -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(). @@ -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. diff --git a/src/jsx_parser.erl b/src/jsx_parser.erl index ca341c0e..3824200e 100644 --- a/src/jsx_parser.erl +++ b/src/jsx_parser.erl @@ -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); @@ -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.