summaryrefslogtreecommitdiff
path: root/include/modules
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 /include/modules
parent8330cd119ac390036cb5cadd51019e6b496c1bfc (diff)
Add support for more RPC data types.
Diffstat (limited to 'include/modules')
-rw-r--r--include/modules/rpc.h69
1 files changed, 60 insertions, 9 deletions
diff --git a/include/modules/rpc.h b/include/modules/rpc.h
index 092f98f59..35db305c7 100644
--- a/include/modules/rpc.h
+++ b/include/modules/rpc.h
@@ -10,11 +10,69 @@
#include "httpd.h"
+#include <variant>
+
+class RPCBlock
+{
+public:
+ /** Represents possible types of RPC value. */
+ using RPCValue = std::variant<RPCBlock, Anope::string, std::nullptr_t, bool, double, int64_t, uint64_t>;
+
+ /** Retrieves the list of RPC replies. */
+ inline const auto &GetReplies() const { return this->replies; }
+
+ template <typename Stringable>
+ inline RPCBlock &Reply(const Anope::string &key, const Stringable &value)
+ {
+ this->replies.emplace(key, Anope::ToString(value));
+ return *this;
+ }
+
+ inline RPCBlock &ReplyBlock(const Anope::string &key)
+ {
+ auto it = this->replies.emplace(key, RPCBlock());
+ return std::get<RPCBlock>(it.first->second);
+ }
+
+ inline RPCBlock &ReplyBool(const Anope::string &key, bool value)
+ {
+ this->replies.emplace(key, value);
+ return *this;
+ }
+
+ inline RPCBlock &ReplyFloat(const Anope::string &key, double value)
+ {
+ this->replies.emplace(key, value);
+ return *this;
+ }
+
+ inline RPCBlock &ReplyInt(const Anope::string &key, int64_t value)
+ {
+ this->replies.emplace(key, value);
+ return *this;
+ }
+
+ inline RPCBlock &ReplyNull(const Anope::string &key)
+ {
+ this->replies.emplace(key, nullptr);
+ return *this;
+ }
+
+ inline RPCBlock &ReplyUInt(const Anope::string &key, uint64_t value)
+ {
+ this->replies.emplace(key, value);
+ return *this;
+ }
+
+private:
+ Anope::map<RPCValue> replies;
+};
+
class RPCRequest final
+ : public RPCBlock
{
private:
std::optional<std::pair<int64_t, Anope::string>> error;
- std::map<Anope::string, Anope::string> replies;
public:
Anope::string name;
@@ -32,14 +90,7 @@ public:
this->error.emplace(errcode, errstr);
}
- inline void Reply(const Anope::string &dname, const Anope::string &ddata)
- {
- this->replies.emplace(dname, ddata);
- }
-
- inline const auto &GetError() { return this->error; }
-
- inline const auto &GetReplies() { return this->replies; }
+ inline const auto &GetError() const { return this->error; }
};
class RPCServiceInterface;