improvement: describe operator types

This commit is contained in:
Zach Daniel 2020-12-29 14:23:37 -05:00
parent 6198fe1fea
commit 31e58bfc70
9 changed files with 43 additions and 8 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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}

View file

@ -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}

View file

@ -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

View file

@ -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

View file

@ -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