From 53af35ae0c20329b7836021b31e298daf7e1c04e Mon Sep 17 00:00:00 2001 From: Pawel Zak Date: Wed, 4 Dec 2024 17:58:58 +0100 Subject: [PATCH] address review comment --- internal/content_coding.go | 44 ++++----- internal/content_coding_test.go | 94 +++++++++---------- .../amqp_consumer/amqp_consumer_test.go | 4 +- .../inputs/cloud_pubsub/cloud_pubsub_test.go | 2 +- plugins/outputs/influxdb_v2/influxdb_v2.go | 2 +- 5 files changed, 73 insertions(+), 73 deletions(-) diff --git a/internal/content_coding.go b/internal/content_coding.go index 9070196706cde..0bb75e3a03ee1 100644 --- a/internal/content_coding.go +++ b/internal/content_coding.go @@ -45,7 +45,7 @@ func WithCompressionLevel(level int) EncodingOption { func NewStreamContentDecoder(encoding string, r io.Reader) (io.Reader, error) { switch encoding { case "gzip": - return newGzipReader(r) + return NewGzipReader(r) case "identity", "": return r, nil default: @@ -60,7 +60,7 @@ type gzipReader struct { endOfStream bool } -func newGzipReader(r io.Reader) (io.Reader, error) { +func NewGzipReader(r io.Reader) (io.Reader, error) { // We need a read that implements ByteReader in order to line up the next // stream. br := bufio.NewReader(r) @@ -103,13 +103,13 @@ func (r *gzipReader) Read(b []byte) (int, error) { func NewContentEncoder(encoding string, options ...EncodingOption) (ContentEncoder, error) { switch encoding { case "gzip": - return newGzipEncoder(options...) + return NewGzipEncoder(options...) case "identity", "": - return newIdentityEncoder(options...) + return NewIdentityEncoder(options...) case "zlib": - return newZlibEncoder(options...) + return NewZlibEncoder(options...) case "zstd": - return newZstdEncoder(options...) + return NewZstdEncoder(options...) default: return nil, errors.New("invalid value for content_encoding") } @@ -132,11 +132,11 @@ func (a *autoDecoder) Decode(data []byte) ([]byte, error) { return a.identity.Decode(data) } -func newAutoContentDecoder(options ...DecodingOption) *autoDecoder { +func NewAutoContentDecoder(options ...DecodingOption) *autoDecoder { var a autoDecoder - a.identity = newIdentityDecoder(options...) - a.gzip = newGzipDecoder(options...) + a.identity = NewIdentityDecoder(options...) + a.gzip = NewGzipDecoder(options...) return &a } @@ -144,15 +144,15 @@ func newAutoContentDecoder(options ...DecodingOption) *autoDecoder { func NewContentDecoder(encoding string, options ...DecodingOption) (ContentDecoder, error) { switch encoding { case "auto": - return newAutoContentDecoder(options...), nil + return NewAutoContentDecoder(options...), nil case "gzip": - return newGzipDecoder(options...), nil + return NewGzipDecoder(options...), nil case "identity", "": - return newIdentityDecoder(options...), nil + return NewIdentityDecoder(options...), nil case "zlib": - return newZlibDecoder(options...), nil + return NewZlibDecoder(options...), nil case "zstd": - return newZstdDecoder(options...) + return NewZstdDecoder(options...) default: return nil, errors.New("invalid value for content_encoding") } @@ -170,7 +170,7 @@ type gzipEncoder struct { buf *bytes.Buffer } -func newGzipEncoder(options ...EncodingOption) (*gzipEncoder, error) { +func NewGzipEncoder(options ...EncodingOption) (*gzipEncoder, error) { cfg := encoderConfig{level: gzip.DefaultCompression} for _, o := range options { o(&cfg) @@ -244,7 +244,7 @@ type zlibEncoder struct { buf *bytes.Buffer } -func newZlibEncoder(options ...EncodingOption) (*zlibEncoder, error) { +func NewZlibEncoder(options ...EncodingOption) (*zlibEncoder, error) { cfg := encoderConfig{level: zlib.DefaultCompression} for _, o := range options { o(&cfg) @@ -284,7 +284,7 @@ type zstdEncoder struct { encoder *zstd.Encoder } -func newZstdEncoder(options ...EncodingOption) (*zstdEncoder, error) { +func NewZstdEncoder(options ...EncodingOption) (*zstdEncoder, error) { cfg := encoderConfig{level: 3} for _, o := range options { o(&cfg) @@ -318,7 +318,7 @@ func (e *zstdEncoder) Encode(data []byte) ([]byte, error) { // identityEncoder is a null encoder that applies no transformation. type identityEncoder struct{} -func newIdentityEncoder(options ...EncodingOption) (*identityEncoder, error) { +func NewIdentityEncoder(options ...EncodingOption) (*identityEncoder, error) { if len(options) > 0 { return nil, errors.New("identity encoder does not support options") } @@ -344,7 +344,7 @@ type gzipDecoder struct { maxDecompressionSize int64 } -func newGzipDecoder(options ...DecodingOption) *gzipDecoder { +func NewGzipDecoder(options ...DecodingOption) *gzipDecoder { cfg := decoderConfig{maxDecompressionSize: defaultMaxDecompressionSize} for _, o := range options { o(&cfg) @@ -418,7 +418,7 @@ type zlibDecoder struct { maxDecompressionSize int64 } -func newZlibDecoder(options ...DecodingOption) *zlibDecoder { +func NewZlibDecoder(options ...DecodingOption) *zlibDecoder { cfg := decoderConfig{maxDecompressionSize: defaultMaxDecompressionSize} for _, o := range options { o(&cfg) @@ -459,7 +459,7 @@ type zstdDecoder struct { decoder *zstd.Decoder } -func newZstdDecoder(options ...DecodingOption) (*zstdDecoder, error) { +func NewZstdDecoder(options ...DecodingOption) (*zstdDecoder, error) { cfg := decoderConfig{maxDecompressionSize: defaultMaxDecompressionSize} for _, o := range options { o(&cfg) @@ -481,7 +481,7 @@ func (d *zstdDecoder) Decode(data []byte) ([]byte, error) { type identityDecoder struct { } -func newIdentityDecoder(_ ...DecodingOption) *identityDecoder { +func NewIdentityDecoder(_ ...DecodingOption) *identityDecoder { return &identityDecoder{} } diff --git a/internal/content_coding_test.go b/internal/content_coding_test.go index 2d3a1712f0687..46401b1129e3f 100644 --- a/internal/content_coding_test.go +++ b/internal/content_coding_test.go @@ -13,9 +13,9 @@ import ( const maxDecompressionSize = 1024 func TestGzipEncodeDecode(t *testing.T) { - enc, err := newGzipEncoder() + enc, err := NewGzipEncoder() require.NoError(t, err) - dec := newGzipDecoder(WithMaxDecompressionSize(maxDecompressionSize)) + dec := NewGzipDecoder(WithMaxDecompressionSize(maxDecompressionSize)) payload, err := enc.Encode([]byte("howdy")) require.NoError(t, err) @@ -27,9 +27,9 @@ func TestGzipEncodeDecode(t *testing.T) { } func TestGzipReuse(t *testing.T) { - enc, err := newGzipEncoder() + enc, err := NewGzipEncoder() require.NoError(t, err) - dec := newGzipDecoder(WithMaxDecompressionSize(maxDecompressionSize)) + dec := NewGzipDecoder(WithMaxDecompressionSize(maxDecompressionSize)) payload, err := enc.Encode([]byte("howdy")) require.NoError(t, err) @@ -49,9 +49,9 @@ func TestGzipReuse(t *testing.T) { } func TestZlibEncodeDecode(t *testing.T) { - enc, err := newZlibEncoder() + enc, err := NewZlibEncoder() require.NoError(t, err) - dec := newZlibDecoder(WithMaxDecompressionSize(maxDecompressionSize)) + dec := NewZlibDecoder(WithMaxDecompressionSize(maxDecompressionSize)) payload, err := enc.Encode([]byte("howdy")) require.NoError(t, err) @@ -63,9 +63,9 @@ func TestZlibEncodeDecode(t *testing.T) { } func TestZlibEncodeDecodeWithTooLargeMessage(t *testing.T) { - enc, err := newZlibEncoder() + enc, err := NewZlibEncoder() require.NoError(t, err) - dec := newZlibDecoder(WithMaxDecompressionSize(3)) + dec := NewZlibDecoder(WithMaxDecompressionSize(3)) payload, err := enc.Encode([]byte("howdy")) require.NoError(t, err) @@ -75,9 +75,9 @@ func TestZlibEncodeDecodeWithTooLargeMessage(t *testing.T) { } func TestZstdEncodeDecode(t *testing.T) { - enc, err := newZstdEncoder() + enc, err := NewZstdEncoder() require.NoError(t, err) - dec, err := newZstdDecoder(WithMaxDecompressionSize(maxDecompressionSize)) + dec, err := NewZstdDecoder(WithMaxDecompressionSize(maxDecompressionSize)) require.NoError(t, err) payload, err := enc.Encode([]byte("howdy")) @@ -90,9 +90,9 @@ func TestZstdEncodeDecode(t *testing.T) { } func TestZstdReuse(t *testing.T) { - enc, err := newZstdEncoder() + enc, err := NewZstdEncoder() require.NoError(t, err) - dec, err := newZstdDecoder(WithMaxDecompressionSize(maxDecompressionSize)) + dec, err := NewZstdDecoder(WithMaxDecompressionSize(maxDecompressionSize)) require.NoError(t, err) payload, err := enc.Encode([]byte("howdy")) @@ -113,8 +113,8 @@ func TestZstdReuse(t *testing.T) { } func TestIdentityEncodeDecode(t *testing.T) { - dec := newIdentityDecoder(WithMaxDecompressionSize(maxDecompressionSize)) - enc, err := newIdentityEncoder() + dec := NewIdentityDecoder(WithMaxDecompressionSize(maxDecompressionSize)) + enc, err := NewIdentityEncoder() require.NoError(t, err) payload, err := enc.Encode([]byte("howdy")) @@ -142,7 +142,7 @@ func TestStreamIdentityDecode(t *testing.T) { } func TestStreamGzipDecode(t *testing.T) { - enc, err := newGzipEncoder() + enc, err := NewGzipEncoder() require.NoError(t, err) written, err := enc.Encode([]byte("howdy")) require.NoError(t, err) @@ -229,9 +229,9 @@ func BenchmarkGzipEncode(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 64)) dataLen := int64(len(data)) + 1 - enc, err := newGzipEncoder() + enc, err := NewGzipEncoder() require.NoError(b, err) - dec := newGzipDecoder(WithMaxDecompressionSize(dataLen)) + dec := NewGzipDecoder(WithMaxDecompressionSize(dataLen)) payload, err := enc.Encode(data) require.NoError(b, err) actual, err := dec.Decode(payload) @@ -248,9 +248,9 @@ func BenchmarkGzipDecode(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 64)) dataLen := int64(len(data)) + 1 - enc, err := newGzipEncoder() + enc, err := NewGzipEncoder() require.NoError(b, err) - dec := newGzipDecoder(WithMaxDecompressionSize(dataLen)) + dec := NewGzipDecoder(WithMaxDecompressionSize(dataLen)) payload, err := enc.Encode(data) require.NoError(b, err) actual, err := dec.Decode(payload) @@ -267,9 +267,9 @@ func BenchmarkGzipEncodeDecode(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 64)) dataLen := int64(len(data)) + 1 - enc, err := newGzipEncoder() + enc, err := NewGzipEncoder() require.NoError(b, err) - dec := newGzipDecoder(WithMaxDecompressionSize(dataLen)) + dec := NewGzipDecoder(WithMaxDecompressionSize(dataLen)) payload, err := enc.Encode(data) require.NoError(b, err) actual, err := dec.Decode(payload) @@ -289,9 +289,9 @@ func BenchmarkGzipEncodeBig(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 1024*1024)) dataLen := int64(len(data)) + 1 - enc, err := newGzipEncoder() + enc, err := NewGzipEncoder() require.NoError(b, err) - dec := newGzipDecoder(WithMaxDecompressionSize(dataLen)) + dec := NewGzipDecoder(WithMaxDecompressionSize(dataLen)) payload, err := enc.Encode(data) require.NoError(b, err) actual, err := dec.Decode(payload) @@ -308,9 +308,9 @@ func BenchmarkGzipDecodeBig(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 1024*1024)) dataLen := int64(len(data)) + 1 - enc, err := newGzipEncoder() + enc, err := NewGzipEncoder() require.NoError(b, err) - dec := newGzipDecoder(WithMaxDecompressionSize(dataLen)) + dec := NewGzipDecoder(WithMaxDecompressionSize(dataLen)) payload, err := enc.Encode(data) require.NoError(b, err) actual, err := dec.Decode(payload) @@ -327,9 +327,9 @@ func BenchmarkGzipEncodeDecodeBig(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 1024*1024)) dataLen := int64(len(data)) + 1 - enc, err := newGzipEncoder() + enc, err := NewGzipEncoder() require.NoError(b, err) - dec := newGzipDecoder(WithMaxDecompressionSize(dataLen)) + dec := NewGzipDecoder(WithMaxDecompressionSize(dataLen)) payload, err := enc.Encode(data) require.NoError(b, err) actual, err := dec.Decode(payload) @@ -349,9 +349,9 @@ func BenchmarkZstdEncode(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 64)) dataLen := int64(len(data)) + 1 - enc, err := newZstdEncoder() + enc, err := NewZstdEncoder() require.NoError(b, err) - dec, err := newZstdDecoder(WithMaxDecompressionSize(dataLen)) + dec, err := NewZstdDecoder(WithMaxDecompressionSize(dataLen)) require.NoError(b, err) payload, err := enc.Encode(data) require.NoError(b, err) @@ -369,9 +369,9 @@ func BenchmarkZstdDecode(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 64)) dataLen := int64(len(data)) + 1 - enc, err := newZstdEncoder() + enc, err := NewZstdEncoder() require.NoError(b, err) - dec, err := newZstdDecoder(WithMaxDecompressionSize(dataLen)) + dec, err := NewZstdDecoder(WithMaxDecompressionSize(dataLen)) require.NoError(b, err) payload, err := enc.Encode(data) require.NoError(b, err) @@ -389,9 +389,9 @@ func BenchmarkZstdEncodeDecode(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 64)) dataLen := int64(len(data)) + 1 - enc, err := newZstdEncoder() + enc, err := NewZstdEncoder() require.NoError(b, err) - dec, err := newZstdDecoder(WithMaxDecompressionSize(dataLen)) + dec, err := NewZstdDecoder(WithMaxDecompressionSize(dataLen)) require.NoError(b, err) payload, err := enc.Encode(data) require.NoError(b, err) @@ -412,9 +412,9 @@ func BenchmarkZstdEncodeBig(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 1024*1024)) dataLen := int64(len(data)) + 1 - enc, err := newZstdEncoder() + enc, err := NewZstdEncoder() require.NoError(b, err) - dec, err := newZstdDecoder(WithMaxDecompressionSize(dataLen)) + dec, err := NewZstdDecoder(WithMaxDecompressionSize(dataLen)) require.NoError(b, err) payload, err := enc.Encode(data) require.NoError(b, err) @@ -432,9 +432,9 @@ func BenchmarkZstdDecodeBig(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 1024*1024)) dataLen := int64(len(data)) + 1 - enc, err := newZstdEncoder() + enc, err := NewZstdEncoder() require.NoError(b, err) - dec, err := newZstdDecoder(WithMaxDecompressionSize(dataLen)) + dec, err := NewZstdDecoder(WithMaxDecompressionSize(dataLen)) require.NoError(b, err) payload, err := enc.Encode(data) require.NoError(b, err) @@ -452,9 +452,9 @@ func BenchmarkZstdEncodeDecodeBig(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 1024*1024)) dataLen := int64(len(data)) + 1 - enc, err := newZstdEncoder() + enc, err := NewZstdEncoder() require.NoError(b, err) - dec, err := newZstdDecoder(WithMaxDecompressionSize(dataLen)) + dec, err := NewZstdDecoder(WithMaxDecompressionSize(dataLen)) require.NoError(b, err) payload, err := enc.Encode(data) require.NoError(b, err) @@ -475,9 +475,9 @@ func BenchmarkZlibEncode(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 64)) dataLen := int64(len(data)) + 1 - enc, err := newZlibEncoder() + enc, err := NewZlibEncoder() require.NoError(b, err) - dec := newZlibDecoder(WithMaxDecompressionSize(dataLen)) + dec := NewZlibDecoder(WithMaxDecompressionSize(dataLen)) payload, err := enc.Encode(data) require.NoError(b, err) actual, err := dec.Decode(payload) @@ -494,9 +494,9 @@ func BenchmarkZlibDecode(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 64)) dataLen := int64(len(data)) + 1 - enc, err := newZlibEncoder() + enc, err := NewZlibEncoder() require.NoError(b, err) - dec := newZlibDecoder(WithMaxDecompressionSize(dataLen)) + dec := NewZlibDecoder(WithMaxDecompressionSize(dataLen)) payload, err := enc.Encode(data) require.NoError(b, err) actual, err := dec.Decode(payload) @@ -513,9 +513,9 @@ func BenchmarkZlibEncodeDecode(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 64)) dataLen := int64(len(data)) + 1 - enc, err := newZlibEncoder() + enc, err := NewZlibEncoder() require.NoError(b, err) - dec := newZlibDecoder(WithMaxDecompressionSize(dataLen)) + dec := NewZlibDecoder(WithMaxDecompressionSize(dataLen)) payload, err := enc.Encode(data) require.NoError(b, err) actual, err := dec.Decode(payload) @@ -535,8 +535,8 @@ func BenchmarkIdentityEncodeDecode(b *testing.B) { data := []byte(strings.Repeat("-howdy stranger-", 64)) dataLen := int64(len(data)) + 1 - dec := newIdentityDecoder(WithMaxDecompressionSize(dataLen)) - enc, err := newIdentityEncoder() + dec := NewIdentityDecoder(WithMaxDecompressionSize(dataLen)) + enc, err := NewIdentityEncoder() require.NoError(b, err) payload, err := enc.Encode(data) diff --git a/plugins/inputs/amqp_consumer/amqp_consumer_test.go b/plugins/inputs/amqp_consumer/amqp_consumer_test.go index 1bb8063539aac..481d9fa3ef0d0 100644 --- a/plugins/inputs/amqp_consumer/amqp_consumer_test.go +++ b/plugins/inputs/amqp_consumer/amqp_consumer_test.go @@ -21,7 +21,7 @@ import ( func TestAutoEncoding(t *testing.T) { // Setup a gzipped payload - enc, err := internal.NewContentEncoder("gzip") + enc, err := internal.NewGzipEncoder() require.NoError(t, err) payloadGZip, err := enc.Encode([]byte(`measurementName fieldKey="gzip" 1556813561098000000`)) require.NoError(t, err) @@ -50,7 +50,7 @@ func TestAutoEncoding(t *testing.T) { acc.AssertContainsFields(t, "measurementName", map[string]interface{}{"fieldKey": "gzip"}) // Check the decoding - encIdentity, err := internal.NewContentEncoder("identity") + encIdentity, err := internal.NewIdentityEncoder() require.NoError(t, err) payload, err := encIdentity.Encode([]byte(`measurementName2 fieldKey="identity" 1556813561098000000`)) require.NoError(t, err) diff --git a/plugins/inputs/cloud_pubsub/cloud_pubsub_test.go b/plugins/inputs/cloud_pubsub/cloud_pubsub_test.go index 16c7314d6fb7e..4362f87f7680a 100644 --- a/plugins/inputs/cloud_pubsub/cloud_pubsub_test.go +++ b/plugins/inputs/cloud_pubsub/cloud_pubsub_test.go @@ -143,7 +143,7 @@ func TestRunGzipDecode(t *testing.T) { require.NotNil(t, ps.sub) testTracker := &testTracker{} - enc, err := internal.NewContentEncoder("gzip") + enc, err := internal.NewGzipEncoder() require.NoError(t, err) gzippedMsg, err := enc.Encode([]byte(msgInflux)) require.NoError(t, err) diff --git a/plugins/outputs/influxdb_v2/influxdb_v2.go b/plugins/outputs/influxdb_v2/influxdb_v2.go index a1b95002508ea..15a66632788e2 100644 --- a/plugins/outputs/influxdb_v2/influxdb_v2.go +++ b/plugins/outputs/influxdb_v2/influxdb_v2.go @@ -69,7 +69,7 @@ func (i *InfluxDB) Init() error { switch i.ContentEncoding { case "", "gzip": i.ContentEncoding = "gzip" - enc, err := internal.NewContentEncoder("gzip") + enc, err := internal.NewGzipEncoder() if err != nil { return fmt.Errorf("setting up gzip encoder failed: %w", err) }