Document a struct and its types

v0.1.11

Give a GuardedStruct module docs that readers can use: a description for each field, a @moduledoc for each nested module, and a t() type that links every nested type to its page. This guide shows the doc: option, what the generated t() and its @typedoc contain, and how to take over a doc yourself.

Before you begin

You need a guarded module and a project that builds docs with ExDoc. Code.fetch_docs/1 shows the same docs without ExDoc. The examples use these two modules.

elixir
defmodule MyApp.Customer do
  use GuardedStruct

  guardedstruct do
    field :name, String.t(), enforce: true
  end
end

defmodule MyApp.Order do
  @moduledoc "An order and its lines."
  use GuardedStruct

  guardedstruct do
    field :email, String.t(), enforce: true, doc: "Where the receipts go."
    field :internal_ref, String.t(), doc: false
    field :customer, struct(), struct: MyApp.Customer

    sub_field :lines, list(struct()), structs: true, doc: "One movement of the balance." do
      field :sku, String.t(), enforce: true, derives: "validate(string, not_empty)"
    end

    sub_field :audit, struct(), doc: false do
      field :by, String.t()
    end
  end
end

Describe a field

Every module gets a @typedoc on t(): a Markdown table with one row for each field. The columns are the field name, its type, whether it is required, its default, its description and its derives. The validator: of a field is listed in the derives column.

The doc: option is the description. It takes a Markdown string, and it works on field, virtual_field, dynamic_field and conditional_field. doc: false leaves the field out of the table. Any other value fails to compile with a Spark.Error.DslError. A virtual_field is listed in a second table, because the builder checks it and leaves it out of the struct.

For MyApp.Order, the table of t() reads:

| Field | Type | Required | Default | Description | Derives |
| --- | --- | --- | --- | --- | --- |
| `:email` | `String.t()` | yes |  | Where the receipts go. |  |
| `:customer` | [`MyApp.Customer.t()`](`t:MyApp.Customer.t/0`) \| `nil` | no |  |  |  |
| `:lines` | [`[MyApp.Order.Lines.t()]`](`t:MyApp.Order.Lines.t/0`) \| `nil` | no |  | One movement of the balance. |  |
| `:audit` | `struct()` \| `nil` | no |  |  |  |

:internal_ref is not in the table because of doc: false.

Document a nested module

Each sub_field with a do block generates a module, and its doc: becomes the @moduledoc of that module. ExDoc then gives the module its own page, and Code.fetch_docs/1 returns the text. The same string is the description in the parent's table. When the string has more than one paragraph, the table shows the first.

  • doc: "text" sets the @moduledoc of MyApp.Order.Lines.
  • doc: false sets @moduledoc false, which hides the module from the docs. MyApp.Order.Audit above is hidden, and the parent keeps the declared type struct() for it.
  • Without doc: the generated module has no @moduledoc.

doc: works on a list sub_field, on a sub_field inside a sub_field and on a sub_field inside a conditional_field. Giving both doc: and the doc setter inside the block fails to compile with a Spark.Error.DslError.

The @moduledoc you write on the parent stays exactly as you wrote it. A sub_field only sets the doc of the module it generates. A sub_field with no do block uses your own module, so its doc: is only the description in the parent's table, and your module keeps its own docs.

Read the types in t()

The type of a field in t() says what the field builds, so ExDoc can link it.

DeclarationType in t()
field :customer, struct(), struct: MyApp.CustomerMyApp.Customer.t()
field :stops, list(struct()), structs: MyApp.Stop[MyApp.Stop.t()]
sub_field :lines, struct() with a do blockMyApp.Order.Lines.t(), and [MyApp.Order.Lines.t()] with structs: true
conditional_field :plan, any()The union of its branches, such as String.t() | MyApp.Order.Plan1.t(), and a list of that union with structs: true

Only a loose type that you declared is replaced: struct(), map(), any(), term(), list(), or a list or union of those. Any other type stays as you wrote it. A sub_field with doc: false keeps its declared type, because its module has no page. A field that is not enforced adds | nil. A module whose fields are named with regexes has t() as %{optional(String.t()) => ...}, unless the module defines its own t.

For MyApp.Order the type is:

elixir
%MyApp.Order{
  audit: struct() | nil,
  customer: MyApp.Customer.t() | nil,
  email: String.t(),
  internal_ref: String.t() | nil,
  lines: [MyApp.Order.Lines.t()] | nil
}

Dialyzer checks a struct: field against t() of the module it names. A module you wrote by hand needs a t() type for this. Every GuardedStruct module has one. Otherwise, give the field a type of your own.

Write your own docs

A @typedoc that you write above guardedstruct do replaces the generated table.

elixir
defmodule MyApp.Account do
  use GuardedStruct

  @typedoc "An account that can sign in."
  guardedstruct do
    field :email, String.t()
  end
end

The @moduledoc of a module is never changed, with or without doc: on its fields.

Use doc: in an Ash resource

The GuardedStruct.AshResource extension takes the same doc: option on its fields and on sub_field. A sub_field with a do block generates a module there too, and its doc: is that module's @moduledoc. The resource keeps its own @moduledoc and gets no generated @typedoc table, because Ash defines the resource's struct and GuardedStruct does not.

Fix an ExDoc warning about a hidden module

ExDoc warns about a link to a module hidden with @moduledoc false. Because t() now links the module of a struct: field, a hidden struct: module makes this warning. Document the module, or tell ExDoc to skip the warning in mix.exs. The option matches the module whose docs hold the link, which is the module that declares the struct: field, or its type id, and not the hidden module:

elixir
docs: [
  skip_undefined_reference_warnings_on: ["MyApp.Order", "MyApp.Order.t/0"]
]

Next steps