Skip to main content
Version: 2.x

Documentation Generation

Every config description can produce a document that lists its settings. generateDocs turns a Config[A] into a ConfigDocs value, ConfigDocs#toTable turns that into a Table, and the table renders as GitHub or Confluence Markdown. The output is a reference for the people who operate the application: it shows each key, its format and its description.

The document is generated from the description, so it stays in step with the code. A setting you remove from the description disappears from the document.

Generating a Documentโ€‹

generateDocs comes from import zio.config._. It works on any description, hand-written or derived:

import zio._
import zio.config._
import zio.config.magnolia.deriveConfig

final case class Server(host: String, port: Int, region: Option[String])

val serverConfig: Config[Server] = deriveConfig[Server]

val docs: ConfigDocs = generateDocs(serverConfig)

Table#toGithubFlavouredMarkdown renders the table as GitHub Markdown:

println(docs.toTable.toGithubFlavouredMarkdown)
//
// ## Configuration Details
//
//
// |FieldName|Format |Description|Sources|
// |--- |--- |--- |--- |
// | |[all-of](#field-descriptions)| | |
//
// ### Field Descriptions
//
// |FieldName|Format |Description |Sources|
// |--- |--- |--- |--- |
// |host |primitive|a text property | |
// |region |primitive|a text property | |
// |port |primitive|an integer property| |
//

The output has a fixed shape. It starts with a ## Configuration Details heading. A case class becomes an all-of row that links to a nested table, here Field Descriptions, because its fields are combined with Config#zip. Each leaf key has the format primitive, and its description starts with a short text from ZIO, such as a text property or an integer property. The Sources column is always empty in ZIO Config 4.

Adding Descriptionsโ€‹

Config#?? attaches text to a key. In a derived description, the @describe annotation does the same. The text is added after the built-in description:

import zio.config.magnolia.describe

@describe("Settings for the HTTP listener")
final case class Listener(
@describe("Host name or IP address to bind") host: String,
@describe("Port number, 1024 or above") port: Int
)

In the generated table, each field shows the built-in text, then its own description, then the description of the case class:

println(generateDocs(deriveConfig[Listener]).toTable.toGithubFlavouredMarkdown)
//
// ## Configuration Details
//
//
// |FieldName|Format |Description|Sources|
// |--- |--- |--- |--- |
// | |[all-of](#field-descriptions)| | |
//
// ### Field Descriptions
//
// |FieldName|Format |Description |Sources|
// |--- |--- |--- |--- |
// |host |primitive|a text property, Host name or IP address to bind, Settings for the HTTP listener| |
// |port |primitive|an integer property, Port number, 1024 or above, Settings for the HTTP listener | |
//

On Scala 3, @describe on the case class itself is currently ignored; field descriptions work on both versions.

A hand-written description uses Config#?? on each key, and on the whole description:

val handWritten: Config[Server] =
((Config.string("HOST") ?? "Host name") zip
(Config.int("PORT") ?? "Port number") zip
(Config.string("REGION").optional ?? "Region, optional")).to[Server] ?? "The HTTP server"

Nested descriptions produce one table per level. A prefix added with Config#nested becomes a row named after the prefix, which links to a table with the keys under it:

println(generateDocs(handWritten.nested("server")).toTable.toGithubFlavouredMarkdown)
//
// ## Configuration Details
//
//
// |FieldName |Format |Description |Sources|
// |--- |--- |--- |--- |
// |[server](#server)|[all-of](#server)|The HTTP server| |
//
// ### server
//
// |FieldName|Format |Description |Sources|
// |--- |--- |--- |--- |
// |HOST |primitive|a text property, Host name | |
// |PORT |primitive|an integer property, Port number | |
// |REGION |primitive|a text property, Region, optional| |
//

Formatsโ€‹

The Format column tells the reader what shape of value a key expects. These are the values of Table.Format:

FormatRendered asProduced by
PrimitiveprimitiveA single value such as Config.string or Config.int
NestednestedA prefix that directly contains another prefix, such as nested("app", "db")
AllOfall-ofConfig#zip: the keys in the nested table are read together, including optional ones
AnyOneOfany-one-ofConfig#orElse, a sealed trait, or a key with a default: one of the alternatives is enough
ListlistConfig.listOf and the other sequence descriptions
MapmapConfig.table, or Config#switch
RecursionrecursionDefined for recursive structures; generateDocs does not produce it in ZIO Config 4
RecursionListlist of recursionDefined for recursive lists; generateDocs does not produce it in ZIO Config 4
NotApplicableemptyA row with no format

A sealed trait or Config#orElse produces an any-one-of row. For example, a choice between two database settings:

val database: Config[String] =
(Config.string("url") ?? "Full JDBC URL") orElse (Config.string("host") ?? "Host name only")

The db row has the format any-one-of, and its table lists both alternatives:

println(generateDocs(database.nested("db")).toTable.toGithubFlavouredMarkdown)
//
// ## Configuration Details
//
//
// |FieldName|Format |Description|Sources|
// |--- |--- |--- |--- |
// |[db](#db)|[any-one-of](#db)| | |
//
// ### db
//
// |FieldName|Format |Description |Sources|
// |--- |--- |--- |--- |
// |url |primitive|a text property, Full JDBC URL | |
// |host |primitive|a text property, Host name only| |
//

Rendering Markdownโ€‹

Table has three rendering functions:

FunctionOutput
Table#toGithubFlavouredMarkdownMarkdown with GitHub-style heading anchors; it also renders on GitLab
Table#toConfluenceMarkdown(baseUrl)Confluence-flavoured Markdown; baseUrl: Option[String] is the URL of the Confluence page, used to build the links
Table#toMarkdown(getLink)Markdown with links built by your own function

To render for a Confluence page:

println(docs.toTable.toConfluenceMarkdown(Some("https://wiki.example.com/display/OPS/Config")))
//
// ## Configuration Details
//
//
// |FieldName|Format |Description|Sources|
// |--- |--- |--- |--- |
// | |[all-of|https://wiki.example.com/display/OPS/Config-FieldDescriptions]| | |
//
// ### Field Descriptions
//
// |FieldName|Format |Description |Sources|
// |--- |--- |--- |--- |
// |host |primitive|a text property | |
// |region |primitive|a text property | |
// |port |primitive|an integer property| |
//

Table#toMarkdown takes a function (Table.Heading, Int, Either[Table.FieldName, Table.Format]) => Table.Link. Table.githubFlavoured and Table.confluenceFlavoured(baseUrl) are the two built-in functions, and Table.Link.githubLink, Table.Link.confluenceLink and Table.Link.rawString build links. A function that never links gives plain text in the first two columns:

println(docs.toTable.toMarkdown((_, _, nameOrFormat) => Table.Link.rawString(nameOrFormat.fold(_.asString(None), _.asString))))
//
// ## Configuration Details
//
//
// |FieldName|Format|Description|Sources|
// |--- |--- |--- |--- |
// | |all-of| | |
//
// ### Field Descriptions
//
// |FieldName|Format |Description |Sources|
// |--- |--- |--- |--- |
// |host |primitive|a text property | |
// |region |primitive|a text property | |
// |port |primitive|an integer property| |
//

Working With the Tableโ€‹

Table is a case class with a list of TableRow values, so you can read it or change it before rendering. Each TableRow has these fields:

FieldTypeMeaning
pathsList[Table.FieldName]The key path of the row; FieldName.Blank for a root row
formatOption[Table.Format]The format, if known
descriptionList[ConfigDocs.Description]Built-in and user descriptions
nestedOption[Table]The nested table that a link points to
sourcesSet[String]Always empty in ZIO Config 4

To list every leaf key with its descriptions, walk the rows and their nested tables:

def leafKeys(table: Table): List[String] =
table.rows.flatMap { row =>
row.nested match {
case Some(nested) => leafKeys(nested)
case None => List(row.paths.map(_.asString(None)).mkString(".") + ": " + row.description.map(_.description).mkString(", "))
}
}

leafKeys(generateDocs(handWritten).toTable).foreach(println)
// HOST: a text property, Host name, The HTTP server
// PORT: an integer property, Port number, The HTTP server
// REGION: a text property, Region, optional, The HTTP server

Table#++ joins two tables. Table#withFormat and Table#mapFormat change the format column of every row, for example to hide it with Table.Format.NotApplicable.

ConfigDocs is the tree that generateDocs builds before it becomes a table. Its cases follow the shape of the description: Leaf for a primitive, Nested for a prefix, Zip for Config#zip, OrElse for Config#orElse, Sequence for a list, and DynamicMap for a table or a switch. The case classes are private to zio.config, so call ConfigDocs#toTable and work with the Table instead.

Writing the Document to a Fileโ€‹

The rendered table is a plain String, so any file API can write it. To keep a committed copy up to date and check it in CI, follow How to keep a configuration reference in sync with CI.

Limitsโ€‹

generateDocs does not support recursive descriptions built with Config.defer: it keeps following the recursion and never returns. Generate documents only for descriptions without recursion.

Next Stepsโ€‹

ZIO Chat

๐Ÿ”ฅ Ignite your ZIO journey with ZIO Chat! ๐Ÿ”ฅ Ready to unravel the intricacies of ZIO? As your trusty sidekick, I'm here to light the way. No matter if you're wandering through functional forests or traversing effectful landscapes, I've got answers to keep you on track. Your ZIO adventure starts here โ€“ ask me anything and let's explore together!