Skip to content

Commit ec97173

Browse files
fix: don't raise when an operator on two literals yields a struct (ash-project#2847)
1 parent dcf2551 commit ec97173

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

lib/ash/filter/filter.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4086,10 +4086,13 @@ defmodule Ash.Filter do
40864086
end
40874087
end
40884088

4089-
defp maybe_operator_expression(operator, context) when is_struct(operator) do
4089+
# Over two literals the operator is evaluated on construction, so the result arrives here.
4090+
defp maybe_operator_expression(
4091+
%{__operator__?: true, left: left, right: right} = operator,
4092+
context
4093+
) do
40904094
case Ash.Query.Operator.operator_expression(operator) do
40914095
{:ok, expr_module} ->
4092-
%{left: left, right: right} = operator
40934096
arguments = [left, right]
40944097
resource = context[:resource]
40954098

test/expr_test.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,36 @@ defmodule Ash.Test.ExprTest do
162162
end
163163
end
164164

165+
describe "operators over two literals" do
166+
test "a result that is not a struct" do
167+
assert eval!(expr(^1 + ^2)) == 3
168+
end
169+
170+
test "a decimal result" do
171+
assert eval!(expr(^Decimal.new(1) + ^Decimal.new(2))) == Decimal.new(3)
172+
end
173+
174+
test "a datetime result" do
175+
now = DateTime.utc_now()
176+
duration = Duration.new!(day: 7)
177+
178+
assert eval!(expr(^now + ^duration)) == DateTime.shift(now, duration)
179+
end
180+
181+
test "a date result" do
182+
today = Date.utc_today()
183+
duration = Duration.new!(day: 7)
184+
185+
assert eval!(expr(^today + ^duration)) == Date.shift(today, duration)
186+
end
187+
188+
test "a duration result" do
189+
duration = Duration.new!(day: 7)
190+
191+
assert eval!(expr(^duration + ^duration)) == Duration.add(duration, duration)
192+
end
193+
end
194+
165195
describe "rem expressions" do
166196
test "evaluates" do
167197
expr = expr(rem(1, 2) == 0)

0 commit comments

Comments
 (0)