Skip to content

Commit

Permalink
fix type expansion in allowed_operators/1
Browse files Browse the repository at this point in the history
  • Loading branch information
woylie committed Aug 19, 2024
1 parent ef211b7 commit 1c1ef1e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
43 changes: 24 additions & 19 deletions lib/flop/filter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,19 @@ defmodule Flop.Filter do
end

def allowed_operators(%FieldInfo{ecto_type: ecto_type}) do
ecto_type |> expand_type() |> allowed_operators()
ecto_type |> expand_type() |> get_allowed_operators()
end

def allowed_operators(type) when type in [:decimal, :float, :id, :integer] do
def allowed_operators(type) do
type |> expand_type() |> get_allowed_operators()
end

defp get_allowed_operators(type)
when type in [:decimal, :float, :id, :integer] do
[:==, :!=, :empty, :not_empty, :<=, :<, :>=, :>, :in, :not_in]
end

def allowed_operators(type) when type in [:binary_id, :string] do
defp get_allowed_operators(type) when type in [:binary_id, :string] do
[
:==,
:!=,
Expand All @@ -306,11 +311,11 @@ defmodule Flop.Filter do
]
end

def allowed_operators(:boolean) do
defp get_allowed_operators(:boolean) do
[:==, :!=, :=~, :empty, :not_empty]
end

def allowed_operators({:array, _}) do
defp get_allowed_operators({:array, _}) do
[
:==,
:!=,
Expand All @@ -327,28 +332,28 @@ defmodule Flop.Filter do
]
end

def allowed_operators({:map, _}) do
defp get_allowed_operators({:map, _}) do
[:==, :!=, :empty, :not_empty, :in, :not_in]
end

def allowed_operators(:map) do
defp get_allowed_operators(:map) do
[:==, :!=, :empty, :not_empty, :in, :not_in]
end

def allowed_operators(type)
when type in [
:date,
:time,
:time_usec,
:naive_datetime,
:naive_datetime_usec,
:utc_datetime,
:utc_datetime_usec
] do
defp get_allowed_operators(type)
when type in [
:date,
:time,
:time_usec,
:naive_datetime,
:naive_datetime_usec,
:utc_datetime,
:utc_datetime_usec
] do
[:==, :!=, :empty, :not_empty, :<=, :<, :>=, :>, :in, :not_in]
end

def allowed_operators({:parameterized, {Ecto.Enum, _}}) do
defp get_allowed_operators({:parameterized, {Ecto.Enum, _}}) do
[
:==,
:!=,
Expand All @@ -363,7 +368,7 @@ defmodule Flop.Filter do
]
end

def allowed_operators(_) do
defp get_allowed_operators(_) do
[
:==,
:!=,
Expand Down
13 changes: 10 additions & 3 deletions test/flop/filter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Flop.FilterTest do
end

describe "allowed_operators/1" do
test "returns a list of operators for each native Ecto type" do
test "returns a list of operators for each Ecto type" do
types = [
:id,
:binary_id,
Expand All @@ -36,12 +36,19 @@ defmodule Flop.FilterTest do
:naive_datetime_usec,
:utc_datetime,
:utc_datetime_usec,
{:parameterized, {Ecto.Enum, %{type: :string}}}
{:parameterized, {Ecto.Enum, %{type: :string}}},
{:ecto_enum, [:one, :two]},
{:from_schema, MyApp.Pet, :mood}
]

for type <- types do
assert [op | _] = Filter.allowed_operators(type)
assert [op | _] = ops_for_type = Filter.allowed_operators(type)
assert is_atom(op)

ops_for_field =
Filter.allowed_operators(%Flop.FieldInfo{ecto_type: type})

assert ops_for_type == ops_for_field
end
end

Expand Down

0 comments on commit 1c1ef1e

Please sign in to comment.