From 1743f40b09ae44f7ce523e3b51d59d184fda01d7 Mon Sep 17 00:00:00 2001 From: Sebastien Merle Date: Thu, 14 Nov 2024 13:20:44 +0100 Subject: [PATCH] Fixes from review --- src/grisp_connect_jsonrpc.erl | 17 +++++++---------- test/grisp_connect_jsonrpc_SUITE.erl | 18 +++++++++++++++--- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/grisp_connect_jsonrpc.erl b/src/grisp_connect_jsonrpc.erl index ae6d899..be0f5ce 100644 --- a/src/grisp_connect_jsonrpc.erl +++ b/src/grisp_connect_jsonrpc.erl @@ -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()}. @@ -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 -> @@ -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 @@ -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; @@ -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). diff --git a/test/grisp_connect_jsonrpc_SUITE.erl b/test/grisp_connect_jsonrpc_SUITE.erl index 2e43fc9..9273333 100644 --- a/test/grisp_connect_jsonrpc_SUITE.erl +++ b/test/grisp_connect_jsonrpc_SUITE.erl @@ -11,7 +11,8 @@ invalid_json/1, invalid_request/1, batch/1, - result/1]). + result/1, + null_values/1]). all() -> [ positional_parameters, @@ -21,7 +22,8 @@ all() -> [ invalid_json, invalid_request, batch, - result + result, + null_values ]. positional_parameters(_) -> @@ -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\"">>, @@ -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).