From e9b3dc1691ee93c6d3647741a17b78d2cdb60753 Mon Sep 17 00:00:00 2001 From: Luiz Rodrigo de Souza <me@lurodrigo.com> Date: Wed, 30 Oct 2024 20:00:54 -0300 Subject: [PATCH 1/3] first try --- lib/type_id/ecto.ex | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/type_id/ecto.ex b/lib/type_id/ecto.ex index 4137fe2..815bc2e 100644 --- a/lib/type_id/ecto.ex +++ b/lib/type_id/ecto.ex @@ -95,22 +95,39 @@ if Code.ensure_loaded?(Ecto.ParameterizedType) do def load(_, _, _), do: :error + defp validate_type!(type) do + unless type in ~w[string uuid]a do + raise ArgumentError, "`type` must be `:string` or `:uuid`" + end + + type + end + + defp get_type(opts) do + default_type = Application.get_env(:typeid_elixir, :default_type, :string) + + case Keyword.get(opts, :column_type) do + nil -> + opts + |> Keyword.get(:type, default_type) + |> validate_type!() + + type -> + validate_type!(type) + end + end + defp validate_opts!(opts) do primary_key = Keyword.get(opts, :primary_key, false) schema = Keyword.fetch!(opts, :schema) field = Keyword.fetch!(opts, :field) - default_type = Application.get_env(:typeid_elixir, :default_type, :string) - type = Keyword.get(opts, :type, default_type) prefix = Keyword.get(opts, :prefix) + type = get_type(opts) if primary_key do TypeID.validate_prefix!(prefix) end - unless type in ~w[string uuid]a do - raise ArgumentError, "`type` must be `:string` or `:uuid`" - end - if primary_key do %{primary_key: primary_key, schema: schema, field: field, prefix: prefix, type: type} else From 262f47cd6c527918363a0c13d095e294a40c5640 Mon Sep 17 00:00:00 2001 From: Luiz Rodrigo de Souza <me@lurodrigo.com> Date: Wed, 30 Oct 2024 20:18:30 -0300 Subject: [PATCH 2/3] better support for default type --- lib/type_id/ecto.ex | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/type_id/ecto.ex b/lib/type_id/ecto.ex index 815bc2e..114d529 100644 --- a/lib/type_id/ecto.ex +++ b/lib/type_id/ecto.ex @@ -110,6 +110,10 @@ if Code.ensure_loaded?(Ecto.ParameterizedType) do nil -> opts |> Keyword.get(:type, default_type) + |> case do + TypeID -> default_type + type -> type + end |> validate_type!() type -> @@ -118,6 +122,7 @@ if Code.ensure_loaded?(Ecto.ParameterizedType) do end defp validate_opts!(opts) do + IO.inspect(opts) primary_key = Keyword.get(opts, :primary_key, false) schema = Keyword.fetch!(opts, :schema) field = Keyword.fetch!(opts, :field) @@ -133,6 +138,7 @@ if Code.ensure_loaded?(Ecto.ParameterizedType) do else %{schema: schema, field: field, type: type, prefix: prefix} end + |> IO.inspect() end defp find_prefix(%{prefix: prefix}) when not is_nil(prefix), do: prefix From b5095ed0e76188299bea79fa699cbf876e3ddca6 Mon Sep 17 00:00:00 2001 From: Luiz Rodrigo de Souza <me@lurodrigo.com> Date: Thu, 31 Oct 2024 13:55:54 -0300 Subject: [PATCH 3/3] remove IO.inspect --- lib/type_id/ecto.ex | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/type_id/ecto.ex b/lib/type_id/ecto.ex index 114d529..2da4736 100644 --- a/lib/type_id/ecto.ex +++ b/lib/type_id/ecto.ex @@ -122,7 +122,6 @@ if Code.ensure_loaded?(Ecto.ParameterizedType) do end defp validate_opts!(opts) do - IO.inspect(opts) primary_key = Keyword.get(opts, :primary_key, false) schema = Keyword.fetch!(opts, :schema) field = Keyword.fetch!(opts, :field) @@ -138,7 +137,6 @@ if Code.ensure_loaded?(Ecto.ParameterizedType) do else %{schema: schema, field: field, type: type, prefix: prefix} end - |> IO.inspect() end defp find_prefix(%{prefix: prefix}) when not is_nil(prefix), do: prefix