From 21d9ebe33b3eaf65a4118fc5c3e7364e64d63769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sat, 4 Oct 2014 13:21:16 +0300 Subject: Reverse the order of arguments of match_* functions Wasn't following the same order as the rest of the module. --- doc/src/guide/cookies.ezdoc | 6 +-- doc/src/guide/req.ezdoc | 6 +-- doc/src/manual/cowboy_req.ezdoc | 4 +- examples/cookie/src/toppage_handler.erl | 2 +- examples/echo_get/src/toppage_handler.erl | 2 +- examples/rest_pastebin/src/toppage_handler.erl | 4 +- src/cowboy_req.erl | 52 +++++++++++++------------- test/http_SUITE_data/http_errors.erl | 2 +- test/http_SUITE_data/http_req_attr.erl | 2 +- test/http_SUITE_data/rest_resource_etags.erl | 2 +- 10 files changed, 41 insertions(+), 41 deletions(-) diff --git a/doc/src/guide/cookies.ezdoc b/doc/src/guide/cookies.ezdoc index 459d4d8..e6d1aeb 100644 --- a/doc/src/guide/cookies.ezdoc +++ b/doc/src/guide/cookies.ezdoc @@ -132,7 +132,7 @@ Cookies = cowboy_req:parse_cookies(Req), You can match the cookies into a map. ``` erlang -#{id := ID, lang := Lang} = cowboy_req:match_cookies(Req, [id, lang]). +#{id := ID, lang := Lang} = cowboy_req:match_cookies([id, lang], Req). ``` You can use constraints to validate the values while matching @@ -142,7 +142,7 @@ the `id` cookie value will be converted to an integer term, saving you a conversion step. ``` erlang -CookiesMap = cowboy_req:match_cookies(Req, [{id, int}, {lang, nonempty}]). +CookiesMap = cowboy_req:match_cookies([{id, int}, {lang, nonempty}], Req). ``` Note that if two cookies share the same name, then the map value @@ -155,7 +155,7 @@ if the `lang` cookie is not found. It will not be used if the cookie is found but has an empty value. ``` erlang -#{lang := Lang} = cowboy_req:match_cookies(Req, [{lang, [], <<"en-US">>}]). +#{lang := Lang} = cowboy_req:match_cookies([{lang, [], <<"en-US">>}], Req). ``` If no default is provided and the value is missing, the diff --git a/doc/src/guide/req.ezdoc b/doc/src/guide/req.ezdoc index bc60227..add6166 100644 --- a/doc/src/guide/req.ezdoc +++ b/doc/src/guide/req.ezdoc @@ -142,7 +142,7 @@ QsVals = cowboy_req:parse_qs(Req), You can match the query string into a map. ``` erlang -#{id := ID, lang := Lang} = cowboy_req:match_qs(Req, [id, lang]). +#{id := ID, lang := Lang} = cowboy_req:match_qs([id, lang], Req). ``` You can use constraints to validate the values while matching @@ -152,7 +152,7 @@ the `id` value will be converted to an integer term, saving you a conversion step. ``` erlang -QsMap = cowboy_req:match_qs(Req, [{id, int}, {lang, nonempty}]). +QsMap = cowboy_req:match_qs([{id, int}, {lang, nonempty}], Req). ``` Note that in the case of duplicate query string keys, the map @@ -165,7 +165,7 @@ if the `lang` key is not found. It will not be used if the key is found but has an empty value. ``` erlang -#{lang := Lang} = cowboy_req:match_qs(Req, [{lang, [], <<"en-US">>}]). +#{lang := Lang} = cowboy_req:match_qs([{lang, [], <<"en-US">>}], Req). ``` If no default is provided and the value is missing, the diff --git a/doc/src/manual/cowboy_req.ezdoc b/doc/src/manual/cowboy_req.ezdoc index 9a2f34f..9b73886 100644 --- a/doc/src/manual/cowboy_req.ezdoc +++ b/doc/src/manual/cowboy_req.ezdoc @@ -128,7 +128,7 @@ Return the requested URL excluding the path component. This function will always return `undefined` until the `cowboy_router` middleware has been executed. -: match_cookies(Req, Fields) -> Map +: match_cookies(Fields, Req) -> Map Types: @@ -153,7 +153,7 @@ be converted through the use of constraints, making this function able to extract, validate and convert values all in one step. -: match_qs(Req, Fields) -> Map +: match_qs(Fields, Req) -> Map Types: diff --git a/examples/cookie/src/toppage_handler.erl b/examples/cookie/src/toppage_handler.erl index 52e155b..7331906 100644 --- a/examples/cookie/src/toppage_handler.erl +++ b/examples/cookie/src/toppage_handler.erl @@ -10,7 +10,7 @@ init(Req, Opts) -> Req2 = cowboy_req:set_resp_cookie( <<"server">>, NewValue, [{path, <<"/">>}], Req), #{client := ClientCookie, server := ServerCookie} - = cowboy_req:match_cookies(Req2, [client, server]), + = cowboy_req:match_cookies([client, server], Req2), {ok, Body} = toppage_dtl:render([ {client, ClientCookie}, {server, ServerCookie} diff --git a/examples/echo_get/src/toppage_handler.erl b/examples/echo_get/src/toppage_handler.erl index a7c8d7f..800284e 100644 --- a/examples/echo_get/src/toppage_handler.erl +++ b/examples/echo_get/src/toppage_handler.erl @@ -7,7 +7,7 @@ init(Req, Opts) -> Method = cowboy_req:method(Req), - #{echo := Echo} = cowboy_req:match_qs(Req, [echo]), + #{echo := Echo} = cowboy_req:match_qs([echo], Req), Req2 = echo(Method, Echo, Req), {ok, Req2, Opts}. diff --git a/examples/rest_pastebin/src/toppage_handler.erl b/examples/rest_pastebin/src/toppage_handler.erl index 80974fe..57b9315 100644 --- a/examples/rest_pastebin/src/toppage_handler.erl +++ b/examples/rest_pastebin/src/toppage_handler.erl @@ -57,13 +57,13 @@ create_paste(Req, State) -> paste_html(Req, index) -> {read_file("index.html"), Req, index}; paste_html(Req, Paste) -> - #{lang := Lang} = cowboy_req:match_qs(Req, [lang]), + #{lang := Lang} = cowboy_req:match_qs([lang], Req), {format_html(Paste, Lang), Req, Paste}. paste_text(Req, index) -> {read_file("index.txt"), Req, index}; paste_text(Req, Paste) -> - #{lang := Lang} = cowboy_req:match_qs(Req, [lang]), + #{lang := Lang} = cowboy_req:match_qs([lang], Req), {format_text(Paste, Lang), Req, Paste}. % Private diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl index 757ca83..a197110 100644 --- a/src/cowboy_req.erl +++ b/src/cowboy_req.erl @@ -221,9 +221,9 @@ qs(Req) -> parse_qs(#http_req{qs=Qs}) -> cow_qs:parse_qs(Qs). --spec match_qs(req(), cowboy:fields()) -> map(). -match_qs(Req, Fields) -> - filter(kvlist_to_map(parse_qs(Req), Fields), Fields). +-spec match_qs(cowboy:fields(), req()) -> map(). +match_qs(Fields, Req) -> + filter(Fields, kvlist_to_map(Fields, parse_qs(Req))). %% The URL includes the scheme, host and port only. -spec host_url(req()) -> undefined | binary(). @@ -380,9 +380,9 @@ parse_header(Name, Req, Default, ParseFun) -> parse_cookies(Req) -> parse_header(<<"cookie">>, Req). --spec match_cookies(req(), cowboy:fields()) -> map(). -match_cookies(Req, Fields) -> - filter(kvlist_to_map(parse_cookies(Req), Fields), Fields). +-spec match_cookies(cowboy:fields(), req()) -> map(). +match_cookies(Fields, Req) -> + filter(Fields, kvlist_to_map(Fields, parse_cookies(Req))). -spec meta(atom(), req()) -> any() | undefined. meta(Name, Req) -> @@ -1214,63 +1214,63 @@ status(B) when is_binary(B) -> B. %% Create map, convert keys to atoms and group duplicate keys into lists. %% Keys that are not found in the user provided list are entirely skipped. %% @todo Can probably be done directly while parsing. -kvlist_to_map(KvList, Fields) -> +kvlist_to_map(Fields, KvList) -> Keys = [case K of {Key, _} -> Key; {Key, _, _} -> Key; Key -> Key end || K <- Fields], - kvlist_to_map(KvList, Keys, #{}). + kvlist_to_map(Keys, KvList, #{}). -kvlist_to_map([], _, Map) -> +kvlist_to_map(_, [], Map) -> Map; -kvlist_to_map([{Key, Value}|Tail], Keys, Map) -> +kvlist_to_map(Keys, [{Key, Value}|Tail], Map) -> try binary_to_existing_atom(Key, utf8) of Atom -> case lists:member(Atom, Keys) of true -> case maps:find(Atom, Map) of {ok, MapValue} when is_list(MapValue) -> - kvlist_to_map(Tail, Keys, + kvlist_to_map(Keys, Tail, maps:put(Atom, [Value|MapValue], Map)); {ok, MapValue} -> - kvlist_to_map(Tail, Keys, + kvlist_to_map(Keys, Tail, maps:put(Atom, [Value, MapValue], Map)); error -> - kvlist_to_map(Tail, Keys, + kvlist_to_map(Keys, Tail, maps:put(Atom, Value, Map)) end; false -> - kvlist_to_map(Tail, Keys, Map) + kvlist_to_map(Keys, Tail, Map) end catch error:badarg -> - kvlist_to_map(Tail, Keys, Map) + kvlist_to_map(Keys, Tail, Map) end. %% Loop through fields, if value is missing and no default, crash; %% else if value is missing and has a default, set default; %% otherwise apply constraints. If constraint fails, crash. -filter(Map, []) -> +filter([], Map) -> Map; -filter(Map, [{Key, Constraints}|Tail]) -> - filter_constraints(Map, Tail, Key, maps:get(Key, Map), Constraints); -filter(Map, [{Key, Constraints, Default}|Tail]) -> +filter([{Key, Constraints}|Tail], Map) -> + filter_constraints(Tail, Map, Key, maps:get(Key, Map), Constraints); +filter([{Key, Constraints, Default}|Tail], Map) -> case maps:find(Key, Map) of {ok, Value} -> - filter_constraints(Map, Tail, Key, Value, Constraints); + filter_constraints(Tail, Map, Key, Value, Constraints); error -> - filter(maps:put(Key, Default, Map), Tail) + filter(Tail, maps:put(Key, Default, Map)) end; -filter(Map, [Key|Tail]) -> +filter([Key|Tail], Map) -> true = maps:is_key(Key, Map), - filter(Map, Tail). + filter(Tail, Map). -filter_constraints(Map, Tail, Key, Value, Constraints) -> +filter_constraints(Tail, Map, Key, Value, Constraints) -> case cowboy_constraints:validate(Value, Constraints) of true -> - filter(Map, Tail); + filter(Tail, Map); {true, Value2} -> - filter(maps:put(Key, Value2, Map), Tail) + filter(Tail, maps:put(Key, Value2, Map)) end. %% Tests. diff --git a/test/http_SUITE_data/http_errors.erl b/test/http_SUITE_data/http_errors.erl index f105c58..f646df5 100644 --- a/test/http_SUITE_data/http_errors.erl +++ b/test/http_SUITE_data/http_errors.erl @@ -5,7 +5,7 @@ -export([init/2]). init(Req, _Opts) -> - #{'case' := Case} = cowboy_req:match_qs(Req, ['case']), + #{'case' := Case} = cowboy_req:match_qs(['case'], Req), case_init(Case, Req). case_init(<<"init_before_reply">> = Case, _Req) -> diff --git a/test/http_SUITE_data/http_req_attr.erl b/test/http_SUITE_data/http_req_attr.erl index ce9c185..d8483a5 100644 --- a/test/http_SUITE_data/http_req_attr.erl +++ b/test/http_SUITE_data/http_req_attr.erl @@ -7,7 +7,7 @@ -export([init/2]). init(Req, Opts) -> - #{attr := Attr} = cowboy_req:match_qs(Req, [attr]), + #{attr := Attr} = cowboy_req:match_qs([attr], Req), <<"host_and_port">> = Attr, Host = cowboy_req:host(Req), Port = cowboy_req:port(Req), diff --git a/test/http_SUITE_data/rest_resource_etags.erl b/test/http_SUITE_data/rest_resource_etags.erl index 23b9dfc..1ea3005 100644 --- a/test/http_SUITE_data/rest_resource_etags.erl +++ b/test/http_SUITE_data/rest_resource_etags.erl @@ -9,7 +9,7 @@ init(Req, Opts) -> {cowboy_rest, Req, Opts}. generate_etag(Req, State) -> - #{type := Type} = cowboy_req:match_qs(Req, [type]), + #{type := Type} = cowboy_req:match_qs([type], Req), case Type of %% Correct return values from generate_etag/2. <<"tuple-weak">> -> -- cgit v1.2.3