liangyuanpeng commented on a change in pull request #229:
URL: https://github.com/apache/incubator-tubemq/pull/229#discussion_r466448803



##########
File path: tubemq-client-twins/tubemq-client-go/codec/codec/serialization.go
##########
@@ -0,0 +1,119 @@
+package codec
+
+import (
+       "errors"
+       "math"
+       "sync"
+
+       "github.com/golang/protobuf/proto"
+)
+
+// Serialization defines the serialization of data
+type Serialization interface {
+       Marshal(interface{}) ([]byte, error)
+       Unmarshal([]byte, interface{}) error
+}
+
+const (
+       Proto   = "proto"   // protobuf
+       MsgPack = "msgpack" // msgpack
+       Json    = "json"    // json
+)
+
+var serializationMap = make(map[string]Serialization)
+
+// DefaultSerialization defines the default serialization
+var DefaultSerialization = NewSerialization()
+
+// NewSerialization returns a globally unique serialization
+var NewSerialization = func() Serialization {
+       return &pbSerialization{}
+}
+
+func init() {
+       registerSerialization("proto", DefaultSerialization)
+}
+
+func registerSerialization(name string, serialization Serialization) {
+       if serializationMap == nil {
+               serializationMap = make(map[string]Serialization)
+       }
+       serializationMap[name] = serialization
+}
+
+// GetSerialization get a Serialization by a serialization name
+func GetSerialization(name string) Serialization {
+       if v, ok := serializationMap[name]; ok {
+               return v
+       }
+       return DefaultSerialization
+}
+
+type pbSerialization struct{}
+
+func (d *pbSerialization) Marshal(v interface{}) ([]byte, error) {
+       if v == nil {
+               return nil, errors.New("marshal nil interface{}")
+       }
+       if pm, ok := v.(proto.Marshaler); ok {
+               // 可以 marshal 自身,无需 buffer
+               return pm.Marshal()
+       }
+       buffer := bufferPool.Get().(*cachedBuffer)
+       protoMsg := v.(proto.Message)
+       lastMarshaledSize := make([]byte, 0, buffer.lastMarshaledSize)
+       buffer.SetBuf(lastMarshaledSize)
+       buffer.Reset()
+
+       if err := buffer.Marshal(protoMsg); err != nil {
+               return nil, err
+       }
+       data := buffer.Bytes()
+       buffer.lastMarshaledSize = upperLimit(len(data))
+       buffer.SetBuf(nil)
+       bufferPool.Put(buffer)
+
+       return data, nil
+}
+
+func (d *pbSerialization) Unmarshal(data []byte, v interface{}) error {
+       if data == nil || len(data) == 0 {
+               return errors.New("unmarshal nil or empty bytes")
+       }
+
+       protoMsg := v.(proto.Message)
+       protoMsg.Reset()
+
+       if pu, ok := protoMsg.(proto.Unmarshaler); ok {
+               // 可以 unmarshal 自身,无需 buffer

Review comment:
       May be should not chinese. :)

##########
File path: tubemq-client-twins/tubemq-client-go/codec/codec/serialization.go
##########
@@ -0,0 +1,119 @@
+package codec
+
+import (
+       "errors"
+       "math"
+       "sync"
+
+       "github.com/golang/protobuf/proto"
+)
+
+// Serialization defines the serialization of data
+type Serialization interface {
+       Marshal(interface{}) ([]byte, error)
+       Unmarshal([]byte, interface{}) error
+}
+
+const (
+       Proto   = "proto"   // protobuf
+       MsgPack = "msgpack" // msgpack
+       Json    = "json"    // json
+)
+
+var serializationMap = make(map[string]Serialization)
+
+// DefaultSerialization defines the default serialization
+var DefaultSerialization = NewSerialization()
+
+// NewSerialization returns a globally unique serialization
+var NewSerialization = func() Serialization {
+       return &pbSerialization{}
+}
+
+func init() {
+       registerSerialization("proto", DefaultSerialization)
+}
+
+func registerSerialization(name string, serialization Serialization) {
+       if serializationMap == nil {
+               serializationMap = make(map[string]Serialization)
+       }
+       serializationMap[name] = serialization
+}
+
+// GetSerialization get a Serialization by a serialization name
+func GetSerialization(name string) Serialization {
+       if v, ok := serializationMap[name]; ok {
+               return v
+       }
+       return DefaultSerialization
+}
+
+type pbSerialization struct{}
+
+func (d *pbSerialization) Marshal(v interface{}) ([]byte, error) {
+       if v == nil {
+               return nil, errors.New("marshal nil interface{}")
+       }
+       if pm, ok := v.(proto.Marshaler); ok {
+               // 可以 marshal 自身,无需 buffer

Review comment:
       May be should not chinese. :)




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to