Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correctly handle empty lists #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/ej.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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]) ->
Expand Down
7 changes: 6 additions & 1 deletion test/ej_alt_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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}]},
Expand Down Expand Up @@ -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() ->
Expand Down
4 changes: 3 additions & 1 deletion test/menu.alt_terms
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
[{[{<<"value">>,<<"New">>},{<<"onclick">>,<<"CreateNewDoc()">>}]},
{[{<<"value">>,<<"Open">>},{<<"onclick">>,<<"OpenDoc()">>}]},
{[{<<"value">>,<<"Close">>},
{<<"onclick">>,<<"CloseDoc()">>}]}]}]}}]}}]}.
{<<"onclick">>,<<"CloseDoc()">>}]}]},
{<<"titleitem">>,
[]}]}}]}}]}.
3 changes: 2 additions & 1 deletion test/menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
],
"titleitem": []
}
}}

4 changes: 3 additions & 1 deletion test/menu.terms
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
{<<"onclick">>,<<"OpenDoc()">>}]},
{struct,
[{<<"value">>,<<"Close">>},
{<<"onclick">>,<<"CloseDoc()">>}]}]}]}}]}}]}.
{<<"onclick">>,<<"CloseDoc()">>}]}]},
{<<"titleitem">>,
[]}]}}]}}]}.
1 change: 1 addition & 0 deletions test/widget.alt_terms
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{[{<<"widget">>,
{[{<<"version">>,<<"1">>},
{<<"values">>,[1,2,3,4,5]},
{<<"keys">>,[]},
{<<"debug">>,<<"on">>},
{<<"window">>,
{[{<<"title">>,<<"Sample Konfabulator Widget">>},
Expand Down
1 change: 1 addition & 0 deletions test/widget.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{"widget": {
"version": "1",
"values": [1,2,3,4,5],
"keys": [],
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
Expand Down
1 change: 1 addition & 0 deletions test/widget.terms
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{struct,[{<<"widget">>,
{struct,[{<<"version">>,<<"1">>},
{<<"values">>,[1,2,3,4,5]},
{<<"keys">>,[]},
{<<"debug">>,<<"on">>},
{<<"window">>,
{struct,[{<<"title">>,<<"Sample Konfabulator Widget">>},
Expand Down