Ecto.embedded_dump doesn't seem to call c:dump/1

Given this embedded Ecto schema…

defmodule Foo do
  use Ecto.Schema

  embedded_schema do
    field :state, Ecto.Enum, values: [:foo, :bar, :baz]
  end
end

Loading data seems to work properly (i.e. properly cast string to atom for the enum field:

record = Ecto.embedded_load(Foo, %{state: "foo"}, :json)

%Foo{id: nil, state: :foo}

But dumping doesn’t do the reverse…

data = Ecto.embedded_dump(record, :json)

%{id: nil, state: :foo}

Notice state is still an atom.

I figured it might have something to do with the format (:json), but I’m not sure what else to use, the docs are not clear what the options are.

I have the same problem with user implemented Ecto.Types.

What am I missing? Thanks for the help!

Take a look at the Ecto.Type.embed_as callback. Ecto types by default do not use dump/load but only json encode/decode+cast.

2 Likes

Perfect, thank you.

So I can do something like…

Ecto.embedded_dump(record, :cql)

Then define that callback in my custom type to pattern match on :cql.

I’m guessing there is no way to get at that for the built-in types like Ecto.Enum, eh?