Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add get_field function to adapter #383

Merged
merged 1 commit into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/flop/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ defmodule Flop.Adapter do
@callback count(queryable, opts) :: non_neg_integer

@callback list(queryable, opts) :: [any]

@callback get_field(any, atom, Flop.FieldInfo.t()) :: any
end
20 changes: 20 additions & 0 deletions lib/flop/adapter/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,26 @@ defmodule Flop.Adapter.Ecto do
end)
end

@impl Flop.Adapter
def get_field(%{} = item, _field, %FieldInfo{
extra: %{type: :compound, fields: fields}
}) do
Enum.map_join(fields, " ", &get_field(item, &1, %FieldInfo{}))
end

def get_field(%{} = item, _field, %FieldInfo{
extra: %{type: :join, path: path}
}) do
Enum.reduce(path, item, fn
field, %{} = acc -> Map.get(acc, field)
_, _ -> nil
end)
end

def get_field(%{} = item, field, %FieldInfo{}) do
Map.get(item, field)
end

@impl Flop.Adapter
def apply_filter(
query,
Expand Down
49 changes: 10 additions & 39 deletions lib/flop/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ defimpl Flop.Schema, for: Any do
order_by_func =
build_order_by_func(compound_fields, join_fields, alias_fields)

get_field_func = build_get_field_func(compound_fields, join_fields)
get_field_func = build_get_field_func(struct, adapter, adapter_opts)

cursor_dynamic_func_compound =
build_cursor_dynamic_func_compound(compound_fields)
Expand Down Expand Up @@ -1354,46 +1354,17 @@ defimpl Flop.Schema, for: Any do
end
end

def build_get_field_func(compound_fields, join_fields) do
compound_field_funcs =
for {name, fields} <- compound_fields do
quote do
def get_field(struct, unquote(name)) do
Enum.map_join(
unquote(fields),
" ",
&Flop.Schema.get_field(struct, &1)
)
end
end
end

join_field_funcs =
for {name, %{path: path}} <- join_fields do
quote do
def get_field(struct, unquote(name)) do
Enum.reduce(unquote(path), struct, fn field, acc ->
case acc do
%{} -> Map.get(acc, field)
_ -> nil
end
end)

# assoc = Map.get(struct, unquote(assoc_field)) || %{}
# Map.get(assoc, unquote(field))
end
end
end

fallback_func =
def build_get_field_func(struct, adapter, adapter_opts) do
for {field, field_info} <- adapter.fields(struct, adapter_opts) do
quote do
def get_field(struct, field), do: Map.get(struct, field)
def get_field(struct, unquote(field)) do
unquote(adapter).get_field(
struct,
unquote(field),
unquote(Macro.escape(field_info))
)
end
end

quote do
unquote(compound_field_funcs)
unquote(join_field_funcs)
unquote(fallback_func)
end
end

Expand Down