mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 05:23:03 +12:00
refactor(filter): moves defimpl Inspect to corresponding struct files
Colocates defimpl Inspect with the corresponding struct. This should help colocate the struct itself with the IO representation of the entity.
This commit is contained in:
parent
91a4a668bf
commit
665f82a45e
8 changed files with 183 additions and 185 deletions
|
@ -45,3 +45,11 @@ defmodule Ash.Filter.And do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter.And do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(%{left: left, right: right}, opts) do
|
||||
concat([to_doc(left, opts), " and ", to_doc(right, opts)])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,3 +16,12 @@ defmodule Ash.Filter.Eq do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter.Eq do
|
||||
import Inspect.Algebra
|
||||
import Ash.Filter.InspectHelpers
|
||||
|
||||
def inspect(%{value: value}, opts) do
|
||||
concat([attr(opts), " == ", to_doc(value, opts)])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -852,3 +852,134 @@ defmodule Ash.Filter do
|
|||
|
||||
defp add_error(%{errors: errors} = filter, error), do: %{filter | errors: [error | errors]}
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter do
|
||||
import Inspect.Algebra
|
||||
import Ash.Filter.InspectHelpers
|
||||
|
||||
def inspect(%Ash.Filter{impossible?: impossible, not: not_filter} = filter, opts)
|
||||
when not is_nil(not_filter) do
|
||||
impossible =
|
||||
if impossible do
|
||||
"X"
|
||||
else
|
||||
""
|
||||
end
|
||||
|
||||
if root?(opts) do
|
||||
concat([
|
||||
"#Filter<#{impossible} not (",
|
||||
to_doc(not_filter, make_non_root(opts)),
|
||||
") and ",
|
||||
to_doc(%{filter | not: nil}, make_non_root(opts)),
|
||||
">"
|
||||
])
|
||||
else
|
||||
concat([
|
||||
"not (",
|
||||
to_doc(not_filter, make_non_root(opts)),
|
||||
") and ",
|
||||
to_doc(%{filter | not: nil}, make_non_root(opts))
|
||||
])
|
||||
end
|
||||
end
|
||||
|
||||
def inspect(
|
||||
%Ash.Filter{
|
||||
ors: ors,
|
||||
relationships: relationships,
|
||||
attributes: attributes,
|
||||
impossible?: impossible
|
||||
},
|
||||
opts
|
||||
)
|
||||
when ors in [nil, []] and relationships in [nil, %{}] and attributes in [nil, %{}] do
|
||||
impossible =
|
||||
if impossible do
|
||||
"X"
|
||||
else
|
||||
""
|
||||
end
|
||||
|
||||
if root?(opts) do
|
||||
concat(["#Filter<#{impossible}", to_doc(nil, opts), ">"])
|
||||
else
|
||||
concat([impossible])
|
||||
end
|
||||
end
|
||||
|
||||
def inspect(%{impossible?: impossible} = filter, opts) do
|
||||
rels =
|
||||
filter
|
||||
|> Map.get(:relationships)
|
||||
|> case do
|
||||
rels when rels == %{} ->
|
||||
[]
|
||||
|
||||
rels ->
|
||||
Enum.map(rels, fn {key, value} ->
|
||||
to_doc(value, add_to_path(opts, key))
|
||||
end)
|
||||
end
|
||||
|
||||
attrs =
|
||||
filter
|
||||
|> Map.get(:attributes)
|
||||
|> case do
|
||||
attrs when attrs == %{} ->
|
||||
[]
|
||||
|
||||
attrs ->
|
||||
Enum.map(attrs, fn {key, value} ->
|
||||
to_doc(value, put_attr(opts, key))
|
||||
end)
|
||||
end
|
||||
|
||||
and_container =
|
||||
case attrs ++ rels do
|
||||
[] ->
|
||||
empty()
|
||||
|
||||
and_clauses ->
|
||||
Inspect.Algebra.container_doc("(", and_clauses, ")", opts, fn term, _ -> term end,
|
||||
break: :flex,
|
||||
separator: " and"
|
||||
)
|
||||
end
|
||||
|
||||
all_container =
|
||||
case Map.get(filter, :ors) do
|
||||
nil ->
|
||||
and_container
|
||||
|
||||
[] ->
|
||||
and_container
|
||||
|
||||
ors ->
|
||||
inspected_ors = Enum.map(ors, fn filter -> to_doc(filter, make_non_root(opts)) end)
|
||||
|
||||
Inspect.Algebra.container_doc(
|
||||
"",
|
||||
[and_container | inspected_ors],
|
||||
"",
|
||||
opts,
|
||||
fn term, _ -> term end,
|
||||
break: :strict,
|
||||
separator: " or "
|
||||
)
|
||||
end
|
||||
|
||||
impossible =
|
||||
if impossible do
|
||||
"X"
|
||||
else
|
||||
""
|
||||
end
|
||||
|
||||
if root?(opts) do
|
||||
concat(["#Filter<#{impossible}", all_container, ">"])
|
||||
else
|
||||
concat([impossible, all_container])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -37,3 +37,12 @@ defmodule Ash.Filter.In do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter.In do
|
||||
import Inspect.Algebra
|
||||
import Ash.Filter.InspectHelpers
|
||||
|
||||
def inspect(%{values: values}, opts) do
|
||||
concat([attr(opts), " in ", to_doc(values, opts)])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -41,188 +41,3 @@ defmodule Ash.Filter.InspectHelpers do
|
|||
opts.custom_options[:path]
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter do
|
||||
import Inspect.Algebra
|
||||
import Ash.Filter.InspectHelpers
|
||||
|
||||
def inspect(%Ash.Filter{impossible?: impossible, not: not_filter} = filter, opts)
|
||||
when not is_nil(not_filter) do
|
||||
impossible =
|
||||
if impossible do
|
||||
"X"
|
||||
else
|
||||
""
|
||||
end
|
||||
|
||||
if root?(opts) do
|
||||
concat([
|
||||
"#Filter<#{impossible} not (",
|
||||
to_doc(not_filter, make_non_root(opts)),
|
||||
") and ",
|
||||
to_doc(%{filter | not: nil}, make_non_root(opts)),
|
||||
">"
|
||||
])
|
||||
else
|
||||
concat([
|
||||
"not (",
|
||||
to_doc(not_filter, make_non_root(opts)),
|
||||
") and ",
|
||||
to_doc(%{filter | not: nil}, make_non_root(opts))
|
||||
])
|
||||
end
|
||||
end
|
||||
|
||||
def inspect(
|
||||
%Ash.Filter{
|
||||
ors: ors,
|
||||
relationships: relationships,
|
||||
attributes: attributes,
|
||||
impossible?: impossible
|
||||
},
|
||||
opts
|
||||
)
|
||||
when ors in [nil, []] and relationships in [nil, %{}] and attributes in [nil, %{}] do
|
||||
impossible =
|
||||
if impossible do
|
||||
"X"
|
||||
else
|
||||
""
|
||||
end
|
||||
|
||||
if root?(opts) do
|
||||
concat(["#Filter<#{impossible}", to_doc(nil, opts), ">"])
|
||||
else
|
||||
concat([impossible])
|
||||
end
|
||||
end
|
||||
|
||||
def inspect(%{impossible?: impossible} = filter, opts) do
|
||||
rels =
|
||||
filter
|
||||
|> Map.get(:relationships)
|
||||
|> case do
|
||||
rels when rels == %{} ->
|
||||
[]
|
||||
|
||||
rels ->
|
||||
Enum.map(rels, fn {key, value} ->
|
||||
to_doc(value, add_to_path(opts, key))
|
||||
end)
|
||||
end
|
||||
|
||||
attrs =
|
||||
filter
|
||||
|> Map.get(:attributes)
|
||||
|> case do
|
||||
attrs when attrs == %{} ->
|
||||
[]
|
||||
|
||||
attrs ->
|
||||
Enum.map(attrs, fn {key, value} ->
|
||||
to_doc(value, put_attr(opts, key))
|
||||
end)
|
||||
end
|
||||
|
||||
and_container =
|
||||
case attrs ++ rels do
|
||||
[] ->
|
||||
empty()
|
||||
|
||||
and_clauses ->
|
||||
Inspect.Algebra.container_doc("(", and_clauses, ")", opts, fn term, _ -> term end,
|
||||
break: :flex,
|
||||
separator: " and"
|
||||
)
|
||||
end
|
||||
|
||||
all_container =
|
||||
case Map.get(filter, :ors) do
|
||||
nil ->
|
||||
and_container
|
||||
|
||||
[] ->
|
||||
and_container
|
||||
|
||||
ors ->
|
||||
inspected_ors = Enum.map(ors, fn filter -> to_doc(filter, make_non_root(opts)) end)
|
||||
|
||||
Inspect.Algebra.container_doc(
|
||||
"",
|
||||
[and_container | inspected_ors],
|
||||
"",
|
||||
opts,
|
||||
fn term, _ -> term end,
|
||||
break: :strict,
|
||||
separator: " or "
|
||||
)
|
||||
end
|
||||
|
||||
impossible =
|
||||
if impossible do
|
||||
"X"
|
||||
else
|
||||
""
|
||||
end
|
||||
|
||||
if root?(opts) do
|
||||
concat(["#Filter<#{impossible}", all_container, ">"])
|
||||
else
|
||||
concat([impossible, all_container])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter.And do
|
||||
import Inspect.Algebra
|
||||
import Ash.Filter.InspectHelpers
|
||||
|
||||
def inspect(%{left: left, right: right}, opts) do
|
||||
concat([to_doc(left, opts), " and ", to_doc(right, opts)])
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter.Or do
|
||||
import Inspect.Algebra
|
||||
import Ash.Filter.InspectHelpers
|
||||
|
||||
def inspect(%{left: left, right: right}, opts) do
|
||||
concat([to_doc(left, opts), " or ", to_doc(right, opts)])
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter.Eq do
|
||||
import Inspect.Algebra
|
||||
import Ash.Filter.InspectHelpers
|
||||
|
||||
def inspect(%{value: value}, opts) do
|
||||
concat([attr(opts), " == ", to_doc(value, opts)])
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter.NotEq do
|
||||
import Inspect.Algebra
|
||||
import Ash.Filter.InspectHelpers
|
||||
|
||||
def inspect(%{value: value}, opts) do
|
||||
concat([attr(opts), " != ", to_doc(value, opts)])
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter.In do
|
||||
import Inspect.Algebra
|
||||
import Ash.Filter.InspectHelpers
|
||||
|
||||
def inspect(%{values: values}, opts) do
|
||||
concat([attr(opts), " in ", to_doc(values, opts)])
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter.NotIn do
|
||||
import Inspect.Algebra
|
||||
import Ash.Filter.InspectHelpers
|
||||
|
||||
def inspect(%{values: values}, opts) do
|
||||
concat([attr(opts), " not in ", to_doc(values, opts)])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,3 +16,12 @@ defmodule Ash.Filter.NotEq do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter.NotEq do
|
||||
import Inspect.Algebra
|
||||
import Ash.Filter.InspectHelpers
|
||||
|
||||
def inspect(%{value: value}, opts) do
|
||||
concat([attr(opts), " != ", to_doc(value, opts)])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -37,3 +37,12 @@ defmodule Ash.Filter.NotIn do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter.NotIn do
|
||||
import Inspect.Algebra
|
||||
import Ash.Filter.InspectHelpers
|
||||
|
||||
def inspect(%{values: values}, opts) do
|
||||
concat([attr(opts), " not in ", to_doc(values, opts)])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -32,3 +32,11 @@ defmodule Ash.Filter.Or do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Ash.Filter.Or do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(%{left: left, right: right}, opts) do
|
||||
concat([to_doc(left, opts), " or ", to_doc(right, opts)])
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue