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

Run processor examples, gather specifications #1384

Merged
merged 8 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 3 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ run:
timeout: 3m
skip-dirs-use-default: false
skip-dirs:
- /examples/
- /ui/.*
- ^examples/
- ^ui/
- ^pkg/plugin/processor/builtin/internal/diff # external code

linters-settings:
depguard:
Expand Down
4 changes: 2 additions & 2 deletions 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-20240112191423-58fcb3055cf2
github.com/conduitio/conduit-commons v0.0.0-20240215172003-10082a421fa6
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 All @@ -17,7 +17,7 @@ require (
github.com/conduitio/conduit-connector-protocol v0.5.1-0.20240104160905-e9e61586fb8d
github.com/conduitio/conduit-connector-s3 v0.5.1
github.com/conduitio/conduit-connector-sdk v0.8.0
github.com/conduitio/conduit-processor-sdk v0.0.0-20240208142744-ba2d1942a9bd
github.com/conduitio/conduit-processor-sdk v0.0.0-20240215190258-450d7813d529
github.com/conduitio/yaml/v3 v3.3.0
github.com/dgraph-io/badger/v4 v4.2.0
github.com/gammazero/deque v0.2.1
Expand Down
8 changes: 4 additions & 4 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-20240112191423-58fcb3055cf2 h1:KWVS2fHWB1NI3hdUg1xxzTB0TT7iNPUt35O8IoW1p5o=
github.com/conduitio/conduit-commons v0.0.0-20240112191423-58fcb3055cf2/go.mod h1:43mPxHKxsZbmqf1Gw+hpx3ByXAmp8doWTQNNfCoyXp8=
github.com/conduitio/conduit-commons v0.0.0-20240215172003-10082a421fa6 h1:SDl2/gBXHQBVKhNcMfIiZH7hv9/MBt497SMjGCcb7Jg=
github.com/conduitio/conduit-commons v0.0.0-20240215172003-10082a421fa6/go.mod h1:MyXrpmK8n3aAQeICqiO/ky9ysILILatfYeZnM34+4QY=
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 All @@ -1095,8 +1095,8 @@ github.com/conduitio/conduit-connector-s3 v0.5.1 h1:yRo8004ryCIZc/S3iWQ1rN6pm6bj
github.com/conduitio/conduit-connector-s3 v0.5.1/go.mod h1:nbxzsyS95gbFJ28Job9vFFB+byRFINSv70/13Yi4mKQ=
github.com/conduitio/conduit-connector-sdk v0.8.0 h1:gvchqoj5d3AQsBoIosx4i32L8Ex9+5BuAyHi/IM9VD4=
github.com/conduitio/conduit-connector-sdk v0.8.0/go.mod h1:nOz4K3X6fD8YMe5CPbULwSEE18Eu02ZrpT6o6KwQfxs=
github.com/conduitio/conduit-processor-sdk v0.0.0-20240208142744-ba2d1942a9bd h1:P1ISUuykm1/q9JqceqhSSW4p7EZ0+9acXOhhNl6hBTw=
github.com/conduitio/conduit-processor-sdk v0.0.0-20240208142744-ba2d1942a9bd/go.mod h1:Bsy8AhU+INPr7KIkwASnvmMzF3lCIaeNfgK/+YzbNb8=
github.com/conduitio/conduit-processor-sdk v0.0.0-20240215190258-450d7813d529 h1:omvTpesnnkUd2wwY3AUxVc4hxPLpABI6kpxfLgRCwjk=
github.com/conduitio/conduit-processor-sdk v0.0.0-20240215190258-450d7813d529/go.mod h1:2EfkE4hx0vcVwleKic+PPljd1UVwSPZCBdKDId4EE6g=
github.com/conduitio/yaml/v3 v3.3.0 h1:kbbaOSHcuH39gP4+rgbJGl6DSbLZcJgEaBvkEXJlCsI=
github.com/conduitio/yaml/v3 v3.3.0/go.mod h1:JNgFMOX1t8W4YJuRZOh6GggVtSMsgP9XgTw+7dIenpc=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
Expand Down
88 changes: 88 additions & 0 deletions pkg/plugin/processor/builtin/examples_exporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright © 2024 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build export_processors

package builtin

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

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

func TestMain(m *testing.M) {
code := m.Run()
if code > 0 {
os.Exit(code)
}

// tests passed, export the processors
const outputFile = "processors.json"

f, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("failed to open %s: %v", outputFile, err)
}
defer f.Close()

exportProcessors(f)
}

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)
}

_, err = output.Write(bytes)
if err != nil {
log.Fatalf("failed to write processors to output: %v", err)
}
}

func sortProcessors(processors map[string]*procInfo) []*procInfo {
names := make([]string, 0, len(processors))
for k, _ := range processors {
names = append(names, k)
}
sort.Strings(names)

sorted := make([]*procInfo, len(names))
for i, name := range names {
// also sort examples for each processor
proc := processors[name]
proc.Examples = sortExamples(proc.Examples)
sorted[i] = proc
}

return sorted
}

func sortExamples(examples []example) []example {
sort.Slice(examples, func(i, j int) bool {
if examples[i].Order != examples[j].Order {
return examples[i].Order < examples[j].Order
}
return strings.Compare(examples[i].Description, examples[j].Description) < 0
})
return examples
}
115 changes: 115 additions & 0 deletions pkg/plugin/processor/builtin/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright © 2024 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:generate go test -count=1 -tags export_processors .

package builtin

import (
"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/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

// -- HELPERS ------------------------------------------------------------------

var processors = map[string]*procInfo{}

type procInfo struct {
Specification sdk.Specification `json:"specification"`
Examples []example `json:"examples"`
}

type example struct {
// Order is an optional field that is used to order examples in the
// documentation. If omitted, the example will be ordered by description.
Order int `json:"-"`
Description string `json:"description"`
Config map[string]string `json:"config"`
Have opencdc.Record `json:"have"`
Want sdk.ProcessedRecord `json:"want"`
}

// RunExample runs the given example with the given processor and logs the
// result. It is intended to be used in example functions. Additionally, it
// stores the processor specification and example in a global map so it can be
// used to generate documentation.
func RunExample(p sdk.Processor, e example) {
spec, err := p.Specification()
if err != nil {
log.Fatalf("failed to fetch specification: %v", err)
}

pi, ok := processors[spec.Name]
if !ok {
pi = &procInfo{Specification: spec}
processors[spec.Name] = pi
}

ctx := context.Background()
err = p.Configure(ctx, e.Config)
if err != nil {
log.Fatalf("failed to configure processor: %v", err)
}

err = p.Open(ctx)
if err != nil {
log.Fatalf("failed to open processor: %v", err)
}

got := p.Process(ctx, []opencdc.Record{e.Have.Clone()})
if len(got) != 1 {
log.Fatalf("expected 1 record to be returned, got %d", len(got))
}

if d := cmp.Diff(e.Want, got[0], cmpopts.IgnoreUnexported(sdk.SingleRecord{})); d != "" {
log.Fatalf("processed record did not match expectation:\n%v", d)
}

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

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

edits := diff.Strings(string(havePrettyJSON), string(gotPrettyJSON))
unified, err := diff.ToUnified("before", "after", string(havePrettyJSON)+"\n", edits, 100)
if err != nil {
log.Fatalf("failed to produce unified diff: %v", err)
}

fmt.Printf("processor transformed record:\n%s\n", unified)
case sdk.FilterRecord:
fmt.Println("processor filtered record out")
case sdk.ErrorRecord:
fmt.Printf("processor returned error: %s\n", rec.Error)
}

// append example to processor
pi.Examples = append(pi.Examples, e)
}
18 changes: 18 additions & 0 deletions pkg/plugin/processor/builtin/internal/diff/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Diff
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package doesn't really need to be reviewed, see readme.


This package contains code taken from <https://github.com/golang/tools/tree/master/internal/diff>
on February 15th, 2024. We need the code to create a unified diff between two strings.

The code is left as-is, except 3 changes:

- The imports were changed to reference the Conduit module path. This was done
using the following command:

```sh
find . -type f -exec sed -i '' 's/golang.org\/x\/tools\/internal/github.com\/conduitio\/conduit\/pkg\/plugin\/processor\/builtin\/internal/g' {} +
```

- The package `golang.org/x/tools/internal/diff/myers` was removed, as it's deprecated.

- The package `golang.org/x/tools/internal/testenv` was added into the `diff` package,
as that's the only place it's used. It also only includes the required functions.
Loading
Loading