From 1055522dfdb24ef5a286295ad0d2e93849f8cc57 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Tue, 30 Jan 2024 08:05:03 +0100 Subject: [PATCH] Revert "Merge pull request #43 from solnic/type-options" This reverts commit a867a3e1367611f2c2333074909af190ee753c13, reversing changes made to acd62687e6204fdda5356614f9405a8af1e1fe72. Found a nicer way with @opts that will be introduced in the Number PR --- lib/drops/type.ex | 6 ++---- lib/drops/type/compiler.ex | 10 +++------- lib/drops/type/dsl.ex | 17 ----------------- lib/drops/types/union.ex | 4 ++-- test/contract/type_test.exs | 15 --------------- test/contract/types/string_test.exs | 15 --------------- test/contract/types/union_test.exs | 18 ------------------ 7 files changed, 7 insertions(+), 78 deletions(-) diff --git a/lib/drops/type.ex b/lib/drops/type.ex index c7eb06a..2560b41 100644 --- a/lib/drops/type.ex +++ b/lib/drops/type.ex @@ -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 @@ -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 diff --git a/lib/drops/type/compiler.ex b/lib/drops/type/compiler.ex index d7938f1..4661930 100644 --- a/lib/drops/type/compiler.ex +++ b/lib/drops/type/compiler.ex @@ -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) @@ -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 diff --git a/lib/drops/type/dsl.ex b/lib/drops/type/dsl.ex index 89080d3..99373fc 100644 --- a/lib/drops/type/dsl.ex +++ b/lib/drops/type/dsl.ex @@ -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. @@ -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 diff --git a/lib/drops/types/union.ex b/lib/drops/types/union.ex index f1681c5..8baed90 100644 --- a/lib/drops/types/union.ex +++ b/lib/drops/types/union.ex @@ -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 diff --git a/test/contract/type_test.exs b/test/contract/type_test.exs index c671e33..8d777d9 100644 --- a/test/contract/type_test.exs +++ b/test/contract/type_test.exs @@ -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 diff --git a/test/contract/types/string_test.exs b/test/contract/types/string_test.exs index 18504c8..cd30c81 100644 --- a/test/contract/types/string_test.exs +++ b/test/contract/types/string_test.exs @@ -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 diff --git a/test/contract/types/union_test.exs b/test/contract/types/union_test.exs index c09faad..7bf3431 100644 --- a/test/contract/types/union_test.exs +++ b/test/contract/types/union_test.exs @@ -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