Skip to content

Commit

Permalink
Return undefined instead of failing or returning "" when a value does…
Browse files Browse the repository at this point in the history
…n't exist.
  • Loading branch information
Loïc Hoguin committed Mar 27, 2011
1 parent d0d9b0e commit 150723c
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/cowboy_http_req.erl
Expand Up @@ -71,13 +71,15 @@ raw_path(Req) ->
{Req#http_req.raw_path, Req}.

-spec qs_val(Name::string(), Req::#http_req{})
-> {Value::string() | true, Req::#http_req{}}.
-> {Value::string() | true | undefined, Req::#http_req{}}.
qs_val(Name, Req=#http_req{raw_qs=RawQs, qs_vals=undefined}) ->
QsVals = parse_qs(RawQs),
qs_val(Name, Req#http_req{qs_vals=QsVals});
qs_val(Name, Req) ->
{Name, Value} = lists:keyfind(Name, 1, Req#http_req.qs_vals),
{Value, Req}.
case lists:keyfind(Name, 1, Req#http_req.qs_vals) of
{Name, Value} -> {Value, Req};
false -> {undefined, Req}
end.

-spec qs_val(Name::string(), Default::term(), Req::#http_req{})
-> {Value::string() | term() | true, Req::#http_req{}}.
Expand All @@ -101,10 +103,12 @@ raw_qs(Req) ->
{Req#http_req.raw_qs, Req}.

-spec binding(Name::atom(), Req::#http_req{})
-> {Value::string(), Req::#http_req{}}.
-> {Value::string() | undefined, Req::#http_req{}}.
binding(Name, Req) ->
{Name, Value} = lists:keyfind(Name, 1, Req#http_req.bindings),
{Value, Req}.
case lists:keyfind(Name, 1, Req#http_req.bindings) of
{Name, Value} -> {Value, Req};
false -> {undefined, Req}
end.

-spec binding(Name::atom(), Default::term(), Req::#http_req{})
-> {Value::string() | term(), Req::#http_req{}}.
Expand All @@ -118,11 +122,11 @@ bindings(Req) ->
{Req#http_req.bindings, Req}.

-spec header(Name::atom() | string(), Req::#http_req{})
-> {Value::string(), Req::#http_req{}}.
-> {Value::string() | undefined, Req::#http_req{}}.
header(Name, Req) ->
case lists:keyfind(Name, 1, Req#http_req.headers) of
{Name, Value} -> {Value, Req};
false -> {"", Req}
false -> {undefined, Req}
end.

-spec header(Name::atom() | string(), Default::term(), Req::#http_req{})
Expand Down

0 comments on commit 150723c

Please sign in to comment.