Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BP: Display processor example raw data as string #1397

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/NYTimes/gziphandler v1.1.1
github.com/bufbuild/buf v1.29.0
github.com/conduitio/conduit-commons v0.0.0-20240216175021-58737e44eb62
github.com/conduitio/conduit-commons v0.0.0-20240228185905-49ce3f31fbcd
github.com/conduitio/conduit-connector-file v0.6.0
github.com/conduitio/conduit-connector-generator v0.5.0
github.com/conduitio/conduit-connector-kafka v0.7.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1077,8 +1077,8 @@ github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWH
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/colinmarc/hdfs/v2 v2.1.1/go.mod h1:M3x+k8UKKmxtFu++uAZ0OtDU8jR3jnaZIAc6yK4Ue0c=
github.com/conduitio/conduit-commons v0.0.0-20240216175021-58737e44eb62 h1:h1AcneviaadJqw1KAUfzIDBVrODma4rneg13q726KQU=
github.com/conduitio/conduit-commons v0.0.0-20240216175021-58737e44eb62/go.mod h1:Hhr5nik71/Wz3yrLjaKSZ2HRQp1wNSOPY2JGGKz7fsw=
github.com/conduitio/conduit-commons v0.0.0-20240228185905-49ce3f31fbcd h1:BuKtwSyIMzhGWkU7diay5vimV4qJfKQZxMhwNjPaygc=
github.com/conduitio/conduit-commons v0.0.0-20240228185905-49ce3f31fbcd/go.mod h1:Hhr5nik71/Wz3yrLjaKSZ2HRQp1wNSOPY2JGGKz7fsw=
github.com/conduitio/conduit-connector-file v0.6.0 h1:8tsGeGhKvFwYQZztOOL5/tmOhVShsfo9lQ3b/0fX8kQ=
github.com/conduitio/conduit-connector-file v0.6.0/go.mod h1:ju7PiB4kTJgqng4KVXDt/Gvw/53kFwSzi5Ez9EDXxNI=
github.com/conduitio/conduit-connector-generator v0.5.0 h1:zpXHif89DCJ13nftKLv31uI2AJGicpY5H1V7SwldRNo=
Expand Down
11 changes: 6 additions & 5 deletions pkg/plugin/processor/builtin/examples_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package builtin

import (
"context"
"io"
"log"
"os"
"sort"
"strings"
"testing"

"github.com/conduitio/conduit-commons/opencdc"
"github.com/goccy/go-json"
)

Expand All @@ -48,12 +50,11 @@ func TestMain(m *testing.M) {
func exportProcessors(output io.Writer) {
sorted := sortProcessors(processors)

bytes, err := json.MarshalIndent(sorted, "", " ")
if err != nil {
log.Fatalf("failed to marshal processors to JSON: %v", err)
}
ctx := opencdc.WithJSONMarshalOptions(context.Background(), &opencdc.JSONMarshalOptions{RawDataAsString: true})
enc := json.NewEncoder(output)
enc.SetIndent("", " ")

_, err = output.Write(bytes)
err := enc.EncodeContext(ctx, sorted)
if err != nil {
log.Fatalf("failed to write processors to output: %v", err)
}
Expand Down
29 changes: 23 additions & 6 deletions pkg/plugin/processor/builtin/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
package builtin

import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"

"github.com/conduitio/conduit-commons/opencdc"
sdk "github.com/conduitio/conduit-processor-sdk"
"github.com/conduitio/conduit/pkg/plugin/processor/builtin/internal/diff"
"github.com/goccy/go-json"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
Expand Down Expand Up @@ -86,15 +87,15 @@ func RunExample(p sdk.Processor, e example) {

switch rec := got[0].(type) {
case sdk.SingleRecord:
// produce JSON diff
havePrettyJSON, err := json.MarshalIndent(e.Have, "", " ")
// Serialize records to pretty JSON for comparison.
havePrettyJSON, err := recordToPrettyJSON(e.Have)
if err != nil {
log.Fatalf("failed to marshal test record to JSON: %v", err)
log.Fatalf("failed to marshal test record to pretty JSON: %v", err)
}

gotPrettyJSON, err := json.MarshalIndent(rec, "", " ")
gotPrettyJSON, err := recordToPrettyJSON(opencdc.Record(rec))
if err != nil {
log.Fatalf("failed to marshal processed record to JSON: %v", err)
log.Fatalf("failed to marshal processed record to pretty JSON: %v", err)
}

edits := diff.Strings(string(havePrettyJSON), string(gotPrettyJSON))
Expand All @@ -113,3 +114,19 @@ func RunExample(p sdk.Processor, e example) {
// append example to processor
pi.Examples = append(pi.Examples, e)
}

func recordToPrettyJSON(r opencdc.Record) ([]byte, error) {
serializer := opencdc.JSONSerializer{RawDataAsString: true}

// Serialize records to pretty JSON for comparison.
haveJSON, err := serializer.Serialize(r)
if err != nil {
return nil, fmt.Errorf("failed to marshal test record to JSON: %w", err)
}
var buf bytes.Buffer
err = json.Indent(&buf, haveJSON, "", " ")
if err != nil {
return nil, fmt.Errorf("failed to indent test record JSON: %w", err)
}
return buf.Bytes(), nil
}
2 changes: 1 addition & 1 deletion pkg/plugin/processor/builtin/processors.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[]
[]
Loading