关于在写onebot12协议java-sdk的一些想法 #247
eiriksgata
started this conversation in
想法 / 建议
Replies: 2 comments
-
|
而他的实体类语法类型自然也就变的优雅: public class Event<T> {
private String id;
private Long time;
private String type;
private String detail_type;
private String sub_type;
private Self self;
private T content;
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
元事件-connect
{
"id": "b6e65187-5ac0-489c-b431-53078e9d2bbb",
"time": 1632847927.599013,
"type": "meta",
"detail_type": "connect",
"sub_type": "",
"version": {
"impl": "go-onebot-qq",
"version": "1.2.0",
"onebot_version": "12"
}
}元事件-heartbeat {
"id": "b6e65187-5ac0-489c-b431-53078e9d2bbb",
"time": 1632847927.599013,
"type": "meta",
"detail_type": "heartbeat",
"sub_type": "",
"interval": 5000
}从上述两个例子中可以看出,决定属性的,是由 type 以及 detail_type来决定的。 {
"id": "b6e65187-5ac0-489c-b431-53078e9d2bbb",
"time": 1632847927.599013,
"type": "meta-connect",
"sub_type": "",
"version": {
"impl": "go-onebot-qq",
"version": "1.2.0",
"onebot_version": "12"
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
讨论1: 关于事件的数据拓展字段。
我们都知道在12标准协议中,必填字段有 id、time、type、detail_type、sub_type ,其余如果想要进行拓展,那么其拓展的字段是位于 当前 Event 所在的并级下,我认为这是一种不合理的设计。
如文档中给出的例子:
{ "id": "b6e65187-5ac0-489c-b431-53078e9d2bbb", "self": { "platform": "qq", "user_id": "123234" }, "time": 1632847927.599013, "type": "message", "detail_type": "private", "sub_type": "", "message_id": "6283", "message": [ { "type": "text", "data": { "text": "OneBot is not a bot" } } ], "alt_message": "OneBot is not a bot", "user_id": "123456788", "qq.nickname": "海阔天空" }根据例子来看,其事件是往机器人平台发送一条私聊消息,其中包含 message、 user_id 等相关字段,这些字段都是位于 Event 所在的当前层级,对于常规的弱语言(形如脚本语言)来说,json增加字段确实没什么问题,但是对于强语言类型来说,我们必然是要定义一个Class 来 实现实体类,或者是 ts 定义一个 type | interface 来增强语言类型。
想message、user_id 这种拓展字段 ,本身应该是填写在 Event 所在的 某个字段下级,例如下面例子:
{ "id": "b6e65187-5ac0-489c-b431-53078e9d2bbb", "self": { "platform": "qq", "user_id": "123234" }, "time": 1632847927.599013, "type": "message", "detail_type": "private", "sub_type": "", "content": { "message_id": "6283", "message": [ { "type": "text", "data": { "text": "OneBot is not a bot" } } ], "alt_message": "OneBot is not a bot", "user_id": "123456788", "qq.nickname": "海阔天空" } }上述例子中 将 message 、user_id等数据 ,放入 content 中, 随着你 type 不同而 对 content 中的字段而不同。
因此我们在写 实体类时,可以采用 泛型 的形式对 json 进行轻松的反序列化,同时也增强了语言的可读性。
Beta Was this translation helpful? Give feedback.
All reactions