Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
kangyili committed Jul 8, 2023
1 parent da28f67 commit fdb882d
Showing 1 changed file with 2 additions and 80 deletions.
82 changes: 2 additions & 80 deletions process/message_no_zstd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,9 @@ import (
"reflect"
"strconv"

"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
)

type zstd struct{}

func (z zstd) Decompress(_ []byte, _ []byte) ([]byte, error) {
return nil, errors.New("zstd not supported")
}

func (z zstd) Compress(_ []byte, _ []byte) ([]byte, error) {
return nil, errors.New("zstd not supported")
}

type zstd_0 struct{}

func (z zstd_0) Decompress(_ []byte, _ []byte) ([]byte, error) {
return nil, errors.New("zstd not supported")
}

func (z zstd_0) Compress(_ []byte, _ []byte) ([]byte, error) {
return nil, errors.New("zstd not supported")
}

// MessageEncoding represents how messages will be encoded or decoded for
// over-the-wire transfer. Protobuf should be used for server-side messages
// (e.g. from collector <-> server) and JSON should be used for client-side.
Expand Down Expand Up @@ -76,25 +55,7 @@ type MessageHeader struct {
}

func unmarshal(enc MessageEncoding, body []byte, m proto.Message) error {
switch enc {
case MessageEncodingProtobuf:
return proto.Unmarshal(body, m)
case MessageEncodingJSON:
return jsonpb.Unmarshal(bytes.NewReader(body), m)
case MessageEncodingZstdPB, MessageEncodingZstd1xPB:
var d []byte
var err error
if enc == MessageEncodingZstd1xPB {
d, err = zstd{}.Decompress(nil, body)
} else {
d, err = zstd_0{}.Decompress(nil, body)
}
if err != nil {
return err
}
return proto.Unmarshal(d, m)
}
return fmt.Errorf("unknown message encoding: %d", enc)
return errors.New("zstd not supported")
}

// MessageType is a string representing the type of a message.
Expand Down Expand Up @@ -375,46 +336,7 @@ func DetectMessageType(b MessageBody) (MessageType, error) {
// EncodeMessage encodes a message object into bytes with protobuf. A type
// header is added for ease of decoding.
func EncodeMessage(m Message) ([]byte, error) {
hb, err := encodeHeader(m.Header)
if err != nil {
return nil, fmt.Errorf("could not encode header: %s", err)
}

b := new(bytes.Buffer)
if _, err := b.Write(hb); err != nil {
return nil, err
}

var p []byte
switch m.Header.Encoding {
case MessageEncodingProtobuf:
p, err = proto.Marshal(m.Body)
if err != nil {
return nil, err
}
case MessageEncodingJSON:
marshaler := jsonpb.Marshaler{EmitDefaults: true}
s, err := marshaler.MarshalToString(m.Body)
if err != nil {
return nil, err
}
p = []byte(s)
case MessageEncodingZstdPB, MessageEncodingZstd1xPB:
pb, err := proto.Marshal(m.Body)
if err != nil {
return nil, err
}

if m.Header.Encoding == MessageEncodingZstd1xPB {
p, err = zstd{}.Compress(nil, pb)
} else {
p, err = zstd_0{}.Compress(nil, pb)
}
default:
return nil, fmt.Errorf("unknown message encoding: %d", m.Header.Encoding)
}
_, err = b.Write(p)
return b.Bytes(), err
return nil, errors.New("zstd not supported")
}

// ReadHeader reads the header off raw message bytes.
Expand Down

0 comments on commit fdb882d

Please sign in to comment.