mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 13:33:20 +12:00
Merge pull request #12 from ash-project/refactor-filter-inspect
refactor(filter, inspect): adds helpers and guards to clarify code
This commit is contained in:
commit
5a5737d25d
1 changed files with 27 additions and 53 deletions
|
@ -857,14 +857,10 @@ defimpl Inspect, for: Ash.Filter do
|
||||||
import Inspect.Algebra
|
import Inspect.Algebra
|
||||||
import Ash.Filter.InspectHelpers
|
import Ash.Filter.InspectHelpers
|
||||||
|
|
||||||
def inspect(%Ash.Filter{impossible?: impossible, not: not_filter} = filter, opts)
|
defguardp is_empty(val) when is_nil(val) or val == [] or val == %{}
|
||||||
when not is_nil(not_filter) do
|
|
||||||
impossible =
|
def inspect(%Ash.Filter{not: not_filter} = filter, opts) when not is_nil(not_filter) do
|
||||||
if impossible do
|
impossible = if Map.fetch!(filter, :impossible?), do: "X", else: ""
|
||||||
"X"
|
|
||||||
else
|
|
||||||
""
|
|
||||||
end
|
|
||||||
|
|
||||||
if root?(opts) do
|
if root?(opts) do
|
||||||
concat([
|
concat([
|
||||||
|
@ -885,21 +881,11 @@ defimpl Inspect, for: Ash.Filter do
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect(
|
def inspect(
|
||||||
%Ash.Filter{
|
%Ash.Filter{ors: ors, relationships: relationships, attributes: attributes} = filter,
|
||||||
ors: ors,
|
|
||||||
relationships: relationships,
|
|
||||||
attributes: attributes,
|
|
||||||
impossible?: impossible
|
|
||||||
},
|
|
||||||
opts
|
opts
|
||||||
)
|
)
|
||||||
when ors in [nil, []] and relationships in [nil, %{}] and attributes in [nil, %{}] do
|
when is_empty(ors) and is_empty(relationships) and is_empty(attributes) do
|
||||||
impossible =
|
impossible = if Map.fetch!(filter, :impossible?), do: "X", else: ""
|
||||||
if impossible do
|
|
||||||
"X"
|
|
||||||
else
|
|
||||||
""
|
|
||||||
end
|
|
||||||
|
|
||||||
if root?(opts) do
|
if root?(opts) do
|
||||||
concat(["#Filter<#{impossible}", to_doc(nil, opts), ">"])
|
concat(["#Filter<#{impossible}", to_doc(nil, opts), ">"])
|
||||||
|
@ -908,32 +894,11 @@ defimpl Inspect, for: Ash.Filter do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect(%{impossible?: impossible} = filter, opts) do
|
def inspect(filter, opts) do
|
||||||
rels =
|
impossible = if Map.fetch!(filter, :impossible?), do: "X", else: ""
|
||||||
filter
|
|
||||||
|> Map.get(:relationships)
|
|
||||||
|> case do
|
|
||||||
rels when rels == %{} ->
|
|
||||||
[]
|
|
||||||
|
|
||||||
rels ->
|
rels = parse_relationships(filter, opts)
|
||||||
Enum.map(rels, fn {key, value} ->
|
attrs = parse_attributes(filter, opts)
|
||||||
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 =
|
and_container =
|
||||||
case attrs ++ rels do
|
case attrs ++ rels do
|
||||||
|
@ -969,17 +934,26 @@ defimpl Inspect, for: Ash.Filter do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
impossible =
|
|
||||||
if impossible do
|
|
||||||
"X"
|
|
||||||
else
|
|
||||||
""
|
|
||||||
end
|
|
||||||
|
|
||||||
if root?(opts) do
|
if root?(opts) do
|
||||||
concat(["#Filter<#{impossible}", all_container, ">"])
|
concat(["#Filter<#{impossible}", all_container, ">"])
|
||||||
else
|
else
|
||||||
concat([impossible, all_container])
|
concat([impossible, all_container])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp parse_relationships(%Ash.Filter{relationships: %{}}, _opts), do: []
|
||||||
|
|
||||||
|
defp parse_relationships(filter, opts) do
|
||||||
|
filter
|
||||||
|
|> Map.fetch!(:relationships)
|
||||||
|
|> Enum.map(fn {key, value} -> to_doc(value, add_to_path(opts, key)) end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_attributes(%Ash.Filter{attributes: %{}}, _opts), do: []
|
||||||
|
|
||||||
|
defp parse_attributes(filter, opts) do
|
||||||
|
filter
|
||||||
|
|> Map.fetch!(:attributes)
|
||||||
|
|> Enum.map(fn {key, value} -> to_doc(value, put_attr(opts, key)) end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue