Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
lovromazgon committed Dec 5, 2023
1 parent d4f11b8 commit 899514e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
interval: "weekly"
commit-message:
prefix: ".github:"

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 2 additions & 1 deletion opencdc/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion opencdc/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions opencdc/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"`
Expand All @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions opencdc/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 899514e

Please sign in to comment.