Ai
1 Star 0 Fork 0

Coder/gox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
api.go 1.26 KB
一键复制 编辑 原始数据 按行查看 历史
Coder 提交于 2025-04-24 15:05 +08:00 . fix
package message
import (
"encoding/json"
"errors"
"fmt"
"google.golang.org/protobuf/proto"
)
func Unmarshal(msgType MessageType, b []byte, v any) error {
if msgType == MessageTypePB { // ProtoBuffer解码(二进制->消息)
pm, ok := v.(proto.Message)
if !ok {
return errors.New("param:%v is not proto.Message while protoType is ProtoTypePB")
}
err := proto.Unmarshal(b, pm)
if err != nil {
return fmt.Errorf("param:%v can not unmarshal to proto.Message while protoType is ProtoTypePB:%v", b, err)
}
return nil
} else if msgType == MessageTypeJson { // json解码(二进制->消息)
err := json.Unmarshal(b, v)
if err != nil {
return fmt.Errorf("param:%v can not unmarshal to json while protoType is ProtoTypeJson:%v", b, err)
}
return nil
} else if msgType == MessageTypeCustom { // 自定义解码(二进制->消息)
bm, ok := v.(CustomMessage)
if !ok {
return fmt.Errorf("param:%v is not message.CustomMessage while protoType is ProtoTypeBN:%v", b, "not CustomMessage")
}
err := bm.Decode(b)
if err != nil {
return fmt.Errorf("param:%v can not unmarshal to message.CustomMessage while protoType is ProtoTypeBN:%v", b, err)
}
return nil
}
return errors.New("param can not unmarshal to message while protoType is invalid")
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/andyxt/gox.git
git@gitee.com:andyxt/gox.git
andyxt
gox
gox
v1.0.51

搜索帮助