mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 13:33:20 +12:00
improvement: describe operator types
This commit is contained in:
parent
6198fe1fea
commit
31e58bfc70
9 changed files with 43 additions and 8 deletions
|
@ -7,7 +7,11 @@ defmodule Ash.Query.Operator.Eq do
|
|||
For comparison, this compares as mutually exclusive with other equality
|
||||
and `is_nil` checks that have the same reference on the left side
|
||||
"""
|
||||
use Ash.Query.Operator, operator: :==, predicate?: true
|
||||
use Ash.Query.Operator,
|
||||
operator: :==,
|
||||
name: :eq,
|
||||
predicate?: true,
|
||||
types: [:any_same_or_ref]
|
||||
|
||||
def new(%Ref{} = ref, nil) do
|
||||
Ash.Query.Operator.new(Ash.Query.Operator.IsNil, ref, true)
|
||||
|
|
|
@ -4,7 +4,11 @@ defmodule Ash.Query.Operator.GreaterThan do
|
|||
|
||||
In comparison, simplifies to `not(left < right + 1)`, so it will never need to be compared against.
|
||||
"""
|
||||
use Ash.Query.Operator, operator: :>, predicate?: true
|
||||
use Ash.Query.Operator,
|
||||
operator: :>,
|
||||
name: :greater_than,
|
||||
predicate?: true,
|
||||
types: [:any_same_or_ref]
|
||||
|
||||
def new(%Ref{attribute: %{type: type}} = left, right) do
|
||||
case Ash.Type.cast_input(type, right) do
|
||||
|
|
|
@ -4,7 +4,11 @@ defmodule Ash.Query.Operator.GreaterThanOrEqual do
|
|||
|
||||
In comparison, simplifies to `not(left < right)`, so it will never need to be compared against.
|
||||
"""
|
||||
use Ash.Query.Operator, operator: :>=, predicate?: true
|
||||
use Ash.Query.Operator,
|
||||
operator: :>=,
|
||||
name: :greater_than_or_equal,
|
||||
predicate?: true,
|
||||
types: [:any_same_or_ref]
|
||||
|
||||
def new(%Ref{attribute: %{type: type}} = left, right) do
|
||||
case Ash.Type.cast_input(type, right) do
|
||||
|
|
|
@ -7,7 +7,10 @@ defmodule Ash.Query.Operator.In do
|
|||
For comparison, this simplifies to a set of "or equals", e.g
|
||||
`{:or, {:or, {:or, left == 1}, left == 2}, left == 3}`
|
||||
"""
|
||||
use Ash.Query.Operator, operator: :in, predicate?: true
|
||||
use Ash.Query.Operator,
|
||||
operator: :in,
|
||||
predicate?: true,
|
||||
types: [[{:ref, :any}, {:array, :same}], [{:array, :same}, {:ref, :any}]]
|
||||
|
||||
@inspect_items_limit 10
|
||||
|
||||
|
|
|
@ -5,7 +5,10 @@ defmodule Ash.Query.Operator.IsNil do
|
|||
This predicate matches if the left is nil when the right is `true` or if the
|
||||
left is not nil when the right is `false`
|
||||
"""
|
||||
use Ash.Query.Operator, operator: :is_nil, predicate?: true
|
||||
use Ash.Query.Operator,
|
||||
operator: :is_nil,
|
||||
predicate?: true,
|
||||
types: [[{:ref, :any}, :boolean], [:boolean, {:ref, :any}]]
|
||||
|
||||
def new(%Ref{} = left, right) when is_boolean(right) do
|
||||
{:ok, left, right}
|
||||
|
|
|
@ -13,7 +13,11 @@ defmodule Ash.Query.Operator.LessThan do
|
|||
right side's are greater than or equal to it.
|
||||
"""
|
||||
|
||||
use Ash.Query.Operator, operator: :<, predicate?: true
|
||||
use Ash.Query.Operator,
|
||||
operator: :<,
|
||||
name: :less_than,
|
||||
predicate?: true,
|
||||
types: [:any_same_or_ref]
|
||||
|
||||
alias Ash.Query.Operator.{Eq, IsNil}
|
||||
|
||||
|
|
|
@ -4,7 +4,11 @@ defmodule Ash.Query.Operator.LessThanOrEqual do
|
|||
|
||||
In comparison, simplifies to `left < right + 1`, so it will never need to be compared against.
|
||||
"""
|
||||
use Ash.Query.Operator, operator: :<=, predicate?: true
|
||||
use Ash.Query.Operator,
|
||||
operator: :<=,
|
||||
name: :less_than_or_equal,
|
||||
predicate?: true,
|
||||
types: [:any_same_or_ref]
|
||||
|
||||
def new(%Ref{attribute: %{type: type}} = left, right) do
|
||||
case Ash.Type.cast_input(type, right) do
|
||||
|
|
|
@ -4,7 +4,11 @@ defmodule Ash.Query.Operator.NotEq do
|
|||
|
||||
In comparison, simplifies to `not(left == right)`
|
||||
"""
|
||||
use Ash.Query.Operator, operator: :!=, predicate?: true
|
||||
use Ash.Query.Operator,
|
||||
operator: :!=,
|
||||
name: :not_eq,
|
||||
predicate?: true,
|
||||
types: [:any_same_or_ref]
|
||||
|
||||
alias Ash.Query.Not
|
||||
alias Ash.Query.Operator.Eq
|
||||
|
|
|
@ -56,6 +56,7 @@ defmodule Ash.Query.Operator do
|
|||
alias Ash.Query.Ref
|
||||
|
||||
def operator, do: unquote(opts[:operator])
|
||||
def name, do: unquote(opts[:name] || opts[:operator])
|
||||
|
||||
if unquote(opts[:predicate?]) do
|
||||
@dialyzer {:nowarn_function, match?: 1}
|
||||
|
@ -64,6 +65,10 @@ defmodule Ash.Query.Operator do
|
|||
end
|
||||
end
|
||||
|
||||
def types do
|
||||
unquote(opts[:types] || [:any_same_or_ref])
|
||||
end
|
||||
|
||||
import Inspect.Algebra
|
||||
|
||||
def to_string(%{left: left, right: right, operator: operator}, opts) do
|
||||
|
|
Loading…
Reference in a new issue