From acacce0d49e932b513da27473eb9c77f9b2e2586 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Tue, 22 Oct 2024 09:31:15 -0700 Subject: [PATCH] Refactor. --- lib/debezium/converters/time.go | 35 +++++++++------------------- lib/debezium/converters/time_test.go | 32 ++++++++++++++++--------- lib/debezium/schema_test.go | 2 +- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/lib/debezium/converters/time.go b/lib/debezium/converters/time.go index 0d24ffc0a..97a01e71f 100644 --- a/lib/debezium/converters/time.go +++ b/lib/debezium/converters/time.go @@ -65,26 +65,17 @@ func (m MicroTime) Convert(value any) (any, error) { return ext.NewExtendedTime(time.UnixMicro(castedVal).In(time.UTC), ext.TimeKindType, m.layout()), nil } -var SupportedDateTimeWithTimezoneFormats = []string{ - "2006-01-02T15:04:05Z", // w/o fractional seconds - "2006-01-02T15:04:05.0Z", // 1 digit - "2006-01-02T15:04:05.00Z", // 2 digits - "2006-01-02T15:04:05.000Z", // 3 digits - "2006-01-02T15:04:05.0000Z", // 4 digits - "2006-01-02T15:04:05.00000Z", // 5 digits - "2006-01-02T15:04:05.000000Z", // 6 digits - "2006-01-02T15:04:05.0000000Z", // 7 digits - "2006-01-02T15:04:05.00000000Z", // 8 digits - "2006-01-02T15:04:05.000000000Z", // 9 digits -} - type ZonedTimestamp struct{} -func (ZonedTimestamp) ToKindDetails() typing.KindDetails { - return typing.NewExtendedTimeDetails(typing.ETime, ext.TimestampTZKindType, "") +func (ZonedTimestamp) layout() string { + return "2006-01-02T15:04:05.999999999Z" } -func (ZonedTimestamp) Convert(value any) (any, error) { +func (z ZonedTimestamp) ToKindDetails() typing.KindDetails { + return typing.NewExtendedTimeDetails(typing.ETime, ext.TimestampTZKindType, z.layout()) +} + +func (z ZonedTimestamp) Convert(value any) (any, error) { valString, isOk := value.(string) if !isOk { return nil, fmt.Errorf("expected string got '%v' with type %T", value, value) @@ -102,16 +93,12 @@ func (ZonedTimestamp) Convert(value any) (any, error) { } } - var err error - var ts time.Time - for _, supportedFormat := range SupportedDateTimeWithTimezoneFormats { - ts, err = ext.ParseTimeExactMatch(supportedFormat, valString) - if err == nil { - return ext.NewExtendedTime(ts, ext.TimestampTZKindType, supportedFormat), nil - } + _time, err := time.Parse(z.layout(), valString) + if err != nil { + return nil, fmt.Errorf("failed to parse %q: %w", valString, err) } - return nil, fmt.Errorf("failed to parse %q, err: %w", valString, err) + return ext.NewExtendedTime(_time, ext.TimestampTZKindType, z.layout()), nil } var SupportedTimeWithTimezoneFormats = []string{ diff --git a/lib/debezium/converters/time_test.go b/lib/debezium/converters/time_test.go index 947844d57..15ec2e88d 100644 --- a/lib/debezium/converters/time_test.go +++ b/lib/debezium/converters/time_test.go @@ -32,83 +32,93 @@ func TestZonedTimestamp_Convert(t *testing.T) { // Valid { // No fractional seconds - val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00Z") + val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:12Z") assert.NoError(t, err) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 000000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05Z") + expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 12, 000000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z") assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) + assert.Equal(t, "2025-09-13T00:00:12Z", val.(*ext.ExtendedTime).String("")) } { // 1 digits val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.1Z") assert.NoError(t, err) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 100000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.0Z") + expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 100000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z") assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) + assert.Equal(t, "2025-09-13T00:00:00.1Z", val.(*ext.ExtendedTime).String("")) } { // 2 digits val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.12Z") assert.NoError(t, err) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 120000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.00Z") + expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 120000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z") assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) + assert.Equal(t, "2025-09-13T00:00:00.12Z", val.(*ext.ExtendedTime).String("")) } { // 3 digits val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.123Z") assert.NoError(t, err) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.000Z") + expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z") assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) + assert.Equal(t, "2025-09-13T00:00:00.123Z", val.(*ext.ExtendedTime).String("")) } { // 4 digits val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.1234Z") assert.NoError(t, err) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123400000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.0000Z") + expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123400000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z") assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) + assert.Equal(t, "2025-09-13T00:00:00.1234Z", val.(*ext.ExtendedTime).String("")) } { // 5 digits val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.12345Z") assert.NoError(t, err) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123450000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.00000Z") + expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123450000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z") assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) + assert.Equal(t, "2025-09-13T00:00:00.12345Z", val.(*ext.ExtendedTime).String("")) } { // 6 digits (microseconds) val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.123456Z") assert.NoError(t, err) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.000000Z") + expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z") assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) + assert.Equal(t, "2025-09-13T00:00:00.123456Z", val.(*ext.ExtendedTime).String("")) } { // 7 digits val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.1234567Z") assert.NoError(t, err) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456700, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.0000000Z") + expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456700, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z") assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) + assert.Equal(t, "2025-09-13T00:00:00.1234567Z", val.(*ext.ExtendedTime).String("")) } { // 8 digits val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.12345678Z") assert.NoError(t, err) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456780, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.00000000Z") + expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456780, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z") assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) + assert.Equal(t, "2025-09-13T00:00:00.12345678Z", val.(*ext.ExtendedTime).String("")) } { // 9 digits (nanoseconds) val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.123456789Z") assert.NoError(t, err) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456789, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.000000000Z") + expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456789, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z") assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) + assert.Equal(t, "2025-09-13T00:00:00.123456789Z", val.(*ext.ExtendedTime).String("")) } } } diff --git a/lib/debezium/schema_test.go b/lib/debezium/schema_test.go index e990c53c0..c8ba9c724 100644 --- a/lib/debezium/schema_test.go +++ b/lib/debezium/schema_test.go @@ -233,7 +233,7 @@ func TestField_ToKindDetails(t *testing.T) { for _, dbzType := range []SupportedDebeziumType{ZonedTimestamp} { kd, err := Field{DebeziumType: dbzType}.ToKindDetails() assert.NoError(t, err) - assert.Equal(t, typing.NewExtendedTimeDetails(typing.ETime, ext.TimestampTZKindType, ""), kd) + assert.Equal(t, typing.NewExtendedTimeDetails(typing.ETime, ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z"), kd) } } {