From 5843dbc56a6da9e6a186dc571b0d2bd1c11ec35c Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Tue, 9 Dec 2014 08:05:48 +0000 Subject: [PATCH] actually i can use flatten in `jsx_to_term`! sweet --- src/jsx_to_term.erl | 51 ++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/jsx_to_term.erl b/src/jsx_to_term.erl index 5a628ea..6dc0c85 100644 --- a/src/jsx_to_term.erl +++ b/src/jsx_to_term.erl @@ -23,7 +23,7 @@ -module(jsx_to_term). --export([to_term/2]). +-export([to_term/2, flatify/1]). -export([init/1, handle_event/2]). -export([ start_term/1, @@ -172,10 +172,10 @@ start_array({Stack, Config}) -> {[{array, []}] ++ Stack, Config}. %% return it if it is the root object finish({[{object, []}], Config}) -> {[{}], Config}; finish({[{object, []}|Rest], Config}) -> insert([{}], {Rest, Config}); -finish({[{object, Pairs}], Config}) -> {Pairs, Config}; -finish({[{object, Pairs}|Rest], Config}) -> insert(Pairs, {Rest, Config}); -finish({[{array, Values}], Config}) -> {Values, Config}; -finish({[{array, Values}|Rest], Config}) -> insert(Values, {Rest, Config}); +finish({[{object, Pairs}], Config}) -> {flatify(Pairs), Config}; +finish({[{object, Pairs}|Rest], Config}) -> insert(flatify(Pairs), {Rest, Config}); +finish({[{array, Values}], Config}) -> {flatify(Values), Config}; +finish({[{array, Values}|Rest], Config}) -> insert(flatify(Values), {Rest, Config}); finish(_) -> erlang:error(badarg). @@ -185,9 +185,9 @@ insert(Value, {[], Config}) -> {Value, Config}; insert(Key, {[{object, Pairs}|Rest], Config}) -> {[{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}) -> - {[{array, Values ++ [Value]}] ++ Rest, Config}; + {[{array, [Values, Value]}] ++ Rest, Config}; insert(_, _) -> erlang:error(badarg). -endif. @@ -212,10 +212,10 @@ finish({[{object, Map}|Rest], Config=#config{return_maps=true}}) -> insert(Map, {Rest, Config}); finish({[{object, []}], Config}) -> {[{}], Config}; finish({[{object, []}|Rest], Config}) -> insert([{}], {Rest, Config}); -finish({[{object, Pairs}], Config}) -> {Pairs, Config}; -finish({[{object, Pairs}|Rest], Config}) -> insert(Pairs, {Rest, Config}); -finish({[{array, Values}], Config}) -> {Values, Config}; -finish({[{array, Values}|Rest], Config}) -> insert(Values, {Rest, Config}); +finish({[{object, Pairs}], Config}) -> {flatify(Pairs), Config}; +finish({[{object, Pairs}|Rest], Config}) -> insert(flatify(Pairs), {Rest, Config}); +finish({[{array, Values}], Config}) -> {flatify(Values), Config}; +finish({[{array, Values}|Rest], Config}) -> insert(flatify(Values), {Rest, Config}); 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}}) -> {[{object, maps:put(Key, Value, Map)}] ++ 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}) -> - {[{array, Values ++ [Value]}] ++ Rest, Config}; + {[{array, [Values, Value]}] ++ Rest, Config}; insert(_, _) -> erlang:error(badarg). -endif. @@ -244,6 +244,15 @@ get_value({Value, _Config}) -> Value; 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 -ifdef(TEST). @@ -331,32 +340,32 @@ rep_manipulation_test_() -> get_key({[{array, []}], #config{}}) )}, {"insert a value into an object", ?_assertEqual( - {[{object, [{key, value}]}, junk], #config{}}, + {[{object, [[], {key, value}]}, junk], #config{}}, insert(value, {[{object, key, []}, junk], #config{}}) )}, {"insert a value into an array", ?_assertEqual( - {[{array, [value]}, junk], #config{}}, + {[{array, [[], value]}, junk], #config{}}, insert(value, {[{array, []}, junk], #config{}}) )}, {"finish an object with no ancestor", ?_assertEqual( {[{x, y}, {a, b}], #config{}}, - finish({[{object, [{x, y}, {a, b}]}], #config{}}) + finish({[{object, [[[], {x, y}], {a, b}]}], #config{}}) )}, {"finish an empty object", ?_assertEqual( {[{}], #config{}}, finish({[{object, []}], #config{}}) )}, {"finish an object with an ancestor", ?_assertEqual( - {[{object, [{foo, bar}, {key, [{x, y}, {a, b}]}]}], #config{}}, - finish({[{object, [{x, y}, {a, b}]}, {object, key, [{foo, bar}]}], #config{}}) + {[{object, [[[], {foo, bar}], {key, [{x, y}, {a, b}]}]}], #config{}}, + finish({[{object, [[[], {x, y}], {a, b}]}, {object, key, [[], {foo, bar}]}], #config{}}) )}, {"finish an array with no ancestor", ?_assertEqual( {[a, b, c], #config{}}, - finish({[{array, [a, b, c]}], #config{}}) + finish({[{array, [[[[], a], b], c]}], #config{}}) )}, {"finish an array with an ancestor", ?_assertEqual( - {[{array, [d, e, f, [a, b, c]]}], #config{}}, - finish({[{array, [a, b, c]}, {array, [d, e, f]}], #config{}}) + {[{array,[[[[[],d],e],f],[a,b,c]]}], #config{}}, + finish({[{array, [[[[], a], b], c]}, {array, [[[[], d], e], f]}], #config{}}) )} ].