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

Tests and examples for converting time using field.convert #1745

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 pkg/plugin/processor/builtin/impl/field/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (p *convertProcessor) stringToType(value, typ string) (any, error) {
unixnano, err := strconv.Atoi(value)
if err == nil {
// it's a number, use it as a unix nanosecond timestamp
return time.Unix(0, int64(unixnano)), nil
return time.Unix(0, int64(unixnano)).UTC(), nil
}
// try to parse it as a time string
return time.Parse(time.RFC3339Nano, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package field

import (
"time"

"github.com/conduitio/conduit-commons/config"
"github.com/conduitio/conduit-commons/opencdc"
sdk "github.com/conduitio/conduit-processor-sdk"
Expand Down Expand Up @@ -144,3 +146,46 @@ func ExampleConvertProcessor_floatToString() {
// }
// }
}

//nolint:govet // a more descriptive example description
func ExampleConvertProcessor_intTotime() {
p := NewConvertProcessor(log.Nop())

timeObj := time.Date(2024, 1, 2, 12, 34, 56, 123456789, time.UTC)

exampleutil.RunExample(p, exampleutil.Example{
Summary: "Convert `string` to `time`",
hariso marked this conversation as resolved.
Show resolved Hide resolved
Description: "This example takes an `int` in field `.Payload.After.createdAt` and parses it as a unix timestamp into a `time.Time` value.",
Config: config.Config{"field": ".Payload.After.createdAt", "type": "time"},
Have: opencdc.Record{
Operation: opencdc.OperationCreate,
Key: opencdc.StructuredData{"id": 123.345},
Payload: opencdc.Change{After: opencdc.StructuredData{"createdAt": timeObj.UnixNano()}},
},
Want: sdk.SingleRecord{
Operation: opencdc.OperationCreate,
Key: opencdc.StructuredData{"id": 123.345},
Payload: opencdc.Change{After: opencdc.StructuredData{"createdAt": timeObj}},
}})

// Output:
// processor transformed record:
// --- before
// +++ after
// @@ -1,14 +1,14 @@
// {
// "position": null,
// "operation": "create",
// "metadata": null,
// "key": {
// "id": 123.345
// },
// "payload": {
// "before": null,
// "after": {
// - "createdAt": 1704198896123456789
// + "createdAt": "2024-01-02T12:34:56.123456789Z"
// }
// }
// }
}
29 changes: 29 additions & 0 deletions pkg/plugin/processor/builtin/impl/field/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"strings"
"testing"
"time"

"github.com/conduitio/conduit-commons/config"
"github.com/conduitio/conduit-commons/opencdc"
Expand Down Expand Up @@ -208,6 +209,26 @@ func TestConvertField_Process(t *testing.T) {
want: sdk.SingleRecord{
Key: opencdc.StructuredData{"id": "foo"},
},
}, {
name: "int to time",
field: ".Key.id",
typ: "time",
record: opencdc.Record{
Key: opencdc.StructuredData{"id": 1611254412345678999},
},
want: sdk.SingleRecord{
Key: opencdc.StructuredData{"id": time.Date(2021, 1, 21, 18, 40, 12, 345678999, time.UTC)},
},
}, {
name: "string to time",
field: ".Key.id",
typ: "time",
record: opencdc.Record{
Key: opencdc.StructuredData{"id": "2021-01-21T18:40:12.345678999Z"},
},
want: sdk.SingleRecord{
Key: opencdc.StructuredData{"id": time.Date(2021, 1, 21, 18, 40, 12, 345678999, time.UTC)},
},
},
}
for _, tc := range testCases {
Expand Down Expand Up @@ -259,6 +280,14 @@ func TestConvertField_ProcessFail(t *testing.T) {
Key: opencdc.StructuredData{"id": 9999999999999999999.0},
},
wantErr: "value out of range",
}, {
name: "string to time, invalid format",
field: ".Key.id",
typ: "time",
record: opencdc.Record{
Key: opencdc.StructuredData{"id": "21.01.2021 18:40:12"},
},
wantErr: `parsing time "21.01.2021 18:40:12" as "2006-01-02T15:04:05.999999999Z07:00": cannot parse "21.01.2021 18:40:12" as "2006"`,
},
}
for _, tc := range testCases {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@
}
},
"examples": [
{
"summary": "Convert `string` to `time`",
"description": "This example takes an `int` in field `.Payload.After.createdAt` and parses it as a unix timestamp into a `time.Time` value.",
"config": {
"field": ".Payload.After.createdAt",
"type": "time"
},
"have": {
"position": null,
"operation": "create",
"metadata": null,
"key": {
"id": 123.345
},
"payload": {
"before": null,
"after": {
"createdAt": 1704198896123456789
}
}
},
"want": {
"position": null,
"operation": "create",
"metadata": null,
"key": {
"id": 123.345
},
"payload": {
"before": null,
"after": {
"createdAt": "2024-01-02T12:34:56.123456789Z"
}
}
}
},
{
"summary": "Convert `float` to `string`",
"description": "This example takes the `float` in field `.Key.id` and changes its data type to `string`.",
Expand Down