package protobuf import ( "errors" "git.noahlan.cn/noahlan/nnet/serialize" "google.golang.org/protobuf/proto" ) // ErrWrongValueType is the error used for marshal the value with protobuf encoding. var ErrWrongValueType = errors.New("protobuf: convert on wrong type value") type Serializer struct{} func NewSerializer() serialize.Serializer { return &Serializer{} } func (s *Serializer) Marshal(v interface{}) ([]byte, error) { pb, ok := v.(proto.Message) if !ok { return nil, ErrWrongValueType } return proto.Marshal(pb) } func (s *Serializer) Unmarshal(data []byte, v interface{}) error { pb, ok := v.(proto.Message) if !ok { return ErrWrongValueType } return proto.Unmarshal(data, pb) }