Skip to content

Commit df97372

Browse files
committed
Fix
1 parent 405e847 commit df97372

File tree

4 files changed

+51
-39
lines changed

4 files changed

+51
-39
lines changed

doc/code_snippets/snippets/config/instances.enabled/application_role_cfg/greeter.lua

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
-- greeter.lua --
22
local log = require('log').new("greeter")
3+
local schema = require('experimental.config.utils.schema')
4+
5+
local greeter_schema = schema.new('greeter', schema.record({
6+
greeting = schema.scalar({
7+
type = 'string',
8+
allowed_values = { 'Hi', 'Hello' }
9+
})
10+
}))
311

412
local function validate(cfg)
5-
if cfg.greeting then
6-
assert(type(cfg.greeting) == "string", "'greeting' should be a string")
7-
assert(cfg.greeting == "Hi" or cfg.greeting == "Hello", "'greeting' should be 'Hi' or 'Hello'")
8-
end
13+
greeter_schema:validate(cfg)
914
end
1015

1116
local function apply(cfg)
12-
log.info("%s from the 'greeter' role!", cfg.greeting)
17+
log.info("%s from the 'greeter' role!", greeter_schema:get(cfg, 'greeting'))
1318
end
1419

1520
local function stop()

doc/platform/app/app_roles.rst

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ To learn how to enable and configure roles, see :ref:`configuration_application_
2727
- A role of a replica set in regard to sharding. Learn more in :ref:`vshard_config_sharding_roles`.
2828

2929

30-
31-
3230
.. _roles_create_custom_role_config:
3331

3432
Providing a role configuration
@@ -44,10 +42,15 @@ This example shows how to enable and configure the ``greeter`` role, which is im
4442
:start-at: instance001
4543
:dedent:
4644

47-
The role's configuration provided in ``roles_cfg`` can be accessed when :ref:`validating <roles_create_custom_role_validate>` and :ref:`applying <roles_create_custom_role_apply>` this configuration.
45+
The role configuration provided in ``roles_cfg`` can be accessed when :ref:`validating <roles_create_custom_role_validate>` and :ref:`applying <roles_create_custom_role_apply>` this configuration.
4846

49-
Given that a role is a :ref:`Lua module <app_server-modules>`, a role's name is passed to ``require()`` to obtain the module.
50-
When :ref:`developing an application <admin-instance_config-develop-app>`, you can place a file with a role's code next to a cluster's configuration file.
47+
Tarantool includes the :ref:`experimental.config.utils.schema <config_utils_schema_module>`
48+
built-in module that provides an for managing user-defined configurations
49+
of applications (``app.cfg``) and roles (``roles_cfg``). The examples below show its
50+
basic usage.
51+
52+
Given that a role is a :ref:`Lua module <app_server-modules>`, a role name is passed to ``require()`` to obtain the module.
53+
When :ref:`developing an application <admin-instance_config-develop-app>`, you can place a file with the role code next to the cluster configuration file.
5154

5255

5356

@@ -63,12 +66,12 @@ Overview
6366

6467
Creating a custom role includes the following steps:
6568

66-
1. Define a function that validates a role's configuration.
69+
1. Define a function that validates a role configuration.
6770
2. Define a function that applies a validated configuration.
6871
3. Define a function that stops a role.
6972
4. (Optional) Define roles from which this custom role depends on.
7073

71-
As a result, a role's module should return an object that has corresponding functions and fields specified:
74+
As a result, a role module should return an object that has corresponding functions and fields specified:
7275

7376
.. code-block:: lua
7477
@@ -86,14 +89,12 @@ The examples below show how to do this.
8689
Code snippets shown in this section are included from the following application: `application_role_cfg <https://github.com/tarantool/doc/tree/latest/doc/code_snippets/snippets/config/instances.enabled/application_role_cfg>`_.
8790

8891

89-
90-
9192
.. _roles_create_custom_role_validate:
9293

9394
Validating a role configuration
9495
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9596

96-
To validate a role's configuration, you need to define the :ref:`validate([cfg]) <roles_api_reference_validate>` function.
97+
To validate a role configuration, you need to define the :ref:`validate([cfg]) <roles_api_reference_validate>` function.
9798
The ``cfg`` argument provides access to the :ref:`role's configuration <roles_create_custom_role_config>` and check its validity.
9899

99100
In the example below, the ``validate()`` function is used to validate the ``greeting`` configuration value:
@@ -107,8 +108,6 @@ In the example below, the ``validate()`` function is used to validate the ``gree
107108
If the configuration is not valid, ``validate()`` reports an unrecoverable error by throwing an error object.
108109

109110

110-
111-
112111
.. _roles_create_custom_role_apply:
113112

114113
Applying a role configuration
@@ -117,7 +116,7 @@ Applying a role configuration
117116
To apply the validated configuration, define the :ref:`apply([cfg]) <roles_api_reference_apply>` function.
118117
As the ``validate()`` function, ``apply()`` provides access to a role's configuration using the ``cfg`` argument.
119118

120-
In the example below, the ``apply()`` function uses the :ref:`log <log-module>` module to write a role's configuration value to the log:
119+
In the example below, the ``apply()`` function uses the :ref:`log <log-module>` module to write a value from the role configuration to the log:
121120

122121
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/application_role_cfg/greeter.lua
123122
:language: lua
@@ -142,7 +141,7 @@ In the example below, the ``stop()`` function uses the :ref:`log <log-module>` m
142141
:end-before: return
143142
:dedent:
144143

145-
When you've defined all the role's functions, you need to return an object that has corresponding functions specified:
144+
When you've defined all the role functions, you need to return an object that has corresponding functions specified:
146145

147146
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/application_role_cfg/greeter.lua
148147
:language: lua

doc/reference/configuration/configuration_reference.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ In the ``app`` section, you can load the application and provide an application
4646

4747
Example on GitHub: `application <https://github.com/tarantool/doc/tree/latest/doc/code_snippets/snippets/config/instances.enabled/application>`_
4848

49+
.. tip::
50+
51+
The :ref:`experimental.config.utils.schema <config_utils_schema_module>`
52+
built-in module provides an API for managing user-defined configurations
53+
of applications (``app.cfg``) and roles (``roles_cfg``).
54+
4955
|
5056
| Type: map
5157
| Default: nil

doc/reference/reference_lua/config/utils_schema.rst

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ the role configuration:
5252
:end-before: local function validate
5353
:dedent:
5454

55-
3. Use the :ref:`schema.validate() <config-schema_object-validate>` function to
55+
3. Use the :ref:`validate() <config-schema_object-validate>` function to
5656
validate configuration values against the schema. In case of a role, call this
5757
function inside the role's :ref:`validate() <roles_create_custom_role_validate>` function:
5858

@@ -62,7 +62,7 @@ the role configuration:
6262
:end-before: local function apply
6363
:dedent:
6464

65-
4. Obtain values of configuration options using :ref:`schema.get() <config-schema_object-get>`.
65+
4. Obtain values of configuration options using :ref:`get() <config-schema_object-get>`.
6666
In case of a role, call it inside the role's :ref:`apply() <roles_create_custom_role_apply>` function:
6767

6868
.. literalinclude:: /code_snippets/snippets/config/instances.enabled/config_schema_nodes_scalar/http_api.lua
@@ -804,7 +804,7 @@ schema_object
804804

805805
:return: configuration data with applied schema defaults
806806

807-
**See also:** :ref:`config-schema_node_annotation-default`, :ref:`config-schema_node_annotation-apply_default_if`
807+
**See also:** :ref:`default <config-schema_node_annotation-default>`, :ref:`apply_default_if <config-schema_node_annotation-apply_default_if>`
808808

809809
.. _config-schema_object-filter:
810810

@@ -905,7 +905,7 @@ schema_object
905905
:param any f: walkthrough node, described below
906906
:param any f_ctx: user-provided context for the transformation function
907907

908-
:return: transformed configuration data
908+
:return: transformed configuration data
909909

910910
.. _config-schema_object-merge:
911911

@@ -981,18 +981,19 @@ schema_object
981981
The method performs the following checks:
982982

983983
- field type checks: field values are checked against the schema node types
984-
- allowed values: if a node has the :ref:`config-schema_node_annotation-allowed_values`
985-
annotations of schema nodes, the corresponding data field is checked
986-
against the allowed values list
984+
- allowed values: if a node has the ``allowed_values`` annotations of schema nodes,
985+
the corresponding data field is checked against the allowed values list
987986
- validation functions: if a validation function is defined for a node
988-
(the :ref:`config-schema_node_annotation-validate` annotation), it is
989-
executed to check that the provided value is valid.
987+
(the ``validate`` annotation), it is executed to check that the provided value is valid.
990988

991989
:param any data: data
992990

993991
**Example:** see :ref:`config_utils_schema_annotation` and
994992
:ref:`config_utils_schema_validating_configuration`
995993

994+
**See also:** :ref:`allowed_values <config-schema_node_annotation-allowed_values>`,
995+
:ref:`validate <config-schema_node_annotation-validate>`
996+
996997
.. _config-schema_object-methods:
997998

998999
.. data:: methods
@@ -1030,15 +1031,16 @@ parsed by the modules as :ref:`built-in annotations <config_utils_schema_built_i
10301031

10311032
A list of allowed values for a node.
10321033

1033-
**See also** :ref:`config-schema_object-validate`
1034+
**See also:** :ref:`schema_object:validate() <config-schema_object-validate>`
1035+
10341036

10351037
.. _config-schema_node_annotation-apply_default_if:
10361038

10371039
- ``apply_default_if``
10381040

10391041
A boolean function that defines whether to apply the default value specified
10401042
using ``default``. If this function returns ``true`` on a provided configuration data,
1041-
the node receives the default value upon the :ref:`config-schema_object-apply_default`
1043+
the node receives the default value upon the :ref:`schema_object.apply_default() <config-schema_object-apply_default>`
10421044
method call.
10431045

10441046
The function takes two arguments:
@@ -1050,7 +1052,7 @@ parsed by the modules as :ref:`built-in annotations <config_utils_schema_built_i
10501052
- ``w.path`` -- the path to the schema node
10511053
- ``w.error()`` -- a function for printing human-readable error messages
10521054

1053-
**See also:** :ref:`config-schema_object-apply_default`.
1055+
**See also:** :ref:`schema_object:apply_default() <config-schema_object-apply_default>`
10541056

10551057
.. _config-schema_node_annotation-default:
10561058

@@ -1060,7 +1062,7 @@ parsed by the modules as :ref:`built-in annotations <config_utils_schema_built_i
10601062

10611063
**Example:** see :ref:`config_utils_schema_transform_configuration`
10621064

1063-
**See also:** :ref:`config-schema_object-apply_default`
1065+
**See also:** :ref:`schema_object:apply_default() <config-schema_object-apply_default>`
10641066

10651067
.. _config-schema_node_annotation-type:
10661068

@@ -1075,7 +1077,7 @@ parsed by the modules as :ref:`built-in annotations <config_utils_schema_built_i
10751077
- ``validate``
10761078

10771079
A boolean function used to validate node data. Node data is valid if this function
1078-
returns ``true``. The function is called upon the :ref:`config-schema_object-validate`
1080+
returns ``true``. The function is called upon the :ref:`schema_object:validate() <config-schema_object-validate>`
10791081
function calls.
10801082

10811083
The function takes two arguments:
@@ -1087,8 +1089,6 @@ parsed by the modules as :ref:`built-in annotations <config_utils_schema_built_i
10871089
- ``w.path`` -- the path to the schema node
10881090
- ``w.error()`` -- a function for printing human-readable error messages
10891091

1090-
See also :ref:`config-schema_object-validate`
1091-
10921092
**Example:**
10931093

10941094
A function that checks that a string a valid IP address:
@@ -1099,6 +1099,8 @@ parsed by the modules as :ref:`built-in annotations <config_utils_schema_built_i
10991099
:end-before: local function validate_port
11001100
:dedent:
11011101

1102+
**See also:** :ref:`schema_object:validate() <config-schema_object-validate>`
1103+
11021104
.. _config-utils-schema_node_object:
11031105

11041106
schema_node_object
@@ -1111,14 +1113,14 @@ schema_node_object
11111113
.. data:: allowed_values
11121114

11131115
A list of values allowed for the node. The values are taken from the
1114-
:ref:`config-schema_node_annotation-allowed_values` node annotation.
1116+
:ref:`allowed_values <config-schema_node_annotation-allowed_values>` node annotation.
11151117

11161118
.. _config-schema_node_object-apply_default_if:
11171119

11181120
.. data:: apply_default_if
11191121

11201122
A function to define when to apply the default node value. The value
1121-
is taken from the :ref:`config-schema_node_annotation-apply_default_if` annotation.
1123+
is taken from the :ref:`apply_default_if <config-schema_node_annotation-apply_default_if>` annotation.
11221124

11231125
.. _config-schema_node_object-computed:
11241126

@@ -1131,7 +1133,7 @@ schema_node_object
11311133
.. data:: default
11321134

11331135
Node's default value. The value
1134-
is taken from the :ref:`config-schema_node_annotation-default` annotation.
1136+
is taken from the :ref:`default <config-schema_node_annotation-default>` annotation.
11351137

11361138
.. _config-schema_node_object-fields:
11371139

@@ -1158,4 +1160,4 @@ schema_node_object
11581160
.. data:: validate
11591161

11601162
Node value validation function. The value
1161-
is taken from the :ref:`config-schema_node_annotation-validate` annotation.
1163+
is taken from the :ref:`validate <config-schema_node_annotation-validate>` annotation.

0 commit comments

Comments
 (0)