test for streaming eep0018 parser
This commit is contained in:
parent
7e8e5a9a48
commit
91f503fc07
2 changed files with 26 additions and 8 deletions
|
@ -79,8 +79,12 @@ extract_parser_opts(Opts) ->
|
|||
collect_strict({event, Start, Next}, Acc, Opts)
|
||||
when Start =:= start_object; Start =:= start_array ->
|
||||
collect(Next(), [[]|Acc], Opts);
|
||||
collect_strict(_, _, _) ->
|
||||
erlang:error(badarg).
|
||||
collect_strict({incomplete, More}, Acc, Opts) ->
|
||||
case proplists:get_value(stream, Opts, false) of
|
||||
true -> {incomplete, fun(JSON) -> collect(More(JSON), Acc, Opts) end}
|
||||
; false -> erlang:error(badarg)
|
||||
end;
|
||||
collect_strict(_, _, _) -> erlang:error(badarg).
|
||||
|
||||
|
||||
%% collect decoder events and convert to eep0018 format
|
||||
|
@ -123,19 +127,23 @@ collect({event, Event, Next}, [Current|Rest], Opts) when is_list(Current) ->
|
|||
collect(Next(), [[event(Event, Opts)] ++ Current] ++ Rest, Opts);
|
||||
collect({event, Event, Next}, [Key, Current|Rest], Opts) ->
|
||||
collect(Next(), [[{Key, event(Event, Opts)}] ++ Current] ++ Rest, Opts);
|
||||
%% if our first returned event is {incomplete, ...} try to force end and return
|
||||
%% if our returned event is {incomplete, ...} try to force end and return
|
||||
%% the Event if one is returned
|
||||
collect({incomplete, More}, [[]], Opts) ->
|
||||
collect({incomplete, More}, Acc, Opts) ->
|
||||
case More(end_stream) of
|
||||
{event, Event, _Next} -> event(Event, Opts)
|
||||
; _ ->
|
||||
; _ ->
|
||||
io:format("hi"),
|
||||
case proplists:get_value(stream, Opts, false) of
|
||||
true -> {incomplete, More}
|
||||
; false -> erlang:error(badarg)
|
||||
true ->
|
||||
{incomplete,
|
||||
fun(JSON) -> collect(More(JSON), Acc, Opts) end
|
||||
}
|
||||
; false -> erlang:error(badarg), io:format("hello!")
|
||||
end
|
||||
end;
|
||||
%% any other event is an error
|
||||
collect(_, _, _) -> erlang:error(badarg).
|
||||
collect(_, _, _) -> io:format(":("), erlang:error(badarg).
|
||||
|
||||
|
||||
%% helper functions for converting jsx events to eep0018 formats
|
||||
|
@ -452,5 +460,15 @@ escape_test_() ->
|
|||
)
|
||||
}
|
||||
].
|
||||
|
||||
stream_test_() ->
|
||||
[
|
||||
{"streaming mode",
|
||||
?_assert(begin
|
||||
{incomplete, F} = json_to_term(<<"{">>, [{stream, true}]),
|
||||
F(<<"}">>)
|
||||
end =:= [{}])
|
||||
}
|
||||
].
|
||||
|
||||
-endif.
|
Loading…
Add table
Add a link
Reference in a new issue