mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 13:33:20 +12:00
improvement: add has
filter predicate
This commit is contained in:
parent
269a556f04
commit
312d19878b
4 changed files with 29 additions and 2 deletions
|
@ -22,6 +22,7 @@ defmodule Ash.Filter do
|
|||
Eq,
|
||||
GreaterThan,
|
||||
GreaterThanOrEqual,
|
||||
Has,
|
||||
In,
|
||||
LessThan,
|
||||
LessThanOrEqual,
|
||||
|
@ -45,7 +46,8 @@ defmodule Ash.Filter do
|
|||
LessThan,
|
||||
GreaterThan,
|
||||
LessThanOrEqual,
|
||||
GreaterThanOrEqual
|
||||
GreaterThanOrEqual,
|
||||
Has
|
||||
] ++ Ash.Query.Operator.Basic.operator_modules()
|
||||
|
||||
@builtins @functions ++ @operators
|
||||
|
|
17
lib/ash/query/operator/has.ex
Normal file
17
lib/ash/query/operator/has.ex
Normal file
|
@ -0,0 +1,17 @@
|
|||
defmodule Ash.Query.Operator.Has do
|
||||
@moduledoc """
|
||||
left has 1
|
||||
|
||||
this predicate matches if the right is in the list on the left
|
||||
|
||||
This actually just reverses the inputs and uses `in`.
|
||||
"""
|
||||
use Ash.Query.Operator,
|
||||
operator: :has,
|
||||
predicate?: true,
|
||||
types: [[{:array, :any}, :same]]
|
||||
|
||||
def new(left, right) do
|
||||
Ash.Query.Operator.In.new(right, left)
|
||||
end
|
||||
end
|
|
@ -211,7 +211,8 @@ defmodule Ash.Query.Operator do
|
|||
Ash.Query.Operator.IsNil,
|
||||
Ash.Query.Operator.LessThanOrEqual,
|
||||
Ash.Query.Operator.LessThan,
|
||||
Ash.Query.Operator.NotEq
|
||||
Ash.Query.Operator.NotEq,
|
||||
Ash.Query.Operator.Has
|
||||
] ++ Ash.Query.Operator.Basic.operator_modules()
|
||||
end
|
||||
|
||||
|
|
|
@ -481,6 +481,13 @@ defmodule Ash.Query do
|
|||
soft_escape(%Ash.Query.Call{name: op, args: args, operator?: true}, escape?)
|
||||
end
|
||||
|
||||
defp do_expr({left, _, [{op, _, [right]}]}, escape?)
|
||||
when is_atom(op) and op in @operator_symbols and is_atom(left) do
|
||||
args = Enum.map([{left, [], nil}, right], &do_expr(&1, false))
|
||||
|
||||
soft_escape(%Ash.Query.Call{name: op, args: args, operator?: true}, escape?)
|
||||
end
|
||||
|
||||
defp do_expr({op, _, args}, escape?) when is_atom(op) and is_list(args) do
|
||||
args = Enum.map(args, &do_expr(&1, false))
|
||||
|
||||
|
|
Loading…
Reference in a new issue