userver: dynamic_config::Key< Variable > Class Template Reference
Loading...
Searching...
No Matches
dynamic_config::Key< Variable > Class Template Reference

#include <userver/dynamic_config/snapshot.hpp>

Detailed Description

template<typename Variable>
class dynamic_config::Key< Variable >

A config key is a unique identifier for a config variable.

See also
dynamic_config_key for declarations with schema metadata.

Definition at line 111 of file snapshot.hpp.

Public Types

using VariableType = Variable
 The type of the parsed config variable.
using JsonParser = Variable (*)(const formats::json::Value&)
using DocsMapParser = Variable (*)(const DocsMap&)

Public Member Functions

 Key (std::string_view name, const VariableType &default_value)
 The constructor for a trivial VariableType, e.g. bool, integer, double, string. The default is passed by value.
template<typename Tag>
requires std::is_same_v<Tag, NoSchemaHash>
 Key (std::string_view name, const VariableType &default_value, Tag)
 Creates a key with a typed default explicitly omitting schema hash metadata. See dynamic_config::NoSchemaHash for source requirements.
 Key (std::string_view name, const VariableType &default_value, SchemaHash schema_hash)
 The constructor for a trivial VariableType with schema metadata.
 Key (std::string_view name, DefaultAsJsonString default_json)
 The constructor for a non-trivial VariableType. The default is passed as a JSON string.
template<typename Tag>
requires std::is_same_v<Tag, NoSchemaHash>
 Key (std::string_view name, DefaultAsJsonString default_json, Tag)
 Creates a key with a JSON default explicitly omitting schema hash metadata. Uses the formats::json::Value Parse customization point.
 Key (std::string_view name, DefaultAsJsonString default_json, SchemaHash schema_hash)
 The constructor for a non-trivial VariableType with schema metadata.
 Key (std::string_view name, JsonParser parser, DefaultAsJsonString default_json)
 The constructor that provides a special parser from JSON.
template<typename Tag>
requires std::is_same_v<Tag, NoSchemaHash>
 Key (std::string_view name, JsonParser parser, DefaultAsJsonString default_json, Tag)
 Creates a key with a custom JSON parser explicitly omitting schema hash metadata. See dynamic_config::NoSchemaHash for source requirements.
 Key (std::string_view name, JsonParser parser, DefaultAsJsonString default_json, SchemaHash schema_hash)
 The constructor with a custom JSON parser and schema metadata.
template<std::size_t N>
 Key (DocsMapParser parser, const ConfigDefault(&default_json_map)[N])
 The constructor that parses multiple JSON config items into a single C++ object.
 Key (ConstantConfig, VariableType value)
 Creates a config that always has the same value.
 Key (const Key &) noexcept=delete
Key & operator= (const Key &) noexcept=delete
std::string_view GetName () const noexcept
VariableType Parse (const DocsMap &docs_map) const
template<typename Variable>
 Key (impl::InternalTag, std::string_view name)
template<typename Variable>
 Key (impl::InternalTag, DocsMapParser parser)

Member Typedef Documentation

◆ DocsMapParser

template<typename Variable>
using dynamic_config::Key< Variable >::DocsMapParser = Variable (*)(const DocsMap&)

Definition at line 117 of file snapshot.hpp.

◆ JsonParser

template<typename Variable>
using dynamic_config::Key< Variable >::JsonParser = Variable (*)(const formats::json::Value&)

Definition at line 116 of file snapshot.hpp.

◆ VariableType

template<typename Variable>
using dynamic_config::Key< Variable >::VariableType = Variable

The type of the parsed config variable.

Definition at line 114 of file snapshot.hpp.

Constructor & Destructor Documentation

◆ Key() [1/13]

template<typename Variable>
dynamic_config::Key< Variable >::Key ( std::string_view name,
const VariableType & default_value )

The constructor for a trivial VariableType, e.g. bool, integer, double, string. The default is passed by value.

Deprecated
Pass a generated dynamic_config::SchemaHash, or explicitly opt out using dynamic_config::NoSchemaHash.

Definition at line 328 of file snapshot.hpp.

◆ Key() [2/13]

template<typename Variable>
requires std::is_same_v<Tag, NoSchemaHash>
template<typename Tag>
requires std::is_same_v<Tag, NoSchemaHash>
dynamic_config::Key< Variable >::Key ( std::string_view name,
const VariableType & default_value,
Tag  )

Creates a key with a typed default explicitly omitting schema hash metadata. See dynamic_config::NoSchemaHash for source requirements.

const dynamic_config::Key<bool> kDocStructConfig{"SAMPLE_BOOL_CONFIG", false, dynamic_config::NoSchemaHash{}};

Definition at line 336 of file snapshot.hpp.

◆ Key() [3/13]

template<typename Variable>
dynamic_config::Key< Variable >::Key ( std::string_view name,
const VariableType & default_value,
SchemaHash schema_hash )

The constructor for a trivial VariableType with schema metadata.

Parameters
schema_hashOpaque schema hash.
Warning
The schema hash must be supplied by code generation. userver treats it as an opaque value and does not calculate it.

Definition at line 341 of file snapshot.hpp.

◆ Key() [4/13]

template<typename Variable>
dynamic_config::Key< Variable >::Key ( std::string_view name,
DefaultAsJsonString default_json )

The constructor for a non-trivial VariableType. The default is passed as a JSON string.

Uses formats::json::Value Parse customization point function to parse VariableType.

Deprecated
Pass a generated dynamic_config::SchemaHash, or explicitly opt out using dynamic_config::NoSchemaHash.

Definition at line 354 of file snapshot.hpp.

◆ Key() [5/13]

template<typename Variable>
requires std::is_same_v<Tag, NoSchemaHash>
template<typename Tag>
requires std::is_same_v<Tag, NoSchemaHash>
dynamic_config::Key< Variable >::Key ( std::string_view name,
DefaultAsJsonString default_json,
Tag  )

Creates a key with a JSON default explicitly omitting schema hash metadata. Uses the formats::json::Value Parse customization point.

// Parser for a type must be defined in the same namespace as the type itself.
// In this example, JSON parser is only needed for dynamic config, so declaring
// it in hpp is not strictly necessary.
return SampleStructConfig{
.is_foo_enabled = value["is_foo_enabled"].As<bool>(),
.bar_period = value["bar_period_ms"].As<std::chrono::milliseconds>(),
};
}
const dynamic_config::Key<SampleStructConfig> kSampleStructConfig{
"SAMPLE_STRUCT_CONFIG",
{
"is_foo_enabled": false,
"bar_period_ms": 42000
}
)"},
};

Definition at line 362 of file snapshot.hpp.

◆ Key() [6/13]

template<typename Variable>
dynamic_config::Key< Variable >::Key ( std::string_view name,
DefaultAsJsonString default_json,
SchemaHash schema_hash )

The constructor for a non-trivial VariableType with schema metadata.

Parameters
schema_hashOpaque schema hash.
Warning
The schema hash must be supplied by code generation. userver treats it as an opaque value and does not calculate it.
"USERVER_CACHES",
::dynamic_config::userver_caches::GetSchemaHash(),
};

Definition at line 367 of file snapshot.hpp.

◆ Key() [7/13]

template<typename Variable>
dynamic_config::Key< Variable >::Key ( std::string_view name,
JsonParser parser,
DefaultAsJsonString default_json )

The constructor that provides a special parser from JSON.

Warning
Prefer the constructors above whenever possible.

Can be used when generic Parse is not applicable. Sometimes used to add validation, e.g. minimum, maximum, string pattern, etc.

Deprecated
Pass a generated dynamic_config::SchemaHash, or explicitly opt out using dynamic_config::NoSchemaHash.

Definition at line 380 of file snapshot.hpp.

◆ Key() [8/13]

template<typename Variable>
requires std::is_same_v<Tag, NoSchemaHash>
template<typename Tag>
requires std::is_same_v<Tag, NoSchemaHash>
dynamic_config::Key< Variable >::Key ( std::string_view name,
JsonParser parser,
DefaultAsJsonString default_json,
Tag  )

Creates a key with a custom JSON parser explicitly omitting schema hash metadata. See dynamic_config::NoSchemaHash for source requirements.

Definition at line 388 of file snapshot.hpp.

◆ Key() [9/13]

template<typename Variable>
dynamic_config::Key< Variable >::Key ( std::string_view name,
JsonParser parser,
DefaultAsJsonString default_json,
SchemaHash schema_hash )

The constructor with a custom JSON parser and schema metadata.

Parameters
nameconfig variable name
parsercustom JSON parser for the variable
default_jsondefault value as a JSON string
schema_hashOpaque schema hash. Stored in the global registry and retrievable via dynamic_config::impl::GetRegisteredConfigsMeta() as dynamic_config::RegisteredConfigMeta::schema_hash.
Warning
The schema hash must be supplied by code generation. userver treats it as an opaque value and does not calculate it.

Definition at line 393 of file snapshot.hpp.

◆ Key() [10/13]

template<typename Variable>
template<std::size_t N>
dynamic_config::Key< Variable >::Key ( DocsMapParser parser,
const ConfigDefault(&) default_json_map[N] )

The constructor that parses multiple JSON config items into a single C++ object.

To register schema metadata for the JSON config items, pass the generated variable_namespace::GetSchemaHash() to each ConfigDefault.

Warning
Prefer to use a separate Key per JSON config item and use the constructors above whenever possible.

Definition at line 406 of file snapshot.hpp.

◆ Key() [11/13]

template<typename Variable>
dynamic_config::Key< Variable >::Key ( ConstantConfig ,
VariableType value )

Creates a config that always has the same value.

Definition at line 425 of file snapshot.hpp.

◆ Key() [12/13]

template<typename Variable>
template<typename Variable>
dynamic_config::Key< Variable >::Key ( impl::InternalTag ,
std::string_view name )

Definition at line 430 of file snapshot.hpp.

◆ Key() [13/13]

template<typename Variable>
template<typename Variable>
dynamic_config::Key< Variable >::Key ( impl::InternalTag ,
DocsMapParser parser )

Definition at line 440 of file snapshot.hpp.

Member Function Documentation

◆ GetName()

template<typename VariableType>
std::string_view dynamic_config::Key< VariableType >::GetName ( ) const
noexcept
Returns
the name of the single registered config item, or the explicit name of an internal derived key. Calling this for a key with zero or multiple config items and no explicit internal name is an invariant violation.

Definition at line 448 of file snapshot.hpp.

◆ Parse()

template<typename VariableType>
VariableType dynamic_config::Key< VariableType >::Parse ( const DocsMap & docs_map) const

Parses the config. Useful only in some very niche scenarios. The config value should be typically be retrieved from dynamic_config::Snapshot, which is obtained from components::DynamicConfig in production or from dynamic_config::StorageMock in unit tests.

Definition at line 453 of file snapshot.hpp.


The documentation for this class was generated from the following files: