Domo - model a business domain with type-safe structs and field type range checks

1.5.8 - September 11, 2022

  • Domo generated constructor functions new!/1/0 and new/2/1/0 become overridable.
  • Improves Ecto changeset validation. Now the Domo.Changeset.validate_type/1 skips has_many and other assoc fields automatically, so the cast_assoc(:field) can be called to do the associations validation sequentually.

The one validate_type/1 call in the changeset gives a 1.5x slower execution than the bunch of equivalent Ecto’s validate_... calls.

At the same time, the execution duration of the pure Domo generated constructor function new!/1 is the same as of building a struct with Ecto changeset approach.

Comparison: 
Domo Album.new!/1                       5.31
Ecto.Changeset validate_.../1           5.21 - 1.02x slower +3.59 ms
Domo.Changeset validate_type/1          3.76 - 1.41x slower +77.93 ms

You can find all benchmark results in the README :victory_hand:

The overridable constructors open the remarkable feature of injecting the generated default values into a struct, keeping the values validated. Let the struct using Domo have a token field, then the generation of the default value can be done like that:

def new!([_|_] = fields) do
  super(Keyword.merge(fields, token: random_string(8)))
end

So no matter what random_string returns, the super function will ensure that it matches the type of the token field defined in t().

Magnificent! :slightly_smiling_face:

5 Likes