增加stream插件消息处理逻辑和example

This commit is contained in:
weiming wu 2023-08-10 16:36:37 +08:00
parent 851bc54daf
commit b37dd68939
4 changed files with 12 additions and 20 deletions

View File

@ -34,7 +34,7 @@ func OnPluginMessageReceived(ctx context.Context, message *plugin.PluginMessage)
if message.AbilityKey == "echo" { if message.AbilityKey == "echo" {
echoRequest := &EchoRequest{} echoRequest := &EchoRequest{}
//将数据转换成插件的请求参数 //将数据转换成插件的请求参数
err := message.ParseData(echoRequest) err := message.ParseRequest(echoRequest)
if err != nil { if err != nil {
return nil, err return nil, err
} }

1
go.mod
View File

@ -10,6 +10,7 @@ require (
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect 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 github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

2
go.sum
View File

@ -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/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 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

View File

@ -1,9 +1,7 @@
package plugin package plugin
import ( import (
"errors" "encoding/json"
"fmt"
"reflect"
) )
type PluginMessage struct { type PluginMessage struct {
@ -15,23 +13,14 @@ type PluginMessage struct {
} }
// 用于将数据转换成插件的请求参数 // 用于将数据转换成插件的请求参数
func (req *PluginMessage) ParseData(model interface{}) (err error) { func (req *PluginMessage) ParseRequest(pluginRequest interface{}) error {
defer func() { data, err := json.Marshal(req.Data)
if e := recover(); e != nil { if err != nil {
err = errors.New(fmt.Sprintf("parse data error: %v", e)) return err
}
}()
m, ok := req.Data.(map[string]interface{})
if !ok {
return errors.New(fmt.Sprintf("invalid data: %v", req.Data))
}
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 return nil
} }