Skip to content

Commit

Permalink
refactor yaml_config: deprecate YamlConfig::Yaml method
Browse files Browse the repository at this point in the history
commit_hash:185cb59e2b6585902380179fcb74056bf26fa57a
  • Loading branch information
Anton3 committed Dec 25, 2024
1 parent 80bc157 commit b294ce6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion core/src/cache/cache_update_trait_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ update-interval: 1s
}

yaml_config::YamlConfig UpdateConfig(yaml_config::YamlConfig& config, formats::yaml::Value&& other) {
formats::yaml::ValueBuilder builder(config.Yaml());
formats::yaml::ValueBuilder builder(config.GetRawYamlWithoutConfigVars());
formats::yaml::ValueBuilder yaml{other};
for (const auto& [name, value] : Items(yaml)) {
builder[name] = value;
Expand Down
8 changes: 5 additions & 3 deletions universal/include/userver/yaml_config/yaml_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ class YamlConfig {
/// YamlConfig = config + config_vars
YamlConfig(formats::yaml::Value yaml, formats::yaml::Value config_vars, Mode mode = Mode::kSecure);

/// Get the plain Yaml without substitutions. It may contain raw references.
const formats::yaml::Value& Yaml() const;

/// @brief Access member by key for read.
/// @throw TypeMismatchException if value is not missing and is not object.
YamlConfig operator[](std::string_view key) const;
Expand Down Expand Up @@ -196,6 +193,11 @@ class YamlConfig {
/// or Null.
const_iterator end() const;

/// @brief Get the plain Yaml without substitutions. It may contain raw references.
/// @deprecated Either use the current `YamlConfig` as a formats value, or use `.As<formats::json::Value>()`
/// to get the correct treatment for `$vars`, `#fallback`, `#env` and `#file`.
formats::yaml::Value GetRawYamlWithoutConfigVars() const;

private:
formats::yaml::Value yaml_;
formats::yaml::Value config_vars_;
Expand Down
10 changes: 5 additions & 5 deletions universal/src/yaml_config/impl/validate_static_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace yaml_config::impl {

namespace {

bool IsTypeValid(FieldType type, const formats::yaml::Value& value) {
bool IsTypeValid(FieldType type, const yaml_config::YamlConfig& value) {
switch (type) {
case FieldType::kInteger:
return value.IsInt() || value.IsUInt64() || value.IsInt64();
Expand All @@ -37,11 +37,11 @@ bool IsTypeValid(FieldType type, const formats::yaml::Value& value) {
}

void CheckType(const YamlConfig& value, const Schema& schema) {
if (!IsTypeValid(schema.type, value.Yaml())) {
if (!IsTypeValid(schema.type, value)) {
throw std::runtime_error(fmt::format(
"Error while validating static config against schema. "
"Value '{}' of field '{}' must be {}",
formats::yaml::ToString(value.Yaml()),
formats::yaml::ToString(value.GetRawYamlWithoutConfigVars()),
value.GetPath(),
ToString(schema.type)
));
Expand Down Expand Up @@ -146,7 +146,7 @@ void CheckNumericBounds(const YamlConfig& value, const Schema& schema) {
ToString(schema.type),
value.GetPath(),
*schema.minimum,
formats::yaml::ToString(value.Yaml())
formats::yaml::ToString(value.GetRawYamlWithoutConfigVars())
));
}

Expand All @@ -157,7 +157,7 @@ void CheckNumericBounds(const YamlConfig& value, const Schema& schema) {
ToString(schema.type),
value.GetPath(),
*schema.maximum,
formats::yaml::ToString(value.Yaml())
formats::yaml::ToString(value.GetRawYamlWithoutConfigVars())
));
}
}
Expand Down
16 changes: 12 additions & 4 deletions universal/src/yaml_config/yaml_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ std::optional<YamlConfig> GetYamlConfig(
/*met_substitution*/ false
);
if (res) {
return YamlConfig{res->Yaml().CloneWithReplacedPath(value.GetPath()), {}, YamlConfig::Mode::kSecure};
return YamlConfig{
res->GetRawYamlWithoutConfigVars().CloneWithReplacedPath(value.GetPath()),
{},
YamlConfig::Mode::kSecure,
};
}
}

Expand All @@ -167,8 +171,6 @@ std::optional<YamlConfig> GetYamlConfig(
YamlConfig::YamlConfig(formats::yaml::Value yaml, formats::yaml::Value config_vars, Mode mode)
: yaml_(std::move(yaml)), config_vars_(std::move(config_vars)), mode_(mode) {}

const formats::yaml::Value& YamlConfig::Yaml() const { return yaml_; }

YamlConfig YamlConfig::operator[](std::string_view key) const {
if (utils::text::EndsWith(key, "#env") || utils::text::EndsWith(key, "#file") ||
utils::text::EndsWith(key, "#fallback")) {
Expand Down Expand Up @@ -204,7 +206,11 @@ YamlConfig YamlConfig::operator[](size_t index) const {
/*met_substitution*/ false
);
if (res) {
return YamlConfig{res->Yaml().CloneWithReplacedPath(value.GetPath()), {}, YamlConfig::Mode::kSecure};
return YamlConfig{
res->GetRawYamlWithoutConfigVars().CloneWithReplacedPath(value.GetPath()),
{},
YamlConfig::Mode::kSecure,
};
}

// Avoid parsing $substitution as a string
Expand Down Expand Up @@ -258,6 +264,8 @@ YamlConfig::const_iterator YamlConfig::begin() const { return const_iterator{*th

YamlConfig::const_iterator YamlConfig::end() const { return const_iterator{*this, yaml_.end()}; }

formats::yaml::Value YamlConfig::GetRawYamlWithoutConfigVars() const { return yaml_; }

bool Parse(const YamlConfig& value, formats::parse::To<bool>) { return value.yaml_.As<bool>(); }

int64_t Parse(const YamlConfig& value, formats::parse::To<int64_t>) { return value.yaml_.As<int64_t>(); }
Expand Down
8 changes: 4 additions & 4 deletions universal/src/yaml_config/yaml_config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ TYPED_TEST(YamlConfigAccessor, SquareBracketOperatorArray) {

yaml_config::YamlConfig root_array_data = conf["root"];
Accessor root_array(root_array_data);
EXPECT_TRUE(root_array_data.Yaml().IsArray());
EXPECT_TRUE(root_array_data.IsArray());

EXPECT_EQ(root_array[0]["member1"].template As<int>(), 42);
EXPECT_EQ(root_array[0]["duration"].template As<std::chrono::milliseconds>(), std::chrono::seconds{10});
Expand Down Expand Up @@ -525,7 +525,7 @@ TYPED_TEST(YamlConfigAccessor, SquareBracketOperatorObject) {

yaml_config::YamlConfig root_object_data = conf["root"];
Accessor root_object(root_object_data);
EXPECT_TRUE(root_object_data.Yaml().IsObject());
EXPECT_TRUE(root_object_data.IsObject());

EXPECT_EQ(root_object["e0"]["member1"].template As<int>(), 42);
EXPECT_EQ(root_object["e0"]["duration"].template As<std::chrono::milliseconds>(), std::chrono::seconds{10});
Expand Down Expand Up @@ -633,7 +633,7 @@ TEST(YamlConfig, SubconfigNotObject) {

yaml_config::YamlConfig conf(std::move(node), std::move(vmap));
EXPECT_EQ(conf["member"].As<std::string>(), "hello");
EXPECT_TRUE(conf.Yaml().IsObject());
EXPECT_TRUE(conf.IsObject());
// Get subconfig that is not an object
auto subconf = conf["member"];
// It must throw an exception
Expand All @@ -659,7 +659,7 @@ TEST(YamlConfig, IteratorArray) {
yaml_config::YamlConfig conf(std::move(node), std::move(vmap));

auto root_array = conf["root"];
EXPECT_TRUE(root_array.Yaml().IsArray());
EXPECT_TRUE(root_array.IsArray());

// Testing basics
auto it = root_array.begin();
Expand Down

0 comments on commit b294ce6

Please sign in to comment.