Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: refactor json.h out of include from discordevents.h #926

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions include/dpp/discordevents.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#include <dpp/export.h>
#include <dpp/json_fwd.h>
#include <dpp/json.h>
#include <dpp/json_interface.h>

namespace dpp {
Expand All @@ -48,21 +47,25 @@ void DPP_EXPORT set_snowflake_not_null(const nlohmann::json* j, const char *keyn
*/
void DPP_EXPORT set_snowflake_array_not_null(const nlohmann::json* j, const char *keyname, std::vector<class snowflake> &v);

/**
* @brief Applies a function to each element of a json array.
* @param j nlohmann::json instance to retrieve value from
* @param key key name to check for the values
* @param fn function to apply to each element
*/
void DPP_EXPORT for_each_json(nlohmann::json* parent, std::string_view key, std::function<void(const nlohmann::json*)> fn);

/** @brief Sets an array of objects from a json field value, if defined, else does nothing
* @tparam T The class of which the array consists of. Must be derived from dpp::json_interface
* @param j nlohmann::json instance to retrieve value from
* @param keyname key name to check for the values
* @param v Value to change
*/
template<class T> std::enable_if_t<std::is_base_of_v<json_interface<T>, T>, void> set_object_array_not_null(nlohmann::json* j, const char *keyname, std::vector<T> &v) {
template<class T> void set_object_array_not_null(nlohmann::json* j, std::string_view key, std::vector<T>& v) {
v.clear();
auto k = j->find(keyname);
if (k != j->end() && !k->is_null()) {
v.reserve(j->at(keyname).size());
for (auto &obj : j->at(keyname)) {
v.emplace_back(T().fill_from_json(&obj));
}
}
for_each_json(j, key, [&v](nlohmann::json* elem) {
v.push_back(T{}.fill_from_json(elem));
});
}

/** @brief Returns a string from a json field value, if defined, else returns an empty string.
Expand Down
9 changes: 9 additions & 0 deletions src/dpp/discordevents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ void set_snowflake_array_not_null(const json* j, const char *keyname, std::vecto
}
}

void for_each_json(const nlohmann::json* parent, const char* key, std::function<void(const nlohmann::json*)> fn) {
auto it = parent->find(key);
if (it == parent->end() || it->is_null()) {
return;
}
for (const nlohmann::json &elem : *parent) {
fn(&elem);
}
}

std::string string_not_null(const json* j, const char *keyname) {
/* Returns empty string if the value is not a string, or is null or not defined */
Expand Down
Loading