mtwilliams / blazon

:beginner: Declarative abstract serializers for Elixir.
Other
4 stars 2 forks source link

Extend our intermediate format to include meta. #4

Open mtwilliams opened 8 years ago

mtwilliams commented 8 years ago

Given something like,

defmodule Person do
  defstruct ~w(name)a
end

defmodule PersonSerializer do
  use Blazon.Serializable
  field name, :string
  field gender, enum: [type: :gender, ~w(male female)a], via: fn (person) ->
    if String.contains?("John")
      :male
    else
      :female
    end
  end
end

We should produce an intermediate form like so:

iex(1)> Blazon.intermediate([PersonSerializer], [%Person{name: "John Doe"}, %Person{name: "Jane Doe"}])
{[[{:name, [type: :string], "John Doe"},
   {:gender, [type: {:enum, [name: :gender], [:male, :female]}], :male}],
  [{:name, [type: :string], "Jane Doe"},
   {:gender, [type: {:enum, [name: :gender], [:male, :female]}], :female}],
 [type: [People]]}

This will allow more structured serializers i.e. xml to provide proper type semantics.

mtwilliams commented 8 years ago

I'm thinking a 3-arity tuple of the form {field, [attributes], generator} would make the most sense.