Skip to content

Commit 83a2097

Browse files
committed
Coercions => Casters
1 parent 7c39816 commit 83a2097

File tree

6 files changed

+20
-20
lines changed

6 files changed

+20
-20
lines changed

lib/drops/casters.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
defmodule Drops.Casters do
2+
def cast(:string, :integer, value), do: String.to_integer(value)
3+
4+
def cast(:string, :float, value) do
5+
{float, _} = Float.parse(value)
6+
float
7+
end
8+
9+
def cast(:integer, :string, value), do: to_string(value)
10+
end

lib/drops/coercions.ex

Lines changed: 0 additions & 10 deletions
This file was deleted.

lib/drops/contract.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Drops.Contract do
22
defmacro __using__(_opts) do
33
quote do
4-
alias Drops.{Coercions, Predicates}
4+
alias Drops.{Casters, Predicates}
55
alias Drops.Contract.Schema
66
alias Drops.Contract.Schema.Key
77

@@ -70,14 +70,14 @@ defmodule Drops.Contract do
7070
def step(
7171
data,
7272
{:validate,
73-
%{type: {:coerce, {{input_type, input_predicates}, output_type}}} = key}
73+
%{type: {:cast, {{input_type, input_predicates}, output_type}}} = key}
7474
) do
7575
value = get_in(data, key.path)
7676

7777
case apply_predicates(value, input_predicates, path: key.path) do
7878
{:ok, _} ->
7979
validate(
80-
Coercions.coerce(input_type, output_type, value),
80+
Casters.cast(input_type, output_type, value),
8181
key.predicates,
8282
path: key.path
8383
)

lib/drops/contract/dsl.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule Drops.Contract.DSL do
88
end
99

1010
def from(type) do
11-
{:coerce, type}
11+
{:cast, type}
1212
end
1313

1414
def type([list: members]) when is_map(members) do
@@ -35,7 +35,7 @@ defmodule Drops.Contract.DSL do
3535
{:type, {type, predicates}}
3636
end
3737

38-
def type({:coerce, input_type}, output_type) do
39-
{:coerce, {type(input_type), type(output_type)}}
38+
def type({:cast, input_type}, output_type) do
39+
{:cast, {type(input_type), type(output_type)}}
4040
end
4141
end

lib/drops/contract/schema/key.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ defmodule Drops.Contract.Schema.Key do
3838
Enum.map(spec, &infer_type(&1, opts))
3939
end
4040

41-
defp infer_type({:coerce, {input_type, output_type}}, opts) do
42-
{:coerce,
41+
defp infer_type({:cast, {input_type, output_type}}, opts) do
42+
{:cast,
4343
{{infer_type(input_type, opts), infer_predicates(input_type, opts)},
4444
infer_type(output_type, opts)}}
4545
end
4646

47-
defp infer_predicates({:coerce, {_input_type, output_type}}, opts) do
47+
defp infer_predicates({:cast, {_input_type, output_type}}, opts) do
4848
infer_predicates(output_type, opts)
4949
end
5050

test/contract/coercions_test.exs renamed to test/contract/casters_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Drops.CoercionTest do
1+
defmodule Drops.CastersTest do
22
use Drops.ContractCase
33

44
describe ":integer => :string" do

0 commit comments

Comments
 (0)