Skip to content

Commit

Permalink
Adds support for empty per spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohan Relan authored and edgurgel committed Jan 7, 2025
1 parent 525696e commit 3fd9d33
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/solid/expression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ defmodule Solid.Expression do
def eval({v1, :<, nil}) when is_number(v1), do: false
def eval({nil, :>=, v2}) when is_number(v2), do: false
def eval({nil, :>, v2}) when is_number(v2), do: false
def eval({v1, :==, :empty}) when is_list(v1), do: v1 == []
def eval({v1, :!=, :empty}) when is_list(v1), do: v1 != []
def eval({v1, :==, :empty}) when is_binary(v1), do: v1 == ""
def eval({v1, :!=, :empty}) when is_binary(v1), do: v1 != ""
def eval({v1, op, v2}), do: apply(Kernel, op, [v1, v2])

def eval(value) do
Expand Down
5 changes: 5 additions & 0 deletions lib/solid/parser/literal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ defmodule Solid.Parser.Literal do
string("nil")
|> replace(nil)

empty =
string("empty")
|> replace(:empty)

frac =
string(".")
|> concat(integer(min: 1))
Expand All @@ -72,6 +76,7 @@ defmodule Solid.Parser.Literal do
true_value,
false_value,
null,
empty,
single_quoted_string(),
double_quoted_string()
])
Expand Down
3 changes: 2 additions & 1 deletion test/cases/if/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
}
},
"enabled?" : true,
"enabled2?" : false
"enabled2?" : false,
"var4": ""
}
6 changes: 6 additions & 0 deletions test/cases/if/input.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@

{% if array contains 1 %} true {% else %} false {% endif %}


{% if array == empty %} true {% else %} false {% endif %}
{% if array != empty %} true {% else %} false {% endif %}
{% if var4 == empty %} true {% else %} false {% endif %}
{% if var4 != empty %} true {% else %} false {% endif %}

{% if string-array[1] == "b" %}
letter b
{% endif %}
Expand Down
6 changes: 3 additions & 3 deletions test/cases/object/input.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
{{ multiarray[12] }}
{{ multiarray[-1][1] }}

{{ empty[0][1] }}
{{ empty[1][1] }}
{{ empty[12] }}
{{ nullobj[0][1] }}
{{ nullobj[1][1] }}
{{ nullobj[12] }}

{{ string[0][1] }}
{{ string[1][1] }}
Expand Down

0 comments on commit 3fd9d33

Please sign in to comment.