Skip to content

Commit

Permalink
Implement stream end function (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamthome authored Aug 8, 2024
1 parent e1332a6 commit ce5bea9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/euneus.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-export([decode_stream_start/1]).
-export([decode_stream_start/2]).
-export([decode_stream_continue/2]).
-export([decode_stream_end/1]).
-export([minify/1]).
-export([format/2]).

Expand All @@ -31,6 +32,7 @@
-ignore_xref([decode_stream_start/1]).
-ignore_xref([decode_stream_start/2]).
-ignore_xref([decode_stream_continue/2]).
-ignore_xref([decode_stream_end/1]).
-ignore_xref([minify/1]).
-ignore_xref([format/2]).

Expand Down Expand Up @@ -193,6 +195,25 @@ decode_stream_start(JSON, Opts) ->
decode_stream_continue(JSON, State) ->
euneus_decoder:stream_continue(JSON, State).

-spec decode_stream_end(State) -> Result when
State :: euneus_decoder:stream_state(),
Result :: euneus_decoder:stream_result().
%% @equiv euneus_decoder:stream_continue(end_of_input, State)
%%
%% @doc Ends parsing a stream of bytes of a JSON value.
%%
%% <em>Example:</em>
%%
%% ```
%% 1> begin
%% .. {continue, State} = euneus:decode_stream_start(<<"123">>),
%% .. euneus:decode_stream_end(State)
%% .. end.
%% {end_of_input,123}
%% '''
decode_stream_end(State) ->
euneus_decoder:stream_continue(end_of_input, State).

-spec minify(JSON) -> binary() when
JSON :: binary().
%% @doc Minifies a binary JSON.
Expand Down
14 changes: 10 additions & 4 deletions test/euneus_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ decode_iodata_test(Config) when is_list(Config) ->
].

decode_stream_test(Config) when is_list(Config) ->
{continue, State} = euneus:decode_stream_start(<<"{\"foo\":">>),
?assertEqual(
{end_of_input, #{<<"foo">> => 1}}, euneus:decode_stream_continue(<<"1}">>, State)
).
{continue, State1} = euneus:decode_stream_start(<<"{\"foo\":">>),
{continue, State2} = euneus:decode_stream_start(<<"123">>),
[
?assertEqual(
{end_of_input, #{<<"foo">> => 1}}, euneus:decode_stream_continue(<<"1}">>, State1)
),
?assertEqual(
{end_of_input, 123}, euneus:decode_stream_end(State2)
)
].

minify_test(Config) when is_list(Config) ->
?assertEqual(
Expand Down

0 comments on commit ce5bea9

Please sign in to comment.