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

Support for custom types #36

Merged
merged 9 commits into from
Jan 19, 2024
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
16 changes: 8 additions & 8 deletions examples/contract/types-01.exs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
Drops.Types.from_spec({:type, {:string, []}}, [])
Drops.Type.Compiler.visit({:type, {:string, []}}, [])

Drops.Types.from_spec({:type, {:string, [:filled?]}}, [])
Drops.Type.Compiler.visit({:type, {:string, [:filled?]}}, [])

Drops.Types.from_spec({:type, {:list, []}}, [])
Drops.Type.Compiler.visit({:type, {:list, []}}, [])

Drops.Types.from_spec({:type, {:list, {:type, {:integer, []}}}}, [])
Drops.Type.Compiler.visit({:type, {:list, {:type, {:integer, []}}}}, [])

Drops.Types.from_spec([{:type, {:string, []}}, {:type, {:integer, []}}], [])
Drops.Type.Compiler.visit([{:type, {:string, []}}, {:type, {:integer, []}}], [])

Drops.Types.from_spec({:type, {:map, []}}, [])
Drops.Type.Compiler.visit({:type, {:map, []}}, [])

Drops.Types.from_spec(
Drops.Type.Compiler.visit(
%{
{:required, :name} => {:type, {:string, []}},
{:optional, :age} => {:type, {:string, []}}
},
[]
)

Drops.Types.from_spec(
Drops.Type.Compiler.visit(
{:cast, {{:type, {:integer, []}}, {:type, {:date_time, []}}, [:miliseconds]}},
[]
)
30 changes: 30 additions & 0 deletions examples/types/custom-01.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule FilledString do
use Drops.Type, {:string, [:filled?]}
end

defmodule UserContract do
use Drops.Contract

schema do
%{
required(:name) => FilledString
}
end
end

UserContract.conform(%{name: "Jane Doe"})
# {:ok, %{name: "Jane Doe"}}

{:error, errors} = UserContract.conform(%{name: 1})
Enum.map(errors, &to_string/1)
# ["name must be a string"]

{:error, errors} = UserContract.conform(%{name: ""})
Enum.map(errors, &to_string/1)
# ["name must be filled"]

[%{type: type}]= UserContract.schema().keys
# %FilledString{
# primitive: :string,
# constraints: {:and, [predicate: {:type?, :string}, predicate: {:filled?, []}]}
# }
2 changes: 1 addition & 1 deletion lib/drops/casters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Drops.Casters do
Drops.Casters is a module that provides functions for casting values
from one type to another.

This module is the default caster module used by the Drops.Types.Map.DSL.cast function.
This module is the default caster module used by the Drops.Type.DSL.cast function.
"""

@doc ~S"""
Expand Down
162 changes: 66 additions & 96 deletions lib/drops/contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule Drops.Contract do
use Drops.Validator

import Drops.Contract
import Drops.Types.Map.DSL
import Drops.Type.DSL

@behaviour Drops.Contract

Expand All @@ -55,70 +55,44 @@ defmodule Drops.Contract do
@before_compile Drops.Contract

def conform(data) do
conform(data, schema(), root: true)
end

def conform(data, %Types.Map{atomize: true} = schema, root: root) do
conform(Types.Map.atomize(data, schema.keys), schema.keys, root: root)
end

def conform(data, %Types.Map{} = schema, root: root) do
case validate(data, schema) do
{:ok, {_, validated_data}} ->
conform(validated_data, schema.keys, root: root)

error ->
{:error, @message_backend.errors(error)}
end
end

def conform(data, %Types.Sum{} = type, root: root) do
case conform(data, type.left, root: root) do
{:ok, value} ->
{:ok, value}

{:error, _} = left_errors ->
case conform(data, type.right, root: root) do
{:ok, value} ->
{:ok, value}

{:error, error} = right_errors ->
{:error, @message_backend.errors({:or, {left_errors, right_errors}})}
end
end
conform(data, schema(), path: [])
end

def conform(data, %Types.Map{} = schema, path: path) do
case conform(data, schema, root: false) do
{:ok, value} ->
{:ok, {path, value}}

{:error, errors} ->
{:error, nest_errors(errors, path)}
end
end
case Drops.Type.Validator.validate(schema, data) do
{outcome, {:map, items}} = result ->
output = to_output(result)
errors = if outcome == :ok, do: [], else: Enum.reject(items, &is_ok/1)

def conform(data, keys, root: root) when is_list(keys) do
results = validate(data, keys)
output = to_output(results)
errors = Enum.reject(results, &is_ok/1)
all_errors = if Enum.empty?(path), do: errors ++ apply_rules(output), else: errors

all_errors = if root, do: errors ++ apply_rules(output), else: errors
if length(all_errors) > 0 do
{:error, @message_backend.errors(all_errors)}
else
{:ok, output}
end

if length(all_errors) > 0 do
{:error, @message_backend.errors(collapse_errors(all_errors))}
else
{:ok, output}
{:error, meta} ->
{:error, @message_backend.errors({:error, {path, meta}})}
end
end

def validate(value, %Types.Map{} = schema, path: path) do
case validate(value, schema.constraints, path: path) do
{:ok, {_, validated_value}} ->
conform(validated_value, schema, path: path)

error ->
error
def conform(data, %Types.Sum{} = type, path: path) do
case conform(data, type.left, path: path) do
{:ok, output} = success ->
success

{:error, left_error} ->
case conform(data, type.right, path: path) do
{:ok, output} = success ->
success

{:error, right_error} ->
{:error,
@message_backend.errors(
{:error, {path, {:or, {left_error, right_error}}}}
)}
end
end
end

Expand Down Expand Up @@ -339,60 +313,56 @@ defmodule Drops.Contract do
end
end

def collapse_errors(errors) when is_list(errors) do
Enum.map(errors, fn
{:error, {path, name, args}} ->
{:error, {path, name, args}}
def to_output({_, {:map, [head | tail]}}) do
to_output(tail, to_output(head, %{}))
end

{:error, error_list} ->
collapse_errors(error_list)
def to_output({_, {:list, results}}) do
to_output(results)
end

def to_output({:list, results}) do
to_output(results)
end

{:or, {left_errors, right_errors}} ->
{:or, {collapse_errors(left_errors), collapse_errors(right_errors)}}
def to_output({:map, results}) do
to_output(results, %{})
end

result ->
result
end)
|> List.flatten()
def to_output([head | tail]) do
[to_output(head) | to_output(tail)]
end

def collapse_errors({:error, errors}) do
{:error, collapse_errors(errors)}
def to_output({:ok, value}) do
to_output(value)
end

def collapse_errors(errors), do: errors
def to_output(value) do
value
end

def nest_errors(errors, root) do
List.flatten(Enum.map(errors, &Messages.Error.Conversions.nest(&1, root)))
def to_output(:ok, output) do
output
end

def map_list_results(members) do
Enum.map(members, fn member ->
case member do
{:ok, {_, value}} ->
if is_list(value), do: map_list_results(value), else: value
def to_output([], output) do
output
end

value ->
value
end
end)
def to_output({:ok, {path, result}}, output) do
put_in(output, Enum.map(path, &Access.key(&1, %{})), to_output(result))
end

def to_output(results) do
Enum.reduce(results, %{}, fn result, acc ->
case result do
{:ok, {path, value}} ->
if is_list(value),
do: put_in(acc, path, map_list_results(value)),
else: put_in(acc, path, value)
def to_output({:error, _}, output) do
output
end

:ok ->
acc
def to_output({:list, results}, output) do
to_output(results, output)
end

{:error, _} ->
acc
end
end)
def to_output([head | tail], output) do
to_output(tail, to_output(head, output))
end

defp set_schema(_caller, name, opts, block) do
Expand All @@ -407,7 +377,7 @@ defmodule Drops.Contract do
Map.put(
schemas,
unquote(name),
Drops.Types.from_spec(unquote(block), unquote(opts))
Drops.Type.Compiler.visit(unquote(block), unquote(opts))
)
)
end
Expand Down
34 changes: 34 additions & 0 deletions lib/drops/predicates/helpers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule Drops.Predicates.Helpers do
alias Drops.Predicates

def apply_predicates(value, {:and, predicates}) do
apply_predicates(value, predicates)
end

def apply_predicates(value, predicates) do
Enum.reduce(predicates, {:ok, value}, &apply_predicate(&1, &2))
end

def apply_predicate({:predicate, {name, args}}, {:ok, value}) do
apply_args =
case args do
[arg] -> [arg, value]
[] -> [value]
arg -> [arg, value]
end

if apply(Predicates, name, apply_args) do
{:ok, value}
else
if is_list(value) do
{:error, [input: value, predicate: name, args: apply_args]}
else
{:error, {value, predicate: name, args: apply_args}}
end
end
end

def apply_predicate(_, {:error, _} = error) do
error
end
end
Loading
Loading