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

This looks like a great library!

I was going through the limitations sections but didn’t understand much.

Can you give an example of what is not supported?

Thank you.

The following user types are not supported: parametrized types @type dict(key, value) :: [{key, value}] where key and value are another types. And self referencing types @type my_type :: [my_type].

These are quite rarely used, so if you don’t, it’s ok :slightly_smiling_face:

1 Like

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 :v:

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

How would one go for recursive types (which is done via @type my_type :: [my_type] ) ?

Say, a tree with nodes / leaves has to be defined. How can types of a leaves/nodes of tree be ensured using Domo?

One way I can think is this. @type node :: :root | [node]

Hey, thanks for your message. The self-referencing struct type suitable for building trees is exactly the case that is not supported by the library yet. PRs are welcome :slightly_smiling_face:

1.5.9 - November 12, 2022

  • Fix to run with Phoenix server with hot reload enabled in the root of an umbrella app.
  • Fix to use the current group leader of the caller in ResolvePlanner server for printing verbose debugging messages.
  • Rename option name_of_new_function to gen_constructor_name. Please, update your project if you use it :slightly_smiling_face:
  • Make Domo generated new!/1/0 and new/2/1/0 composable instead of overridable, see Custom constructor function section in README.
1 Like

1.5.10 - November 16, 2022

  • Improve compatibility with ElixirLS. After running mix deps.update domo, please, remove .elixir_ls in your project directory and reopen VSCode.

Now struct type checking errors are in the VSCode view :tada:

5 Likes

That’s amazing, thank you.

1 Like