diff --git a/.golangci.yml b/.golangci.yml index 83f147f..0ba64cb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -38,6 +38,7 @@ issues: - forcetypeassert - funlen - goerr113 + - dupl linters: # please, do not use `enable-all`: it's deprecated and will be removed soon. diff --git a/opencdc/data.go b/opencdc/data.go index 571eee1..3b7c8b3 100644 --- a/opencdc/data.go +++ b/opencdc/data.go @@ -19,7 +19,6 @@ import ( "fmt" opencdcv1 "github.com/conduitio/conduit-commons/proto/opencdc/v1" - "github.com/goccy/go-json" ) diff --git a/opencdc/errors.go b/opencdc/errors.go index 201bff7..4b47984 100644 --- a/opencdc/errors.go +++ b/opencdc/errors.go @@ -25,4 +25,8 @@ var ( // ErrUnknownOperation is returned when trying to parse an Operation string // and encountering an unknown operation. ErrUnknownOperation = errors.New("unknown operation") + + // ErrInvalidProtoDataType is returned when trying to convert a proto data + // type to raw or structured data. + ErrInvalidProtoDataType = errors.New("invalid proto data type") ) diff --git a/opencdc/fromproto.go b/opencdc/fromproto.go index 8211914..1c4c993 100644 --- a/opencdc/fromproto.go +++ b/opencdc/fromproto.go @@ -15,7 +15,6 @@ package opencdc import ( - "errors" "fmt" opencdcv1 "github.com/conduitio/conduit-commons/proto/opencdc/v1" @@ -79,7 +78,7 @@ func (c *Change) FromProto(proto *opencdcv1.Change) error { func dataFromProto(proto *opencdcv1.Data) (Data, error) { if proto == nil { - return nil, nil + return nil, nil //nolint:nilnil // This is the expected behavior. } switch v := proto.Data.(type) { @@ -88,8 +87,8 @@ func dataFromProto(proto *opencdcv1.Data) (Data, error) { case *opencdcv1.Data_StructuredData: return StructuredData(v.StructuredData.AsMap()), nil case nil: - return nil, nil + return nil, nil //nolint:nilnil // This is the expected behavior. default: - return nil, errors.New("invalid Data type") + return nil, ErrInvalidProtoDataType } } diff --git a/opencdc/fromproto_test.go b/opencdc/fromproto_test.go index 19c2dc1..ac1e767 100644 --- a/opencdc/fromproto_test.go +++ b/opencdc/fromproto_test.go @@ -32,18 +32,20 @@ func TestRecord_FromProto(t *testing.T) { Key: &opencdcv1.Data{Data: &opencdcv1.Data_RawData{RawData: []byte("padlock-key")}}, Payload: &opencdcv1.Change{ Before: &opencdcv1.Data{Data: &opencdcv1.Data_RawData{RawData: []byte("yellow")}}, - After: &opencdcv1.Data{Data: &opencdcv1.Data_StructuredData{StructuredData: &structpb.Struct{ - Fields: map[string]*structpb.Value{ - "bool": {Kind: &structpb.Value_BoolValue{BoolValue: true}}, - "int": {Kind: &structpb.Value_NumberValue{NumberValue: 1}}, - "int32": {Kind: &structpb.Value_NumberValue{NumberValue: 1}}, - "int64": {Kind: &structpb.Value_NumberValue{NumberValue: 1}}, - "float32": {Kind: &structpb.Value_NumberValue{NumberValue: 1.2}}, - "float64": {Kind: &structpb.Value_NumberValue{NumberValue: 1.2}}, - "string": {Kind: &structpb.Value_StringValue{StringValue: "orange"}}, - }, - }}, - }}, + After: &opencdcv1.Data{ + Data: &opencdcv1.Data_StructuredData{StructuredData: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + "bool": {Kind: &structpb.Value_BoolValue{BoolValue: true}}, + "int": {Kind: &structpb.Value_NumberValue{NumberValue: 1}}, + "int32": {Kind: &structpb.Value_NumberValue{NumberValue: 1}}, + "int64": {Kind: &structpb.Value_NumberValue{NumberValue: 1}}, + "float32": {Kind: &structpb.Value_NumberValue{NumberValue: 1.2}}, + "float64": {Kind: &structpb.Value_NumberValue{NumberValue: 1.2}}, + "string": {Kind: &structpb.Value_StringValue{StringValue: "orange"}}, + }, + }}, + }, + }, } want := Record{ @@ -78,18 +80,20 @@ func BenchmarkRecord_FromProto_Structured(b *testing.B) { Key: &opencdcv1.Data{Data: &opencdcv1.Data_RawData{RawData: []byte("padlock-key")}}, Payload: &opencdcv1.Change{ Before: &opencdcv1.Data{Data: &opencdcv1.Data_RawData{RawData: []byte("yellow")}}, - After: &opencdcv1.Data{Data: &opencdcv1.Data_StructuredData{StructuredData: &structpb.Struct{ - Fields: map[string]*structpb.Value{ - "bool": {Kind: &structpb.Value_BoolValue{BoolValue: true}}, - "int": {Kind: &structpb.Value_NumberValue{NumberValue: 1}}, - "int32": {Kind: &structpb.Value_NumberValue{NumberValue: 1}}, - "int64": {Kind: &structpb.Value_NumberValue{NumberValue: 1}}, - "float32": {Kind: &structpb.Value_NumberValue{NumberValue: 1.2}}, - "float64": {Kind: &structpb.Value_NumberValue{NumberValue: 1.2}}, - "string": {Kind: &structpb.Value_StringValue{StringValue: "orange"}}, - }, - }}, - }}, + After: &opencdcv1.Data{ + Data: &opencdcv1.Data_StructuredData{StructuredData: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + "bool": {Kind: &structpb.Value_BoolValue{BoolValue: true}}, + "int": {Kind: &structpb.Value_NumberValue{NumberValue: 1}}, + "int32": {Kind: &structpb.Value_NumberValue{NumberValue: 1}}, + "int64": {Kind: &structpb.Value_NumberValue{NumberValue: 1}}, + "float32": {Kind: &structpb.Value_NumberValue{NumberValue: 1.2}}, + "float64": {Kind: &structpb.Value_NumberValue{NumberValue: 1.2}}, + "string": {Kind: &structpb.Value_StringValue{StringValue: "orange"}}, + }, + }}, + }, + }, } // reuse the same target record diff --git a/opencdc/toproto.go b/opencdc/toproto.go index 08c9b3f..59bf403 100644 --- a/opencdc/toproto.go +++ b/opencdc/toproto.go @@ -103,7 +103,7 @@ func (d StructuredData) ToProto(proto *opencdcv1.Data) error { } data, err := structpb.NewStruct(d) if err != nil { - return err + return fmt.Errorf("could not convert structured data to proto: %w", err) } protoStructuredData.StructuredData = data return nil diff --git a/opencdc/toproto_test.go b/opencdc/toproto_test.go index 66fc917..2d0523a 100644 --- a/opencdc/toproto_test.go +++ b/opencdc/toproto_test.go @@ -50,7 +50,7 @@ func TestRecord_ToProto(t *testing.T) { after, err := structpb.NewStruct(r1.Payload.After.(StructuredData)) is.NoErr(err) - want := opencdcv1.Record{ + want := &opencdcv1.Record{ Position: r1.Position, Operation: opencdcv1.Operation(r1.Operation), Metadata: r1.Metadata, @@ -64,16 +64,16 @@ func TestRecord_ToProto(t *testing.T) { var got opencdcv1.Record err = r1.ToProto(&got) is.NoErr(err) - is.Equal(got, want) + is.Equal(&got, want) // writing another record to the same target should overwrite the previous - want2 := opencdcv1.Record{ + want2 := &opencdcv1.Record{ Payload: &opencdcv1.Change{}, // there's always a change } err = Record{}.ToProto(&got) is.NoErr(err) - is.Equal(got, want2) + is.Equal(&got, want2) } func BenchmarkRecord_ToProto_Structured(b *testing.B) { @@ -106,7 +106,6 @@ func BenchmarkRecord_ToProto_Structured(b *testing.B) { for i := 0; i < b.N; i++ { _ = r1.ToProto(&r2) } - _ = r2 } func BenchmarkRecord_ToProto_Raw(b *testing.B) { @@ -130,7 +129,6 @@ func BenchmarkRecord_ToProto_Raw(b *testing.B) { for i := 0; i < b.N; i++ { _ = r1.ToProto(&r2) } - _ = r2 }) } } diff --git a/proto/opencdc/v1/opencdc.proto b/proto/opencdc/v1/opencdc.proto index de08865..a92e381 100644 --- a/proto/opencdc/v1/opencdc.proto +++ b/proto/opencdc/v1/opencdc.proto @@ -90,5 +90,4 @@ message Data { // Structured data contains data in form of a struct with fields. google.protobuf.Struct structured_data = 2; } - // TODO schema will be added here in future iterations. }