Skip to content

Commit

Permalink
Merge pull request #6 from edgurgel/add-access-to-arrays
Browse files Browse the repository at this point in the history
Add access support to arrays through Solid.Argument
  • Loading branch information
edgurgel authored Feb 27, 2017
2 parents 725fdfa + d1356d4 commit 92ca4f2
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 5 deletions.
16 changes: 14 additions & 2 deletions lib/solid/argument.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,23 @@ defmodule Solid.Argument do
123
iex> Solid.Argument.get({:value, "value"}, %Solid.Context{})
"value"
iex> Solid.Argument.get({:field, "key", [{:access, 1},{:access, 1}]}, %Solid.Context{vars: %{"key" => [1, [1,2,3], 3]}})
2
iex> Solid.Argument.get({:field, "key", [{:access, 1}]}, %Solid.Context{vars: %{"key" => "a string"}})
nil
"""
@spec get({:field, String.t} | {:value, term}, Context.t) :: term
@spec get({:field, String.t} | {:field, String.t, [{:access, non_neg_integer}]} | {:value, term}, Context.t) :: term
def get({:value, val}, _hash), do: val
def get({:field, key}, %Context{vars: vars}) do
key = key |> String.split(".")
get_in(vars, key)
end
def get({:value, val}, _hash), do: val
def get({:field, key, accesses}, context) do
value = get({:field, key}, context)
Enum.reduce(accesses, value,
fn {:access, index}, acc when is_list(acc) ->
Enum.at(acc, index)
_, _ -> nil
end)
end
end
2 changes: 1 addition & 1 deletion lib/solid/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ defmodule Solid.Tag do
end
end

defp do_eval({:assign_exp, field, argument}, context) do
defp do_eval({:assign_exp, {:field, field}, argument}, context) do
context = %{context | vars: Map.put(context.vars, field, Argument.get(argument, context))}
{nil, context}
end
Expand Down
15 changes: 13 additions & 2 deletions src/solid_parser.peg
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,19 @@ case Node of
[Arg1, Args] -> [Arg1 | [Arg || [_, _, _, Arg] <- Args]]
end
`;
argument <- value:value / field:field ~;
argument <- value:value / field ~;
field <- [0-9a-zA-Z\.]* access* `
case Node of
[FieldName | [[]]] -> {field, iolist_to_binary(FieldName)};
[FieldName | [Accesses]] ->
{field, iolist_to_binary(FieldName), Accesses}
end
`;
access <- "[" int "]" `
case Node of
[_, Integer, _] -> {access, list_to_integer(binary_to_list(iolist_to_binary(Integer)))}
end
`;
value <- space (string / number / true / false / null) space `lists:nth(2, Node)`;
string <- single_quoted_string / double_quoted_string `
iolist_to_binary(proplists:get_value(string, Node))
Expand All @@ -136,7 +148,6 @@ case <- "case" ~;
when <- "when" ~;
endcase <- "endcase" ~;
assign <- "assign" ~;
field <- [0-9a-zA-Z\.]* `iolist_to_binary(Node)`;
filter <- [a-zA-Z]* `iolist_to_binary(Node)`;
space <- [ \t\n\s\r]* `{space, iolist_to_binary(Node)}`;

Expand Down
1 change: 1 addition & 0 deletions test/cases/023/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "var" : [1, 2, 3]}
1 change: 1 addition & 0 deletions test/cases/023/input.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ var[1] }}
1 change: 1 addition & 0 deletions test/cases/024/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "var" : [["a", "b", "c"], [1, 2, 3]]}
3 changes: 3 additions & 0 deletions test/cases/024/input.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ var[0][1] }}
{{ var[1][1] }}
{{ var[12] }}
1 change: 1 addition & 0 deletions test/cases/025/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions test/cases/025/input.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ var[0][1] }}
{{ var[1][1] }}
{{ var[12] }}
1 change: 1 addition & 0 deletions test/cases/026/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "var" : "string" }
3 changes: 3 additions & 0 deletions test/cases/026/input.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ var[0][1] }}
{{ var[1][1] }}
{{ var[12] }}
1 change: 1 addition & 0 deletions test/cases/027/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "var" : ["a", "b", "c"] }
1 change: 1 addition & 0 deletions test/cases/027/input.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ var[1] | upcase }}
1 change: 1 addition & 0 deletions test/cases/028/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "var" : ["a", "b", "c"] }
3 changes: 3 additions & 0 deletions test/cases/028/input.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% if var[1] == "b" %}
letter b
{% endif %}

0 comments on commit 92ca4f2

Please sign in to comment.