summaryrefslogtreecommitdiff
path: root/modules/rpc/jsonrpc.cpp
diff options
context:
space:
mode:
authorSadie Powell <sadie@witchery.services>2025-02-20 12:33:59 +0000
committerSadie Powell <sadie@witchery.services>2025-02-20 12:53:15 +0000
commitb498f4f4d91ff09120bfce6a10f7362fc40a011a (patch)
treeab773a5316a1d33c291441e8f882930b37222d1b /modules/rpc/jsonrpc.cpp
parent8330cd119ac390036cb5cadd51019e6b496c1bfc (diff)
Add support for more RPC data types.
Diffstat (limited to 'modules/rpc/jsonrpc.cpp')
-rw-r--r--modules/rpc/jsonrpc.cpp54
1 files changed, 47 insertions, 7 deletions
diff --git a/modules/rpc/jsonrpc.cpp b/modules/rpc/jsonrpc.cpp
index cd24dc55c..1442115b7 100644
--- a/modules/rpc/jsonrpc.cpp
+++ b/modules/rpc/jsonrpc.cpp
@@ -12,6 +12,9 @@
#include "yyjson/yyjson.c"
+template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
+template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
+
inline Anope::string yyjson_get_astr(yyjson_val *val, const char *key)
{
const auto *str = yyjson_get_str(yyjson_obj_get(val, key));
@@ -25,7 +28,7 @@ class MyJSONRPCServiceInterface final
private:
Anope::map<RPCEvent *> events;
- void SendError(HTTPReply &reply, int64_t code, const Anope::string &message, const Anope::string &id)
+ static void SendError(HTTPReply &reply, int64_t code, const Anope::string &message, const Anope::string &id)
{
Log(LOG_DEBUG) << "JSON-RPC error " << code << ": " << message;
@@ -56,6 +59,48 @@ private:
yyjson_mut_doc_free(doc);
}
+ static void SerializeObject(yyjson_mut_doc *doc, yyjson_mut_val *root, const char *key, const RPCBlock &block)
+ {
+ auto *result = yyjson_mut_obj(doc);
+ for (const auto &reply : block.GetReplies())
+ {
+ // Captured structured bindings are a C++20 extension.
+ const auto &k = reply.first;
+ std::visit(overloaded
+ {
+ [&doc, &result, &k](const RPCBlock &b)
+ {
+ SerializeObject(doc, result, k.c_str(), b);
+ },
+ [&doc, &result, &k](const Anope::string &s)
+ {
+ yyjson_mut_obj_add_strn(doc, result, k.c_str(), s.c_str(), s.length());
+ },
+ [&doc, &result, &k](std::nullptr_t)
+ {
+ yyjson_mut_obj_add_null(doc, result, k.c_str());
+ },
+ [&doc, &result, &k](bool b)
+ {
+ yyjson_mut_obj_add_bool(doc, result, k.c_str(), b);
+ },
+ [&doc, &result, &k](double d)
+ {
+ yyjson_mut_obj_add_real(doc, result, k.c_str(), d);
+ },
+ [&doc, &result, &k](int64_t i)
+ {
+ yyjson_mut_obj_add_int(doc, result, k.c_str(), i);
+ },
+ [&doc, &result, &k](uint64_t u)
+ {
+ yyjson_mut_obj_add_uint(doc, result, k.c_str(), u);
+ },
+ }, reply.second);
+ }
+ yyjson_mut_obj_add_val(doc, root, key, result);
+ }
+
public:
MyJSONRPCServiceInterface(Module *creator, const Anope::string &sname)
: RPCServiceInterface(creator, sname)
@@ -149,12 +194,7 @@ public:
yyjson_mut_obj_add_strn(doc, root, "id", request.id.c_str(), request.id.length());
if (!request.GetReplies().empty())
- {
- auto *result = yyjson_mut_obj(doc);
- for (const auto &[k, v] : request.GetReplies())
- yyjson_mut_obj_add_strn(doc, result, k.c_str(), v.c_str(), v.length());
- yyjson_mut_obj_add_val(doc, root, "result", result);
- }
+ SerializeObject(doc, root, "result", request);
yyjson_mut_obj_add_str(doc, root, "jsonrpc", "2.0");