Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
util::Config Class Referencefinal

Convenience wrapper to query a JSON configuration file. More...

#include <Config.hpp>

Public Types

using KeyType = std::string
 
using ArrayType = std::vector<Config>
 
using WriteCursorType = std::pair<std::optional<std::reference_wrapper<boost::json::value>>, KeyType>
 

Public Member Functions

 Config (boost::json::value store={})
 Construct a new Config object.
 
 operator bool () const noexcept
 Checks whether underlying store is not null.
 
bool contains (KeyType key) const
 Checks whether something exists under given key.
 
template<typename Result >
std::optional< Result > maybeValue (KeyType key) const
 Interface for fetching values by key that returns std::optional.
 
template<typename Result >
Result value (KeyType key) const
 Interface for fetching values by key.
 
template<typename Result >
Result valueOr (KeyType key, Result fallback) const
 Interface for fetching values by key with fallback.
 
template<typename Result >
Result valueOrThrow (KeyType key, std::string_view err) const
 Interface for fetching values by key with custom error handling.
 
std::optional< ArrayType > maybeArray (KeyType key) const
 Interface for fetching an array by key that returns std::optional.
 
ArrayType array (KeyType key) const
 Interface for fetching an array by key.
 
ArrayType arrayOr (KeyType key, ArrayType fallback) const
 Interface for fetching an array by key with fallback.
 
ArrayType arrayOrThrow (KeyType key, std::string_view err) const
 Interface for fetching an array by key with custom error handling.
 
Config section (KeyType key) const
 Interface for fetching a sub section by key.
 
Config sectionOr (KeyType key, boost::json::object fallback) const
 Interface for fetching a sub section by key with a fallback object.
 
template<typename Result >
std::optional< Result > maybeValue () const
 Interface for reading the value directly referred to by the instance. Wraps as std::optional.
 
template<typename Result >
Result value () const
 Interface for reading the value directly referred to by the instance.
 
template<typename Result >
Result valueOr (Result fallback) const
 Interface for reading the value directly referred to by the instance with user-specified fallback.
 
template<typename Result >
Result valueOrThrow (std::string_view err) const
 Interface for reading the value directly referred to by the instance with user-specified error message.
 
ArrayType array () const
 Interface for reading the array directly referred to by the instance.
 

Static Public Member Functions

static std::chrono::milliseconds toMilliseconds (float value)
 Method to convert a float seconds value to milliseconds.
 

Detailed Description

Convenience wrapper to query a JSON configuration file.

Any custom data type can be supported by implementing the right tag_invoke for boost::json::value_to.

Constructor & Destructor Documentation

◆ Config()

util::Config::Config ( boost::json::value store = {})
explicit

Construct a new Config object.

Parameters
storeboost::json::value that backs this instance

Member Function Documentation

◆ array() [1/2]

Config::ArrayType util::Config::array ( ) const
nodiscard

Interface for reading the array directly referred to by the instance.

See array(KeyType) const for how this works.

Returns
The array

◆ array() [2/2]

Config::ArrayType util::Config::array ( KeyType key) const
nodiscard

Interface for fetching an array by key.

Will attempt to fetch an array under the desired key. If the array exists then it will be returned. If the array does not exist under the specified key an std::logic_error is thrown.

Parameters
keyThe key to check
Returns
The array
Exceptions
std::logic_errorThrown if there is no array under the desired key or the key is of invalid format

◆ arrayOr()

Config::ArrayType util::Config::arrayOr ( KeyType key,
ArrayType fallback ) const
nodiscard

Interface for fetching an array by key with fallback.

Will attempt to fetch an array under the desired key. If the array exists then it will be returned. If the array does not exist or another type is stored under the desired key - user specified fallback is returned.

Parameters
keyThe key to check
fallbackThe fallback array
Returns
The array
Exceptions
std::logic_errorThrown if the key is of invalid format

◆ arrayOrThrow()

Config::ArrayType util::Config::arrayOrThrow ( KeyType key,
std::string_view err ) const
nodiscard

Interface for fetching an array by key with custom error handling.

Will attempt to fetch an array under the desired key. If the array exists then it will be returned. If the array does not exist or another type is stored under the desired key - std::runtime_error is thrown with the user specified error message.

Parameters
keyThe key to check
errThe custom error message
Returns
The array
Exceptions
std::runtime_errorThrown if there is no array under the desired key

◆ contains()

bool util::Config::contains ( KeyType key) const
nodiscard

Checks whether something exists under given key.

Parameters
keyThe key to check
Returns
true If something exists under key
false If nothing exists under key
Exceptions
std::logic_errorIf the key is of invalid format

◆ maybeArray()

std::optional< Config::ArrayType > util::Config::maybeArray ( KeyType key) const
nodiscard

Interface for fetching an array by key that returns std::optional.

Will attempt to fetch an array under the desired key. If the array exists then it will be returned wrapped in an optional. If the array does not exist under the specified key - std::nullopt is returned.

Parameters
keyThe key to check
Returns
Optional array
Exceptions
std::logic_errorThrown if the key is of invalid format

◆ maybeValue() [1/2]

template<typename Result >
std::optional< Result > util::Config::maybeValue ( ) const
inlinenodiscard

Interface for reading the value directly referred to by the instance. Wraps as std::optional.

See maybeValue(KeyType) const for how this works.

Returns
Optional value

◆ maybeValue() [2/2]

template<typename Result >
std::optional< Result > util::Config::maybeValue ( KeyType key) const
inlinenodiscard

Interface for fetching values by key that returns std::optional.

Will attempt to fetch the value under the desired key. If the value exists and can be represented by the desired type Result then it will be returned wrapped in an optional. If the value exists but the conversion to Result is not possible - a runtime_error will be thrown. If the value does not exist under the specified key - std::nullopt is returned.

Template Parameters
ResultThe desired return type
Parameters
keyThe key to check
Returns
Optional value of desired type
Exceptions
std::logic_errorThrown if conversion to Result is not possible or key is of invalid format

◆ operator bool()

util::Config::operator bool ( ) const
noexcept

Checks whether underlying store is not null.

Returns
true If the store is null
false If the store is not null

◆ section()

Config util::Config::section ( KeyType key) const
nodiscard

Interface for fetching a sub section by key.

Will attempt to fetch an entire section under the desired key and return it as a Config instance. If the section does not exist or another type is stored under the desired key - std::logic_error is thrown.

Parameters
keyThe key to check
Returns
Section represented as a separate instance of Config
Exceptions
std::logic_errorThrown if there is no section under the desired key or the key is of invalid format

◆ sectionOr()

Config util::Config::sectionOr ( KeyType key,
boost::json::object fallback ) const
nodiscard

Interface for fetching a sub section by key with a fallback object.

Will attempt to fetch an entire section under the desired key and return it as a Config instance. If the section does not exist or another type is stored under the desired key - fallback object is used instead.

Parameters
keyThe key to check
fallbackThe fallback object
Returns
Section represented as a separate instance of Config

◆ toMilliseconds()

std::chrono::milliseconds util::Config::toMilliseconds ( float value)
static

Method to convert a float seconds value to milliseconds.

Parameters
valueThe value to convert
Returns
The value in milliseconds

◆ value() [1/2]

template<typename Result >
Result util::Config::value ( ) const
inlinenodiscard

Interface for reading the value directly referred to by the instance.

See value(KeyType) const for how this works.

Returns
The value

◆ value() [2/2]

template<typename Result >
Result util::Config::value ( KeyType key) const
inlinenodiscard

Interface for fetching values by key.

Will attempt to fetch the value under the desired key. If the value exists and can be represented by the desired type Result then it will be returned. If the value exists but the conversion to Result is not possible OR the value does not exist - a logic_error will be thrown.

Template Parameters
ResultThe desired return type
Parameters
keyThe key to check
Returns
Value of desired type
Exceptions
std::logic_errorThrown if conversion to Result is not possible, value does not exist under specified key path or the key is of invalid format

◆ valueOr() [1/2]

template<typename Result >
Result util::Config::valueOr ( KeyType key,
Result fallback ) const
inlinenodiscard

Interface for fetching values by key with fallback.

Will attempt to fetch the value under the desired key. If the value exists and can be represented by the desired type Result then it will be returned. If the value exists but the conversion to Result is not possible - a logic_error will be thrown. If the value does not exist under the specified key - user specified fallback is returned.

Template Parameters
ResultThe desired return type
Parameters
keyThe key to check
fallbackThe fallback value
Returns
Value of desired type
Exceptions
std::logic_errorThrown if conversion to Result is not possible or the key is of invalid format

◆ valueOr() [2/2]

template<typename Result >
Result util::Config::valueOr ( Result fallback) const
inlinenodiscard

Interface for reading the value directly referred to by the instance with user-specified fallback.

See valueOr(KeyType, Result) const for how this works.

Parameters
fallbackThe fallback value
Returns
The value

◆ valueOrThrow() [1/2]

template<typename Result >
Result util::Config::valueOrThrow ( KeyType key,
std::string_view err ) const
inlinenodiscard

Interface for fetching values by key with custom error handling.

Will attempt to fetch the value under the desired key. If the value exists and can be represented by the desired type Result then it will be returned. If the value exists but the conversion to Result is not possible OR the value does not exist - a runtime_error will be thrown with the user specified message.

Template Parameters
ResultThe desired return type
Parameters
keyThe key to check
errThe custom error message
Returns
Value of desired type
Exceptions
std::runtime_errorThrown if conversion to Result is not possible or value does not exist under key

◆ valueOrThrow() [2/2]

template<typename Result >
Result util::Config::valueOrThrow ( std::string_view err) const
inlinenodiscard

Interface for reading the value directly referred to by the instance with user-specified error message.

See valueOrThrow(KeyType, std::string_view) const for how this works.

Parameters
errThe custom error message
Returns
The value

The documentation for this class was generated from the following files: