Просмотр исходного кода

Reverse the order of arguments of match_* functions

Wasn't following the same order as the rest of the module.
Loïc Hoguin 10 лет назад
Родитель
Сommit
21d9ebe33b

+ 3 - 3
doc/src/guide/cookies.ezdoc

@@ -132,7 +132,7 @@ Cookies = cowboy_req:parse_cookies(Req),
 You can match the cookies into a map.
 You can match the cookies into a map.
 
 
 ``` erlang
 ``` 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
 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.
 you a conversion step.
 
 
 ``` erlang
 ``` 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
 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.
 the cookie is found but has an empty value.
 
 
 ``` erlang
 ``` 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
 If no default is provided and the value is missing, the

+ 3 - 3
doc/src/guide/req.ezdoc

@@ -142,7 +142,7 @@ QsVals = cowboy_req:parse_qs(Req),
 You can match the query string into a map.
 You can match the query string into a map.
 
 
 ``` erlang
 ``` 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
 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.
 you a conversion step.
 
 
 ``` erlang
 ``` 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
 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.
 the key is found but has an empty value.
 
 
 ``` erlang
 ``` 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
 If no default is provided and the value is missing, the

+ 2 - 2
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
 This function will always return `undefined` until the
 `cowboy_router` middleware has been executed.
 `cowboy_router` middleware has been executed.
 
 
-: match_cookies(Req, Fields) -> Map
+: match_cookies(Fields, Req) -> Map
 
 
 Types:
 Types:
 
 
@@ -153,7 +153,7 @@ be converted through the use of constraints, making this
 function able to extract, validate and convert values all
 function able to extract, validate and convert values all
 in one step.
 in one step.
 
 
-: match_qs(Req, Fields) -> Map
+: match_qs(Fields, Req) -> Map
 
 
 Types:
 Types:
 
 

+ 1 - 1
examples/cookie/src/toppage_handler.erl

@@ -10,7 +10,7 @@ init(Req, Opts) ->
 	Req2 = cowboy_req:set_resp_cookie(
 	Req2 = cowboy_req:set_resp_cookie(
 		<<"server">>, NewValue, [{path, <<"/">>}], Req),
 		<<"server">>, NewValue, [{path, <<"/">>}], Req),
 	#{client := ClientCookie, server := ServerCookie}
 	#{client := ClientCookie, server := ServerCookie}
-		= cowboy_req:match_cookies(Req2, [client, server]),
+		= cowboy_req:match_cookies([client, server], Req2),
 	{ok, Body} = toppage_dtl:render([
 	{ok, Body} = toppage_dtl:render([
 		{client, ClientCookie},
 		{client, ClientCookie},
 		{server, ServerCookie}
 		{server, ServerCookie}

+ 1 - 1
examples/echo_get/src/toppage_handler.erl

@@ -7,7 +7,7 @@
 
 
 init(Req, Opts) ->
 init(Req, Opts) ->
 	Method = cowboy_req:method(Req),
 	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),
 	Req2 = echo(Method, Echo, Req),
 	{ok, Req2, Opts}.
 	{ok, Req2, Opts}.
 
 

+ 2 - 2
examples/rest_pastebin/src/toppage_handler.erl

@@ -57,13 +57,13 @@ create_paste(Req, State) ->
 paste_html(Req, index) ->
 paste_html(Req, index) ->
 	{read_file("index.html"), Req, index};
 	{read_file("index.html"), Req, index};
 paste_html(Req, Paste) ->
 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}.
 	{format_html(Paste, Lang), Req, Paste}.
 
 
 paste_text(Req, index) ->
 paste_text(Req, index) ->
 	{read_file("index.txt"), Req, index};
 	{read_file("index.txt"), Req, index};
 paste_text(Req, Paste) ->
 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}.
 	{format_text(Paste, Lang), Req, Paste}.
 
 
 % Private
 % Private

+ 26 - 26
src/cowboy_req.erl

@@ -221,9 +221,9 @@ qs(Req) ->
 parse_qs(#http_req{qs=Qs}) ->
 parse_qs(#http_req{qs=Qs}) ->
 	cow_qs:parse_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.
 %% The URL includes the scheme, host and port only.
 -spec host_url(req()) -> undefined | binary().
 -spec host_url(req()) -> undefined | binary().
@@ -380,9 +380,9 @@ parse_header(Name, Req, Default, ParseFun) ->
 parse_cookies(Req) ->
 parse_cookies(Req) ->
 	parse_header(<<"cookie">>, 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.
 -spec meta(atom(), req()) -> any() | undefined.
 meta(Name, Req) ->
 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.
 %% 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.
 %% Keys that are not found in the user provided list are entirely skipped.
 %% @todo Can probably be done directly while parsing.
 %% @todo Can probably be done directly while parsing.
-kvlist_to_map(KvList, Fields) ->
+kvlist_to_map(Fields, KvList) ->
 	Keys = [case K of
 	Keys = [case K of
 		{Key, _} -> Key;
 		{Key, _} -> Key;
 		{Key, _, _} -> Key;
 		{Key, _, _} -> Key;
 		Key -> Key
 		Key -> Key
 	end || K <- Fields],
 	end || K <- Fields],
-	kvlist_to_map(KvList, Keys, #{}).
+	kvlist_to_map(Keys, KvList, #{}).
 
 
-kvlist_to_map([], _, Map) ->
+kvlist_to_map(_, [], 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
 	try binary_to_existing_atom(Key, utf8) of
 		Atom ->
 		Atom ->
 			case lists:member(Atom, Keys) of
 			case lists:member(Atom, Keys) of
 				true ->
 				true ->
 					case maps:find(Atom, Map) of
 					case maps:find(Atom, Map) of
 						{ok, MapValue} when is_list(MapValue) ->
 						{ok, MapValue} when is_list(MapValue) ->
-							kvlist_to_map(Tail, Keys,
+							kvlist_to_map(Keys, Tail,
 								maps:put(Atom, [Value|MapValue], Map));
 								maps:put(Atom, [Value|MapValue], Map));
 						{ok, MapValue} ->
 						{ok, MapValue} ->
-							kvlist_to_map(Tail, Keys,
+							kvlist_to_map(Keys, Tail,
 								maps:put(Atom, [Value, MapValue], Map));
 								maps:put(Atom, [Value, MapValue], Map));
 						error ->
 						error ->
-							kvlist_to_map(Tail, Keys,
+							kvlist_to_map(Keys, Tail,
 								maps:put(Atom, Value, Map))
 								maps:put(Atom, Value, Map))
 					end;
 					end;
 				false ->
 				false ->
-					kvlist_to_map(Tail, Keys, Map)
+					kvlist_to_map(Keys, Tail, Map)
 			end
 			end
 	catch error:badarg ->
 	catch error:badarg ->
-		kvlist_to_map(Tail, Keys, Map)
+		kvlist_to_map(Keys, Tail, Map)
 	end.
 	end.
 
 
 %% Loop through fields, if value is missing and no default, crash;
 %% Loop through fields, if value is missing and no default, crash;
 %% else if value is missing and has a default, set default;
 %% else if value is missing and has a default, set default;
 %% otherwise apply constraints. If constraint fails, crash.
 %% otherwise apply constraints. If constraint fails, crash.
-filter(Map, []) ->
+filter([], Map) ->
 	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
 	case maps:find(Key, Map) of
 		{ok, Value} ->
 		{ok, Value} ->
-			filter_constraints(Map, Tail, Key, Value, Constraints);
+			filter_constraints(Tail, Map, Key, Value, Constraints);
 		error ->
 		error ->
-			filter(maps:put(Key, Default, Map), Tail)
+			filter(Tail, maps:put(Key, Default, Map))
 	end;
 	end;
-filter(Map, [Key|Tail]) ->
+filter([Key|Tail], Map) ->
 	true = maps:is_key(Key, 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
 	case cowboy_constraints:validate(Value, Constraints) of
 		true ->
 		true ->
-			filter(Map, Tail);
+			filter(Tail, Map);
 		{true, Value2} ->
 		{true, Value2} ->
-			filter(maps:put(Key, Value2, Map), Tail)
+			filter(Tail, maps:put(Key, Value2, Map))
 	end.
 	end.
 
 
 %% Tests.
 %% Tests.

+ 1 - 1
test/http_SUITE_data/http_errors.erl

@@ -5,7 +5,7 @@
 -export([init/2]).
 -export([init/2]).
 
 
 init(Req, _Opts) ->
 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(Case, Req).
 
 
 case_init(<<"init_before_reply">> = Case, _Req) ->
 case_init(<<"init_before_reply">> = Case, _Req) ->

+ 1 - 1
test/http_SUITE_data/http_req_attr.erl

@@ -7,7 +7,7 @@
 -export([init/2]).
 -export([init/2]).
 
 
 init(Req, Opts) ->
 init(Req, Opts) ->
-	#{attr := Attr} = cowboy_req:match_qs(Req, [attr]),
+	#{attr := Attr} = cowboy_req:match_qs([attr], Req),
 	<<"host_and_port">> = Attr,
 	<<"host_and_port">> = Attr,
 	Host = cowboy_req:host(Req),
 	Host = cowboy_req:host(Req),
 	Port = cowboy_req:port(Req),
 	Port = cowboy_req:port(Req),

+ 1 - 1
test/http_SUITE_data/rest_resource_etags.erl

@@ -9,7 +9,7 @@ init(Req, Opts) ->
 	{cowboy_rest, Req, Opts}.
 	{cowboy_rest, Req, Opts}.
 
 
 generate_etag(Req, State) ->
 generate_etag(Req, State) ->
-	#{type := Type} = cowboy_req:match_qs(Req, [type]),
+	#{type := Type} = cowboy_req:match_qs([type], Req),
 	case Type of
 	case Type of
 		%% Correct return values from generate_etag/2.
 		%% Correct return values from generate_etag/2.
 		<<"tuple-weak">> ->
 		<<"tuple-weak">> ->