mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 05:23:03 +12:00
improvement: detect more parent expressions
This commit is contained in:
parent
7296874952
commit
4797e148ac
1 changed files with 29 additions and 4 deletions
|
@ -1564,24 +1564,49 @@ defmodule Ash.Actions.Load do
|
|||
end
|
||||
end
|
||||
|
||||
defp has_parent_expr?(%{filter: filter}, depth \\ 0) do
|
||||
defp has_parent_expr?(%{filter: filter, sort: sort}) do
|
||||
do_has_parent_expr?(filter) || has_parent_expr_in_sort?(sort)
|
||||
end
|
||||
|
||||
defp has_parent_expr_in_sort?(sort) do
|
||||
sort
|
||||
|> List.wrap()
|
||||
|> Enum.any?(fn
|
||||
# TODO: check any resource calculation references here
|
||||
atom when is_atom(atom) ->
|
||||
false
|
||||
|
||||
{atom, _} when is_atom(atom) ->
|
||||
false
|
||||
|
||||
%Ash.Query.Calculation{} = calculation ->
|
||||
expression = calculation.module.expression(calculation.opts, calculation.context)
|
||||
do_has_parent_expr?(expression)
|
||||
|
||||
{%Ash.Query.Calculation{} = calculation, _} ->
|
||||
expression = calculation.module.expression(calculation.opts, calculation.context)
|
||||
do_has_parent_expr?(expression)
|
||||
end)
|
||||
end
|
||||
|
||||
defp do_has_parent_expr?(filter, depth \\ 0) do
|
||||
not is_nil(
|
||||
Ash.Filter.find(filter, fn
|
||||
%Ash.Query.Call{name: :parent, args: [expr]} ->
|
||||
if depth == 0 do
|
||||
true
|
||||
else
|
||||
has_parent_expr?(expr, depth - 1)
|
||||
do_has_parent_expr?(expr, depth - 1)
|
||||
end
|
||||
|
||||
%Ash.Query.Exists{expr: expr} ->
|
||||
has_parent_expr?(expr, depth + 1)
|
||||
do_has_parent_expr?(expr, depth + 1)
|
||||
|
||||
%Ash.Query.Parent{expr: expr} ->
|
||||
if depth == 0 do
|
||||
true
|
||||
else
|
||||
has_parent_expr?(expr, depth - 1)
|
||||
do_has_parent_expr?(expr, depth - 1)
|
||||
end
|
||||
|
||||
_ ->
|
||||
|
|
Loading…
Reference in a new issue