From b37dd68939daee1d3fbffbad669c8f93e6e4fd39 Mon Sep 17 00:00:00 2001 From: weiming wu Date: Thu, 10 Aug 2023 16:36:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0stream=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=92=8C?= =?UTF-8?q?example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/example.go | 2 +- go.mod | 1 + go.sum | 2 ++ plugin/model.go | 27 ++++++++------------------- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/example/example.go b/example/example.go index 4a9dc41..0af836f 100644 --- a/example/example.go +++ b/example/example.go @@ -34,7 +34,7 @@ func OnPluginMessageReceived(ctx context.Context, message *plugin.PluginMessage) if message.AbilityKey == "echo" { echoRequest := &EchoRequest{} //将数据转换成插件的请求参数 - err := message.ParseData(echoRequest) + err := message.ParseRequest(echoRequest) if err != nil { return nil, err } diff --git a/go.mod b/go.mod index b70cd2a..c9eb776 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index c3dad28..0d40687 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/plugin/model.go b/plugin/model.go index c4f30ac..a8970f6 100644 --- a/plugin/model.go +++ b/plugin/model.go @@ -1,9 +1,7 @@ package plugin import ( - "errors" - "fmt" - "reflect" + "encoding/json" ) type PluginMessage struct { @@ -15,23 +13,14 @@ type PluginMessage struct { } // 用于将数据转换成插件的请求参数 -func (req *PluginMessage) ParseData(model interface{}) (err error) { - defer func() { - if e := recover(); e != nil { - err = errors.New(fmt.Sprintf("parse data error: %v", e)) - } - }() - m, ok := req.Data.(map[string]interface{}) - if !ok { - return errors.New(fmt.Sprintf("invalid data: %v", req.Data)) +func (req *PluginMessage) ParseRequest(pluginRequest interface{}) error { + data, err := json.Marshal(req.Data) + if err != nil { + return err } - pValue := reflect.ValueOf(model).Elem() - pType := pValue.Type() - for i := 0; i < pType.NumField(); i++ { - field := pType.Field(i) - if value, ok := m[field.Name]; ok { - pValue.Field(i).Set(reflect.ValueOf(value)) - } + err = json.Unmarshal(data, pluginRequest) + if err != nil { + return err } return nil }