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.
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
endDescribe 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@moduledocofMyApp.Order.Lines.doc: falsesets@moduledoc false, which hides the module from the docs.MyApp.Order.Auditabove is hidden, and the parent keeps the declared typestruct()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.
| Declaration | Type in t() |
|---|---|
field :customer, struct(), struct: MyApp.Customer | MyApp.Customer.t() |
field :stops, list(struct()), structs: MyApp.Stop | [MyApp.Stop.t()] |
sub_field :lines, struct() with a do block | MyApp.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:
%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.
defmodule MyApp.Account do
use GuardedStruct
@typedoc "An account that can sign in."
guardedstruct do
field :email, String.t()
end
endThe @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.
Next steps
- Nest structs with sub_field: generated modules, your own module and lists.
- DSL reference: the
docoption next to every other option. - Inspect, diff and observe structs: read the same metadata at run time with
GuardedStruct.Info.