Skip to content

Commit

Permalink
Revert "Merge pull request #43 from solnic/type-options"
Browse files Browse the repository at this point in the history
This reverts commit a867a3e, reversing
changes made to acd6268.

Found a nicer way with @opts that will be introduced in the Number PR
  • Loading branch information
solnic committed Jan 30, 2024
1 parent a867a3e commit 1055522
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 78 deletions.
6 changes: 2 additions & 4 deletions lib/drops/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ defmodule Drops.Type do

deftype(
primitive: Type.infer_primitive(unquote(spec)),
constraints: Type.infer_constraints(unquote(spec)),
opts: []
constraints: Type.infer_constraints(unquote(spec))
)

def new(attributes) when is_list(attributes) do
Expand Down Expand Up @@ -79,8 +78,7 @@ defmodule Drops.Type do
quote do
deftype(
primitive: unquote(primitive),
constraints: type(unquote(primitive)),
opts: []
constraints: type(unquote(primitive))
)
end
end
Expand Down
10 changes: 3 additions & 7 deletions lib/drops/type/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Drops.Type.Compiler do
end

def visit({:union, {left, right}}, opts) do
Union.new(visit(left, opts), visit(right, opts), opts)
Union.new(visit(left, opts), visit(right, opts))
end

def visit({:type, {:list, member_type}}, opts)
Expand Down Expand Up @@ -56,11 +56,7 @@ defmodule Drops.Type.Compiler do
mod.new(opts)
end

def visit({:opts, {type, opts}}, more_opts) do
visit(type, Keyword.merge(more_opts, opts))
end

def visit(spec, opts) when is_tuple(spec) do
Elixir.Map.merge(Primitive.new(spec), %{opts: opts})
def visit(spec, _opts) when is_tuple(spec) do
Primitive.new(spec)
end
end
17 changes: 0 additions & 17 deletions lib/drops/type/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Drops.Type.DSL do
"""

@type type() :: {:type, {atom(), keyword()}}
@type opts() :: {:opts, {type(), keyword()}}

@doc ~S"""
Returns a required key specification.
Expand Down Expand Up @@ -433,20 +432,4 @@ defmodule Drops.Type.DSL do
def map(predicates) when is_list(predicates) do
type(:map, predicates)
end

@doc ~S"""
Add options to a type specification.
## Examples
# a string with a custom name
opts(string(:filled?), name: :email)
"""
@doc since: "0.2.0"

@spec opts(type(), Keyword.t()) :: opts()

def opts(type, opts) do
{:opts, {type, opts}}
end
end
4 changes: 2 additions & 2 deletions lib/drops/types/union.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ defmodule Drops.Types.Union do
use Drops.Type do
deftype([:left, :right, :opts])

def new(left, right, opts \\ []) when is_struct(left) and is_struct(right) do
struct(__MODULE__, left: left, right: right, opts: opts)
def new(left, right) when is_struct(left) and is_struct(right) do
struct(__MODULE__, left: left, right: right)
end
end

Expand Down
15 changes: 0 additions & 15 deletions test/contract/type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,4 @@ defmodule Drops.Contract.TypeTest do
)
end
end

describe "type/1 with a type atom and options" do
contract do
schema do
%{required(:test) => opts(type(:string, [:filled?]), name: :test_name)}
end
end

test "returns success with valid data", %{contract: contract} do
[key] = contract.schema().keys
%{opts: opts} = key.type

assert Keyword.get(opts, :name) == :test_name
end
end
end
15 changes: 0 additions & 15 deletions test/contract/types/string_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,4 @@ defmodule Drops.Contract.Types.StringTest do
assert_errors(["test must be filled"], contract.conform(%{test: ""}))
end
end

describe "string/1 with a type atom and options" do
contract do
schema do
%{required(:test) => opts(string(:filled?), name: :test_name)}
end
end

test "returns success with valid data", %{contract: contract} do
[key] = contract.schema().keys
%{opts: opts} = key.type

assert Keyword.get(opts, :name) == :test_name
end
end
end
18 changes: 0 additions & 18 deletions test/contract/types/union_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,4 @@ defmodule Drops.Contract.Types.UnionTest do
)
end
end

describe "a union of two primitive types and with opts" do
contract do
schema do
%{
required(:test) =>
union([string(size?: 5), integer(gt?: 0)]) |> opts(name: :str_or_int)
}
end
end

test "returns success when left side is a success", %{contract: contract} do
[key] = contract.schema().keys
%{opts: opts} = key.type

assert Keyword.get(opts, :name) == :str_or_int
end
end
end

0 comments on commit 1055522

Please sign in to comment.