Skip to content

config/schema: :get() can't access a field inside 'any' type #10205

@Totktonada

Description

@Totktonada

For example, we have some logger module/service and its configuration contains a user-defined context without a strict structure. We want to acquire a particular value from it (if exists):

local schema = require('experimental.config.utils.schema')

local logger_config_schema = schema.new('logger_config', schema.record({
    level = schema.enum({
        'fatal',
        'syserror',
        'error',
        'crit',
        'warn',
        'info',
        'verbose',
        'debug',
    }, {
        default = 'info',
    }),
    -- Initial context (MDC).
    context = schema.map({
        key = schema.scalar({type = 'string'}),
        value = schema.scalar({type = 'any'}),   -- !!
    }),
}))

logger_config = logger_config_schema:apply_default({})
logger_config.context = {
    hostname = box.info.hostname,
    process = {
        pid = box.info.pid,
    },
}

-- It is OK.
local hostname = logger_config_schema:get(logger_config, 'context.hostname')
print('hostname', hostname)

-- It raises an error.
local pid = logger_config_schema:get(logger_config, 'context.process.pid')
-- error: [logger_config] context.process: Attempt to index a
--        scalar value of type any by field "pid"
print('pid', pid)

It seems convenient to have an ability to access a field that is inside a schemaless part of the configuration (inside the any type).

Also, this ability would be helpful for config:get(), when values nested into app.cfg or roles_cfg are accessed.

See also #10204.

Metadata

Metadata

Assignees

Labels

3.2Target is 3.2 and all newer release/master branchesconfigfeatureA new functionality

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions