#include <userver/yaml_config/yaml_config.hpp>
Datatype that represents YAML with substituted variables.
If YAML has value that starts with an $
, then such value is treated as a variable from config_vars
. For example if config_vars
contains variable: 42
and the YAML is following:
Then the result of yaml["some_element"]["some"].As<int>()
is 42
.
If YAML key ends on '#env' and the mode is YamlConfig::Mode::kEnvAllowed, then the value of the key is searched in environment variables of the process and returned as a value. For example:
If YAML key ends on '#fallback', then the value of the key is used as a fallback for environment and $
variables. For example for the following YAML with YamlConfig::Mode::kEnvAllowed:
The result of yaml["some_element"]["some"].As<int>()
is the value of variable
from config_vars
if it exists; otherwise the value is the contents of the environment variable SOME_ENV_VARIABLE
if it exists; otherwise the value if 100500
, from the fallback.
Another example:
With YamlConfig::Mode::kEnvAllowed the result of yaml["some_element"]["value"].As<int>()
is the value of ENV_NAME
environment variable if it exists; otherwise it is 5
.
#env
and read any of your environment variables, including variables that contain passwords and other sensitive data. Definition at line 61 of file yaml_config.hpp.
Classes | |
struct | DefaultConstructed |
struct | IterTraits |
Public Types | |
enum class | Mode { kSecure , kEnvAllowed } |
using | const_iterator = Iterator<IterTraits> |
using | Exception = yaml_config::Exception |
using | ParseException = yaml_config::ParseException |
Public Member Functions | |
YamlConfig (formats::yaml::Value yaml, formats::yaml::Value config_vars, Mode mode=Mode::kSecure) | |
YamlConfig = config + config_vars. | |
const formats::yaml::Value & | Yaml () const |
Get the plain Yaml without substitutions. It may contain raw references. | |
YamlConfig | operator[] (std::string_view key) const |
Access member by key for read. | |
YamlConfig | operator[] (size_t index) const |
Access member by index for read. | |
std::size_t | GetSize () const |
Returns array size or object members count. | |
bool | IsMissing () const noexcept |
Returns true if *this holds nothing. When IsMissing() returns true any attempt to get the actual value or iterate over *this will throw MemberMissingException. | |
bool | IsNull () const noexcept |
Returns true if *this holds 'null'. | |
bool | IsBool () const noexcept |
Returns true if *this is convertible to bool. | |
bool | IsInt () const noexcept |
Returns true if *this is convertible to int. | |
bool | IsInt64 () const noexcept |
Returns true if *this is convertible to int64_t. | |
bool | IsUInt64 () const noexcept |
Returns true if *this is convertible to uint64_t. | |
bool | IsDouble () const noexcept |
Returns true if *this is convertible to double. | |
bool | IsString () const noexcept |
Returns true if *this is convertible to std::string. | |
bool | IsArray () const noexcept |
Returns true if *this is an array (Type::kArray). | |
bool | IsObject () const noexcept |
Returns true if *this is a map (Type::kObject). | |
void | CheckNotMissing () const |
void | CheckArray () const |
void | CheckArrayOrNull () const |
void | CheckObjectOrNull () const |
void | CheckObject () const |
void | CheckString () const |
void | CheckObjectOrArrayOrNull () const |
template<typename T > | |
auto | As () const |
Returns value of *this converted to T. | |
template<typename T , typename First , typename... Rest> | |
auto | As (First &&default_arg, Rest &&... more_default_args) const |
Returns value of *this converted to T or T(args) if this->IsMissing(). | |
template<typename T > | |
auto | As (DefaultConstructed) const |
Returns value of *this converted to T or T() if this->IsMissing(). | |
bool | HasMember (std::string_view key) const |
Returns true if *this holds a key . | |
std::string | GetPath () const |
Returns full path to this value. | |
const_iterator | begin () const |
Returns an iterator to the beginning of the held array or map. | |
const_iterator | end () const |
Returns an iterator to the end of the held array or map. | |
Definition at line 75 of file yaml_config.hpp.
Definition at line 76 of file yaml_config.hpp.
Definition at line 77 of file yaml_config.hpp.
|
strong |
Enumerator | |
---|---|
kEnvAllowed | < secure mode, without reading environment variables |
Definition at line 70 of file yaml_config.hpp.
auto yaml_config::YamlConfig::As | ( | ) | const |
Returns value of *this converted to T.
Anything | derived from std::exception. |
Definition at line 203 of file yaml_config.hpp.
auto yaml_config::YamlConfig::As | ( | YamlConfig::DefaultConstructed | ) | const |
Returns value of *this converted to T or T() if this->IsMissing().
Anything | derived from std::exception. |
value.As<T>({})
Definition at line 237 of file yaml_config.hpp.
auto yaml_config::YamlConfig::As | ( | First && | default_arg, |
Rest &&... | more_default_args ) const |
Returns value of *this converted to T or T(args) if this->IsMissing().
Anything | derived from std::exception. |
Definition at line 226 of file yaml_config.hpp.
const_iterator yaml_config::YamlConfig::begin | ( | ) | const |
Returns an iterator to the beginning of the held array or map.
TypeMismatchException | is the value of *this is not a map, array or Null. |
void yaml_config::YamlConfig::CheckArray | ( | ) | const |
MemberMissingException | if *this is not an array. |
void yaml_config::YamlConfig::CheckArrayOrNull | ( | ) | const |
MemberMissingException | if *this is not an array or Null. |
void yaml_config::YamlConfig::CheckNotMissing | ( | ) | const |
MemberMissingException | if this->IsMissing() . |
void yaml_config::YamlConfig::CheckObject | ( | ) | const |
TypeMismatchException | if *this is not a map. |
void yaml_config::YamlConfig::CheckObjectOrArrayOrNull | ( | ) | const |
TypeMismatchException | if *this is not a map, array or Null. |
void yaml_config::YamlConfig::CheckObjectOrNull | ( | ) | const |
TypeMismatchException | if *this is not a map or Null. |
void yaml_config::YamlConfig::CheckString | ( | ) | const |
TypeMismatchException | if *this is not convertible to std::string. |
const_iterator yaml_config::YamlConfig::end | ( | ) | const |
Returns an iterator to the end of the held array or map.
TypeMismatchException | is the value of *this is not a map, array or Null. |
std::size_t yaml_config::YamlConfig::GetSize | ( | ) | const |
Returns array size or object members count.
TypeMismatchException | if not array or object value. |
bool yaml_config::YamlConfig::HasMember | ( | std::string_view | key | ) | const |
Returns true if *this holds a key
.
Nothing. |
YamlConfig yaml_config::YamlConfig::operator[] | ( | size_t | index | ) | const |
Access member by index for read.
TypeMismatchException | if value is not missing and is not array. |
YamlConfig yaml_config::YamlConfig::operator[] | ( | std::string_view | key | ) | const |
Access member by key for read.
TypeMismatchException | if value is not missing and is not object. |