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.

"USERVER_NO_LOG_SPANS", dynamic_config::DefaultAsJsonString{R"(
{
"names": [],
"prefixes": []
}
)"}};
Examples
components/component_sample_test.cpp.

Definition at line 45 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.
 
 Key (std::string_view name, DefaultAsJsonString default_json)
 The constructor for a non-trivial VariableType. The default is passed as a JSON string.
 
 Key (std::string_view name, JsonParser parser, DefaultAsJsonString default_json)
 The constructor that provides a special parser from JSON.
 
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
 
Keyoperator= (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 51 of file snapshot.hpp.

◆ JsonParser

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

Definition at line 50 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 48 of file snapshot.hpp.

Constructor & Destructor Documentation

◆ Key() [1/7]

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.

Usage example:

Definition at line 183 of file snapshot.hpp.

◆ Key() [2/7]

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.

Usage example:

"USERVER_NO_LOG_SPANS", dynamic_config::DefaultAsJsonString{R"(
{
"names": [],
"prefixes": []
}
)"}};

Definition at line 192 of file snapshot.hpp.

◆ Key() [3/7]

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.

Definition at line 201 of file snapshot.hpp.

◆ Key() [4/7]

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.

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

Usage example:

constexpr dynamic_config::DefaultAsJsonString kThrottleDefaults{R"(
{
"http-limit": 6000,
"http-per-second": 1500,
"https-limit": 100,
"https-per-second": 25,
"max-size": 100,
"per-host-limit": 3000,
"per-host-per-second": 500,
"token-update-interval-ms": 0
}
)"};
const dynamic_config::Key kClientConfig{
clients::http::impl::ParseConfig,
{
{"HTTP_CLIENT_CONNECTION_POOL_SIZE", 1000},
{"USERVER_HTTP_PROXY", ""},
{"HTTP_CLIENT_CONNECT_THROTTLE", kThrottleDefaults},
},
};

Definition at line 212 of file snapshot.hpp.

◆ Key() [5/7]

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

Creates a config that always has the same value.

Definition at line 222 of file snapshot.hpp.

◆ Key() [6/7]

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

Definition at line 231 of file snapshot.hpp.

◆ Key() [7/7]

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

Definition at line 240 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 config variable, as passed at the construction.

Definition at line 249 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 254 of file snapshot.hpp.


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