22#include "util/config/impl/Helpers.hpp"
24#include <boost/json.hpp>
25#include <boost/json/kind.hpp>
26#include <boost/json/object.hpp>
27#include <boost/json/value.hpp>
50 boost::json::value store_;
51 static constexpr char kSEPARATOR =
'.';
54 using KeyType = std::string;
55 using ArrayType = std::vector<Config>;
56 using WriteCursorType = std::pair<std::optional<std::reference_wrapper<boost::json::value>>, KeyType>;
62 explicit Config(boost::json::value store = {});
74 operator bool()
const noexcept;
106 template <
typename Result>
107 [[nodiscard]] std::optional<Result>
110 auto maybeElement = lookup(key);
112 return std::make_optional<Result>(checkedAs<Result>(key, *maybeElement));
132 template <
typename Result>
156 template <
typename Result>
183 template <
typename Result>
189 }
catch (std::exception
const&) {
190 throw std::runtime_error(std::string{err});
206 [[nodiscard]] std::optional<ArrayType>
222 [[nodiscard]] ArrayType
223 array(KeyType key)
const;
238 [[nodiscard]] ArrayType
239 arrayOr(KeyType key, ArrayType fallback)
const;
255 [[nodiscard]] ArrayType
285 sectionOr(KeyType key, boost::json::object fallback)
const;
298 template <
typename Result>
299 [[nodiscard]] std::optional<Result>
302 if (store_.is_null())
304 return std::make_optional<Result>(checkedAs<Result>(
"_self_", store_));
314 template <
typename Result>
329 template <
typename Result>
345 template <
typename Result>
351 }
catch (std::exception
const&) {
352 throw std::runtime_error(std::string{err});
363 [[nodiscard]] ArrayType
372 static std::chrono::milliseconds
376 template <
typename Return>
378 checkedAs(KeyType key, boost::json::value
const&
value)
const
380 using boost::json::value_to;
382 auto errorIf = [&key, &
value](
bool condition) {
384 throw std::runtime_error(
385 "Type for key '" + key +
"' is '" + std::string{to_string(
value.kind())} +
386 "' in JSON but requested '" + impl::typeName<Return>() +
"'"
391 if constexpr (std::is_same_v<Return, bool>) {
392 errorIf(not
value.is_bool());
393 }
else if constexpr (std::is_same_v<Return, std::string>) {
394 errorIf(not
value.is_string());
395 }
else if constexpr (std::is_same_v<Return, double> or std::is_same_v<Return, float>) {
396 errorIf(not
value.is_number());
397 }
else if constexpr (std::is_convertible_v<Return, uint64_t> || std::is_convertible_v<Return, int64_t>) {
398 errorIf(not
value.is_int64() && not
value.is_uint64());
401 return value_to<Return>(
value);
404 std::optional<boost::json::value>
405 lookup(KeyType key)
const;
408 lookupForWrite(KeyType key);
426 open(std::filesystem::path path);
Simple configuration file reader.
Definition Config.hpp:417
static Config open(std::filesystem::path path)
Read in a configuration file.
Definition Config.cpp:191
Convenience wrapper to query a JSON configuration file.
Definition Config.hpp:49
Result valueOrThrow(KeyType key, std::string_view err) const
Interface for fetching values by key with custom error handling.
Definition Config.hpp:185
std::optional< ArrayType > maybeArray(KeyType key) const
Interface for fetching an array by key that returns std::optional.
Definition Config.cpp:104
Result value() const
Interface for reading the value directly referred to by the instance.
Definition Config.hpp:316
ArrayType array() const
Interface for reading the array directly referred to by the instance.
Definition Config.cpp:170
Config(boost::json::value store={})
Construct a new Config object.
Definition Config.cpp:53
ArrayType arrayOr(KeyType key, ArrayType fallback) const
Interface for fetching an array by key with fallback.
Definition Config.cpp:134
bool contains(KeyType key) const
Checks whether something exists under given key.
Definition Config.cpp:63
Result valueOr(Result fallback) const
Interface for reading the value directly referred to by the instance with user-specified fallback.
Definition Config.hpp:331
Config sectionOr(KeyType key, boost::json::object fallback) const
Interface for fetching a sub section by key with a fallback object.
Definition Config.cpp:161
ArrayType arrayOrThrow(KeyType key, std::string_view err) const
Interface for fetching an array by key with custom error handling.
Definition Config.cpp:142
Result valueOr(KeyType key, Result fallback) const
Interface for fetching values by key with fallback.
Definition Config.hpp:158
Config section(KeyType key) const
Interface for fetching a sub section by key.
Definition Config.cpp:152
Result value(KeyType key) const
Interface for fetching values by key.
Definition Config.hpp:134
std::optional< Result > maybeValue() const
Interface for reading the value directly referred to by the instance. Wraps as std::optional.
Definition Config.hpp:300
static std::chrono::milliseconds toMilliseconds(float value)
Method to convert a float seconds value to milliseconds.
Definition Config.cpp:184
std::optional< Result > maybeValue(KeyType key) const
Interface for fetching values by key that returns std::optional.
Definition Config.hpp:108
Result valueOrThrow(std::string_view err) const
Interface for reading the value directly referred to by the instance with user-specified error messag...
Definition Config.hpp:347
This namespace contains various utilities.
Definition AccountUtils.hpp:30
Thrown when a Store (config's storage) related error occurs.
Definition Helpers.hpp:43