Skip to content

Commit

Permalink
Merge pull request #57 from pinealctx/add-marshal-empty
Browse files Browse the repository at this point in the history
add MarshalEmpty method
  • Loading branch information
xpineal authored Jul 27, 2022
2 parents 226d277 + 34a1f6a commit c3d16e2
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions mpb/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (

var (
_defaultMsgPacker = NewMsgPacker()
_emptyData = make([]byte, 4)
)

// FingerprintMsg extends "Fingerprint() uint32" to proto message
Expand Down Expand Up @@ -86,7 +87,7 @@ func (x *MsgPacker) MarshalMsg(msg proto.Message) ([]byte, error) {
case FingerprintMsg:
return x.marshalMsg(v)
case *emptypb.Empty:
return tagEmptyMsg(), nil
return _emptyData, nil
default:
return nil, errorx.NewfWithStack("unsupported message:%+v", msg.ProtoReflect().Descriptor().FullName())
}
Expand Down Expand Up @@ -202,6 +203,11 @@ func MarshalError(err error) ([]byte, error) {
return marshalProtoMsg(ErrMark, v.Proto())
}

// MarshalEmpty tag an empty message
func MarshalEmpty(msg proto.Message) []byte {
return _emptyData
}

func marshalProtoMsg(fingerprint uint32, msg proto.Message) ([]byte, error) {
var data, err = proto.Marshal(msg)
if err != nil {
Expand All @@ -216,8 +222,6 @@ func marshalProtoMsg(fingerprint uint32, msg proto.Message) ([]byte, error) {
return buf.Bytes(), nil
}

func tagEmptyMsg() []byte {
var header [4]byte
binary.LittleEndian.PutUint32(header[:], EmptyMark)
return header[:]
func init() {
binary.LittleEndian.PutUint32(_emptyData, EmptyMark)
}

0 comments on commit c3d16e2

Please sign in to comment.