iceberg-cpp
Loading...
Searching...
No Matches
config.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19#pragma once
20
21#include <format>
22#include <functional>
23#include <string>
24#include <unordered_map>
25
26#include "iceberg/exception.h"
27#include "iceberg/util/macros.h"
28#include "iceberg/util/string_util.h"
29
30namespace iceberg {
31namespace internal {
32// Default conversion functions
33template <typename U>
34std::string DefaultToString(const U& val) {
35 if constexpr ((std::is_signed_v<U> && std::is_integral_v<U>) ||
36 std::is_floating_point_v<U>) {
37 return std::to_string(val);
38 } else if constexpr (std::is_same_v<U, bool>) {
39 return val ? "true" : "false";
40 } else if constexpr (std::is_same_v<U, std::string> ||
41 std::is_same_v<U, std::string_view>) {
42 return val;
43 } else {
44 throw IcebergError(
45 std::format("Explicit to_str() is required for {}", typeid(U).name()));
46 }
47}
48
49template <typename U>
50U DefaultFromString(const std::string& val) {
51 if constexpr (std::is_same_v<U, std::string>) {
52 return val;
53 } else if constexpr (std::is_same_v<U, bool>) {
54 return val == "true";
55 } else if constexpr ((std::is_signed_v<U> && std::is_integral_v<U>) ||
56 std::is_floating_point_v<U>) {
57 ICEBERG_ASSIGN_OR_THROW(auto res, StringUtils::ParseNumber<U>(val));
58 return res;
59 } else {
60 throw IcebergError(
61 std::format("Explicit from_str() is required for {}", typeid(U).name()));
62 }
63}
64} // namespace internal
65
66template <class ConcreteConfig>
68 public:
69 template <typename T>
70 class Entry {
71 public:
72 Entry(std::string key, T val,
73 std::function<std::string(const T&)> to_str = internal::DefaultToString<T>,
74 std::function<T(const std::string&)> from_str = internal::DefaultFromString<T>)
75 : key_{std::move(key)},
76 default_{std::move(val)},
77 to_str_{std::move(to_str)},
78 from_str_{std::move(from_str)} {}
79
80 private:
81 const std::string key_;
82 const T default_;
83 const std::function<std::string(const T&)> to_str_;
84 const std::function<T(const std::string&)> from_str_;
85
86 friend ConfigBase;
87 friend ConcreteConfig;
88
89 public:
90 const std::string& key() const { return key_; }
91
92 const T& value() const { return default_; }
93 };
94
95 template <typename T>
96 ConfigBase& Set(const Entry<T>& entry, const T& val) {
97 configs_[entry.key_] = entry.to_str_(val);
98 return *this;
99 }
100
101 template <typename T>
102 ConfigBase& Unset(const Entry<T>& entry) {
103 configs_.erase(entry.key_);
104 return *this;
105 }
106
107 ConfigBase& Reset() {
108 configs_.clear();
109 return *this;
110 }
111
112 template <typename T>
113 T Get(const Entry<T>& entry) const {
114 auto iter = configs_.find(entry.key_);
115 return iter != configs_.cend() ? entry.from_str_(iter->second) : entry.default_;
116 }
117
118 const std::unordered_map<std::string, std::string>& configs() const { return configs_; }
119
120 std::unordered_map<std::string, std::string>& mutable_configs() { return configs_; }
121
125 std::unordered_map<std::string, std::string> Extract(std::string_view prefix) const {
126 std::unordered_map<std::string, std::string> extracted;
127 for (const auto& [key, value] : configs_) {
128 if (key.starts_with(prefix)) {
129 extracted[key.substr(prefix.length())] = value;
130 }
131 }
132 return extracted;
133 }
134
135 protected:
136 std::unordered_map<std::string, std::string> configs_;
137};
138
139} // namespace iceberg
Definition config.h:70
Definition config.h:67
std::unordered_map< std::string, std::string > Extract(std::string_view prefix) const
Extracts the prefix from the configuration.
Definition config.h:125