strip out checks for repeated keys in generated json. no one cares
about repeated keys
This commit is contained in:
parent
d2a7064e4e
commit
9842a00b4d
5 changed files with 28 additions and 112 deletions
|
@ -27,15 +27,7 @@
|
|||
-export([init/1, handle_event/2]).
|
||||
|
||||
|
||||
-record(config, {
|
||||
repeated_keys = true
|
||||
}).
|
||||
|
||||
-type config() :: [].
|
||||
-export_type([config/0]).
|
||||
|
||||
|
||||
-spec is_json(Source::binary(), Config::config()) -> true | false | {incomplete, jsx:decoder()}.
|
||||
-spec is_json(Source::binary(), Config::jsx_config:config()) -> true | false | {incomplete, jsx:decoder()}.
|
||||
|
||||
is_json(Source, Config) when is_list(Config) ->
|
||||
try (jsx:decoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source)
|
||||
|
@ -43,7 +35,7 @@ is_json(Source, Config) when is_list(Config) ->
|
|||
end.
|
||||
|
||||
|
||||
-spec is_term(Source::any(), Config::config()) -> true | false | {incomplete, jsx:encoder()}.
|
||||
-spec is_term(Source::any(), Config::jsx_config:config()) -> true | false | {incomplete, jsx:encoder()}.
|
||||
|
||||
is_term(Source, Config) when is_list(Config) ->
|
||||
try (jsx:encoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source)
|
||||
|
@ -51,15 +43,15 @@ is_term(Source, Config) when is_list(Config) ->
|
|||
end.
|
||||
|
||||
|
||||
parse_config(Config) -> parse_config(Config, #config{}).
|
||||
parse_config(Config) -> parse_config(Config, []).
|
||||
|
||||
%% ignore deprecated flags
|
||||
parse_config([no_repeated_keys|Rest], Config) ->
|
||||
parse_config(Rest, Config#config{repeated_keys=false});
|
||||
%% deprecated, use `no_repeated_keys`
|
||||
parse_config(Rest, Config);
|
||||
parse_config([{repeated_keys, Val}|Rest], Config) when Val == true; Val == false ->
|
||||
parse_config(Rest, Config#config{repeated_keys=Val});
|
||||
parse_config(Rest, Config);
|
||||
parse_config([repeated_keys|Rest], Config) ->
|
||||
parse_config(Rest, Config#config{repeated_keys=true});
|
||||
parse_config(Rest, Config);
|
||||
parse_config([{K, _}|Rest] = Options, Config) ->
|
||||
case lists:member(K, jsx_config:valid_flags()) of
|
||||
true -> parse_config(Rest, Config);
|
||||
|
@ -73,27 +65,18 @@ parse_config([K|Rest] = Options, Config) ->
|
|||
parse_config([], Config) ->
|
||||
Config.
|
||||
|
||||
-type state() :: {#config{}, any()}.
|
||||
|
||||
%% we don't actually need any state for this
|
||||
-type state() :: [].
|
||||
-spec init(Config::proplists:proplist()) -> state().
|
||||
|
||||
init(Config) -> {parse_config(Config), []}.
|
||||
init(Config) -> parse_config(Config).
|
||||
|
||||
|
||||
-spec handle_event(Event::any(), State::state()) -> state().
|
||||
|
||||
handle_event(end_json, _) -> true;
|
||||
|
||||
handle_event(_, {Config, _} = State) when Config#config.repeated_keys == true -> State;
|
||||
|
||||
handle_event(start_object, {Config, Keys}) -> {Config, [dict:new()] ++ Keys};
|
||||
handle_event(end_object, {Config, [_|Keys]}) -> {Config, Keys};
|
||||
|
||||
handle_event({key, Key}, {Config, [CurrentKeys|Keys]}) ->
|
||||
case dict:is_key(Key, CurrentKeys) of
|
||||
true -> erlang:error(badarg);
|
||||
false -> {Config, [dict:store(Key, blah, CurrentKeys)|Keys]}
|
||||
end;
|
||||
|
||||
handle_event(_, State) -> State.
|
||||
|
||||
|
||||
|
@ -105,15 +88,15 @@ handle_event(_, State) -> State.
|
|||
|
||||
config_test_() ->
|
||||
[
|
||||
{"empty config", ?_assertEqual(#config{}, parse_config([]))},
|
||||
{"no repeat keys", ?_assertEqual(#config{repeated_keys=false}, parse_config([no_repeated_keys]))},
|
||||
{"bare repeated keys", ?_assertEqual(#config{}, parse_config([repeated_keys]))},
|
||||
{"empty config", ?_assertEqual([], parse_config([]))},
|
||||
{"no repeat keys", ?_assertEqual([], parse_config([no_repeated_keys]))},
|
||||
{"bare repeated keys", ?_assertEqual([], parse_config([repeated_keys]))},
|
||||
{"repeated keys true", ?_assertEqual(
|
||||
#config{},
|
||||
[],
|
||||
parse_config([{repeated_keys, true}])
|
||||
)},
|
||||
{"repeated keys false", ?_assertEqual(
|
||||
#config{repeated_keys=false},
|
||||
[],
|
||||
parse_config([{repeated_keys, false}])
|
||||
)},
|
||||
{"invalid opt flag", ?_assertError(badarg, parse_config([error]))},
|
||||
|
@ -121,50 +104,13 @@ config_test_() ->
|
|||
].
|
||||
|
||||
|
||||
repeated_keys_test_() ->
|
||||
RepeatedKey = [
|
||||
start_object,
|
||||
{key, <<"alpha">>},
|
||||
{literal, true},
|
||||
{key, <<"alpha">>},
|
||||
{literal, false},
|
||||
end_object,
|
||||
end_json
|
||||
],
|
||||
NestedKey = [
|
||||
start_object,
|
||||
{key, <<"alpha">>},
|
||||
start_object,
|
||||
{key, <<"alpha">>},
|
||||
start_object,
|
||||
{key, <<"alpha">>},
|
||||
{literal, true},
|
||||
end_object,
|
||||
end_object,
|
||||
end_object,
|
||||
end_json
|
||||
],
|
||||
[
|
||||
{"repeated key", ?_assert(
|
||||
lists:foldl(fun handle_event/2, {#config{}, []}, RepeatedKey)
|
||||
)},
|
||||
{"no repeated key", ?_assertError(
|
||||
badarg,
|
||||
lists:foldl(fun handle_event/2, {#config{repeated_keys=false}, []}, RepeatedKey)
|
||||
)},
|
||||
{"nested key", ?_assert(
|
||||
lists:foldl(fun handle_event/2, {#config{repeated_keys=false}, []}, NestedKey)
|
||||
)}
|
||||
].
|
||||
|
||||
|
||||
handle_event_test_() ->
|
||||
Data = jsx:test_cases() ++ jsx:special_test_cases(),
|
||||
[
|
||||
{
|
||||
Title, ?_assertEqual(
|
||||
true,
|
||||
lists:foldl(fun handle_event/2, {#config{}, []}, Events ++ [end_json])
|
||||
lists:foldl(fun handle_event/2, [], Events ++ [end_json])
|
||||
)
|
||||
} || {Title, _, _, Events} <- Data
|
||||
].
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue