View Source Spark.Dsl.Entity (spark v1.0.6)

Declares a DSL entity.

A dsl entity represents a dsl constructor who's resulting value is a struct. This lets the user create complex objects with arbitrary(mostly) validation rules.

The lifecycle of creating entities is complex, happening as Elixir is compiling the modules in question. Some of the patterns around validating/transforming entities have not yet solidified. If you aren't careful and don't follow the guidelines listed here, you can have subtle and strange bugs during compilation. Anything not isolated to simple value validations should be done in transformers. See Spark.Dsl.Transformer.

An entity has a target indicating which struct will ultimately be built. An entity also has a schema. This schema is used for documentation, and the options are validated against it before continuing on with the DSL.

To create positional arguments to the builder, use args. The values provided to args need to be in the provided schema as well. They will be positional arguments in the same order that they are provided in the args key.

auto_set_fields will set the provided values into the produced struct (they do not need to be included in the schema).

transform is a function that takes a created struct and can alter it. This happens immediately after handling the DSL options, and can be useful for setting field values on a struct based on other values in that struct. If you need things that aren't contained in that struct, use an Spark.Dsl.Transformer.

entities allows you to specify a keyword list of nested entities. Nested entities are stored on the struct in the corresponding key, and are used in the same way entities are otherwise.

identifier expresses that a given entity is unique by that field, validated by the DSL.

For a full example, see Spark.Dsl.Extension.

Link to this section Summary

Link to this section Types

@type t() :: %Spark.Dsl.Entity{
  args: [atom()],
  auto_set_fields: Keyword.t(any()),
  deprecations: Keyword.t(String.t()),
  describe: String.t(),
  docs: String.t(),
  entities: Keyword.t(t()),
  examples: [String.t()],
  hide: [atom()],
  identifier: term(),
  imports: [module()],
  links: Keyword.t([String.t()]) | nil,
  modules: [atom()],
  name: atom() | nil,
  no_depend_modules: [atom()],
  recursive_as: atom() | nil,
  schema: Spark.OptionsHelpers.schema(),
  snippet: String.t(),
  target: module() | nil,
  transform: mfa() | nil
}

Link to this section Functions