aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2017-02-19 16:51:16 +0100
committerLoïc Hoguin <[email protected]>2017-02-19 16:51:16 +0100
commit9255cdf1d783ff5deff783ae13b0d003f484bb86 (patch)
tree352ae4164e8ebb1eac9d11448fd095b4231795e6 /src
parent91ae70b06c9cc486ea2c2cf91b94de799ceb53b2 (diff)
downloadcowboy-9255cdf1d783ff5deff783ae13b0d003f484bb86.tar.gz
cowboy-9255cdf1d783ff5deff783ae13b0d003f484bb86.tar.bz2
cowboy-9255cdf1d783ff5deff783ae13b0d003f484bb86.zip
Change the type of bindings from a list to a map
Maps make more sense because the keys are unique.
Diffstat (limited to 'src')
-rw-r--r--src/cowboy_req.erl10
-rw-r--r--src/cowboy_router.erl30
2 files changed, 19 insertions, 21 deletions
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index ca31105..ffc6e12 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -326,18 +326,18 @@ binding(Name, Req) ->
-spec binding(atom(), req(), Default) -> any() | Default when Default::any().
binding(Name, #{bindings := Bindings}, Default) when is_atom(Name) ->
- case lists:keyfind(Name, 1, Bindings) of
- {_, Value} -> Value;
- false -> Default
+ case Bindings of
+ #{Name := Value} -> Value;
+ _ -> Default
end;
binding(Name, _, Default) when is_atom(Name) ->
Default.
--spec bindings(req()) -> [{atom(), any()}].
+-spec bindings(req()) -> cowboy_router:bindings().
bindings(#{bindings := Bindings}) ->
Bindings;
bindings(_) ->
- [].
+ #{}.
-spec header(binary(), req()) -> binary() | undefined.
header(Name, Req) ->
diff --git a/src/cowboy_router.erl b/src/cowboy_router.erl
index d15f938..7e70025 100644
--- a/src/cowboy_router.erl
+++ b/src/cowboy_router.erl
@@ -28,7 +28,7 @@
-export([compile/1]).
-export([execute/2]).
--type bindings() :: [{atom(), binary()}].
+-type bindings() :: #{atom() => any()}.
-type tokens() :: [binary()].
-export_type([bindings/0]).
-export_type([tokens/0]).
@@ -218,10 +218,10 @@ match([], _, _) ->
{error, notfound, host};
%% If the host is '_' then there can be no constraints.
match([{'_', [], PathMatchs}|_Tail], _, Path) ->
- match_path(PathMatchs, undefined, Path, []);
+ match_path(PathMatchs, undefined, Path, #{});
match([{HostMatch, Fields, PathMatchs}|Tail], Tokens, Path)
when is_list(Tokens) ->
- case list_match(Tokens, HostMatch, []) of
+ case list_match(Tokens, HostMatch, #{}) of
false ->
match(Tail, Tokens, Path);
{true, Bindings, HostInfo} ->
@@ -276,21 +276,19 @@ check_constraints([Field|Tail], Bindings) when is_atom(Field) ->
check_constraints(Tail, Bindings);
check_constraints([Field|Tail], Bindings) ->
Name = element(1, Field),
- case lists:keyfind(Name, 1, Bindings) of
- false ->
- check_constraints(Tail, Bindings);
- {_, Value} ->
+ case Bindings of
+ #{Name := Value} ->
Constraints = element(2, Field),
case cowboy_constraints:validate(Value, Constraints) of
true ->
check_constraints(Tail, Bindings);
{true, Value2} ->
- Bindings2 = lists:keyreplace(Name, 1, Bindings,
- {Name, Value2}),
- check_constraints(Tail, Bindings2);
+ check_constraints(Tail, Bindings#{Name => Value2});
false ->
nomatch
- end
+ end;
+ _ ->
+ check_constraints(Tail, Bindings)
end.
-spec split_host(binary()) -> tokens().
@@ -369,13 +367,13 @@ list_match([E|Tail], [E|TailMatch], Binds) ->
%% Bind E to the variable name V and continue,
%% unless V was already defined and E isn't identical to the previous value.
list_match([E|Tail], [V|TailMatch], Binds) when is_atom(V) ->
- case lists:keyfind(V, 1, Binds) of
- {_, E} ->
+ case Binds of
+ #{V := E} ->
list_match(Tail, TailMatch, Binds);
- {_, _} ->
+ #{V := _} ->
false;
- false ->
- list_match(Tail, TailMatch, [{V, E}|Binds])
+ _ ->
+ list_match(Tail, TailMatch, Binds#{V => E})
end;
%% Match complete.
list_match([], [], Binds) ->