Skip to content

Commit

Permalink
optimize json encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
lovromazgon committed Oct 25, 2023
1 parent 4ee1ab5 commit 31b6775
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 4 deletions.
5 changes: 3 additions & 2 deletions acceptance_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"errors"
"fmt"
"maps"

Check failure on line 22 in acceptance_testing.go

View workflow job for this annotation

GitHub Actions / build

package maps is not in GOROOT (/opt/hostedtoolcache/go/1.20.10/x64/src/maps)
"math/rand"
"reflect"
"regexp"
Expand Down Expand Up @@ -179,11 +180,11 @@ func (d ConfigurableAcceptanceTestDriver) Connector() Connector {
}

func (d ConfigurableAcceptanceTestDriver) SourceConfig(*testing.T) map[string]string {
return d.Config.SourceConfig
return maps.Clone(d.Config.SourceConfig)
}

func (d ConfigurableAcceptanceTestDriver) DestinationConfig(*testing.T) map[string]string {
return d.Config.DestinationConfig
return maps.Clone(d.Config.DestinationConfig)
}

func (d ConfigurableAcceptanceTestDriver) BeforeTest(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/conduitio/conduit-connector-protocol v0.5.0
github.com/goccy/go-json v0.10.2
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.0
github.com/jpillora/backoff v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
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/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
Expand Down
2 changes: 1 addition & 1 deletion record.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package sdk

import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/conduitio/conduit-connector-protocol/cpluginv1"
"github.com/goccy/go-json"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion record_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ package sdk

import (
"bytes"
"encoding/json"
"fmt"
"strings"
"text/template"

"github.com/Masterminds/sprig/v3"
"github.com/conduitio/conduit-connector-sdk/kafkaconnect"
"github.com/goccy/go-json"
)

// RecordFormatter is a type that can format a record to bytes. It's used in
Expand Down
26 changes: 26 additions & 0 deletions record_formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ import (
"github.com/matryer/is"
)

var (
encBytesSink []byte
encErrSink error
)

func BenchmarkJSONEncoder(b *testing.B) {
rec := Record{
Position: Position("foo"),
Operation: OperationCreate,
Metadata: Metadata{MetadataConduitSourcePluginName: "example"},
Key: RawData("bar"),
Payload: Change{
Before: nil,
After: StructuredData{
"foo": "bar",
"baz": "qux",
},
},
}

enc := JSONEncoder{}
for i := 0; i < b.N; i++ {
encBytesSink, encErrSink = enc.Encode(rec)
}
}

func TestOpenCDCConverter(t *testing.T) {
is := is.New(t)
var converter OpenCDCConverter
Expand Down
14 changes: 14 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package sdk

import (
"fmt"
"reflect"
"strings"

"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -81,6 +82,7 @@ func parseConfig(raw map[string]string, config interface{}) error {
WeaklyTypedInput: true,
Result: &config,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
emptyStringToZeroValueHookFunc(),
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
),
Expand All @@ -95,3 +97,15 @@ func parseConfig(raw map[string]string, config interface{}) error {
err = decoder.Decode(breakUpConfig(raw))
return err
}

func emptyStringToZeroValueHookFunc() mapstructure.DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
if f.Kind() != reflect.String || data != "" {
return data, nil
}
return reflect.New(t).Elem().Interface(), nil
}
}
2 changes: 2 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ func TestParseConfig_Simple_Struct(t *testing.T) {
type Person struct {
Name string `json:"person_name"`
Age int
Dur time.Duration
}

input := map[string]string{
"person_name": "meroxa",
"age": "91",
"dur": "", // empty value should result in zero value
}
want := Person{
Name: "meroxa",
Expand Down

0 comments on commit 31b6775

Please sign in to comment.