From 899514e0d66f6d2c515e05241dafb01beb43c438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lovro=20Ma=C5=BEgon?= Date: Tue, 5 Dec 2023 18:49:42 +0100 Subject: [PATCH] review changes - change github actions dependabot interval to weekly - use goccy/go-json instead of encoding/json - fix comments - add Record.WithSerializer - add benchmark for Record.Clone --- .github/dependabot.yml | 2 +- go.mod | 1 + go.sum | 2 ++ opencdc/data.go | 3 ++- opencdc/position.go | 2 +- opencdc/record.go | 17 +++++++++++++---- opencdc/record_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 62 insertions(+), 7 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index ed7a994..9b777ec 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -6,7 +6,7 @@ updates: - package-ecosystem: "github-actions" directory: "/" schedule: - interval: "daily" + interval: "weekly" commit-message: prefix: ".github:" diff --git a/go.mod b/go.mod index f1fbec2..dad70f6 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/conduitio/conduit-commons go 1.21.1 require ( + github.com/goccy/go-json v0.10.2 github.com/golangci/golangci-lint v1.55.2 github.com/google/go-cmp v0.6.0 github.com/google/uuid v1.4.0 diff --git a/go.sum b/go.sum index cc3ce18..af28330 100644 --- a/go.sum +++ b/go.sum @@ -194,6 +194,8 @@ github.com/go-xmlfmt/xmlfmt v1.1.2 h1:Nea7b4icn8s57fTx1M5AI4qQT5HEM3rVUO8MuE6g80 github.com/go-xmlfmt/xmlfmt v1.1.2/go.mod h1:aUCEOzzezBEjDBbFBoSiya/gduyIiWYRP6CnSFIV8AM= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= diff --git a/opencdc/data.go b/opencdc/data.go index e985c9d..1bb6775 100644 --- a/opencdc/data.go +++ b/opencdc/data.go @@ -16,8 +16,9 @@ package opencdc import ( "bytes" - "encoding/json" "fmt" + + "github.com/goccy/go-json" ) // Data is a structure that contains some bytes. The only structs implementing diff --git a/opencdc/position.go b/opencdc/position.go index e14d665..55d0bd0 100644 --- a/opencdc/position.go +++ b/opencdc/position.go @@ -14,7 +14,7 @@ package opencdc -// Position is a unique identifier for a record being process. +// Position is a unique identifier for a record being processed. // It's a Source's responsibility to choose and assign record positions, // as they will be used by the Source in subsequent pipeline runs. type Position []byte diff --git a/opencdc/record.go b/opencdc/record.go index bfd3439..d2a40a4 100644 --- a/opencdc/record.go +++ b/opencdc/record.go @@ -16,8 +16,9 @@ package opencdc import ( "bytes" - "encoding/json" "fmt" + + "github.com/goccy/go-json" ) // Record represents a single data record produced by a source and/or consumed @@ -30,7 +31,7 @@ type Record struct { // operations are encountered during normal CDC operation, while "snapshot" // is meant to represent records during an initial load. Depending on the // operation, the record will contain either the payload before the change, - // after the change, or both (see field Payload). + // after the change, both or none (see field Payload). Operation Operation `json:"operation"` // Metadata contains additional information regarding the record. Metadata Metadata `json:"metadata"` @@ -45,9 +46,17 @@ type Record struct { serializer RecordSerializer } +// WithSerializer returns a new record which is serialized using the provided +// serializer when Bytes gets called. If serializer is nil, the serializing +// behavior is reset to the default (JSON). +func (r Record) WithSerializer(serializer RecordSerializer) Record { + r.serializer = serializer + return r +} + // Bytes returns the serialized representation of the Record. By default, this -// function returns a JSON representation. The serialization can be changed -// using SetSerializer. +// function returns a JSON representation. The serialization logic can be changed +// using WithSerializer. func (r Record) Bytes() []byte { if r.serializer != nil { b, err := r.serializer.Serialize(r) diff --git a/opencdc/record_test.go b/opencdc/record_test.go index 020b0b9..3586bbb 100644 --- a/opencdc/record_test.go +++ b/opencdc/record_test.go @@ -142,3 +142,45 @@ func TestRecord_ToMap(t *testing.T) { } is.Equal(want, got) } + +func BenchmarkRecord_Clone(b *testing.B) { + type user struct { + Name string + } + + r1 := Record{ + Position: Position("standing"), + Operation: OperationUpdate, + Metadata: Metadata{"foo": "bar"}, + Key: RawData("padlock-key"), + Payload: Change{ + Before: RawData("yellow"), + After: StructuredData{ + "bool": true, + + "int": 1, + "int8": int8(1), + "int16": int16(1), + "int32": int32(1), + "int64": int64(1), + + "float32": float32(1.2), + "float64": 1.2, + + "string": "orange", + + "string-slice": []string{"a"}, + "map": map[string]string{"a": "A", "b": "B"}, + + "user": user{Name: "john"}, + }, + }, + } + var r2 Record + + b.ResetTimer() + for i := 0; i < b.N; i++ { + r2 = r1.Clone() + } + _ = r2 +}