add incomplete_handler and tests
This commit is contained in:
parent
ca89432e7a
commit
2d7b690a91
4 changed files with 86 additions and 38 deletions
|
@ -105,34 +105,42 @@ decoder(Handler, State, Config) ->
|
|||
|
||||
|
||||
-ifndef(incomplete).
|
||||
-define(incomplete(State, Rest, Handler, Stack, Config),
|
||||
{incomplete, fun(Stream) when is_binary(Stream) ->
|
||||
State(<<Rest/binary, Stream/binary>>, Handler, Stack, Config)
|
||||
; (end_stream) ->
|
||||
case State(<<Rest/binary, <<" ">>/binary>>,
|
||||
Handler,
|
||||
Stack,
|
||||
Config#config{explicit_end=false}) of
|
||||
{incomplete, _} -> ?error(State, Rest, Handler, Stack, Config)
|
||||
; Events -> Events
|
||||
end
|
||||
end
|
||||
}
|
||||
).
|
||||
-define(incomplete(State, Rest, Handler, Acc, Stack, Config),
|
||||
{incomplete, fun(Stream) when is_binary(Stream) ->
|
||||
State(<<Rest/binary, Stream/binary>>, Handler, Acc, Stack, Config)
|
||||
; (end_stream) ->
|
||||
case State(<<Rest/binary, <<" ">>/binary>>,
|
||||
Handler,
|
||||
Acc,
|
||||
Stack,
|
||||
Config#config{explicit_end=false}) of
|
||||
{incomplete, _} -> ?error(State, Rest, Handler, Acc, Stack, Config)
|
||||
; Events -> Events
|
||||
case Config#config.incomplete_handler of
|
||||
false ->
|
||||
{incomplete, fun(Stream) when is_binary(Stream) ->
|
||||
State(<<Rest/binary, Stream/binary>>, Handler, Acc, Stack, Config)
|
||||
; (end_stream) ->
|
||||
case State(<<Rest/binary, <<" ">>/binary>>,
|
||||
Handler,
|
||||
Acc,
|
||||
Stack,
|
||||
Config#config{explicit_end=false}) of
|
||||
{incomplete, _} -> ?error(State, Rest, Handler, Acc, Stack, Config)
|
||||
; Events -> Events
|
||||
end
|
||||
end
|
||||
end
|
||||
}
|
||||
};
|
||||
F -> F(Rest, {decoder, State, Handler, Acc, Stack}, Config)
|
||||
end
|
||||
).
|
||||
-define(incomplete(State, Rest, Handler, Stack, Config),
|
||||
case Config#config.incomplete_handler of
|
||||
false ->
|
||||
{incomplete, fun(Stream) when is_binary(Stream) ->
|
||||
State(<<Rest/binary, Stream/binary>>, Handler, Stack, Config)
|
||||
; (end_stream) ->
|
||||
case State(<<Rest/binary, <<" ">>/binary>>,
|
||||
Handler,
|
||||
Stack,
|
||||
Config#config{explicit_end=false}) of
|
||||
{incomplete, _} -> ?error(State, Rest, Handler, Stack, Config)
|
||||
; Events -> Events
|
||||
end
|
||||
end
|
||||
};
|
||||
F -> F(Rest, {decoder, State, Handler, null, Stack}, Config)
|
||||
end
|
||||
).
|
||||
-endif.
|
||||
|
||||
|
@ -2041,5 +2049,15 @@ custom_error_handler_test_() ->
|
|||
].
|
||||
|
||||
|
||||
custom_incomplete_handler_test_() ->
|
||||
Decode = fun(JSON, Config) -> start(JSON, {jsx, []}, [], jsx_utils:parse_config(Config)) end,
|
||||
[
|
||||
{"custom incomplete handler", ?_assertError(
|
||||
badarg,
|
||||
Decode(<<>>, [{incomplete_handler, fun(_, _, _) -> erlang:error(badarg) end}])
|
||||
)}
|
||||
].
|
||||
|
||||
|
||||
|
||||
-endif.
|
Loading…
Add table
Add a link
Reference in a new issue