Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ConfigDefinition.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/Assert.hpp"
23#include "util/config/Array.hpp"
24#include "util/config/ConfigConstraints.hpp"
25#include "util/config/ConfigFileInterface.hpp"
26#include "util/config/ConfigValue.hpp"
27#include "util/config/Error.hpp"
28#include "util/config/ObjectView.hpp"
29#include "util/config/ValueView.hpp"
30
31#include <algorithm>
32#include <chrono>
33#include <cstddef>
34#include <initializer_list>
35#include <optional>
36#include <string>
37#include <string_view>
38#include <unordered_map>
39#include <utility>
40#include <variant>
41#include <vector>
42
43namespace util::config {
44
51public:
52 using KeyValuePair = std::pair<std::string_view, std::variant<ConfigValue, Array>>;
53
62 ClioConfigDefinition(std::initializer_list<KeyValuePair> pair);
63
73 [[nodiscard]] std::optional<std::vector<Error>>
74 parse(ConfigFileInterface const& config);
75
83 [[nodiscard]] ObjectView
84 getObject(std::string_view prefix, std::optional<std::size_t> idx = std::nullopt) const;
85
92 [[nodiscard]] ValueView
93 getValueView(std::string_view fullKey) const;
94
102 template <typename T>
103 T
104 get(std::string_view fullKey) const
105 {
106 ASSERT(map_.contains(fullKey), "key {} does not exist in config", fullKey);
107 auto const val = map_.at(fullKey);
108 if (std::holds_alternative<ConfigValue>(val)) {
109 return ValueView{std::get<ConfigValue>(val)}.getValueImpl<T>();
110 }
111 std::unreachable();
112 }
113
121 [[nodiscard]] ValueView
122 getValueInArray(std::string_view fullKey, std::size_t index) const;
123
130 [[nodiscard]] ArrayView
131 getArray(std::string_view prefix) const;
132
139 [[nodiscard]] bool
140 contains(std::string_view key) const;
141
148 [[nodiscard]] bool
149 hasItemsWithPrefix(std::string_view key) const;
150
157 [[nodiscard]] Array const&
158 asArray(std::string_view key) const;
159
166 [[nodiscard]] std::size_t
167 arraySize(std::string_view prefix) const;
168
175 static std::chrono::milliseconds
176 toMilliseconds(float value);
177
185 template <typename T>
186 std::optional<T>
187 maybeValue(std::string_view fullKey) const
188 {
189 return getValueView(fullKey).asOptional<T>();
190 }
191
197 [[nodiscard]] auto
198 begin() const
199 {
200 return map_.begin();
201 }
202
208 [[nodiscard]] auto
209 end() const
210 {
211 return map_.end();
212 }
213
214private:
221 [[nodiscard]] auto
222 getArrayIterator(std::string_view key) const
223 {
224 auto const fullKey = addBracketsForArrayKey(key);
225 auto const it = std::ranges::find_if(map_, [&fullKey](auto pair) { return pair.first == fullKey; });
226
227 ASSERT(it != map_.end(), "key {} does not exist in config", fullKey);
228 ASSERT(std::holds_alternative<Array>(it->second), "Value of {} is not an array", fullKey);
229
230 return it;
231 }
232
239 [[nodiscard]] static std::string
240 addBracketsForArrayKey(std::string_view key)
241 {
242 std::string fullKey = std::string(key);
243 if (!key.contains(".[]"))
244 fullKey += ".[]";
245 return fullKey;
246 }
247
248 std::unordered_map<std::string_view, std::variant<ConfigValue, Array>> map_;
249};
250
257ClioConfigDefinition&
258getClioConfig();
259
260} // namespace util::config
View for array structure for config.
Definition ArrayView.hpp:42
Array definition to store multiple values provided by the user from Json/Yaml.
Definition Array.hpp:41
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:50
auto begin() const
Returns an iterator to the beginning of the configuration map.
Definition ConfigDefinition.hpp:198
std::size_t arraySize(std::string_view prefix) const
Returns the size of an Array.
Definition ConfigDefinition.cpp:145
ArrayView getArray(std::string_view prefix) const
Returns the specified Array object from ClioConfigDefinition.
Definition ConfigDefinition.cpp:84
static std::chrono::milliseconds toMilliseconds(float value)
Method to convert a float seconds value to milliseconds.
Definition ConfigDefinition.cpp:124
auto end() const
Returns an iterator to the end of the configuration map.
Definition ConfigDefinition.hpp:209
std::optional< std::vector< Error > > parse(ConfigFileInterface const &config)
Parses the configuration file.
Definition ConfigDefinition.cpp:159
bool contains(std::string_view key) const
Checks if a key is present in the configuration map.
Definition ConfigDefinition.cpp:101
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:63
bool hasItemsWithPrefix(std::string_view key) const
Checks if any key in config starts with "key".
Definition ConfigDefinition.cpp:107
ClioConfigDefinition(std::initializer_list< KeyValuePair > pair)
Constructs a new ClioConfigDefinition.
Definition ConfigDefinition.cpp:53
Array const & asArray(std::string_view key) const
Returns the Array object associated with the specified key.
Definition ConfigDefinition.cpp:138
ValueView getValueView(std::string_view fullKey) const
Returns the specified ValueView object associated with the key.
Definition ConfigDefinition.cpp:113
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:187
T get(std::string_view fullKey) const
Returns the specified value of given string if value exists.
Definition ConfigDefinition.hpp:104
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:131
The interface for configuration files.
Definition ConfigFileInterface.hpp:37
Provides a view into a subset of configuration data defined by a prefix.
Definition ObjectView.hpp:40
Provides view into ConfigValues that represents values in Clio Config.
Definition ValueView.hpp:46
T getValueImpl() const
Retrieves the stored value as the specified type T.
Definition ValueView.hpp:165
std::optional< T > asOptional() const
Returns an optional value of the specified type T if valid.
Definition ValueView.hpp:193