59 #include <fmt/format.h>
79 explicit Conf(
Json data) : data_(std::move(data)) {}
81 Conf(
Json data, std::string
file) : file_(std::move(
file)), data_(std::move(data)) {}
83 : file_(std::move(
file)), root_(std::move(
root)), data_(std::move(data)) {}
99 [[nodiscard]]
const std::string&
file()
const {
return file_; }
104 [[nodiscard]]
bool is_empty()
const {
return data_.is_null(); }
125 [[nodiscard]] std::string
root()
const {
return (root_.empty() ?
"/" : root_); }
132 [[nodiscard]]
bool has(
const std::string& key)
const {
return data_.count(key) != 0; }
134 [[nodiscard]]
bool has_pointer(
const std::string& key)
const {
return has(
JsonPointer(key)); }
141 [[nodiscard]] Conf
at(
const std::string& key)
const;
142 [[nodiscard]] Conf
at(
const JsonPointer& key)
const;
143 [[nodiscard]] Conf at_pointer(
const std::string& key)
const {
return at(JsonPointer(key)); }
149 size_t erase(
const std::string& key);
150 size_t erase(
const JsonPointer& key);
151 size_t erase_pointer(
const std::string& key) {
return erase(
JsonPointer(key)); }
158 [[nodiscard]] std::vector<Conf>
to_array()
const;
165 template <
typename T>
166 [[nodiscard]] T
get()
const {
168 return data_.get<T>();
169 }
catch (nlohmann::detail::type_error&) {
179 template <
typename T>
180 [[nodiscard]] T
get(
const std::string& key)
const {
182 return data_.at(key).get<T>();
183 }
catch (nlohmann::detail::out_of_range&) {
185 }
catch (nlohmann::detail::type_error&) {
186 throw_wrong_type(key);
190 template <
typename T>
193 return data_.at(key).get<T>();
194 }
catch (nlohmann::detail::out_of_range&) {
195 throw_missing(key.to_string());
196 }
catch (nlohmann::detail::type_error&) {
197 throw_wrong_type(key.to_string());
201 template <
typename T>
202 [[nodiscard]] T get_pointer(
const std::string& key)
const {
210 template <
typename T>
211 [[nodiscard]] T
get_or(
const std::string& key, T def)
const {
212 if (!data_.count(key)) {
216 return data_.at(key).get<T>();
217 }
catch (nlohmann::detail::type_error&) {
218 throw_wrong_type(key);
222 template <
typename T>
225 return data_.at(key).get<T>();
226 }
catch (nlohmann::detail::out_of_range&) {
228 }
catch (nlohmann::detail::type_error&) {
229 throw_wrong_type(key.to_string());
233 template <
typename T>
234 [[nodiscard]] T get_pointer_or(
const std::string& key, T def)
const {
244 template <
typename T>
245 void with(
const std::string& key, std::function<
void(
const T&)> fn)
const {
246 if (data_.count(key)) {
251 template <
typename T>
252 void with(
const JsonPointer& key, std::function<
void(
const T&)> fn)
const {
255 }
catch (nlohmann::detail::out_of_range&) {
260 template <
typename T>
261 void with_pointer(
const std::string& key, std::function<
void(
const T&)> fn)
const {
270 template <
typename T>
271 void try_from(
const std::string& key, T& val)
const {
272 if (data_.count(key)) {
277 template <
typename T>
280 val = data_.at(key).get<T>();
281 }
catch (nlohmann::detail::out_of_range& e) {
283 }
catch (nlohmann::detail::type_error& e) {
284 throw_wrong_type(key.to_string());
288 template <
typename T>
289 void try_from_pointer(
const std::string& key, T& val)
const {
296 void assert_has(
const std::string& key)
const;
297 void assert_has(
const JsonPointer& key)
const;
306 void assert_has_pointer_type(
const std::string& key, JsonType t)
const {
314 void assert_has_not(
const std::string& key,
const std::string& msg =
"")
const;
315 void assert_has_not(
const JsonPointer& key,
const std::string& msg =
"")
const;
316 void assert_has_pointer_not(
const std::string& key,
const std::string& msg =
"")
const {
330 [[nodiscard]] std::filesystem::path
resolve_file(
const std::filesystem::path& filename)
const;
331 [[nodiscard]] std::string
resolve_file(
const std::string& filename)
const;
333 template <
typename... Args>
334 [[noreturn]]
void throw_error(std::string_view format, Args&&... args)
const {
335 throw_error(fmt::format(fmt::runtime(format), std::forward<Args>(args)...));
337 [[noreturn]]
void throw_error(
const std::string& msg)
const;
338 [[noreturn]]
void throw_unexpected(
const std::string& key,
const std::string& msg =
"")
const;
339 [[noreturn]]
void throw_missing(
const std::string& key)
const;
340 [[noreturn]]
void throw_wrong_type(
const std::string& key =
"")
const;
341 [[noreturn]]
void throw_wrong_type(
const std::string& key, JsonType type)
const;
343 friend void to_json(Json& j,
const Conf& c) { j = *c; }
344 friend void from_json(
const Json& j, Conf& c) { *c = j; }
bool has(const std::string &key) const
Definition: conf.hpp:132
T get_or(const std::string &key, T def) const
Definition: conf.hpp:211
void with(const std::string &key, std::function< void(const T &)> fn) const
Definition: conf.hpp:245
const Json * operator->() const
Definition: conf.hpp:119
std::filesystem::path resolve_file(const std::filesystem::path &filename) const
Definition: conf.cpp:151
std::string root() const
Definition: conf.hpp:125
void assert_has_type(const std::string &key, JsonType t) const
Definition: conf.cpp:137
void assert_has(const std::string &key) const
Definition: conf.cpp:109
void try_from(const std::string &key, T &val) const
Definition: conf.hpp:271
const std::string & file() const
Definition: conf.hpp:99
const Json & operator*() const
Definition: conf.hpp:111
size_t erase(const std::string &key)
Definition: conf.cpp:74
bool is_from_file() const
Definition: conf.hpp:94
Conf at(const std::string &key) const
Definition: conf.cpp:58
void assert_has_not(const std::string &key, const std::string &msg="") const
Definition: conf.cpp:121
T get() const
Definition: conf.hpp:166
bool is_empty() const
Definition: conf.hpp:104
std::vector< Conf > to_array() const
Definition: conf.cpp:92
T get(const std::string &key) const
Definition: conf.hpp:180
nlohmann::json Json
Definition: fable_fwd.hpp:35
nlohmann::json_pointer< std::string > JsonPointer
Definition: fable_fwd.hpp:36