From 0ad1d315e8e1ef62271cfc1dfe874f3e4ab4a8c7 Mon Sep 17 00:00:00 2001 From: Badlop Date: Mon, 6 May 2024 17:07:26 +0200 Subject: [PATCH] Use maps instead of tuple+list+tuple as arguments in jiffy functions --- src/ejabberd_ctl.erl | 2 +- src/ejabberd_oauth.erl | 13 ++++----- src/ejabberd_oauth_rest.erl | 58 ++++++++++++++++++++++++------------- src/ext_mod.erl | 24 +++++++-------- src/mod_conversejs.erl | 3 +- src/mod_http_api.erl | 13 ++++----- src/rest.erl | 2 +- 7 files changed, 65 insertions(+), 50 deletions(-) diff --git a/src/ejabberd_ctl.erl b/src/ejabberd_ctl.erl index 7fc73f57a..3a6f984c5 100644 --- a/src/ejabberd_ctl.erl +++ b/src/ejabberd_ctl.erl @@ -374,7 +374,7 @@ format_arg(Arg, {tuple, Elements}) -> list_to_tuple(format_args(Args, Elements)); format_arg(Arg, Format) -> S = unicode:characters_to_binary(Arg, utf8), - JSON = jiffy:decode(S), + JSON = jiffy:decode(S, [return_maps]), mod_http_api:format_arg(JSON, Format). format_arg2(Arg, Parse)-> diff --git a/src/ejabberd_oauth.erl b/src/ejabberd_oauth.erl index 026597f00..9fd6aee3c 100644 --- a/src/ejabberd_oauth.erl +++ b/src/ejabberd_oauth.erl @@ -721,11 +721,10 @@ process(_Handlers, ExpiresIn end, {ok, VerifiedScope} = oauth2_response:scope(Response), - json_response(200, {[ - {<<"access_token">>, AccessToken}, - {<<"token_type">>, Type}, - {<<"scope">>, str:join(VerifiedScope, <<" ">>)}, - {<<"expires_in">>, Expires}]}); + json_response(200, #{<<"access_token">> => AccessToken, + <<"token_type">> => Type, + <<"scope">> => str:join(VerifiedScope, <<" ">>), + <<"expires_in">> => Expires}); {error, Error} when is_atom(Error) -> json_error(400, <<"invalid_grant">>, Error) end; @@ -762,8 +761,8 @@ json_response(Code, Body) -> %% https://tools.ietf.org/html/draft-ietf-oauth-v2-25#section-5.2 json_error(Code, Error, Reason) -> Desc = json_error_desc(Reason), - Body = {[{<<"error">>, Error}, - {<<"error_description">>, Desc}]}, + Body = #{<<"error">> => Error, + <<"error_description">> => Desc}, json_response(Code, Body). json_error_desc(access_denied) -> <<"Access denied">>; diff --git a/src/ejabberd_oauth_rest.erl b/src/ejabberd_oauth_rest.erl index 9e9bdc17f..e38dceffe 100644 --- a/src/ejabberd_oauth_rest.erl +++ b/src/ejabberd_oauth_rest.erl @@ -50,11 +50,11 @@ store(R) -> case rest:with_retry( post, [ejabberd_config:get_myname(), Path, [], - {[{<<"token">>, R#oauth_token.token}, - {<<"user">>, SJID}, - {<<"scope">>, R#oauth_token.scope}, - {<<"expire">>, R#oauth_token.expire} - ]}], 2, 500) of + #{<<"token">> => R#oauth_token.token, + <<"user">> => SJID, + <<"scope">> => R#oauth_token.scope, + <<"expire">> => R#oauth_token.expire + }], 2, 500) of {ok, Code, _} when Code == 200 orelse Code == 201 -> ok; Err -> @@ -65,14 +65,23 @@ store(R) -> lookup(Token) -> Path = path(<<"lookup">>), case rest:with_retry(post, [ejabberd_config:get_myname(), Path, [], - {[{<<"token">>, Token}]}], + #{<<"token">> => Token}], 2, 500) of - {ok, 200, {Data}} -> - SJID = proplists:get_value(<<"user">>, Data, <<>>), + {ok, 200, Data} -> + SJID = case maps:find(<<"user">>, Data) of + {ok, U} -> U; + error -> <<>> + end, JID = jid:decode(SJID), US = {JID#jid.luser, JID#jid.lserver}, - Scope = proplists:get_value(<<"scope">>, Data, []), - Expire = proplists:get_value(<<"expire">>, Data, 0), + Scope = case maps:find(<<"scope">>, Data) of + {ok, S} -> S; + error -> [] + end, + Expire = case maps:find(<<"expire">>, Data) of + {ok, E} -> E; + error -> 0 + end, {ok, #oauth_token{token = Token, us = US, scope = Scope, @@ -113,11 +122,11 @@ store_client(#oauth_client{client_id = ClientID, case rest:with_retry( post, [ejabberd_config:get_myname(), Path, [], - {[{<<"client_id">>, ClientID}, - {<<"client_name">>, ClientName}, - {<<"grant_type">>, SGrantType}, - {<<"options">>, SOptions} - ]}], 2, 500) of + #{<<"client_id">> => ClientID, + <<"client_name">> => ClientName, + <<"grant_type">> => SGrantType, + <<"options">> => SOptions + }], 2, 500) of {ok, Code, _} when Code == 200 orelse Code == 201 -> ok; Err -> @@ -128,17 +137,26 @@ store_client(#oauth_client{client_id = ClientID, lookup_client(ClientID) -> Path = path(<<"lookup_client">>), case rest:with_retry(post, [ejabberd_config:get_myname(), Path, [], - {[{<<"client_id">>, ClientID}]}], + #{<<"client_id">> => ClientID}], 2, 500) of - {ok, 200, {Data}} -> - ClientName = proplists:get_value(<<"client_name">>, Data, <<>>), - SGrantType = proplists:get_value(<<"grant_type">>, Data, <<>>), + {ok, 200, Data} -> + ClientName = case maps:find(<<"client_name">>, Data) of + {ok, CN} -> CN; + error -> <<>> + end, + SGrantType = case maps:find(<<"grant_type">>, Data) of + {ok, GT} -> GT; + error -> <<>> + end, GrantType = case SGrantType of <<"password">> -> password; <<"implicit">> -> implicit end, - SOptions = proplists:get_value(<<"options">>, Data, <<>>), + SOptions = case maps:find(<<"options">>, Data) of + {ok, O} -> O; + error -> <<>> + end, case misc:base64_to_term(SOptions) of {term, Options} -> {ok, #oauth_client{client_id = ClientID, diff --git a/src/ext_mod.erl b/src/ext_mod.erl index 2c74017b0..6b8344026 100644 --- a/src/ext_mod.erl +++ b/src/ext_mod.erl @@ -884,21 +884,21 @@ get_commit_details2(Path) -> end. parse_details(Body) -> - {Contents} = jiffy:decode(Body), + Contents = jiffy:decode(Body, [return_maps]), - {_, {Commit}} = lists:keyfind(<<"commit">>, 1, Contents), - {_, Sha} = lists:keyfind(<<"sha">>, 1, Commit), - {_, CommitHtmlUrl} = lists:keyfind(<<"html_url">>, 1, Commit), + {ok, Commit} = maps:find(<<"commit">>, Contents), + {ok, Sha} = maps:find(<<"sha">>, Commit), + {ok, CommitHtmlUrl} = maps:find(<<"html_url">>, Commit), - {_, {Commit2}} = lists:keyfind(<<"commit">>, 1, Commit), - {_, Message} = lists:keyfind(<<"message">>, 1, Commit2), - {_, {Author}} = lists:keyfind(<<"author">>, 1, Commit2), - {_, AuthorName} = lists:keyfind(<<"name">>, 1, Author), - {_, {Committer}} = lists:keyfind(<<"committer">>, 1, Commit2), - {_, Date} = lists:keyfind(<<"date">>, 1, Committer), + {ok, Commit2} = maps:find(<<"commit">>, Commit), + {ok, Message} = maps:find(<<"message">>, Commit2), + {ok, Author} = maps:find(<<"author">>, Commit2), + {ok, AuthorName} = maps:find(<<"name">>, Author), + {ok, Committer} = maps:find(<<"committer">>, Commit2), + {ok, Date} = maps:find(<<"date">>, Committer), - {_, {Links}} = lists:keyfind(<<"_links">>, 1, Contents), - {_, Html} = lists:keyfind(<<"html">>, 1, Links), + {ok, Links} = maps:find(<<"_links">>, Contents), + {ok, Html} = maps:find(<<"html">>, Links), #{sha => Sha, date => Date, diff --git a/src/mod_conversejs.erl b/src/mod_conversejs.erl index 2d3af544e..de1564cbb 100644 --- a/src/mod_conversejs.erl +++ b/src/mod_conversejs.erl @@ -78,6 +78,7 @@ process([], #request{method = 'GET', host = Host, raw_path = RawPath}) -> undefined -> Init2; BoshURL -> [{<<"bosh_service_url">>, BoshURL} | Init2] end, + Init4 = maps:from_list(Init3), {200, [html], [<<"">>, <<"">>, @@ -89,7 +90,7 @@ process([], #request{method = 'GET', host = Host, raw_path = RawPath}) -> <<"">>, <<"">>, <<"">>, <<"">>, <<"">>]}; diff --git a/src/mod_http_api.erl b/src/mod_http_api.erl index 5a61ad1c3..958c71c66 100644 --- a/src/mod_http_api.erl +++ b/src/mod_http_api.erl @@ -197,11 +197,8 @@ perform_call(Command, Args, Req, Version) -> %% Be tolerant to make API more easily usable from command-line pipe. extract_args(<<"\n">>) -> []; extract_args(Data) -> - case jiffy:decode(Data) of - List when is_list(List) -> List; - {List} when is_list(List) -> List; - Other -> [Other] - end. + Maps = jiffy:decode(Data, [return_maps]), + maps:to_list(Maps). % get API version N from last "vN" element in URL path get_api_version(#request{path = Path}) -> @@ -509,9 +506,9 @@ json_response(Code, Body) when is_integer(Code) -> %% message is binary json_error(HTTPCode, JSONCode, Message) -> {HTTPCode, ?HEADER(?CT_JSON), - jiffy:encode({[{<<"status">>, <<"error">>}, - {<<"code">>, JSONCode}, - {<<"message">>, Message}]}) + jiffy:encode(#{<<"status">> => <<"error">>, + <<"code">> => JSONCode, + <<"message">> => Message}) }. log(Call, Args, {Addr, Port}) -> diff --git a/src/rest.erl b/src/rest.erl index 03c1f35b5..381e612be 100644 --- a/src/rest.erl +++ b/src/rest.erl @@ -157,7 +157,7 @@ encode_json(Content) -> decode_json(<<>>) -> []; decode_json(<<" ">>) -> []; decode_json(<<"\r\n">>) -> []; -decode_json(Data) -> jiffy:decode(Data). +decode_json(Data) -> jiffy:decode(Data, [return_maps]). custom_headers(Server) -> case ejabberd_option:ext_api_headers(Server) of