From 401e574a1dc983b190c2f46494abbe0c985dd590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lovro=20Ma=C5=BEgon?= Date: Thu, 21 Dec 2023 19:04:41 +0100 Subject: [PATCH] make linter happy --- opencdc/fromjson.go | 23 ++++++++++++++++------- opencdc/fromjson_test.go | 26 ++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/opencdc/fromjson.go b/opencdc/fromjson.go index 284683c..2935fe6 100644 --- a/opencdc/fromjson.go +++ b/opencdc/fromjson.go @@ -14,7 +14,11 @@ package opencdc -import "github.com/goccy/go-json" +import ( + "fmt" + + "github.com/goccy/go-json" +) func (r *Record) UnmarshalJSON(b []byte) error { var raw struct { @@ -30,7 +34,7 @@ func (r *Record) UnmarshalJSON(b []byte) error { err := json.Unmarshal(b, &raw) if err != nil { - return err + return err //nolint:wrapcheck // no additional context to add } key, err := dataUnmarshalJSON(raw.Key) @@ -64,10 +68,15 @@ func dataUnmarshalJSON(b []byte) (Data, error) { if b[0] == '"' { var data RawData err := json.Unmarshal(b, &data) - return data, err - } else { - var data StructuredData - err := json.Unmarshal(b, &data) - return data, err + if err != nil { + return nil, fmt.Errorf("failed to unmarshal raw data: %w", err) + } + return data, nil + } + var data StructuredData + err := json.Unmarshal(b, &data) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal structured data: %w", err) } + return data, nil } diff --git a/opencdc/fromjson_test.go b/opencdc/fromjson_test.go index 6de1263..fdbaa93 100644 --- a/opencdc/fromjson_test.go +++ b/opencdc/fromjson_test.go @@ -25,7 +25,7 @@ import ( func TestRecord_UnmarshalJSON(t *testing.T) { is := is.New(t) - want := Record{ + have := Record{ Position: Position("standing"), Operation: OperationUpdate, Metadata: Metadata{"foo": "bar"}, @@ -46,7 +46,29 @@ func TestRecord_UnmarshalJSON(t *testing.T) { }, }, } - b, err := json.Marshal(want) + want := 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.0, + "int32": 1.0, + "int64": 1.0, + + "float32": 1.2, + "float64": 1.2, + + "string": "orange", + }, + }, + } + + b, err := json.Marshal(have) is.NoErr(err) var got Record