iceberg-cpp
Loading...
Searching...
No Matches
config.h
Go to the documentation of this file.
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
23
24#include <format>
25#include <functional>
26#include <string>
27#include <unordered_map>
28
29#include "iceberg/exception.h"
30#include "iceberg/util/macros.h"
32
33namespace iceberg {
34namespace internal {
35// Default conversion functions
36template <typename U>
37std::string DefaultToString(const U& val) {
38 if constexpr ((std::is_signed_v<U> && std::is_integral_v<U>) ||
39 std::is_floating_point_v<U>) {
40 return std::to_string(val);
41 } else if constexpr (std::is_same_v<U, bool>) {
42 return val ? "true" : "false";
43 } else if constexpr (std::is_same_v<U, std::string> ||
44 std::is_same_v<U, std::string_view>) {
45 return val;
46 } else {
47 throw IcebergError(
48 std::format("Explicit to_str() is required for {}", typeid(U).name()));
49 }
50}
51
52template <typename U>
53U DefaultFromString(const std::string& val) {
54 if constexpr (std::is_same_v<U, std::string>) {
55 return val;
56 } else if constexpr (std::is_same_v<U, bool>) {
57 return StringUtils::EqualsIgnoreCase(val, "true");
58 } else if constexpr ((std::is_signed_v<U> && std::is_integral_v<U>) ||
59 std::is_floating_point_v<U>) {
60 ICEBERG_ASSIGN_OR_THROW(auto res, StringUtils::ParseNumber<U>(val));
61 return res;
62 } else {
63 throw IcebergError(
64 std::format("Explicit from_str() is required for {}", typeid(U).name()));
65 }
66}
67} // namespace internal
68
69template <class ConcreteConfig>
71 public:
72 template <typename T>
73 class Entry {
74 public:
75 Entry(std::string key, T val,
76 std::function<std::string(const T&)> to_str = internal::DefaultToString<T>,
77 std::function<T(const std::string&)> from_str = internal::DefaultFromString<T>)
78 : key_{std::move(key)},
79 default_{std::move(val)},
80 to_str_{std::move(to_str)},
81 from_str_{std::move(from_str)} {}
82
83 private:
84 const std::string key_;
85 const T default_;
86 const std::function<std::string(const T&)> to_str_;
87 const std::function<T(const std::string&)> from_str_;
88
89 friend ConfigBase;
90 friend ConcreteConfig;
91
92 public:
93 const std::string& key() const { return key_; }
94
95 const T& value() const { return default_; }
96 };
97
98 template <typename T>
99 ConfigBase& Set(const Entry<T>& entry, const T& val) {
100 configs_[entry.key_] = entry.to_str_(val);
101 return *this;
102 }
103
104 template <typename T>
105 ConfigBase& Unset(const Entry<T>& entry) {
106 configs_.erase(entry.key_);
107 return *this;
108 }
109
110 ConfigBase& Reset() {
111 configs_.clear();
112 return *this;
113 }
114
115 template <typename T>
116 T Get(const Entry<T>& entry) const {
117 auto iter = configs_.find(entry.key_);
118 return iter != configs_.cend() ? entry.from_str_(iter->second) : entry.default_;
119 }
120
121 const std::unordered_map<std::string, std::string>& configs() const { return configs_; }
122
123 std::unordered_map<std::string, std::string>& mutable_configs() { return configs_; }
124
128 std::unordered_map<std::string, std::string> Extract(std::string_view prefix) const {
129 std::unordered_map<std::string, std::string> extracted;
130 for (const auto& [key, value] : configs_) {
131 if (key.starts_with(prefix)) {
132 extracted[key.substr(prefix.length())] = value;
133 }
134 }
135 return extracted;
136 }
137
138 protected:
139 std::unordered_map<std::string, std::string> configs_;
140};
141
142} // namespace iceberg
Definition config.h:73
Definition config.h:70
std::unordered_map< std::string, std::string > Extract(std::string_view prefix) const
Extracts the prefix from the configuration.
Definition config.h:128
static bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs)
Case-insensitive equality using Unicode simple (1:1) case mapping.
Definition string_util.h:82
Define common Iceberg utility macros.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
Provide string utility helpers.