actually i can use flatten in jsx_to_term! sweet

This commit is contained in:
alisdair sullivan 2014-12-09 08:05:48 +00:00
parent 371b49a9e3
commit 5843dbc56a

View file

@ -23,7 +23,7 @@
-module(jsx_to_term). -module(jsx_to_term).
-export([to_term/2]). -export([to_term/2, flatify/1]).
-export([init/1, handle_event/2]). -export([init/1, handle_event/2]).
-export([ -export([
start_term/1, start_term/1,
@ -172,10 +172,10 @@ start_array({Stack, Config}) -> {[{array, []}] ++ Stack, Config}.
%% return it if it is the root object %% return it if it is the root object
finish({[{object, []}], Config}) -> {[{}], Config}; finish({[{object, []}], Config}) -> {[{}], Config};
finish({[{object, []}|Rest], Config}) -> insert([{}], {Rest, Config}); finish({[{object, []}|Rest], Config}) -> insert([{}], {Rest, Config});
finish({[{object, Pairs}], Config}) -> {Pairs, Config}; finish({[{object, Pairs}], Config}) -> {flatify(Pairs), Config};
finish({[{object, Pairs}|Rest], Config}) -> insert(Pairs, {Rest, Config}); finish({[{object, Pairs}|Rest], Config}) -> insert(flatify(Pairs), {Rest, Config});
finish({[{array, Values}], Config}) -> {Values, Config}; finish({[{array, Values}], Config}) -> {flatify(Values), Config};
finish({[{array, Values}|Rest], Config}) -> insert(Values, {Rest, Config}); finish({[{array, Values}|Rest], Config}) -> insert(flatify(Values), {Rest, Config});
finish(_) -> erlang:error(badarg). finish(_) -> erlang:error(badarg).
@ -185,9 +185,9 @@ insert(Value, {[], Config}) -> {Value, Config};
insert(Key, {[{object, Pairs}|Rest], Config}) -> insert(Key, {[{object, Pairs}|Rest], Config}) ->
{[{object, Key, Pairs}] ++ Rest, Config}; {[{object, Key, Pairs}] ++ Rest, Config};
insert(Value, {[{object, Key, Pairs}|Rest], Config}) -> insert(Value, {[{object, Key, Pairs}|Rest], Config}) ->
{[{object, Pairs ++ [{Key, Value}]}] ++ Rest, Config}; {[{object, [Pairs, {Key, Value}]}] ++ Rest, Config};
insert(Value, {[{array, Values}|Rest], Config}) -> insert(Value, {[{array, Values}|Rest], Config}) ->
{[{array, Values ++ [Value]}] ++ Rest, Config}; {[{array, [Values, Value]}] ++ Rest, Config};
insert(_, _) -> erlang:error(badarg). insert(_, _) -> erlang:error(badarg).
-endif. -endif.
@ -212,10 +212,10 @@ finish({[{object, Map}|Rest], Config=#config{return_maps=true}}) ->
insert(Map, {Rest, Config}); insert(Map, {Rest, Config});
finish({[{object, []}], Config}) -> {[{}], Config}; finish({[{object, []}], Config}) -> {[{}], Config};
finish({[{object, []}|Rest], Config}) -> insert([{}], {Rest, Config}); finish({[{object, []}|Rest], Config}) -> insert([{}], {Rest, Config});
finish({[{object, Pairs}], Config}) -> {Pairs, Config}; finish({[{object, Pairs}], Config}) -> {flatify(Pairs), Config};
finish({[{object, Pairs}|Rest], Config}) -> insert(Pairs, {Rest, Config}); finish({[{object, Pairs}|Rest], Config}) -> insert(flatify(Pairs), {Rest, Config});
finish({[{array, Values}], Config}) -> {Values, Config}; finish({[{array, Values}], Config}) -> {flatify(Values), Config};
finish({[{array, Values}|Rest], Config}) -> insert(Values, {Rest, Config}); finish({[{array, Values}|Rest], Config}) -> insert(flatify(Values), {Rest, Config});
finish(_) -> erlang:error(badarg). finish(_) -> erlang:error(badarg).
@ -229,9 +229,9 @@ insert(Key, {[{object, Pairs}|Rest], Config}) ->
insert(Value, {[{object, Key, Map}|Rest], Config=#config{return_maps=true}}) -> insert(Value, {[{object, Key, Map}|Rest], Config=#config{return_maps=true}}) ->
{[{object, maps:put(Key, Value, Map)}] ++ Rest, Config}; {[{object, maps:put(Key, Value, Map)}] ++ Rest, Config};
insert(Value, {[{object, Key, Pairs}|Rest], Config}) -> insert(Value, {[{object, Key, Pairs}|Rest], Config}) ->
{[{object, Pairs ++ [{Key, Value}]}] ++ Rest, Config}; {[{object, [Pairs, {Key, Value}]}] ++ Rest, Config};
insert(Value, {[{array, Values}|Rest], Config}) -> insert(Value, {[{array, Values}|Rest], Config}) ->
{[{array, Values ++ [Value]}] ++ Rest, Config}; {[{array, [Values, Value]}] ++ Rest, Config};
insert(_, _) -> erlang:error(badarg). insert(_, _) -> erlang:error(badarg).
-endif. -endif.
@ -244,6 +244,15 @@ get_value({Value, _Config}) -> Value;
get_value(_) -> erlang:error(badarg). get_value(_) -> erlang:error(badarg).
%% we know the structure of our accumulator so we can safely
%% flatten like this
flatify(List) -> flatify(List, []).
%% head of list should always be []
flatify([], Tail) -> Tail;
flatify([H, T], Tail) -> flatify(H, [T] ++ Tail).
%% eunit tests %% eunit tests
-ifdef(TEST). -ifdef(TEST).
@ -331,32 +340,32 @@ rep_manipulation_test_() ->
get_key({[{array, []}], #config{}}) get_key({[{array, []}], #config{}})
)}, )},
{"insert a value into an object", ?_assertEqual( {"insert a value into an object", ?_assertEqual(
{[{object, [{key, value}]}, junk], #config{}}, {[{object, [[], {key, value}]}, junk], #config{}},
insert(value, {[{object, key, []}, junk], #config{}}) insert(value, {[{object, key, []}, junk], #config{}})
)}, )},
{"insert a value into an array", ?_assertEqual( {"insert a value into an array", ?_assertEqual(
{[{array, [value]}, junk], #config{}}, {[{array, [[], value]}, junk], #config{}},
insert(value, {[{array, []}, junk], #config{}}) insert(value, {[{array, []}, junk], #config{}})
)}, )},
{"finish an object with no ancestor", ?_assertEqual( {"finish an object with no ancestor", ?_assertEqual(
{[{x, y}, {a, b}], #config{}}, {[{x, y}, {a, b}], #config{}},
finish({[{object, [{x, y}, {a, b}]}], #config{}}) finish({[{object, [[[], {x, y}], {a, b}]}], #config{}})
)}, )},
{"finish an empty object", ?_assertEqual( {"finish an empty object", ?_assertEqual(
{[{}], #config{}}, {[{}], #config{}},
finish({[{object, []}], #config{}}) finish({[{object, []}], #config{}})
)}, )},
{"finish an object with an ancestor", ?_assertEqual( {"finish an object with an ancestor", ?_assertEqual(
{[{object, [{foo, bar}, {key, [{x, y}, {a, b}]}]}], #config{}}, {[{object, [[[], {foo, bar}], {key, [{x, y}, {a, b}]}]}], #config{}},
finish({[{object, [{x, y}, {a, b}]}, {object, key, [{foo, bar}]}], #config{}}) finish({[{object, [[[], {x, y}], {a, b}]}, {object, key, [[], {foo, bar}]}], #config{}})
)}, )},
{"finish an array with no ancestor", ?_assertEqual( {"finish an array with no ancestor", ?_assertEqual(
{[a, b, c], #config{}}, {[a, b, c], #config{}},
finish({[{array, [a, b, c]}], #config{}}) finish({[{array, [[[[], a], b], c]}], #config{}})
)}, )},
{"finish an array with an ancestor", ?_assertEqual( {"finish an array with an ancestor", ?_assertEqual(
{[{array, [d, e, f, [a, b, c]]}], #config{}}, {[{array,[[[[[],d],e],f],[a,b,c]]}], #config{}},
finish({[{array, [a, b, c]}, {array, [d, e, f]}], #config{}}) finish({[{array, [[[[], a], b], c]}, {array, [[[[], d], e], f]}], #config{}})
)} )}
]. ].