From 6890da44c947e59a7bd138c63bc529ebbb38fb15 Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Mon, 2 Sep 2024 17:47:33 +0800 Subject: [PATCH 1/2] feat: implement no gob encoder. --- internal/msggateway/encoder.go | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/internal/msggateway/encoder.go b/internal/msggateway/encoder.go index 3af266374..66d74ccb2 100644 --- a/internal/msggateway/encoder.go +++ b/internal/msggateway/encoder.go @@ -15,9 +15,6 @@ package msggateway import ( - "bytes" - "encoding/gob" - "github.com/openimsdk/tools/errs" ) @@ -33,19 +30,17 @@ func NewGobEncoder() *GobEncoder { } func (g *GobEncoder) Encode(data any) ([]byte, error) { - buff := bytes.Buffer{} - enc := gob.NewEncoder(&buff) - if err := enc.Encode(data); err != nil { - return nil, errs.WrapMsg(err, "GobEncoder.Encode failed", "action", "encode") + if b, ok := data.([]byte); ok { + return b, nil } - return buff.Bytes(), nil + return nil, errs.New("Encoder.Encode failed", "action", "encode") } func (g *GobEncoder) Decode(encodeData []byte, decodeData any) error { - buff := bytes.NewBuffer(encodeData) - dec := gob.NewDecoder(buff) - if err := dec.Decode(decodeData); err != nil { - return errs.WrapMsg(err, "GobEncoder.Decode failed", "action", "decode") + if b, ok := decodeData.(*[]byte); ok { + *b = encodeData + return nil } - return nil + + return errs.New("Encoder.Decode failed", "action", "decode") } From 165eecf0370886fb1403464bfadcec66884297d6 Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Mon, 2 Sep 2024 18:05:49 +0800 Subject: [PATCH 2/2] update unitTest content. --- internal/msggateway/encoder_test.go | 48 +++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 internal/msggateway/encoder_test.go diff --git a/internal/msggateway/encoder_test.go b/internal/msggateway/encoder_test.go new file mode 100644 index 000000000..da77763ca --- /dev/null +++ b/internal/msggateway/encoder_test.go @@ -0,0 +1,48 @@ +package msggateway + +import ( + "testing" +) + +func TestGobEncoder_Encode(t *testing.T) { + encoder := NewGobEncoder() + + // 测试用例1: 编码 []byte 数据 + inputData := []byte("test data") + encodedData, err := encoder.Encode(inputData) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if string(encodedData) != string(inputData) { + t.Fatalf("expected encoded data to be '%s', got '%s'", inputData, encodedData) + } + + // 测试用例2: 编码非 []byte 数据 + nonByteData := "string data" + _, err = encoder.Encode(nonByteData) + if err == nil { + t.Fatalf("expected an error when encoding non-byte data, got none") + } +} + +func TestGobEncoder_Decode(t *testing.T) { + encoder := NewGobEncoder() + + // 测试用例1: 解码到 []byte 数据 + encodedData := []byte("test data") + var decodedData []byte + err := encoder.Decode(encodedData, &decodedData) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if string(decodedData) != string(encodedData) { + t.Fatalf("expected decoded data to be '%s', got '%s'", encodedData, decodedData) + } + + // 测试用例2: 解码到非 []byte 数据 + var nonByteData string + err = encoder.Decode(encodedData, &nonByteData) + if err == nil { + t.Fatalf("expected an error when decoding to non-byte data, got none") + } +}