Skip to content

Commit

Permalink
Consider time.Time as a default value (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Oct 31, 2024
1 parent 2b16ce3 commit a34dac1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/debezium/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"fmt"
"log/slog"
"time"

"github.com/google/uuid"

Expand Down Expand Up @@ -89,23 +90,29 @@ func toInt64(value any) (int64, error) {
return 0, fmt.Errorf("failed to cast value '%v' with type '%T' to int64", value, value)
}

func isTimeSet(ts time.Time) bool {
// Most of Debezium's time types uses Unix time, so we can check for zero value by comparing it to be zero.
if ts.Unix() == 0 {
return false
}

// If time value did not get set, it will return true for [IsZero]
if ts.IsZero() {
return false
}

return true
}

// ShouldSetDefaultValue will filter out computed fields that cannot be properly set with a default value
func (f Field) ShouldSetDefaultValue(defaultValue any) bool {
switch castedDefaultValue := defaultValue.(type) {
case nil:
return false
case time.Time:
return isTimeSet(castedDefaultValue)
case *ext.ExtendedTime:
// Most of Debezium's time types uses Unix time, so we can check for zero value by comparing it to be zero.
if castedDefaultValue.GetTime().Unix() == 0 {
return false
}

// If time value did not get set, it will return true for [IsZero]
if castedDefaultValue.GetTime().IsZero() {
return false
}

return true
return isTimeSet(castedDefaultValue.GetTime())
case string:
if f.DebeziumType == UUID && castedDefaultValue == uuid.Nil.String() {
return false
Expand Down
8 changes: 8 additions & 0 deletions lib/debezium/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func TestField_ShouldSetDefaultValue(t *testing.T) {
var ts time.Time
assert.False(t, field.ShouldSetDefaultValue(ext.NewExtendedTime(ts, ext.TimestampTZKindType, ext.RFC3339Millisecond)))
}
{
// time.Time
field := Field{}
assert.True(t, field.ShouldSetDefaultValue(time.Now()))

assert.False(t, field.ShouldSetDefaultValue(time.Time{}))
assert.False(t, field.ShouldSetDefaultValue(time.Unix(0, 0)))
}
}

func TestToInt64(t *testing.T) {
Expand Down

0 comments on commit a34dac1

Please sign in to comment.