Ai
1 Star 0 Fork 0

李文建/protoactor-go

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
proto_serializer.go 1.04 KB
Copy Edit Raw Blame History
package remote
import (
"fmt"
"reflect"
"github.com/gogo/protobuf/proto"
)
type protoSerializer struct{}
func newProtoSerializer() Serializer {
return &protoSerializer{}
}
func (protoSerializer) Serialize(msg interface{}) ([]byte, error) {
if message, ok := msg.(proto.Message); ok {
bytes, err := proto.Marshal(message)
if err != nil {
return nil, err
}
return bytes, nil
}
return nil, fmt.Errorf("msg must be proto.Message")
}
func (protoSerializer) Deserialize(typeName string, bytes []byte) (interface{}, error) {
protoType := proto.MessageType(typeName)
if protoType == nil {
return nil, fmt.Errorf("unknown message type %v", typeName)
}
t := protoType.Elem()
intPtr := reflect.New(t)
instance := intPtr.Interface().(proto.Message)
proto.Unmarshal(bytes, instance)
return instance, nil
}
func (protoSerializer) GetTypeName(msg interface{}) (string, error) {
if message, ok := msg.(proto.Message); ok {
typeName := proto.MessageName(message)
return typeName, nil
}
return "", fmt.Errorf("msg must be proto.Message")
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lwj8507/protoactor-go.git
git@gitee.com:lwj8507/protoactor-go.git
lwj8507
protoactor-go
protoactor-go
v0.0.1

Search