Skip to content

Commit

Permalink
Adds primitive form nested inputs parser (cowboy_controller_form:pars…
Browse files Browse the repository at this point in the history
…e/1)
  • Loading branch information
Yurii Rashkovskii committed May 3, 2012
1 parent db904de commit 206ee61
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/cowboy_controller_form.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-module(cowboy_controller_form).
-export([parse/1]).
-include_lib("cowboy/include/http.hrl").

parse(#http_req{} = Req) ->
{Form, _} = cowboy_http_req:body_qs(Req),
parse(Form);

parse(Form) ->
parse_1(Form, []).

parse_1([], Acc) ->
Acc;
parse_1([{Key, Value}|T], Acc) ->
Keys = binary:split(Key,[<<$[>>,<<$]>>],[global,trim]),
Acc1 = parse_field(Keys, Value, Acc),
parse_1(T, Acc1).

parse_field([], Value, _Acc) ->
Value;
parse_field([<<>>|T], Value, Acc) ->
parse_field(T, Value, Acc);
parse_field(L, Value, Acc) when not is_list(Acc) ->
parse_field(L, Value, []);
parse_field([H|T], Value, Acc) ->
Dict = proplists:get_value(H, Acc, []),
Dict1 = parse_field(T, Value, Dict),
lists:keystore(H, 1, Acc, {H, Dict1}).

0 comments on commit 206ee61

Please sign in to comment.