Skip to content

Commit

Permalink
Fixes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
sylane committed Nov 14, 2024
1 parent ac82070 commit 1743f40
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
17 changes: 7 additions & 10 deletions src/grisp_connect_jsonrpc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
| {error, Code :: integer(), Message :: undefined | binary(),
Data :: undefined | term(), ReqRef :: undefined | binary() | integer()}
| {decoding_error, Code :: integer(), Message :: undefined | binary(),
Data :: undefined | term(), ReqRef :: undefined | binary()}.
Data :: undefined | term(), ReqRef :: undefined | binary() | integer()}.



Expand Down Expand Up @@ -83,14 +83,12 @@ encode(Message) ->

as_bin(undefined) -> undefined;
as_bin(Binary) when is_binary(Binary) -> Binary;
as_bin(List) when is_list(List) -> list_to_binary(List);
as_bin(Atom) when is_atom(Atom) -> atom_to_binary(Atom).
as_bin(List) when is_list(List) -> list_to_binary(List).

as_id(undefined) -> undefined;
as_id(Integer) when is_integer(Integer) -> Integer;
as_id(Binary) when is_binary(Binary) -> Binary;
as_id(List) when is_list(List) -> list_to_binary(List);
as_id(Atom) when is_atom(Atom) -> atom_to_binary(Atom).
as_id(List) when is_list(List) -> list_to_binary(List).

unpack(#{method := Method, params := Params, id := ID} = M)
when ?is_valid(M), ?is_method(Method), ?is_params(Params), ID =/= undefined ->
Expand Down Expand Up @@ -152,8 +150,8 @@ pack({ErrorTag, Code, Message, Data, ID})
when ErrorTag =:= error orelse ErrorTag =:= decoding_error, is_integer(Code),
Message =:= undefined orelse is_binary(Message), ?is_id(ID) ->
#{?V, error => #{code => Code, message => Message, data => Data}, id => ID};
pack(_Message) ->
erlang:error({badarg, _Message}).
pack(Message) ->
erlang:error({badarg, Message}).

json_to_term(Bin) ->
try jsx:decode(Bin, [{labels, attempt_atom}, return_maps]) of
Expand All @@ -166,14 +164,13 @@ term_to_json(Term) ->
jsx:encode(preprocess(Term)).

postprocess(null) -> undefined;
postprocess(Atom) when is_atom(Atom) -> Atom;
postprocess(Integer) when is_integer(Integer) -> Integer;
postprocess(Float) when is_float(Float) -> Float;
postprocess(Binary) when is_binary(Binary) -> Binary;
postprocess(List) when is_list(List) ->
[postprocess(E) || E <- List];
postprocess(Map) when is_map(Map) ->
maps:from_list([{K, postprocess(V)} || {K, V} <- maps:to_list(Map)]).
maps:map(fun(_K, V) -> postprocess(V) end, Map).

preprocess(undefined) -> null;
preprocess(Atom) when is_atom(Atom) -> Atom;
Expand All @@ -183,4 +180,4 @@ preprocess(Binary) when is_binary(Binary) -> Binary;
preprocess(List) when is_list(List) ->
[preprocess(E) || E <- List];
preprocess(Map) when is_map(Map) ->
maps:from_list([{K, preprocess(V)} || {K, V} <- maps:to_list(Map)]).
maps:map(fun(_K, V) -> preprocess(V) end, Map).
18 changes: 15 additions & 3 deletions test/grisp_connect_jsonrpc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
invalid_json/1,
invalid_request/1,
batch/1,
result/1]).
result/1,
null_values/1]).

all() -> [
positional_parameters,
Expand All @@ -21,7 +22,8 @@ all() -> [
invalid_json,
invalid_request,
batch,
result
result,
null_values
].

positional_parameters(_) ->
Expand Down Expand Up @@ -87,7 +89,7 @@ batch(_) ->
JsonError = grisp_connect_jsonrpc:encode([Term1, Term2]),
?assert(jsonrpc_check([<<"\"id\":\"1\"">>,
<<"\"method\":\"sum\"">>,
<<"params\":[1,2,4]">>,
<<"\"params\":[1,2,4]">>,
<<"\"error\":{">>,
<<"\"code\":-32600">>,
<<"\"message\":\"Invalid request\"">>,
Expand All @@ -101,6 +103,16 @@ result(_) ->
?assert(jsonrpc_check([<<"\"id\":45">>,
<<"\"result\":7">>], Json2)).

null_values(_) ->
Term = {notification, <<"test_null">>, #{array => [undefined], object => #{foo => undefined}, value => undefined}},
Json = <<"{\"jsonrpc\":\"2.0\",\"method\":\"test_null\",\"params\":{\"array\":[null],\"object\":{\"foo\":null},\"value\":null}}">>,
?assertMatch([Term], grisp_connect_jsonrpc:decode(Json)),
Json2 = grisp_connect_jsonrpc:encode(Term),
?assert(jsonrpc_check([<<"\"array\":[null]">>,
<<"\"foo\":null">>,
<<"\"value\":null">>],
Json2)).

jsonrpc_check(Elements, JsonString) ->
Elements2 = [<<"\"jsonrpc\":\"2.0\"">>| Elements],
lists:all(fun(E) -> binary:match(JsonString, E) =/= nomatch end, Elements2).

0 comments on commit 1743f40

Please sign in to comment.