Skip to content

Commit

Permalink
fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lovromazgon committed Dec 21, 2023
1 parent f61e0e7 commit ff18d39
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 37 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ issues:
- forcetypeassert
- funlen
- goerr113
- dupl

linters:
# please, do not use `enable-all`: it's deprecated and will be removed soon.
Expand Down
1 change: 0 additions & 1 deletion opencdc/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"

opencdcv1 "github.com/conduitio/conduit-commons/proto/opencdc/v1"

"github.com/goccy/go-json"
)

Expand Down
4 changes: 4 additions & 0 deletions opencdc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
7 changes: 3 additions & 4 deletions opencdc/fromproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package opencdc

import (
"errors"
"fmt"

opencdcv1 "github.com/conduitio/conduit-commons/proto/opencdc/v1"
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
}
52 changes: 28 additions & 24 deletions opencdc/fromproto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion opencdc/toproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions opencdc/toproto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -130,7 +129,6 @@ func BenchmarkRecord_ToProto_Raw(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = r1.ToProto(&r2)
}
_ = r2
})
}
}
1 change: 0 additions & 1 deletion proto/opencdc/v1/opencdc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

0 comments on commit ff18d39

Please sign in to comment.