Ai
1 Star 0 Fork 0

Souki/go-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
feature_action.go 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
sage 提交于 2023-03-18 15:59 +08:00 . mdofiy
package ws
import (
"encoding/json"
"fmt"
)
//FeatureActionsMgr ws操作事件管理器
type FeatureActionsMgr struct {
actions map[string]IActionEvent
}
func NewFeatureActionsMgr(actions ...IActionEvent) *FeatureActionsMgr {
actionMap := make(map[string]IActionEvent)
for _, v := range actions {
actionMap[v.Name()] = v
}
return &FeatureActionsMgr{
actions: actionMap,
}
}
//Parse 解析数据,数据格式满足一下
func (mgr *FeatureActionsMgr) Parse(data []byte) (IActionEvent, error) {
m := make(map[string]interface{}, 1)
if err := json.Unmarshal(data, &m); err != nil {
return nil, err
}
//logger.Info("%s,%v", string(data), m)
if name, exist := m["action"]; exist {
return mgr.GetAction(name.(string))
}
return nil, fmt.Errorf("action not support")
}
//ParseDo 解析数据并且执行
func (mgr *FeatureActionsMgr) ParseDo(client *Client, data []byte) (IActionEvent, error) {
act, err := mgr.Parse(data)
if err != nil {
return nil, err
}
act.Do(client, data)
return act, nil
}
//GetAction 获取对应action
func (mgr *FeatureActionsMgr) GetAction(name string) (IActionEvent, error) {
if mgr.actions[name] == nil {
return nil, fmt.Errorf("action not support %s", name)
}
return mgr.actions[name], nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/scottq/go-framework.git
git@gitee.com:scottq/go-framework.git
scottq
go-framework
go-framework
v1.1.46

搜索帮助