$darkmode
#include <conf.hpp>
Public Member Functions | |
| Conf (Json data) | |
| Conf (std::string file) | |
| Conf (Json data, std::string file) | |
| Conf (Json data, std::string file, std::string root) | |
| bool | is_from_file () const |
| const std::string & | file () const |
| bool | is_empty () const |
| const Json & | operator* () const |
| Json & | operator* () |
| const Json * | operator-> () const |
| Json * | operator-> () |
| std::string | root () const |
| bool | has (const std::string &key) const |
| bool | has (const JsonPointer &ptr) const |
| bool | has_pointer (const std::string &ptr) const |
| Conf | at (const std::string &key) const |
| Conf | at (const JsonPointer &ptr) const |
| Conf | at_pointer (const std::string &ptr) const |
| size_t | erase (const std::string &key) |
| size_t | erase (const JsonPointer &ptr, bool preserve_empty=false) |
| size_t | erase_pointer (const std::string &ptr, bool preserve_empty=false) |
| std::vector< Conf > | to_array () const |
| template<typename T > | |
| T | get () const |
| template<typename T > | |
| T | get (const std::string &key) const |
| template<typename T > | |
| T | get (const JsonPointer &ptr) const |
| template<typename T > | |
| T | get_pointer (const std::string &ptr) const |
| template<typename T > | |
| T | get_or (const std::string &key, T def) const |
| template<typename T > | |
| T | get_or (const JsonPointer &ptr, T def) const |
| template<typename T > | |
| T | get_pointer_or (const std::string &ptr, T def) const |
| template<typename T > | |
| void | with (const std::string &key, std::function< void(const T &)> fn) const |
| template<typename T > | |
| void | with (const JsonPointer &ptr, std::function< void(const T &)> fn) const |
| template<typename T > | |
| void | with_pointer (const std::string &ptr, std::function< void(const T &)> fn) const |
| template<typename T > | |
| void | try_from (const std::string &key, T &val) const |
| template<typename T > | |
| void | try_from (const JsonPointer &ptr, T &val) const |
| template<typename T > | |
| void | try_from_pointer (const std::string &ptr, T &val) const |
| void | assert_has (const std::string &key) const |
| void | assert_has (const JsonPointer &ptr) const |
| void | assert_has_pointer (const std::string &key) const |
| void | assert_has_type (const std::string &key, JsonType t) const |
| void | assert_has_type (const JsonPointer &ptr, JsonType t) const |
| void | assert_has_pointer_type (const std::string &ptr, JsonType t) const |
| void | assert_has_not (const std::string &key, const std::string &msg="") const |
| void | assert_has_not (const JsonPointer &ptr, const std::string &msg="") const |
| void | assert_has_pointer_not (const std::string &ptr, const std::string &msg="") const |
| std::filesystem::path | resolve_file (const std::filesystem::path &filename) const |
| std::string | resolve_file (const std::string &filename) const |
| template<typename... Args> | |
| void | throw_error (std::string_view format, Args &&... args) const |
| void | throw_error (const std::string &msg) const |
| void | throw_unexpected (const std::string &key, const std::string &msg="") const |
| void | throw_unexpected (const JsonPointer &ptr, const std::string &msg="") const |
| void | throw_missing (const std::string &key) const |
| void | throw_missing (const JsonPointer &ptr) const |
| void | throw_wrong_type (const std::string &key="") const |
| void | throw_wrong_type (const JsonPointer &ptr) const |
| void | throw_wrong_type (const std::string &key, JsonType expected) const |
| void | throw_wrong_type (const JsonPointer &ptr, JsonType expected) const |
Friends | |
| void | to_json (Json &j, const Conf &c) |
| void | from_json (const Json &j, Conf &c) |
Conf wraps a JSON with context that allows for more user-friendly errors.
In general, a Conf wraps JSON content that comes from the filesystem and is used for configuration purposes. Even if a class requires only a subset of the JSON, Conf maintains the connection to the original file, which allows errors to be returned that refer to the file and location.
Conf is best used together with the Confable and Schema types.
This class provides many methods that take JSON pointers, either as strings (these methods have pointer in the name to differentiate them from the plain versions) or as JsonPointer.
For more information, see: https://datatracker.ietf.org/doc/html/rfc6901
| void fable::Conf::assert_has | ( | const JsonPointer & | ptr | ) | const |
| void fable::Conf::assert_has | ( | const std::string & | key | ) | const |
| void fable::Conf::assert_has_not | ( | const JsonPointer & | ptr, |
| const std::string & | msg = "" |
||
| ) | const |
| void fable::Conf::assert_has_not | ( | const std::string & | key, |
| const std::string & | msg = "" |
||
| ) | const |
Assert that the field key is not available, throw a ConfError otherwise.
| key | target field to check for non-existence |
| msg | optional custom error message (use to explain what AND why) |
|
inline |
|
inline |
Assert that the field given by the JSON pointer is not available, throw a ConfError otherwise.
| ptr | JSON pointer to target field to check for non-existence |
| msg | optional custom error message (use to explain what AND why) |
| ConfError | if field exists |
|
inline |
Assert that the pointer ptr exists and resolves to the type t, throw a ConfError otherwise.
| ptr | JSON pointer to target field, e.g. "/foo/bar" |
| t | type target value should have |
| ConfError | if field non-existent or is wrong type |
| Json::parse_error | if ptr is not a valid JSON pointer |
| void fable::Conf::assert_has_type | ( | const JsonPointer & | ptr, |
| JsonType | t | ||
| ) | const |
| void fable::Conf::assert_has_type | ( | const std::string & | key, |
| JsonType | t | ||
| ) | const |
| Conf fable::Conf::at | ( | const JsonPointer & | ptr | ) | const |
Return a new Conf basing off the JSON referred to by the JSON pointer.
The root of the new Conf will contain the current root + ptr.
| ptr | JSON pointer to target field, e.g. "/foo/bar" |
| ConfError | if the ptr cannot be resolved |
| Conf fable::Conf::at | ( | const std::string & | key | ) | const |
Return a new Conf basing off the JSON at the target field.
The root of the new Conf will contain the current root + key.
| key | target field to access |
| ConfError | if the key cannot be found |
|
inline |
Return a new Conf basing off the JSON referred to by the JSON pointer.
The root of the new Conf will contain the current root + ptr.
| ptr | JSON pointer to target field, e.g. "/foo/bar" |
| ConfError | if the ptr cannot be resolved |
| Json::parse_error | if ptr is not a valid JSON pointer |
| size_t fable::Conf::erase | ( | const JsonPointer & | ptr, |
| bool | preserve_empty = false |
||
| ) |
Erase a field from the Conf if it exists and return number of elements removed.
When removing the last element in an object, erase will by default then remove the whole object. This can lead to a cascade, resulting in multiple elements being removed. You can inhibit this behavior by passing true to the preserve_empty parameter.
Notes:
| ptr | JSON pointer to target field to remove |
| preserve_empty | whether to preserve empty objects |
| size_t fable::Conf::erase | ( | const std::string & | key | ) |
Erase a field from the Conf if it exists and return 1 or 0.
Notes:
| key | target field to erase |
|
inline |
Erase a field from the Conf if it exists and return number of elements removed.
When removing the last element in an object, erase will by default then remove the whole object. This can lead to a cascade, resulting in multiple elements being removed. You can inhibit this behavior by passing true to the preserve_empty parameter.
Notes:
| ptr | JSON pointer to target field to remove |
| preserve_empty | whether to preserve empty objects |
| Json::parse_error | if ptr is not a valid JSON pointer |
|
inline |
Return the file associated with this configuration.
|
inline |
Return a value of type T.
| ConfError | if the key is of the wrong type |
|
inline |
Return a value of type T at the pointer ptr.
| ptr | JSON pointer to target field to get |
| ConfError | if the ptr cannot be found or is wrong type |
|
inline |
Return a value of type T at the position key.
| key | target field to get |
| ConfError | if the key cannot be found or is wrong type |
|
inline |
Return a value of type T at the pointer ptr, returning def if the field cannot be found.
| ptr | JSON pointer to target field to get |
| def | default value if target field does not exist |
| ConfError | if target Json is of wrong type |
|
inline |
Return a value of type T for the field key, returning def if the field cannot be found.
| key | target field to get |
| def | default value if target field does not exist |
| ConfError | if target Json is of wrong type |
|
inline |
Return a value of type T at the pointer ptr.
| ptr | JSON pointer to target field to get |
| ConfError | if the ptr cannot be found or is wrong type |
| Json::parse_error | if ptr is not a valid JSON pointer |
|
inline |
Return a value of type T at the pointer ptr, returning def if the field cannot be found.
| ptr | JSON pointer to target field to get |
| def | default value if target field does not exist |
| ConfError | if target Json is of wrong type |
| Json::parse_error | if ptr is not a valid JSON pointer |
| bool fable::Conf::has | ( | const JsonPointer & | ptr | ) | const |
Return whether the field referred to by JSON pointer is present.
Notes:
| ptr | JSON pointer to target field to check for existence |
|
inline |
Return whether the field is present.
Notes:
| key | target field to check for existence |
|
inline |
Return whether the field referred to by JSON pointer is present.
Notes:
| ptr | JSON pointer to target field to check for existence |
| Json::parse_error | if ptr is not a valid JSON pointer |
|
inline |
Return whether this configuration is empty.
|
inline |
Return whether this configuration was read from a file.
|
inline |
Return a reference to the JSON.
This allows operations to be performed on the underlying Json type.
|
inline |
Return a reference to the JSON.
This allows operations to be performed on the underlying Json type.
|
inline |
Return a pointer to the JSON.
This allows operations to be performed with the underlying Json type.
|
inline |
Return a pointer to the JSON.
This allows operations to be performed with the underlying Json type.
| std::filesystem::path fable::Conf::resolve_file | ( | const std::filesystem::path & | filename | ) | const |
Resolve a path to an absolute path by taking the configuration file into account.
| std::string fable::Conf::resolve_file | ( | const std::string & | filename | ) | const |
Resolve a path to an absolute path by taking the configuration file into account.
|
inline |
Return the root of the current JSON formatted as a JSON pointer.
When a Conf is initially created, it has a root of '/'. When at() is used to index into the Conf, a new Conf is returned with the root of that new object set to / plus the field. So if the root is '/foo', and you use at("bar"), then the new root is /foo/bar.
This is primarily used for creating better error messages when something goes wrong.
| void fable::Conf::throw_error | ( | const std::string & | msg | ) | const |
|
inline |
| void fable::Conf::throw_missing | ( | const JsonPointer & | ptr | ) | const |
| void fable::Conf::throw_missing | ( | const std::string & | key | ) | const |
| void fable::Conf::throw_unexpected | ( | const JsonPointer & | ptr, |
| const std::string & | msg = "" |
||
| ) | const |
| void fable::Conf::throw_unexpected | ( | const std::string & | key, |
| const std::string & | msg = "" |
||
| ) | const |
| void fable::Conf::throw_wrong_type | ( | const JsonPointer & | ptr | ) | const |
| void fable::Conf::throw_wrong_type | ( | const JsonPointer & | ptr, |
| JsonType | expected | ||
| ) | const |
| void fable::Conf::throw_wrong_type | ( | const std::string & | key, |
| JsonType | expected | ||
| ) | const |
| void fable::Conf::throw_wrong_type | ( | const std::string & | key = "" | ) | const |
| std::vector< Conf > fable::Conf::to_array | ( | ) | const |
Return an array of Conf values.
This is useful when you want to iterate through individual Conf values (for better error reporting) instead of Json values.
Example:
for (auto&& c : conf.to_array()) {
// do something with c
}
| ConfError | if the object is not an array |
|
inline |
Write the value at the pointer key to val, if the key can be found.
If the field cannot be found, the value is not modified.
| [in] | ptr | JSON pointer to target field to get data from |
| [out] | val | target variable to write value to |
| ConfError | if the value is of the wrong type |
|
inline |
Write the value at the field key to val, if the key can be found.
If the field cannot be found, the value is not modified.
| [in] | key | target field to get data from |
| [out] | val | target variable to write value to |
| ConfError | if the value is of the wrong type |
|
inline |
Write the value at the pointer key to val, if the key can be found.
If the field cannot be found, the value is not modified.
| [in] | ptr | JSON pointer to target field to get data from |
| [out] | val | target variable to write value to |
| a | ConfError if the value is of the wrong type |
| Json::parse_error | if ptr is not a valid JSON pointer |
|
inline |
Perform the function on the value of the pointer ptr, if the field can be found; otherwise the function is not executed.
| ConfError | if the value is of the wrong type |
|
inline |
Perform the function on the value of the field key, if the field can be found; otherwise the function is not executed.
| ConfError | if the value is of the wrong type |
|
inline |
Perform the function on the value of the pointer ptr, if the field can be found; otherwise the function is not executed.
| ConfError | if the value is of the wrong type |
| Json::parse_error | if ptr is not a valid JSON pointer |
Conversion function for automatic "deserialization" of Json in Conf.
This fulfills the interface provided by nlohmann::json.
Conversion function for automatic "serialization" of Conf in Json.
This fulfills the interface provided by nlohmann::json.