From ee4d09e62026c3821cdd61b2ad86742d14828c22 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Mon, 21 Oct 2024 21:01:29 -0700 Subject: [PATCH] More refactor. --- lib/debezium/converters/time.go | 26 ++++++++++---------------- lib/debezium/converters/time_test.go | 2 +- lib/debezium/schema_test.go | 20 +++++++++++++++----- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/lib/debezium/converters/time.go b/lib/debezium/converters/time.go index 0d24ffc0a..f34680e89 100644 --- a/lib/debezium/converters/time.go +++ b/lib/debezium/converters/time.go @@ -114,31 +114,25 @@ func (ZonedTimestamp) Convert(value any) (any, error) { return nil, fmt.Errorf("failed to parse %q, err: %w", valString, err) } -var SupportedTimeWithTimezoneFormats = []string{ - "15:04:05Z", // w/o fractional seconds - "15:04:05.000Z", // ms - "15:04:05.000000Z", // microseconds -} - type TimeWithTimezone struct{} -func (TimeWithTimezone) ToKindDetails() typing.KindDetails { - return typing.NewExtendedTimeDetails(typing.ETime, ext.TimeKindType, "") +func (t TimeWithTimezone) layout() string { + return "15:04:05.999999Z" +} + +func (t TimeWithTimezone) ToKindDetails() typing.KindDetails { + return typing.NewExtendedTimeDetails(typing.ETime, ext.TimeKindType, t.layout()) } -func (TimeWithTimezone) Convert(value any) (any, error) { +func (t TimeWithTimezone) Convert(value any) (any, error) { valString, isOk := value.(string) if !isOk { return nil, fmt.Errorf("expected string got '%v' with type %T", value, value) } - var err error - var ts time.Time - for _, supportedTimeFormat := range SupportedTimeWithTimezoneFormats { - ts, err = ext.ParseTimeExactMatch(supportedTimeFormat, valString) - if err == nil { - return ext.NewExtendedTime(ts, ext.TimeKindType, supportedTimeFormat), nil - } + ts, err := time.Parse(t.layout(), valString) + if err == nil { + return ext.NewExtendedTime(ts, ext.TimeKindType, t.layout()), nil } return nil, fmt.Errorf("failed to parse %q: %w", valString, err) diff --git a/lib/debezium/converters/time_test.go b/lib/debezium/converters/time_test.go index 947844d57..8bba7cd2c 100644 --- a/lib/debezium/converters/time_test.go +++ b/lib/debezium/converters/time_test.go @@ -168,7 +168,7 @@ func TestConvertTimeWithTimezone(t *testing.T) { assert.True(t, isOk) assert.NoError(t, err) - expectedTs := ext.NewExtendedTime(time.Date(0, 1, 1, 23, 2, 6, 745116000, time.UTC), ext.TimeKindType, "15:04:05.000000Z") + expectedTs := ext.NewExtendedTime(time.Date(0, 1, 1, 23, 2, 6, 745116000, time.UTC), ext.TimeKindType, "15:04:05.999999Z") assert.Equal(t, expectedTs, ts) } { diff --git a/lib/debezium/schema_test.go b/lib/debezium/schema_test.go index e990c53c0..109d51c6d 100644 --- a/lib/debezium/schema_test.go +++ b/lib/debezium/schema_test.go @@ -254,11 +254,21 @@ func TestField_ToKindDetails(t *testing.T) { } } { - // Time - for _, dbzType := range []SupportedDebeziumType{Time, TimeKafkaConnect, TimeWithTimezone} { - kd, err := Field{DebeziumType: dbzType}.ToKindDetails() - assert.NoError(t, err) - assert.Equal(t, typing.NewExtendedTimeDetails(typing.ETime, ext.TimeKindType, ""), kd, dbzType) + { + // Time with time zone + for _, dbzType := range []SupportedDebeziumType{TimeWithTimezone} { + kd, err := Field{DebeziumType: dbzType}.ToKindDetails() + assert.NoError(t, err) + assert.Equal(t, typing.NewExtendedTimeDetails(typing.ETime, ext.TimeKindType, "15:04:05.999999Z"), kd, dbzType) + } + } + { + // Time + for _, dbzType := range []SupportedDebeziumType{Time, TimeKafkaConnect} { + kd, err := Field{DebeziumType: dbzType}.ToKindDetails() + assert.NoError(t, err) + assert.Equal(t, typing.NewExtendedTimeDetails(typing.ETime, ext.TimeKindType, ""), kd, dbzType) + } } {