Skip to content

Commit

Permalink
Merge branch 'master' into simplify-typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Sep 4, 2024
2 parents f66403f + 0d0c9b9 commit 93199e6
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/debezium/converters/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ func (JSON) Convert(value any) (any, error) {
func (JSON) ToKindDetails() typing.KindDetails {
return typing.Struct
}

type Int64Passthrough struct{}

func (Int64Passthrough) ToKindDetails() typing.KindDetails {
return typing.Integer
}

func (Int64Passthrough) Convert(value any) (any, error) {
if _, err := typing.AssertType[int64](value); err != nil {
return nil, err
}

return value, nil
}
14 changes: 14 additions & 0 deletions lib/debezium/converters/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,17 @@ func TestJSON_Convert(t *testing.T) {
assert.Equal(t, `{"a":2}`, value)
}
}

func TestInt64Passthrough_Convert(t *testing.T) {
{
// Wrong data type
_, err := Int64Passthrough{}.Convert("123")
assert.ErrorContains(t, err, "expected type int64, got string")
}
{
// Valid data type
value, err := Int64Passthrough{}.Convert(int64(2024))
assert.NoError(t, err)
assert.Equal(t, int64(2024), value)
}
}
6 changes: 5 additions & 1 deletion lib/debezium/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ func (f Field) GetScaleAndPrecision() (int32, *int32, error) {

func (f Field) ToValueConverter() converters.ValueConverter {
switch f.DebeziumType {
case UUID, Enum:
// Passthrough converters
case UUID, LTree, Enum:
return converters.StringPassthrough{}
case Year, MicroDuration:
return &converters.Int64Passthrough{}
case DateTimeWithTimezone:
return converters.DateTimeWithTimezone{}
case TimeWithTimezone:
Expand All @@ -97,6 +100,7 @@ func (f Field) ToValueConverter() converters.ValueConverter {
return converters.JSON{}
case Date, DateKafkaConnect:
return converters.Date{}

// Time
case Time, TimeKafkaConnect:
return converters.Time{}
Expand Down
14 changes: 14 additions & 0 deletions lib/debezium/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ func TestField_ToKindDetails(t *testing.T) {
// Enum
assert.Equal(t, typing.String, Field{DebeziumType: Enum, Type: String}.ToKindDetails())
}
{
// LTree
assert.Equal(t, typing.String, Field{DebeziumType: LTree, Type: String}.ToKindDetails())
}
}
{
// Structs
Expand Down Expand Up @@ -164,4 +168,14 @@ func TestField_ToKindDetails(t *testing.T) {
assert.Equal(t, typing.NewKindDetailsFromTemplate(typing.ETime, ext.TimeKindType), Field{DebeziumType: dbzType}.ToKindDetails())
}
}
{
// Basic
{
// Int64 Passthrough
assert.Equal(t, typing.Integer, Field{DebeziumType: Year}.ToKindDetails())
assert.Equal(t, typing.Integer, Field{DebeziumType: MicroDuration}.ToKindDetails())
}

assert.Equal(t, typing.Struct, Field{DebeziumType: JSON}.ToKindDetails())
}
}
1 change: 1 addition & 0 deletions lib/debezium/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
Enum SupportedDebeziumType = "io.debezium.data.Enum"
EnumSet SupportedDebeziumType = "io.debezium.data.EnumSet"
UUID SupportedDebeziumType = "io.debezium.data.Uuid"
LTree SupportedDebeziumType = "io.debezium.data.Ltree"

// Dates
Date SupportedDebeziumType = "io.debezium.time.Date"
Expand Down
15 changes: 15 additions & 0 deletions lib/debezium/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ func TestField_ParseValue(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "dusty", value)
}
{
// Year
{
// Floats (from JSON marshal), preprocessing should convert it to int64.
value, err := Field{Type: Int32, DebeziumType: Year}.ParseValue(2024.0)
assert.NoError(t, err)
assert.Equal(t, int64(2024), value)
}
{
// Int32
value, err := Field{Type: Int32, DebeziumType: Year}.ParseValue(int32(2024))
assert.NoError(t, err)
assert.Equal(t, int64(2024), value)
}
}
{
// JSON
field := Field{Type: String, DebeziumType: JSON}
Expand Down

0 comments on commit 93199e6

Please sign in to comment.