change returned config from record to list in error and incomplete handler
This commit is contained in:
parent
2d7b690a91
commit
df04f091f7
4 changed files with 68 additions and 7 deletions
|
@ -95,7 +95,7 @@ decoder(Handler, State, Config) ->
|
||||||
-define(error(State, Bin, Handler, Acc, Stack, Config),
|
-define(error(State, Bin, Handler, Acc, Stack, Config),
|
||||||
case Config#config.error_handler of
|
case Config#config.error_handler of
|
||||||
false -> erlang:error(badarg);
|
false -> erlang:error(badarg);
|
||||||
F -> F(Bin, {decoder, State, Handler, Acc, Stack}, Config)
|
F -> F(Bin, {decoder, State, Handler, Acc, Stack}, jsx_utils:config_to_list(Config))
|
||||||
end
|
end
|
||||||
).
|
).
|
||||||
-define(error(State, Bin, Handler, Stack, Config),
|
-define(error(State, Bin, Handler, Stack, Config),
|
||||||
|
@ -121,7 +121,7 @@ decoder(Handler, State, Config) ->
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
};
|
};
|
||||||
F -> F(Rest, {decoder, State, Handler, Acc, Stack}, Config)
|
F -> F(Rest, {decoder, State, Handler, Acc, Stack}, jsx_utils:config_to_list(Config))
|
||||||
end
|
end
|
||||||
).
|
).
|
||||||
-define(incomplete(State, Rest, Handler, Stack, Config),
|
-define(incomplete(State, Rest, Handler, Stack, Config),
|
||||||
|
@ -139,7 +139,7 @@ decoder(Handler, State, Config) ->
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
};
|
};
|
||||||
F -> F(Rest, {decoder, State, Handler, null, Stack}, Config)
|
F -> F(Rest, {decoder, State, Handler, null, Stack}, jsx_utils:config_to_list(Config))
|
||||||
end
|
end
|
||||||
).
|
).
|
||||||
-endif.
|
-endif.
|
||||||
|
|
|
@ -45,7 +45,7 @@ encoder(Handler, State, Config) ->
|
||||||
-define(error(State, Term, Handler, Config),
|
-define(error(State, Term, Handler, Config),
|
||||||
case Config#config.error_handler of
|
case Config#config.error_handler of
|
||||||
false -> erlang:error(badarg);
|
false -> erlang:error(badarg);
|
||||||
F -> erlang:throw(F(Term, {encoder, State, Handler}, Config))
|
F -> erlang:throw(F(Term, {encoder, State, Handler}, jsx_utils:config_to_list(Config)))
|
||||||
end
|
end
|
||||||
).
|
).
|
||||||
-endif.
|
-endif.
|
||||||
|
|
|
@ -40,7 +40,7 @@ parser(Handler, State, Config) ->
|
||||||
-define(error(State, Terms, Handler, Stack, Config),
|
-define(error(State, Terms, Handler, Stack, Config),
|
||||||
case Config#config.error_handler of
|
case Config#config.error_handler of
|
||||||
false -> erlang:error(badarg);
|
false -> erlang:error(badarg);
|
||||||
F -> F(Terms, {parser, State, Handler, Stack}, Config)
|
F -> F(Terms, {parser, State, Handler, Stack}, jsx_utils:config_to_list(Config))
|
||||||
end
|
end
|
||||||
|
|
||||||
).
|
).
|
||||||
|
@ -63,7 +63,7 @@ parser(Handler, State, Config) ->
|
||||||
State(Tokens, Handler, Stack, Config)
|
State(Tokens, Handler, Stack, Config)
|
||||||
end
|
end
|
||||||
};
|
};
|
||||||
F -> F([], {parser, State, Handler, Stack}, Config)
|
F -> F([], {parser, State, Handler, Stack}, jsx_utils:config_to_list(Config))
|
||||||
end
|
end
|
||||||
).
|
).
|
||||||
-endif.
|
-endif.
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
-module(jsx_utils).
|
-module(jsx_utils).
|
||||||
|
|
||||||
-export([parse_config/1]).
|
-export([parse_config/1]).
|
||||||
|
-export([config_to_list/1]).
|
||||||
-export([extract_config/1, valid_flags/0]).
|
-export([extract_config/1, valid_flags/0]).
|
||||||
-export([json_escape_sequence/1]).
|
-export([json_escape_sequence/1]).
|
||||||
-export([clean_string/2]).
|
-export([clean_string/2]).
|
||||||
|
@ -103,6 +104,20 @@ parse_config(Options, Config) ->
|
||||||
erlang:error(badarg, [Options, Config]).
|
erlang:error(badarg, [Options, Config]).
|
||||||
|
|
||||||
|
|
||||||
|
config_to_list(Config) ->
|
||||||
|
lists:map(
|
||||||
|
fun ({pre_encode, F}) -> {pre_encode, F};
|
||||||
|
({error_handler, F}) -> {error_handler, F};
|
||||||
|
({incomplete_handler, F}) -> {incomplete_handler, F};
|
||||||
|
({Key, true}) -> Key
|
||||||
|
end,
|
||||||
|
lists:filter(
|
||||||
|
fun({_, false}) -> false; (_) -> true end,
|
||||||
|
lists:zip(record_info(fields, config), tl(tuple_to_list(Config)))
|
||||||
|
)
|
||||||
|
).
|
||||||
|
|
||||||
|
|
||||||
valid_flags() ->
|
valid_flags() ->
|
||||||
[
|
[
|
||||||
replaced_bad_utf8,
|
replaced_bad_utf8,
|
||||||
|
@ -117,6 +132,7 @@ valid_flags() ->
|
||||||
relax,
|
relax,
|
||||||
pre_encode,
|
pre_encode,
|
||||||
error_handler,
|
error_handler,
|
||||||
|
incomplete_handler,
|
||||||
%% deprecated flags
|
%% deprecated flags
|
||||||
pre_encoder, %% pre_encode
|
pre_encoder, %% pre_encode
|
||||||
loose_unicode, %% replaced_bad_utf8
|
loose_unicode, %% replaced_bad_utf8
|
||||||
|
@ -649,7 +665,52 @@ config_test_() ->
|
||||||
)},
|
)},
|
||||||
{"bad option flag", ?_assertError(badarg, parse_config([error]))}
|
{"bad option flag", ?_assertError(badarg, parse_config([error]))}
|
||||||
].
|
].
|
||||||
|
|
||||||
|
|
||||||
|
config_to_list_test_() ->
|
||||||
|
[
|
||||||
|
{"empty config", ?_assertEqual(
|
||||||
|
[],
|
||||||
|
config_to_list(#config{})
|
||||||
|
)},
|
||||||
|
{"all flags", ?_assertEqual(
|
||||||
|
[
|
||||||
|
replaced_bad_utf8,
|
||||||
|
escaped_forward_slashes,
|
||||||
|
single_quoted_strings,
|
||||||
|
unescaped_jsonp,
|
||||||
|
comments,
|
||||||
|
dirty_strings,
|
||||||
|
ignored_bad_escapes,
|
||||||
|
explicit_end
|
||||||
|
],
|
||||||
|
config_to_list(
|
||||||
|
#config{
|
||||||
|
replaced_bad_utf8=true,
|
||||||
|
escaped_forward_slashes=true,
|
||||||
|
explicit_end=true,
|
||||||
|
single_quoted_strings=true,
|
||||||
|
unescaped_jsonp=true,
|
||||||
|
comments=true,
|
||||||
|
dirty_strings=true,
|
||||||
|
ignored_bad_escapes=true
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)},
|
||||||
|
{"pre_encode", ?_assertEqual(
|
||||||
|
[{pre_encode, fun lists:length/1}],
|
||||||
|
config_to_list(#config{pre_encode=fun lists:length/1})
|
||||||
|
)},
|
||||||
|
{"error handler", ?_assertEqual(
|
||||||
|
[{error_handler, fun ?MODULE:fake_error_handler/3}],
|
||||||
|
config_to_list(#config{error_handler=fun ?MODULE:fake_error_handler/3})
|
||||||
|
)},
|
||||||
|
{"incomplete handler", ?_assertEqual(
|
||||||
|
[{incomplete_handler, fun ?MODULE:fake_error_handler/3}],
|
||||||
|
config_to_list(#config{incomplete_handler=fun ?MODULE:fake_error_handler/3})
|
||||||
|
)}
|
||||||
|
].
|
||||||
|
|
||||||
|
|
||||||
fake_error_handler(_, _, _) -> ok.
|
fake_error_handler(_, _, _) -> ok.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue