From d77bb0a4cb947cc6c10a4924f81557bf8c2b4959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9A=E5=90=83=E7=82=B9=E8=8B=B9=E6=9E=9C?= <73388495+eatmoreapple@users.noreply.github.com> Date: Thu, 2 Feb 2023 00:15:46 +0800 Subject: [PATCH] =?UTF-8?q?[feat]:=20=E6=94=AF=E6=8C=81=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E7=83=AD=E5=AD=98=E5=82=A8=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=9A=84=E5=BA=8F=E5=88=97=E5=8C=96=E5=92=8C=E5=8F=8D?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=20(#222)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.go | 19 ++++++++++++------- serializer.go | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 serializer.go diff --git a/bot.go b/bot.go index 59c633b..39438c6 100644 --- a/bot.go +++ b/bot.go @@ -2,7 +2,6 @@ package openwechat import ( "context" - "encoding/json" "errors" "io" "log" @@ -20,13 +19,14 @@ type Bot struct { SyncCheckCallback func(resp SyncCheckResponse) // 心跳回调 MessageHandler MessageHandler // 获取消息成功的handle MessageErrorHandler func(err error) bool // 获取消息发生错误的handle, 返回true则尝试继续监听 + Serializer Serializer // 序列化器, 默认为json + Storage *Storage + Caller *Caller once sync.Once err error context context.Context cancel context.CancelFunc - Caller *Caller self *Self - Storage *Storage hotReloadStorage HotReloadStorage uuid string loginUUID *string @@ -296,7 +296,7 @@ func (b *Bot) DumpTo(writer io.Writer) error { WechatDomain: b.Caller.Client.Domain, UUID: b.uuid, } - return json.NewEncoder(writer).Encode(item) + return b.Serializer.Encode(writer, item) } // IsHot returns true if is hot login otherwise false @@ -328,8 +328,7 @@ func (b *Bot) reload() error { return errors.New("hotReloadStorage is nil") } var item HotReloadStorageItem - err := json.NewDecoder(b.hotReloadStorage).Decode(&item) - if err != nil { + if err := b.Serializer.Decode(b.hotReloadStorage, &item); err != nil { return err } b.Caller.Client.SetCookieJar(item.Jar) @@ -347,7 +346,13 @@ func NewBot(c context.Context) *Bot { // 默认行为为网页版微信模式 caller.Client.SetMode(normal) ctx, cancel := context.WithCancel(c) - return &Bot{Caller: caller, Storage: &Storage{}, context: ctx, cancel: cancel} + return &Bot{ + Caller: caller, + Storage: &Storage{}, + Serializer: &JsonSerializer{}, + context: ctx, + cancel: cancel, + } } // DefaultBot 默认的Bot的构造方法, diff --git a/serializer.go b/serializer.go new file mode 100644 index 0000000..46c8bfb --- /dev/null +++ b/serializer.go @@ -0,0 +1,25 @@ +package openwechat + +import ( + "encoding/json" + "io" +) + +// Serializer is an interface for encoding and decoding data. +type Serializer interface { + Encode(writer io.Writer, v interface{}) error + Decode(reader io.Reader, v interface{}) error +} + +// JsonSerializer is a serializer for json. +type JsonSerializer struct{} + +// Encode encodes v to writer. +func (j JsonSerializer) Encode(writer io.Writer, v interface{}) error { + return json.NewEncoder(writer).Encode(v) +} + +// Decode decodes data from reader to v. +func (j JsonSerializer) Decode(reader io.Reader, v interface{}) error { + return json.NewDecoder(reader).Decode(v) +}