Skip to content

Commit

Permalink
More refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Oct 22, 2024
1 parent c4d9561 commit ee4d09e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
26 changes: 10 additions & 16 deletions lib/debezium/converters/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/debezium/converters/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
{
Expand Down
20 changes: 15 additions & 5 deletions lib/debezium/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

{
Expand Down

0 comments on commit ee4d09e

Please sign in to comment.