22#include "rpc/common/APIVersion.hpp"
23#include "util/Assert.hpp"
24#include "util/config/Array.hpp"
25#include "util/config/ConfigConstraints.hpp"
26#include "util/config/ConfigFileInterface.hpp"
27#include "util/config/ConfigValue.hpp"
28#include "util/config/Error.hpp"
29#include "util/config/ObjectView.hpp"
30#include "util/config/Types.hpp"
31#include "util/config/ValueView.hpp"
37#include <initializer_list>
42#include <unordered_map>
47namespace util::config {
56 using KeyValuePair = std::pair<std::string_view, std::variant<ConfigValue, Array>>;
77 [[nodiscard]] std::optional<std::vector<Error>>
88 getObject(std::string_view prefix, std::optional<std::size_t> idx = std::nullopt)
const;
106 template <
typename T>
108 get(std::string_view fullKey)
const
110 ASSERT(map_.contains(fullKey),
"key {} does not exist in config", fullKey);
111 auto const val = map_.at(fullKey);
112 if (std::holds_alternative<ConfigValue>(val)) {
135 getArray(std::string_view prefix)
const;
144 contains(std::string_view key)
const;
161 [[nodiscard]]
Array const&
162 asArray(std::string_view key)
const;
170 [[nodiscard]] std::size_t
171 arraySize(std::string_view prefix)
const;
179 static std::chrono::milliseconds
189 template <
typename T>
226 getArrayIterator(std::string_view key)
const
228 auto const fullKey = addBracketsForArrayKey(key);
229 auto const it = std::ranges::find_if(map_, [&fullKey](
auto pair) {
return pair.first == fullKey; });
231 ASSERT(it != map_.end(),
"key {} does not exist in config", fullKey);
232 ASSERT(std::holds_alternative<Array>(it->second),
"Value of {} is not an array", fullKey);
243 [[nodiscard]]
static std::string
244 addBracketsForArrayKey(std::string_view key)
246 std::string fullKey = std::string(key);
247 if (!key.contains(
".[]"))
252 std::unordered_map<std::string_view, std::variant<ConfigValue, Array>> map_;
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:54
auto begin() const
Returns an iterator to the beginning of the configuration map.
Definition ConfigDefinition.hpp:202
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:213
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:191
T get(std::string_view fullKey) const
Returns the specified value of given string if value exists.
Definition ConfigDefinition.hpp:108
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:36
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