Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kuznetsss committed Feb 12, 2025
1 parent baf36bc commit a3ef5d2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
31 changes: 28 additions & 3 deletions src/util/newconfig/ConfigDefinition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>
Expand Down Expand Up @@ -157,16 +158,18 @@ std::optional<std::vector<Error>>
ClioConfigDefinition::parse(ConfigFileInterface const& config)
{
std::vector<Error> listOfErrors;
std::unordered_map<std::string_view, std::vector<std::string_view>> arrayPrefixesToKeysMap;
for (auto& [key, value] : map_) {
// if key doesn't exist in user config, makes sure it is marked as ".optional()" or has ".defaultValue()"" in
// ClioConfigDefitinion above
if (key.contains(".[]")) {
auto const prefix = Array::prefix(key);
arrayPrefixesToKeysMap[prefix].push_back(key);
}
if (!config.containsKey(key)) {
if (std::holds_alternative<ConfigValue>(value)) {
if (!(std::get<ConfigValue>(value).isOptional() || std::get<ConfigValue>(value).hasValue()))
listOfErrors.emplace_back(key, "key is required in user Config");
} else if (std::holds_alternative<Array>(value)) {
if (!(std::get<Array>(value).getArrayPattern().isOptional()))
listOfErrors.emplace_back(key, "key is required in user Config");
}
continue;
}
Expand Down Expand Up @@ -194,6 +197,28 @@ ClioConfigDefinition::parse(ConfigFileInterface const& config)
value
);
}

for (auto const& item : arrayPrefixesToKeysMap) {
size_t maxSize = 0;
std::ranges::for_each(item.second, [&](std::string_view key) {
ASSERT(std::holds_alternative<Array>(map_.at(key)), "{} is not array", key);
maxSize = std::max(maxSize, arraySize(key));
});

std::ranges::for_each(item.second, [&](std::string_view key) {
auto& array = std::get<Array>(map_.at(key));
if (array.size() != maxSize) {
if (array.getArrayPattern().isOptional()) {
while (array.size() < maxSize) {
array.addValue(NullType{});
}
} else {
listOfErrors.emplace_back(key, "is required for all objects in the array");
}
}
});
}

if (!listOfErrors.empty())
return listOfErrors;

Expand Down
4 changes: 2 additions & 2 deletions src/util/newconfig/ConfigDefinition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,10 @@ static ClioConfigDefinition gClioConfig = ClioConfigDefinition{
{"cache.page_fetch_size", ConfigValue{ConfigType::Integer}.defaultValue(512).withConstraint(gValidateUint16)},
{"cache.load", ConfigValue{ConfigType::String}.defaultValue("async").withConstraint(gValidateLoadMode)},

{"log_channels.[].channel", Array{ConfigValue{ConfigType::String}.optional().withConstraint(gValidateChannelName)}
{"log_channels.[].channel", Array{ConfigValue{ConfigType::String}.withConstraint(gValidateChannelName)}
},
{"log_channels.[].log_level",
Array{ConfigValue{ConfigType::String}.optional().withConstraint(gValidateLogLevelName)}},
Array{ConfigValue{ConfigType::String}.withConstraint(gValidateLogLevelName)}},

{"log_level", ConfigValue{ConfigType::String}.defaultValue("info").withConstraint(gValidateLogLevelName)},

Expand Down
11 changes: 7 additions & 4 deletions tests/unit/LoggerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <limits>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -82,8 +84,8 @@ using util::config::ConfigValue;
struct LoggerInitTest : LoggerTest {
protected:
util::config::ClioConfigDefinition config_{
{"log_channels.[].channel", Array{ConfigValue{ConfigType::String}.optional()}},
{"log_channels.[].log_level", Array{ConfigValue{ConfigType::String}.optional()}},
{"log_channels.[].channel", Array{ConfigValue{ConfigType::String}}},
{"log_channels.[].log_level", Array{ConfigValue{ConfigType::String}}},

{"log_level", ConfigValue{ConfigType::String}.defaultValue("info")},

Expand Down Expand Up @@ -193,7 +195,8 @@ TEST_F(LoggerInitTest, InitReturnsErrorIfProvidedInvalidChannel)
{
"log_channels": [
{
"channel": "SomeChannel"
"channel": "SomeChannel",
"log_level": "info"
}
]
})json";
Expand All @@ -203,7 +206,7 @@ TEST_F(LoggerInitTest, InitReturnsErrorIfProvidedInvalidChannel)
ASSERT_FALSE(parsingErrors.has_value());

auto const result = LogService::init(config_);
EXPECT_FALSE(result);
ASSERT_FALSE(result);
EXPECT_EQ(result.error(), "Can't override settings for log channel SomeChannel: invalid channel");
}

Expand Down

0 comments on commit a3ef5d2

Please sign in to comment.