Skip to content

Commit

Permalink
Fix cast/2 implementation of Ecto.ParameterizedType needs to return r…
Browse files Browse the repository at this point in the history
…untime data (#3436)
  • Loading branch information
elielhaouzi committed Oct 7, 2020
1 parent 71b9c93 commit 14bafd4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
12 changes: 10 additions & 2 deletions integration_test/support/types.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ defmodule ParameterizedPrefixedString do
use Ecto.ParameterizedType
def init(opts), do: Enum.into(opts, %{})
def type(_), do: :string
def cast(string, %{prefix: _}), do: {:ok, string |> String.split("-") |> List.last()}
def dump(string, _, %{prefix: _prefix} = opts), do: cast(string, opts)

def cast(data, %{prefix: prefix}) do
if String.starts_with?(data, [prefix <> "-"]) do
{:ok, data}
else
{:ok, prefix <> "-" <> data}
end
end

def load(string, _, %{prefix: prefix}), do: {:ok, prefix <> "-" <> string}
def dump(data, _, %{prefix: _prefix}), do: {:ok, data |> String.split("-") |> List.last()}
def embed_as(_, _), do: :dump
end
34 changes: 24 additions & 10 deletions test/ecto/repo/autogenerate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,21 @@ defmodule Ecto.Repo.AutogenerateTest do

@separator "_"

def init(params), do: Enum.into(params, %{})
def init(opts), do: Enum.into(opts, %{})
def type(_), do: :uuid
def load(uuid, _, %{prefix: prefix}), do: {:ok, prefix <> @separator <> uuid}

def dump(code, _, %{prefix: _prefix}),
do: {:ok, code |> String.split(@separator) |> List.last()}
def cast(data, %{prefix: prefix}) do
if String.starts_with?(data, [prefix <> @separator]) do
{:ok, data}
else
{:ok, prefix <> @separator <> data}
end
end

def load(uuid, _, %{prefix: prefix}), do: {:ok, prefix <> @separator <> uuid}

def cast(code, %{prefix: _}), do: {:ok, code |> String.split(@separator) |> List.last()}
def dump(data, _, %{prefix: _prefix}),
do: {:ok, data |> String.split(@separator) |> List.last()}

def autogenerate(%{autogenerate: true, prefix: prefix, field: :code, schema: _}),
do: prefix <> @separator <> Ecto.UUID.generate()
Expand All @@ -94,14 +101,21 @@ defmodule Ecto.Repo.AutogenerateTest do

@separator "_"

def init(params), do: Enum.into(params, %{})
def init(opts), do: Enum.into(opts, %{})
def type(_), do: :id
def load(id, _, %{prefix: prefix}), do: {:ok, prefix <> @separator <> to_string(id)}

def dump(code, _, %{prefix: _prefix}),
do: {:ok, code |> String.split(@separator) |> List.last() |> Integer.parse()}
def cast(data, %{prefix: prefix}) do
if String.starts_with?(data, [prefix <> @separator]) do
{:ok, data}
else
{:ok, prefix <> @separator <> data}
end
end

def load(id, _, %{prefix: prefix}), do: {:ok, prefix <> @separator <> to_string(id)}

def cast(code, %{prefix: _}), do: {:ok, code |> String.split(@separator) |> List.last()}
def dump(data, _, %{prefix: _prefix}),
do: {:ok, data |> String.split(@separator) |> List.last() |> Integer.parse()}
end

defmodule ParameterizedTypeSchema do
Expand Down

0 comments on commit 14bafd4

Please sign in to comment.