From 5d5d2b9a4b412d9ab4f1a889621864ca47ba2d9b Mon Sep 17 00:00:00 2001 From: Anton Kalyaev Date: Thu, 16 Apr 2015 14:09:57 +0300 Subject: [PATCH] correctly handle empty lists before this commit it was throwing an error: ``` {error, {index_for_non_list,{1,[]}}, [{ej,get_value,2,[{file,"src/ej.erl"},{line,157}]} ``` --- src/ej.erl | 8 ++++++++ test/ej_alt_test.erl | 7 ++++++- test/menu.alt_terms | 4 +++- test/menu.json | 3 ++- test/menu.terms | 4 +++- test/widget.alt_terms | 1 + test/widget.json | 1 + test/widget.terms | 1 + 8 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/ej.erl b/src/ej.erl index 6433c32..45e287a 100644 --- a/src/ej.erl +++ b/src/ej.erl @@ -139,6 +139,14 @@ get_value(Key, []) when is_binary(Key) -> undefined; get_value(Key, null) when is_binary(Key) -> undefined; +get_value(first, []) -> + undefined; +get_value(last, []) -> + undefined; +get_value(Index, []) when is_integer(Index) -> + undefined; +get_value({select, _KeyValue}, []) -> + {from_select, []}; get_value(first, [H|_T]) -> H; get_value(last, List=[_H|_T]) -> diff --git a/test/ej_alt_test.erl b/test/ej_alt_test.erl index e5f816e..8ea0fc8 100644 --- a/test/ej_alt_test.erl +++ b/test/ej_alt_test.erl @@ -24,6 +24,9 @@ ej_alt_test_() -> ?_assertEqual(4, ej:get({"widget", "values", 4}, Widget)), ?_assertEqual(1, ej:get({"widget", "values", first}, Widget)), ?_assertEqual(5, ej:get({"widget", "values", last}, Widget)), + ?_assertEqual(undefined, ej:get({"widget", "keys", first}, Widget)), + ?_assertEqual(undefined, ej:get({"widget", "keys", last}, Widget)), + ?_assertEqual(undefined, ej:get({"widget", "keys", 2}, Widget)), ?_assertEqual({[{<<"id">>, 5}]}, ej:get({<<"objects">>, last}, ObjList)), ?_assertEqual({[{<<"id">>, 1}]}, @@ -65,7 +68,9 @@ ej_alt_test_() -> ComplexBeginning = {{select, {"match", "me"}}}, ?assertMatch([{_}, {_}], ej:get(ComplexBeginning, Data)), ComplexBeginningDeeper = {{select, {"match", "me"}}, "match"}, - ?assertMatch([<<"me">>, <<"me">>], ej:get(ComplexBeginningDeeper, Data)) + ?assertMatch([<<"me">>, <<"me">>], ej:get(ComplexBeginningDeeper, Data)), + PathAgainstEmptyList = {"menu", "popup", "titleitem", {select, {"value", "Title"}}}, + ?assertMatch([], ej:get(PathAgainstEmptyList, Menu)) end}, {"ej:get with multi-level array matching", fun() -> diff --git a/test/menu.alt_terms b/test/menu.alt_terms index bf974b9..9dcd178 100644 --- a/test/menu.alt_terms +++ b/test/menu.alt_terms @@ -6,4 +6,6 @@ [{[{<<"value">>,<<"New">>},{<<"onclick">>,<<"CreateNewDoc()">>}]}, {[{<<"value">>,<<"Open">>},{<<"onclick">>,<<"OpenDoc()">>}]}, {[{<<"value">>,<<"Close">>}, - {<<"onclick">>,<<"CloseDoc()">>}]}]}]}}]}}]}. + {<<"onclick">>,<<"CloseDoc()">>}]}]}, + {<<"titleitem">>, + []}]}}]}}]}. diff --git a/test/menu.json b/test/menu.json index 2689d94..e85c284 100644 --- a/test/menu.json +++ b/test/menu.json @@ -6,7 +6,8 @@ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} - ] + ], + "titleitem": [] } }} diff --git a/test/menu.terms b/test/menu.terms index d3e8ba3..a2a9378 100644 --- a/test/menu.terms +++ b/test/menu.terms @@ -14,4 +14,6 @@ {<<"onclick">>,<<"OpenDoc()">>}]}, {struct, [{<<"value">>,<<"Close">>}, - {<<"onclick">>,<<"CloseDoc()">>}]}]}]}}]}}]}. + {<<"onclick">>,<<"CloseDoc()">>}]}]}, + {<<"titleitem">>, + []}]}}]}}]}. diff --git a/test/widget.alt_terms b/test/widget.alt_terms index b021c50..84dc522 100644 --- a/test/widget.alt_terms +++ b/test/widget.alt_terms @@ -1,6 +1,7 @@ {[{<<"widget">>, {[{<<"version">>,<<"1">>}, {<<"values">>,[1,2,3,4,5]}, + {<<"keys">>,[]}, {<<"debug">>,<<"on">>}, {<<"window">>, {[{<<"title">>,<<"Sample Konfabulator Widget">>}, diff --git a/test/widget.json b/test/widget.json index a043687..d9efe38 100644 --- a/test/widget.json +++ b/test/widget.json @@ -1,6 +1,7 @@ {"widget": { "version": "1", "values": [1,2,3,4,5], + "keys": [], "debug": "on", "window": { "title": "Sample Konfabulator Widget", diff --git a/test/widget.terms b/test/widget.terms index f5f5f01..37ac66b 100644 --- a/test/widget.terms +++ b/test/widget.terms @@ -1,6 +1,7 @@ {struct,[{<<"widget">>, {struct,[{<<"version">>,<<"1">>}, {<<"values">>,[1,2,3,4,5]}, + {<<"keys">>,[]}, {<<"debug">>,<<"on">>}, {<<"window">>, {struct,[{<<"title">>,<<"Sample Konfabulator Widget">>},