Skip to content

Commit

Permalink
a hack to get numeric-indexed deepprops working
Browse files Browse the repository at this point in the history
- don't understand what the original insert_into
  idea for integer indices was
- we don't convert anything to integer and expect
  all the keys to be strings (even if the value itself
  is integer)
- to fix that quickly, we included a deepprops library
  to make nested proplists for us

WARNING: This is not intended to be a definitive solution,
it's just a hack to bring up the expected functionality
to project that relies on it.
  • Loading branch information
ztmr committed Jan 7, 2014
1 parent 41f0c32 commit d767320
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
3 changes: 2 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@

{deps, [
{mimetypes, ".*", {git, "git://github.com/spawngrid/mimetypes.git", "HEAD"}},
{pmod_transform, ".*", {git, "git://github.com/choptastic/pmod_transform.git", "HEAD"}}
{pmod_transform, ".*", {git, "git://github.com/choptastic/pmod_transform.git", "HEAD"}},
{deepprops, ".*", {git, "https://github.com/keynslug/deepprops.git", "HEAD"}}
]}.
22 changes: 1 addition & 21 deletions src/simple_bridge_request_wrapper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,6 @@ parse_deep_post_params([{Key, Value}|Rest], Acc) ->
parse_deep_post_params(Rest, [{Key, Value}|Acc]);
{match, [KeyName, Path]} ->
PathList = re:split(Path, "\\]\\[", [{return, list}]),
parse_deep_post_params(Rest, insert_into(Acc, [KeyName|PathList], Value))
parse_deep_post_params(Rest, deepprops:set ([KeyName|PathList], Value, Acc))
end.

insert_into(_List, [], Value) ->
Value;
insert_into(undefined, PathList, Value) ->
insert_into([], PathList, Value);
insert_into(N, PathList, Value) when is_integer(N) ->
insert_into([], PathList, Value);
insert_into(List, [ThisKey|Rest], Value) ->
case catch list_to_integer(ThisKey) of
{'EXIT', _} ->
ExistingVal = proplists:get_value(ThisKey, List),
[{ThisKey, insert_into(ExistingVal, Rest, Value)}|
proplists:delete(ThisKey, List)];
N when N < erlang:length(List) ->
ExistingVal = lists:nth(N+1, List),
lists:sublist(List, N) ++ [insert_into(ExistingVal, Rest, Value)|
lists:nthtail(N+1, List)];
N when N >= erlang:length(List) ->
List ++ lists:reverse([insert_into(undefined, Rest, Value)|
lists:seq(0, N - erlang:length(List) - 1)])
end.
15 changes: 15 additions & 0 deletions test/request_wrapper_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-module (request_wrapper_tests).

-include_lib ("eunit/include/eunit.hrl").

parse_deep_post_params_test_ () ->
DummyReq = simple_bridge_request_wrapper:new ([], [], [], [], [], []),
Data = [
{"x", "whatever", [{"x", "whatever"}]},
{"x[id]", "whatever", [{"x", [{"id", "whatever"}]}]},
{"x[id][3]", "whatever", [{"x", [{"id", [{"3", "whatever"}]}]}]}
],
[ ?assertEqual (Expect, DummyReq:parse_deep_post_params ([{K, V}], []))
|| {K, V, Expect} <- Data ],
ok.

0 comments on commit d767320

Please sign in to comment.