Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ConfigDefinition.hpp
1#pragma once
2
3#include "util/Assert.hpp"
4#include "util/config/Array.hpp"
5#include "util/config/ConfigConstraints.hpp"
6#include "util/config/ConfigFileInterface.hpp"
7#include "util/config/ConfigValue.hpp"
8#include "util/config/Error.hpp"
9#include "util/config/ObjectView.hpp"
10#include "util/config/ValueView.hpp"
11
12#include <algorithm>
13#include <chrono>
14#include <cstddef>
15#include <initializer_list>
16#include <optional>
17#include <string>
18#include <string_view>
19#include <unordered_map>
20#include <utility>
21#include <variant>
22#include <vector>
23
24namespace util::config {
25
32public:
33 using KeyValuePair = std::pair<std::string_view, std::variant<ConfigValue, Array>>;
34
43 ClioConfigDefinition(std::initializer_list<KeyValuePair> pair);
44
54 [[nodiscard]] std::optional<std::vector<Error>>
55 parse(ConfigFileInterface const& config);
56
64 [[nodiscard]] ObjectView
65 getObject(std::string_view prefix, std::optional<std::size_t> idx = std::nullopt) const;
66
73 [[nodiscard]] ValueView
74 getValueView(std::string_view fullKey) const;
75
83 template <typename T>
84 T
85 get(std::string_view fullKey) const
86 {
87 ASSERT(map_.contains(fullKey), "key {} does not exist in config", fullKey);
88 auto const val = map_.at(fullKey);
89 if (std::holds_alternative<ConfigValue>(val)) {
90 return ValueView{std::get<ConfigValue>(val)}.getValueImpl<T>();
91 }
92 std::unreachable();
93 }
94
102 [[nodiscard]] ValueView
103 getValueInArray(std::string_view fullKey, std::size_t index) const;
104
111 [[nodiscard]] ArrayView
112 getArray(std::string_view prefix) const;
113
120 [[nodiscard]] bool
121 contains(std::string_view key) const;
122
129 [[nodiscard]] bool
130 hasItemsWithPrefix(std::string_view key) const;
131
138 [[nodiscard]] Array const&
139 asArray(std::string_view key) const;
140
147 [[nodiscard]] std::size_t
148 arraySize(std::string_view prefix) const;
149
156 static std::chrono::milliseconds
157 toMilliseconds(float value);
158
166 template <typename T>
167 std::optional<T>
168 maybeValue(std::string_view fullKey) const
169 {
170 return getValueView(fullKey).asOptional<T>();
171 }
172
178 [[nodiscard]] auto
179 begin() const
180 {
181 return map_.begin();
182 }
183
189 [[nodiscard]] auto
190 end() const
191 {
192 return map_.end();
193 }
194
195private:
202 [[nodiscard]] auto
203 getArrayIterator(std::string_view key) const
204 {
205 auto const fullKey = addBracketsForArrayKey(key);
206 auto const it =
207 std::ranges::find_if(map_, [&fullKey](auto pair) { return pair.first == fullKey; });
208
209 ASSERT(it != map_.end(), "key {} does not exist in config", fullKey);
210 ASSERT(std::holds_alternative<Array>(it->second), "Value of {} is not an array", fullKey);
211
212 return it;
213 }
214
221 [[nodiscard]] static std::string
222 addBracketsForArrayKey(std::string_view key)
223 {
224 std::string fullKey = std::string(key);
225 if (!key.contains(".[]"))
226 fullKey += ".[]";
227 return fullKey;
228 }
229
230 std::unordered_map<std::string_view, std::variant<ConfigValue, Array>> map_;
231};
232
240getClioConfig();
241
242} // namespace util::config
View for array structure for config.
Definition ArrayView.hpp:23
Array definition to store multiple values provided by the user from Json/Yaml.
Definition Array.hpp:22
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
auto begin() const
Returns an iterator to the beginning of the configuration map.
Definition ConfigDefinition.hpp:179
std::size_t arraySize(std::string_view prefix) const
Returns the size of an Array.
Definition ConfigDefinition.cpp:132
ArrayView getArray(std::string_view prefix) const
Returns the specified Array object from ClioConfigDefinition.
Definition ConfigDefinition.cpp:66
static std::chrono::milliseconds toMilliseconds(float value)
Method to convert a float seconds value to milliseconds.
Definition ConfigDefinition.cpp:109
auto end() const
Returns an iterator to the end of the configuration map.
Definition ConfigDefinition.hpp:190
std::optional< std::vector< Error > > parse(ConfigFileInterface const &config)
Parses the configuration file.
Definition ConfigDefinition.cpp:146
bool contains(std::string_view key) const
Checks if a key is present in the configuration map.
Definition ConfigDefinition.cpp:84
ObjectView getObject(std::string_view prefix, std::optional< std::size_t > idx=std::nullopt) const
Returns the ObjectView specified with the prefix.
Definition ConfigDefinition.cpp:44
bool hasItemsWithPrefix(std::string_view key) const
Checks if any key in config starts with "key".
Definition ConfigDefinition.cpp:90
ClioConfigDefinition(std::initializer_list< KeyValuePair > pair)
Constructs a new ClioConfigDefinition.
Definition ConfigDefinition.cpp:34
Array const & asArray(std::string_view key) const
Returns the Array object associated with the specified key.
Definition ConfigDefinition.cpp:125
ValueView getValueView(std::string_view fullKey) const
Returns the specified ValueView object associated with the key.
Definition ConfigDefinition.cpp:98
std::optional< T > maybeValue(std::string_view fullKey) const
Returns the specified value of given string of type T if type and value exists.
Definition ConfigDefinition.hpp:168
T get(std::string_view fullKey) const
Returns the specified value of given string if value exists.
Definition ConfigDefinition.hpp:85
ValueView getValueInArray(std::string_view fullKey, std::size_t index) const
Returns the specified ValueView object in an array with a given index.
Definition ConfigDefinition.cpp:118
The interface for configuration files.
Definition ConfigFileInterface.hpp:18
Provides a view into a subset of configuration data defined by a prefix.
Definition ObjectView.hpp:21
Provides view into ConfigValues that represents values in Clio Config.
Definition ValueView.hpp:27
T getValueImpl() const
Retrieves the stored value as the specified type T.
Definition ValueView.hpp:146
std::optional< T > asOptional() const
Returns an optional value of the specified type T if valid.
Definition ValueView.hpp:177