Skip to content

Commit d3b8d90

Browse files
committed
Add text
1 parent 24adbba commit d3b8d90

File tree

1 file changed

+109
-99
lines changed

1 file changed

+109
-99
lines changed

doc/reference/reference_lua/config/utils_schema.rst

Lines changed: 109 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -77,47 +77,44 @@ the role configuration:
7777
Defining a schema
7878
-----------------
7979

80-
An application's or a role's *configuration schema* is a core object that stores all
81-
information about a user-defined configuration. To create a schema, use
82-
the :ref:`schema.new() <config-utils-schema-new>` function. It has the following options:
80+
A *configuration schema* stores information about a user-defined configuration structure
81+
that can be passed inside an :ref:`app.cfg <configuration_reference_app_cfg>`
82+
or a :ref:`roles_cfg <configuration_reference_roles_cfg>` section. It includes
83+
option names, types, hierarchy, and other aspects of a configuration.
8384

84-
- schema name -- an arbitrary string
85-
- schema node -- th
85+
To create a schema, use the :ref:`schema.new() <config-utils-schema-new>` function.
86+
It has the following arguments:
8687

87-
- scalar and composite (record, array, map) types
88-
- annotations (type, validate, and so on)
89-
90-
- (optional) methods
88+
- schema name -- an arbitrary string to use as an identifier
89+
- schema nodes -- a hierarchical structure of configuration options that form
90+
the schema.
91+
- (optional) methods -- functions that can be called on this schema object.
9192

9293
.. _config_utils_schema_nodes:
9394

9495
Schema nodes
9596
~~~~~~~~~~~~
9697

97-
Schema nodes can have one of two types: *scalar* or *composite*.
98-
.. _config_utils_schema_scalar_composite_types:
99-
100-
Scalar and composite types
101-
**************************
98+
Schema nodes describe the hierarchy of options within a schema. There are two types of schema nodes:
10299

103-
There are scalar and composite types.
100+
- *Scalar* nodes hold a single value of a supported primitive type. For example,
101+
a string configuration option of a role is a scalar node its schema.
102+
- *Composite* nodes include multiple values in different forms: records, arrays, or maps.
104103

105-
- Scalar type.
106-
Can be created using ``schema.scalar()``.
107-
There is also a shortcut: :ref:`schema.enum() <config-utils-schema-enum>`.
108-
Learn more about supported data types: :ref:`config_utils_schema_data_types`.
109-
- Composite data types: record, array, map.
110104
Can be created using :ref:`schema.record() <config-utils-schema-record>`, :ref:`schema.array() <config-utils-schema-array>`, :ref:`schema.map() <config-utils-schema-map>`.
111105
There is also a shortcut for arrays: :ref:`schema.set() <config-utils-schema-set>`.
112106

107+
Each schema node is defined by its *annotations*
108+
- annotations (type, validate, and so on)
109+
113110

114111
.. _config_utils_schema_nodes_scalar:
115112

116113
Scalar nodes
117114
************
118115

119-
Scalar nodes hold a single value of a primitive type: a string, a number, a boolean value
120-
and so on. For the full list of available scalar types, see :ref:`config_utils_schema_data_types`.
116+
Scalar nodes hold a single value of a primitive type, for example, a string or a number.
117+
For the full list of supported scalar types, see :ref:`config_utils_schema_data_types`.
121118

122119
This configuration has one scalar node of the ``string`` type:
123120

@@ -135,18 +132,69 @@ The following code defines a configuration schema shown above:
135132
:end-before: local function validate
136133
:dedent:
137134

138-
Todo: enum?
139-
allowed?
135+
If a scalar node has a limited set of allowed values, you can also define it with
136+
the :ref:`schema.enum() <config-utils-schema-enum>`. Pass the list of allowed values as
137+
its argument:
138+
139+
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/config_schema_nodes_record/http_api.lua
140+
:language: lua
141+
:start-at: scheme
142+
:end-before: host
143+
:dedent:
144+
145+
.. note::
146+
147+
Another way to restrict possible option values is the :ref:`allowed_values <config-schema_node_annotation-allowed_values>`
148+
built-in annotation.
149+
150+
.. _config_utils_schema_data_types:
151+
152+
Data types
153+
^^^^^^^^^^
154+
155+
Scalar nodes can have the following data types:
156+
157+
.. container:: table
158+
159+
.. list-table::
160+
:header-rows: 1
161+
162+
* - Scalar type
163+
- Lua type
164+
- Comment
165+
166+
* - ``string``
167+
- ``string``
168+
-
169+
170+
* - ``number``
171+
- ``number``
172+
-
173+
* - ``integer``
174+
- ``number``
175+
- Only integer numbers
176+
177+
* - ``boolean``
178+
- ``boolean``
179+
- ``true`` or ``false``
180+
181+
* - ``string, number``
182+
or
183+
``number, string``
184+
- ``string`` or ``number``
185+
-
186+
187+
* - ``any``
188+
- Arbitrary Lua value
189+
- May be used to declare an arbitrary value that doesn't need validation.
140190

141191
.. _config_utils_schema_nodes_record:
142192

143193
Records
144194
*******
145195

146196
*Record* is a composite node that includes a predefined set of other nodes, scalar
147-
or composite. The names and types of fields in a record are determined by the schema.
148-
149-
In YAML, a record is represented as a node with nested fields.
197+
or composite. In YAML, a record is represented as a node with nested fields.
150198
For example, the following configuration has a record node ``http_api`` with
151199
three scalar fields:
152200

@@ -155,19 +203,15 @@ three scalar fields:
155203
:start-at: roles
156204
:dedent:
157205

158-
The following schema describes this configuration:
206+
To define a record node in a schema, use :ref:`schema.record() <config-utils-schema-record>`:
207+
The following schema describes the configuration above:
159208

160209
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/config_schema_nodes_record/http_api.lua
161210
:language: lua
162211
:start-at: local listen_address_schema
163212
:end-before: local function validate
164213
:dedent:
165214

166-
.. note::
167-
168-
Note the use of the :ref:`schema.enum() <config-utils-schema-enum>` function.
169-
It defines a scalar node with a limited set of allowed values.
170-
171215
Records are also used to define nested schema nodes of non-primitive types. In the example
172216
below, the ``http_api`` node holds a single composite object -- ``listen_address``.
173217

@@ -176,7 +220,6 @@ below, the ``http_api`` node holds a single composite object -- ``listen_address
176220
:start-at: roles
177221
:dedent:
178222

179-
To create a record node in a schema, use :ref:`schema.record() <config-utils-schema-record>`.
180223
The following schema describes this configuration:
181224

182225
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/config_schema_nodes_record_hierarchy/http_api.lua
@@ -194,7 +237,7 @@ Array
194237
*****
195238

196239
*Array* is a composite node type that includes a collection of items of the same
197-
type. The type can be either primitive or complex.
240+
type. The items can be either scalar or composite nodes.
198241

199242
In YAML, array items start with hyphens. For example, the following configuration
200243
includes an array named ``http_api``. Each its item is a record with three fields:
@@ -215,7 +258,7 @@ The following schema describes this configuration:
215258
:dedent:
216259

217260
There is also the :ref:`schema.set() <config-utils-schema-set>` function that enables
218-
creating arrays with a limited set of item values.
261+
creating arrays with a limited set of allowed items.
219262

220263
.. _config_utils_schema_nodes_map:
221264

@@ -226,75 +269,33 @@ Map
226269
of predefined types.
227270

228271
In YAML, a map is represented as a node with nested fields.
229-
For example, the following configuration has a map node ``endpoints`` with
230-
three items:
272+
For example, the following configuration has the ``endpoints`` node:
231273

232274
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/config_schema_nodes_map/config.yaml
233275
:language: yaml
234276
:start-at: roles
235277
:dedent:
236278

237279
To create a map node in a schema, use :ref:`schema.map() <config-utils-schema-map>`.
238-
The following schema describes this configuration:
280+
If this node is declared as a map as shown below, the ``endpoints`` section can include
281+
any number of options with arbitrary names and boolean values.
239282

240283
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/config_schema_nodes_map/http_api.lua
241284
:language: lua
242285
:start-at: local listen_address_schema
243286
:end-before: local function validate
244287
:dedent:
245288

246-
247-
.. _config_utils_schema_data_types:
248-
249-
Data types
250-
**********
251-
252-
Scalar nodes can have the following data types:
253-
254-
255-
.. container:: table
256-
257-
.. list-table::
258-
:header-rows: 1
259-
260-
* - Scalar type
261-
- Lua type
262-
- Comment
263-
264-
* - ``string``
265-
- ``string``
266-
-
267-
268-
* - ``number``
269-
- ``number``
270-
-
271-
* - ``integer``
272-
- ``number``
273-
- Only integer numbers
274-
275-
* - ``boolean``
276-
- ``boolean``
277-
- ``true`` or ``false``
278-
279-
* - ``any``
280-
- Arbitrary Lua value
281-
- May be used to declare an arbitrary value that doesn't need validation.
282-
283-
* - |``string, number``
284-
| or
285-
|``number, string``
286-
- ``string`` or ``number``
287-
-
288-
289289
.. _config_utils_schema_annotation:
290290

291291
Annotations
292292
***********
293293

294-
Each scalar node is defined by a set of *annotations* -- attributes that set its
295-
parameters: type, default value, validation function, etc
294+
Node *annotations* are attributes that define the its various aspects. For example,
295+
scalar nodes have a required annotation ``type`` that defines the option type.
296+
Other annotations can optionally set default values, validation function, and
296297

297-
Annotations are passed as schema.scalar argument rows
298+
Annotations are passed in a table to the node creation function:
298299

299300
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/config_schema_annotations/http_api.lua
300301
:language: lua
@@ -304,9 +305,12 @@ Annotations are passed as schema.scalar argument rows
304305

305306
Node annotations fall into three groups:
306307

307-
- Built-in annotations handled by the module (``validate``, ``allowed_values``, ``default``, ``apply_default_if``). Note that ``validate``, ``allowed_values`` used for validation only. ``default`` and ``apply_default_if`` can transform the configuration.
308-
- User-defined annotations
309-
- Computed annotations
308+
- *Built-in annotations* are handled by the module. These are: ``type``, ``validate``, ``allowed_values``, ``default`` and ``apply_default_if``.
309+
Note that ``validate``, ``allowed_values`` are used for validation only. ``default`` and ``apply_default_if`` can transform the configuration.
310+
- *User-defined annotations* add named node attributes that can be used in the
311+
application or role code.
312+
- *Computed annotations* allow access to annotations of other nodes throughout
313+
the schema.
310314

311315
.. _config_utils_schema_built_in_annotations:
312316

@@ -318,7 +322,11 @@ TODO: check the ``Built-in annotation`` term.
318322
Built-in annotations are interpreted by the module itself. There are the following
319323
built-in annotations:
320324

321-
-
325+
- ``type`` -- the node value type. Mandatory for scalar nodes, except those created with ``schema.enum``.
326+
- ``allowed_values`` -- a list of possible node values.
327+
- ``validate`` -- a validation function for the provided node value.
328+
- ``default`` -- a value to use if the option is not specified in the configuration.
329+
- ``apply_default_if`` -- a function that defines when to apply the default value.
322330

323331
Consider the following role configuration:
324332

@@ -371,27 +379,32 @@ See the full sample here: :ref:`config_utils_schema_env-vars`.
371379
Computed annotations
372380
^^^^^^^^^^^^^^^^^^^^
373381

374-
*Computed annotations* enable access to schema data
382+
*Computed annotations* enable access from a node to annotations of its ancestor nodes.
375383

376-
In the example below, the validate function of the listen_address record
377-
uses computed annotation to access the schema data from outside the record:
384+
In the example below, the ``listen_address`` record validation function refers to the
385+
``protocol`` annotation of its ancestor node:
378386

379387
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/config_schema_computed_annotations/api.lua
380388
:language: lua
381389
:start-at: local listen_address
382390
:end-before: local http_listen_address_schema
383391
:dedent:
384392

385-
The following schema with listen_address passes the validation:
386-
Passes validation:
393+
.. note::
394+
395+
If there are several ancestor nodes with this annotation, its value is taken
396+
from the closest one to the current node.
397+
398+
The following schema with ``listen_address`` passes the validation:
387399

388400
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/config_schema_computed_annotations/api.lua
389401
:language: lua
390402
:start-at: local http_listen_address_schema
391403
:end-before: local iproto_listen_address_schema
392404
:dedent:
393405

394-
If this record is added to a schema with ``protocol = 'iproto'``, an error is raised:
406+
If this record is added to a schema with ``protocol = 'iproto'``, the ``listen_address``
407+
validation fails with an error:
395408

396409
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/config_schema_computed_annotations/api.lua
397410
:language: lua
@@ -406,8 +419,7 @@ If this record is added to a schema with ``protocol = 'iproto'``, an error is ra
406419
User-defined methods
407420
~~~~~~~~~~~~~~~~~~~~
408421

409-
In addition to nodes, a schema can include *methods*. Methods are user-defined
410-
functions that can be called on this schema.
422+
A schema can implement custom logic with *methods* -- user-defined functions that can be called on this schema.
411423

412424
For example, this schema has a method that returns its fields merged in a URI string:
413425

@@ -419,8 +431,6 @@ For example, this schema has a method that returns its fields merged in a URI st
419431

420432

421433

422-
423-
424434
.. _config_utils_schema_process_data:
425435

426436
Processing configuration data

0 commit comments

Comments
 (0)