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:
some_element:
some: $variable
)");
EXPECT_EQ(yaml["some_element"]["some"].As<int>(), 42);
EXPECT_EQ(yaml["some_element"]["some"].GetPath(), "some_element.some");
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 or YamlConfig::Mode::kEnvAndFileAllowed, then the value of the key is searched in environment variables of the process and returned as a value. For example:
some_element:
some#env: ENV_VARIABLE_NAME
)");
::setenv("ENV_VARIABLE_NAME", "100", 1);
EXPECT_EQ(yaml["some_element"]["some"].As<int>(), 100);
If YAML key ends on '#file' and the mode is YamlConfig::Mode::kEnvAndFileAllowed, then the value of the key is the content of specified YAML parsed file. For example:
const auto yaml_content = fmt::format("some#file: {}", path_to_file);
EXPECT_EQ(yaml["some"]["some_key"][0].As<std::string>(), "a");
If YAML key ends on '#fallback', then the value of the key is used as a fallback for environment, file and $
variables. For example for the following YAML with YamlConfig::Mode::kEnvAndFileAllowed:
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 is the content of the file with name file.yaml
; otherwise the value is 100500
, from the fallback.
Another example:
# yaml
some_element:
some#env: ENV_NAME
some#fallback: 5
With YamlConfig::Mode::kEnvAllowed the result of yaml["some_element"]["some"].As<int>()
is the value of ENV_NAME
environment variable if it exists; otherwise it is 5
.
- Note
#env
, #file
and #fallback
also work for keys inside config_vars
.
- Warning
- YamlConfig::Mode::kEnvAllowed or YamlConfig::Mode::kEnvAndFileAllowed should be used only on configs that come from trusted environments. Otherwise, an attacker could create a config and read any of your environment variables of files, including variables that contain passwords and other sensitive data.
Definition at line 72 of file yaml_config.hpp.
|
| 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.
|
|