Skip to content

Commit

Permalink
Cleanup tests, comment out work in progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
atoncetti committed Nov 14, 2023
1 parent fc5aa0b commit 4aea8e3
Show file tree
Hide file tree
Showing 9 changed files with 763 additions and 875 deletions.
48 changes: 21 additions & 27 deletions lib/flop_ash_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ defmodule FlopAshAdapter do
defp do_sort(query, directions) do
Enum.reduce(directions, query, fn
{path, direction}, acc_query when is_list(path) ->
sort(acc_query, Sort.expr_sort(^path))
sort(acc_query, Sort.expr_sort(^{path, direction}))

{[field], direction}, acc_query ->
sort(acc_query, {field, direction})
Expand Down Expand Up @@ -161,29 +161,39 @@ defmodule FlopAshAdapter do
# type ascending, last cursor field
defp cursor_filter([{direction, field, cursor_value, _}])
when direction in [:asc, :asc_nulls_first, :asc_nulls_last] do
[{field, [gt: cursor_value]}]
[{field, [gt: cursor_value]}]
end

# type descending, last cursor field
defp cursor_filter([{direction, field, cursor_value, _}])
when direction in [:desc, :desc_nulls_first, :desc_nulls_last] do
[{field, [lt: cursor_value]}]
[{field, [lt: cursor_value]}]
end

# type ascending, more cursor fields to come
defp cursor_filter([
{direction, field, cursor_value, _} | [{_, _, _, _} | _] = tail
])
when direction in [:asc, :asc_nulls_first, :asc_nulls_last] do
[and: [[{field, [gte: cursor_value]}], [or: [[{field, [gt: cursor_value]}], cursor_filter(tail)] ]]]
[
and: [
[{field, [gte: cursor_value]}],
[or: [[{field, [gt: cursor_value]}], cursor_filter(tail)]]
]
]
end

# type descending, more cursor fields to come
defp cursor_filter([
{direction, field, cursor_value, _} | [{_, _, _, _} | _] = tail
])
when direction in [:desc, :desc_nulls_first, :desc_nulls_last] do
[and: [[{field, [lte: cursor_value]}], [or: [[{field, [lt: cursor_value]}], cursor_filter(tail)] ]]]
[
and: [
[{field, [lte: cursor_value]}],
[or: [[{field, [lt: cursor_value]}], cursor_filter(tail)]]
]
]
end

@impl Flop.Adapter
Expand All @@ -193,7 +203,7 @@ defmodule FlopAshAdapter do

@impl Flop.Adapter
def list(query, opts) do
apply_using_api(:read!, [query|>dbg], opts)
apply_using_api(:read!, [query], opts)
end

defp apply_using_api(api_fn, args, opts) do
Expand All @@ -205,8 +215,7 @@ defmodule FlopAshAdapter do
end

@impl Flop.Adapter
def get_field(%{} = _item, _field, %FieldInfo{} = field_info) do
end
def get_field(%{} = item, field, %FieldInfo{}), do: Map.get(item, field)

defp get_field_info(nil, field),
do: %FieldInfo{extra: %{type: :normal, field: field}}
Expand Down Expand Up @@ -278,25 +287,7 @@ defmodule FlopAshAdapter do
defp array_or_map({:parameterized, Ash.Type.Map.EctoType, _}), do: :map
defp array_or_map(_), do: :other

# for op <- [:ilike, :like] do
# defp build_op(
# _schema_struct,
# %FieldInfo{extra: %{type: :normal, field: field}},
# %Filter{op: unquote(op), value: value}
# ) do
# op_config(unquote(op))
# end
# end

defp build_op(
_schema_struct,
%FieldInfo{extra: %{type: :normal, field: field}},
%Filter{op: :like, value: value}
) do
Ash.Query.expr(like(owner_name, quote do: var!(valued)))
end

for op <- @operators -- [:like_and, :like_or, :ilike_and, :ilike_or, :like] do
for op <- @operators -- [:like_and, :like_or, :ilike_and, :ilike_or] do
fragment = op_config(op)

defp build_op(
Expand All @@ -307,4 +298,7 @@ defmodule FlopAshAdapter do
unquote(fragment)
end
end

@impl Flop.Adapter
def custom_func_builder(_opts), do: []
end
103 changes: 52 additions & 51 deletions lib/operators.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,103 +13,104 @@ defmodule FlopAshAdapter.Operators do
end

def op_config(:==) do
quote do
[{var!(field), [equals: var!(value)]}]
end
quote do
[{var!(field), [equals: var!(value)]}]
end
end

def op_config(:!=) do
quote do
[{var!(field), [not_equals: var!(value)]}]
end
quote do
[{var!(field), [not_equals: var!(value)]}]
end
end

def op_config(:>=) do
quote do
[{var!(field), [gte: var!(value)]}]
end
quote do
[{var!(field), [gte: var!(value)]}]
end
end

def op_config(:<=) do
quote do
[{var!(field), [lte: var!(value)]}]
end
quote do
[{var!(field), [lte: var!(value)]}]
end
end

def op_config(:>) do
quote do
[{var!(field), [gt: var!(value)]}]
end
quote do
[{var!(field), [gt: var!(value)]}]
end
end

def op_config(:<) do
quote do
[{var!(field), [lt: var!(value)]}]
end
quote do
[{var!(field), [lt: var!(value)]}]
end
end

def op_config(:empty) do
empty()
end

def op_config(:not_empty) do
quote do
[{var!(field), [is_nil: not var!(value)]}]
end
quote do
[{var!(field), [is_nil: not var!(value)]}]
end
end

def op_config(:in) do
quote do
var!(field) in [var!(value)]
end
quote do
var!(field) in var!(value)
end
end

def op_config(:contains) do
quote do
var!(field) in [var!(value)]
end
quote do
{:ok, contains_expr} = Ash.Query.Operator.In.new(var!(field), [var!(value)])
Ash.Query.expr(contains_expr)
end
end

def op_config(:not_contains) do
quote do
var!(field) not in [var!(value)]
end
quote do
{:ok, contains_expr} = Ash.Query.Operator.In.new(var!(field), [var!(value)])
Ash.Query.expr(not contains_expr)
end
end

def op_config(:like) do
# {:ok, like_expr} = Functions.Like.new([var!(field), Flop.Misc.add_wildcard(var!(value))])
# like_expr
quote do
like(var!(field), Flop.Misc.add_wildcard(var!(value)))
end
quote do
{:ok, like_expr} = Functions.Like.new([var!(field), Flop.Misc.add_wildcard(var!(value))])
like_expr
end
end

def op_config(:not_like) do
quote do
{:ok, like_expr} = Functions.Like.new([var!(field), Flop.Misc.add_wildcard(var!(value))])
expr(not like_expr)
end
quote do
{:ok, like_expr} = Functions.Like.new([var!(field), Flop.Misc.add_wildcard(var!(value))])
expr(not like_expr)
end
end

def op_config(:=~) do
quote do
{:ok, i_like_expr} = Functions.ILike.new([var!(field), Flop.Misc.add_wildcard(var!(value))])
i_like_expr
end
quote do
{:ok, i_like_expr} = Functions.ILike.new([var!(field), Flop.Misc.add_wildcard(var!(value))])
i_like_expr
end
end

def op_config(:ilike) do
quote do
{:ok, i_like_expr} = Functions.ILike.new([var!(field), Flop.Misc.add_wildcard(var!(value))])
i_like_expr
end
quote do
{:ok, i_like_expr} = Functions.ILike.new([var!(field), Flop.Misc.add_wildcard(var!(value))])
i_like_expr
end
end

def op_config(:not_ilike) do
quote do
{:ok, i_like_expr} = Functions.ILike.new([var!(field), Flop.Misc.add_wildcard(var!(value))])
expr(not i_like_expr)
end
quote do
{:ok, i_like_expr} = Functions.ILike.new([var!(field), Flop.Misc.add_wildcard(var!(value))])
expr(not i_like_expr)
end
end

def op_config(:not_in) do
Expand Down
18 changes: 18 additions & 0 deletions priv/repo/migrations/20231030195516_add_pets_in_second_schema.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Flop.Repo.Migrations.AddPetsInSecondSchema do
use Ecto.Migration

def change do
execute("CREATE SCHEMA other_schema;", "DROP SCHEMA other_schema;")

create table(:pets, prefix: "other_schema") do
add :age, :integer
add :family_name, :string
add :given_name, :string
add :name, :string
add :owner_id, :integer
add :species, :string
add :mood, :string
add :tags, {:array, :string}
end
end
end
Loading

0 comments on commit 4aea8e3

Please sign in to comment.