Skip to content

Commit

Permalink
Share loading/dumping logic between embed and typed maps, closes #2637
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Aug 22, 2018
1 parent 66cf7e4 commit 0e8ab76
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion integration_test/cases/type.exs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ defmodule Ecto.Integration.TypeTest do
end

@tag :map_type
test "typed map" do
test "typed string map" do
post1 = TestRepo.insert!(%Post{links: %{"foo" => "http://foo.com", "bar" => "http://bar.com"}})
post2 = TestRepo.insert!(%Post{links: %{foo: "http://foo.com", bar: "http://bar.com"}})

Expand All @@ -203,6 +203,14 @@ defmodule Ecto.Integration.TypeTest do
[%{"foo" => "http://foo.com", "bar" => "http://bar.com"}]
end

@tag :map_type
test "typed float map" do
post = TestRepo.insert!(%Post{intensities: %{"foo" => 1.0, "bar" => 416500.0}})

assert TestRepo.all(from p in Post, where: p.id == ^post.id, select: p.intensities) ==
[%{"foo" => 1.0, "bar" => 416500}]
end

@tag :map_type
test "map type on update" do
post = TestRepo.insert!(%Post{meta: %{"world" => "hello"}})
Expand Down
1 change: 1 addition & 0 deletions integration_test/support/migration.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule Ecto.Integration.Migration do
add :uuid, :uuid
add :meta, :map
add :links, {:map, :string}
add :intensities, {:map, :float}
add :public, :boolean
add :cost, :decimal, precision: 2, scale: 1
add :visits, :integer
Expand Down
1 change: 1 addition & 0 deletions integration_test/support/schemas.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule Ecto.Integration.Post do
field :uuid, Ecto.UUID, autogenerate: true
field :meta, :map
field :links, {:map, :string}
field :intensities, {:map, :float}
field :posted, :date
has_many :comments, Ecto.Integration.Comment, on_delete: :delete_all, on_replace: :delete
has_one :permalink, Ecto.Integration.Permalink, on_delete: :delete_all, on_replace: :delete
Expand Down
1 change: 1 addition & 0 deletions lib/ecto/adapters/postgres.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ defmodule Ecto.Adapters.Postgres do
# Support arrays in place of IN
@doc false
def dumpers({:embed, _} = type, _), do: [&Ecto.Adapters.SQL.dump_embed(type, &1)]
def dumpers({:map, _} = type, _), do: [&Ecto.Adapters.SQL.dump_embed(type, &1)]
def dumpers({:in, sub}, {:in, sub}), do: [{:array, sub}]
def dumpers(:binary_id, type), do: [type, Ecto.UUID]
def dumpers(_, type), do: [type]
Expand Down
2 changes: 2 additions & 0 deletions lib/ecto/adapters/sql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ defmodule Ecto.Adapters.SQL do

@doc false
def loaders({:embed, _} = type, _), do: [&Ecto.Adapters.SQL.load_embed(type, &1)]
def loaders({:map, _} = type, _), do: [&Ecto.Adapters.SQL.load_embed(type, &1)]
def loaders(:binary_id, type), do: [Ecto.UUID, type]
def loaders(_, type), do: [type]

@doc false
def dumpers({:embed, _} = type, _), do: [&Ecto.Adapters.SQL.dump_embed(type, &1)]
def dumpers({:map, _} = type, _), do: [&Ecto.Adapters.SQL.dump_embed(type, &1)]
def dumpers(:binary_id, type), do: [type, Ecto.UUID]
def dumpers(_, type), do: [type]

Expand Down

0 comments on commit 0e8ab76

Please sign in to comment.