-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from valyukov/extra-config
Provide extra configuration
- Loading branch information
Showing
24 changed files
with
517 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,63 @@ | ||
defmodule ExSieve.Config do | ||
@moduledoc """ | ||
A `ExSieve.Config` can be created with a `ignore_errors` true or false | ||
``` | ||
%ExSieve.Config{ | ||
ignore_errors: true | ||
} | ||
``` | ||
defstruct ignore_errors: true, max_depth: :full, except_predicates: nil, only_predicates: nil | ||
|
||
@typedoc """ | ||
`ExSieve` configuration options: | ||
* `:ignore_errors` - when `true` recoverable errors are ignored. Recoverable | ||
errors include for instance missing attribute or missing predicate, in that | ||
case the query is returned without taking into account the filter causing the | ||
error. Defaults to `true` | ||
* `:max_depth` - the maximum level of nested relations that can be queried. | ||
Defaults to `:full` meaning no limit | ||
* `:only_predicates` - a list of allowed predicates. The list can contain `:basic` | ||
and `:composite`, in that case all corresponding predicates are added to the list. | ||
When not given or when `nil` no limit is applied. Defaults to `nil` | ||
* `:except_predicates` - a list of excluded predicates. The list can contain `:basic` | ||
and `:composite`, in that case all corresponding predicates are added to the list. | ||
When not given or when `nil` no limit is applied. If both `:only_predicates` and | ||
`:except_predicates` are given `:only_predicates` takes precedence and | ||
`:except_predicates` is ignored. Defaults to `nil` | ||
""" | ||
defstruct ignore_errors: true | ||
@type t :: %__MODULE__{ | ||
ignore_errors: boolean(), | ||
max_depth: non_neg_integer() | :full, | ||
except_predicates: [String.t() | :basic | :composite] | nil, | ||
only_predicates: [String.t() | :basic | :composite] | nil | ||
} | ||
|
||
@type t :: %__MODULE__{} | ||
@keys [:ignore_errors, :max_depth, :except_predicates, :only_predicates] | ||
|
||
@doc false | ||
@spec new(Keyword.t(), map) :: ExSieve.Config.t() | ||
def new(defaults, options \\ %{}) do | ||
%ExSieve.Config{ignore_errors: ignore_errors?(defaults, options)} | ||
@spec new(Keyword.t(), call_options :: map, schema :: module()) :: __MODULE__.t() | ||
def new(defaults, call_options, schema) do | ||
defaults = normalize_options(defaults) | ||
call_options = normalize_options(call_options) | ||
schema_options = schema |> options_from_schema() |> normalize_options() | ||
|
||
opts = | ||
defaults | ||
|> Map.merge(schema_options) | ||
|> Map.merge(call_options) | ||
|> Map.take(@keys) | ||
|
||
struct(__MODULE__, opts) | ||
end | ||
|
||
defp normalize_options(options) do | ||
Enum.reduce(options, %{}, fn {k, v}, map -> | ||
Map.put(map, to_string(k), v) | ||
end) | ||
defp options_from_schema(schema) do | ||
cond do | ||
function_exported?(schema, :__ex_sieve_options__, 0) -> apply(schema, :__ex_sieve_options__, []) | ||
true -> %{} | ||
end | ||
end | ||
|
||
defp ignore_errors?(defaults, options) do | ||
options | ||
|> normalize_options | ||
|> Map.get("ignore_errors", Keyword.get(defaults, :ignore_errors, true)) | ||
defp normalize_options(options) when is_list(options) or is_map(options) do | ||
Map.new(options, fn | ||
{key, val} when is_atom(key) -> {key, val} | ||
{key, val} when is_bitstring(key) -> {String.to_existing_atom(key), val} | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,16 @@ | ||
defmodule ExSieve.Filter do | ||
@moduledoc false | ||
|
||
alias ExSieve.{Builder, Config, Node} | ||
alias ExSieve.{Builder, Config, Node, Utils} | ||
|
||
@spec filter(Ecto.Queryable.t(), %{(binary | atom) => term}, Config.t()) :: ExSieve.result() | {:error, any()} | ||
def filter(queryable, params, %Config{} = config) do | ||
case extract_schema(queryable) do | ||
{:ok, schema} -> | ||
params | ||
|> Node.call(schema, config) | ||
|> result(queryable, config) | ||
|
||
err -> | ||
err | ||
end | ||
end | ||
|
||
defp result({:error, reason}, _queryable, _config), do: {:error, reason} | ||
|
||
defp result({:ok, groupings, sorts}, queryable, config) do | ||
case Builder.call(queryable, groupings, sorts, config) do | ||
{:ok, result} -> result | ||
err -> err | ||
end | ||
end | ||
|
||
defp extract_schema(%Ecto.Query{from: %{source: {_, module}}}), do: extract_schema(module) | ||
|
||
defp extract_schema(schema) when is_atom(schema) do | ||
cond do | ||
function_exported?(schema, :__schema__, 1) -> {:ok, schema} | ||
true -> {:error, :invalid_query} | ||
@spec filter(Ecto.Queryable.t(), %{(binary | atom) => term}, defaults :: Keyword.t(), options :: map) :: | ||
ExSieve.result() | ||
def filter(queryable, params, defaults \\ [], options \\ %{}) do | ||
with {:ok, schema} <- Utils.extract_schema(queryable), | ||
config <- Config.new(defaults, options, schema), | ||
{:ok, groupings, sorts} <- Node.call(params, schema, config), | ||
{:ok, result} <- Builder.call(queryable, groupings, sorts, config) do | ||
result | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.