Skip to content

Commit

Permalink
improvement: support providing an input key in predicate maps
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Dec 21, 2023
1 parent 097f8b0 commit 3a1923f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 16 additions & 2 deletions lib/ash/filter/filter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2569,13 +2569,23 @@ defmodule Ash.Filter do
{input || %{}, nested}

nested ->
{%{}, nested}
cond do
is_map(nested) and Map.has_key?(nested, "input") ->
{Map.get(nested, "input"), Map.delete(nested, "input")}

is_map(nested) and Map.has_key?(nested, :input) ->
{Map.get(nested, :input), Map.delete(nested, :input)}

true ->
{%{}, nested}
end
end

with {:ok, args} <-
Ash.Query.validate_calculation_arguments(
resource_calculation,
input
input,
!context[:input?]
),
{:ok, calculation} <-
Calculation.new(
Expand Down Expand Up @@ -3441,6 +3451,10 @@ defmodule Ash.Filter do
parse_predicates([eq: value], field, context)
end

defp parse_predicates(value, field, context) when value == %{} do
parse_predicates([eq: true], field, context)
end

defp parse_predicates(%struct{} = value, field, context)
when struct not in [Not, BooleanExpression, Ref, Call] do
parse_predicates([eq: value], field, context)
Expand Down
4 changes: 2 additions & 2 deletions lib/ash/query/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ defmodule Ash.Query do
end

@doc false
def validate_calculation_arguments(calculation, args) do
def validate_calculation_arguments(calculation, args, allow_expr? \\ true) do
args =
if Keyword.keyword?(args) do
Map.new(args)
Expand All @@ -1464,7 +1464,7 @@ defmodule Ash.Query do
)

cond do
expr?(value) && argument.allow_expr? ->
expr?(value) && argument.allow_expr? && allow_expr? ->
{:cont,
{:ok,
Map.put(
Expand Down

0 comments on commit 3a1923f

Please sign in to comment.