0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Reverse the order of arguments of match_* functions

Wasn't following the same order as the rest of the module.
This commit is contained in:
Loïc Hoguin 2014-10-04 13:21:16 +03:00
parent 4bc8e330fa
commit 21d9ebe33b
10 changed files with 41 additions and 41 deletions

View file

@ -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

View file

@ -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

View file

@ -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:

View file

@ -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}

View file

@ -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}.

View file

@ -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

View file

@ -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.

View file

@ -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) ->

View file

@ -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),

View file

@ -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">> ->