Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/iceberg/util/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
#include <string>
#include <unordered_map>

namespace iceberg {
#include <iceberg/exception.h>

namespace iceberg {
namespace internal {
// Default conversion functions
template <typename U>
std::string defaultToString(const U& val) {
std::string DefaultToString(const U& val) {
if constexpr ((std::is_signed_v<U> && std::is_integral_v<U>) ||
std::is_floating_point_v<U>) {
return std::to_string(val);
Expand All @@ -37,13 +39,13 @@ std::string defaultToString(const U& val) {
std::is_same_v<U, std::string_view>) {
return val;
} else {
throw std::runtime_error(
std::format("Explicit toStr() is required for {}", typeid(U).name()));
throw IcebergError(
std::format("Explicit to_str() is required for {}", typeid(U).name()));
}
}

template <typename U>
U defaultFromString(const std::string& val) {
U DefaultFromString(const std::string& val) {
if constexpr (std::is_same_v<U, std::string>) {
return val;
} else if constexpr (std::is_same_v<U, bool>) {
Expand All @@ -53,10 +55,11 @@ U defaultFromString(const std::string& val) {
} else if constexpr (std::is_floating_point_v<U>) {
return static_cast<U>(std::stod(val));
} else {
throw std::runtime_error(
std::format("Explicit toT() is required for {}", typeid(U).name()));
throw IcebergError(
std::format("Explicit from_str() is required for {}", typeid(U).name()));
}
}
} // namespace internal

template <class ConcreteConfig>
class ConfigBase {
Expand All @@ -65,8 +68,8 @@ class ConfigBase {
class Entry {
public:
Entry(std::string key, const T& val,
std::function<std::string(const T&)> to_str = defaultToString<T>,
std::function<T(const std::string&)> from_str = defaultFromString<T>)
std::function<std::string(const T&)> to_str = internal::DefaultToString<T>,
std::function<T(const std::string&)> from_str = internal::DefaultFromString<T>)
: key_{std::move(key)}, default_{val}, to_str_{to_str}, from_str_{from_str} {}

private:
Expand All @@ -85,29 +88,29 @@ class ConfigBase {
};

template <typename T>
ConfigBase& set(const Entry<T>& entry, const T& val) {
configs_[entry.key_] = entry.to_str_(val);
ConfigBase& Set(const Entry<T>& entry, const T& val) {
configs_.emplace(entry.key_, entry.to_str_(val));
return *this;
}

template <typename T>
ConfigBase& unset(const Entry<T>& entry) {
ConfigBase& Unset(const Entry<T>& entry) {
configs_.erase(entry.key_);
return *this;
}

ConfigBase& reset() {
ConfigBase& Reset() {
configs_.clear();
return *this;
}

template <typename T>
T get(const Entry<T>& entry) const {
T Get(const Entry<T>& entry) const {
auto iter = configs_.find(entry.key_);
return iter != configs_.cend() ? entry.from_str_(iter->second) : entry.default_;
}

std::unordered_map<std::string, std::string> const& configs() const { return configs_; }
const std::unordered_map<std::string, std::string>& configs() const { return configs_; }

protected:
std::unordered_map<std::string, std::string> configs_;
Expand Down
58 changes: 29 additions & 29 deletions test/config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,42 +69,42 @@ TEST(ConfigTest, BasicOperations) {
TestConfig config;

// Test default values
ASSERT_EQ(config.get(TestConfig::STRING_CONFIG), std::string("default_value"));
ASSERT_EQ(config.get(TestConfig::INT_CONFIG), 25);
ASSERT_EQ(config.get(TestConfig::BOOL_CONFIG), false);
ASSERT_EQ(config.get(TestConfig::ENUM_CONFIG), TestEnum::VALUE1);
ASSERT_EQ(config.get(TestConfig::DOUBLE_CONFIG), 3.14);
ASSERT_EQ(config.Get(TestConfig::STRING_CONFIG), std::string("default_value"));
ASSERT_EQ(config.Get(TestConfig::INT_CONFIG), 25);
ASSERT_EQ(config.Get(TestConfig::BOOL_CONFIG), false);
ASSERT_EQ(config.Get(TestConfig::ENUM_CONFIG), TestEnum::VALUE1);
ASSERT_EQ(config.Get(TestConfig::DOUBLE_CONFIG), 3.14);

// Test setting values
config.set(TestConfig::STRING_CONFIG, std::string("new_value"));
config.set(TestConfig::INT_CONFIG, 100);
config.set(TestConfig::BOOL_CONFIG, true);
config.set(TestConfig::ENUM_CONFIG, TestEnum::VALUE2);
config.set(TestConfig::DOUBLE_CONFIG, 2.99);
config.Set(TestConfig::STRING_CONFIG, std::string("new_value"));
config.Set(TestConfig::INT_CONFIG, 100);
config.Set(TestConfig::BOOL_CONFIG, true);
config.Set(TestConfig::ENUM_CONFIG, TestEnum::VALUE2);
config.Set(TestConfig::DOUBLE_CONFIG, 2.99);

ASSERT_EQ(config.get(TestConfig::STRING_CONFIG), "new_value");
ASSERT_EQ(config.get(TestConfig::INT_CONFIG), 100);
ASSERT_EQ(config.get(TestConfig::BOOL_CONFIG), true);
ASSERT_EQ(config.get(TestConfig::ENUM_CONFIG), TestEnum::VALUE2);
ASSERT_EQ(config.get(TestConfig::DOUBLE_CONFIG), 2.99);
ASSERT_EQ(config.Get(TestConfig::STRING_CONFIG), "new_value");
ASSERT_EQ(config.Get(TestConfig::INT_CONFIG), 100);
ASSERT_EQ(config.Get(TestConfig::BOOL_CONFIG), true);
ASSERT_EQ(config.Get(TestConfig::ENUM_CONFIG), TestEnum::VALUE2);
ASSERT_EQ(config.Get(TestConfig::DOUBLE_CONFIG), 2.99);

// Test unsetting a value
config.unset(TestConfig::INT_CONFIG);
config.unset(TestConfig::ENUM_CONFIG);
config.unset(TestConfig::DOUBLE_CONFIG);
ASSERT_EQ(config.get(TestConfig::INT_CONFIG), 25);
ASSERT_EQ(config.get(TestConfig::STRING_CONFIG), "new_value");
ASSERT_EQ(config.get(TestConfig::BOOL_CONFIG), true);
ASSERT_EQ(config.get(TestConfig::ENUM_CONFIG), TestEnum::VALUE1);
ASSERT_EQ(config.get(TestConfig::DOUBLE_CONFIG), 3.14);
config.Unset(TestConfig::INT_CONFIG);
config.Unset(TestConfig::ENUM_CONFIG);
config.Unset(TestConfig::DOUBLE_CONFIG);
ASSERT_EQ(config.Get(TestConfig::INT_CONFIG), 25);
ASSERT_EQ(config.Get(TestConfig::STRING_CONFIG), "new_value");
ASSERT_EQ(config.Get(TestConfig::BOOL_CONFIG), true);
ASSERT_EQ(config.Get(TestConfig::ENUM_CONFIG), TestEnum::VALUE1);
ASSERT_EQ(config.Get(TestConfig::DOUBLE_CONFIG), 3.14);

// Test resetting all values
config.reset();
ASSERT_EQ(config.get(TestConfig::STRING_CONFIG), "default_value");
ASSERT_EQ(config.get(TestConfig::INT_CONFIG), 25);
ASSERT_EQ(config.get(TestConfig::BOOL_CONFIG), false);
ASSERT_EQ(config.get(TestConfig::ENUM_CONFIG), TestEnum::VALUE1);
ASSERT_EQ(config.get(TestConfig::DOUBLE_CONFIG), 3.14);
config.Reset();
ASSERT_EQ(config.Get(TestConfig::STRING_CONFIG), "default_value");
ASSERT_EQ(config.Get(TestConfig::INT_CONFIG), 25);
ASSERT_EQ(config.Get(TestConfig::BOOL_CONFIG), false);
ASSERT_EQ(config.Get(TestConfig::ENUM_CONFIG), TestEnum::VALUE1);
ASSERT_EQ(config.Get(TestConfig::DOUBLE_CONFIG), 3.14);
}

} // namespace iceberg
Loading