From a5590527513a4faa72c8305a6a92e20a84a3f555 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Fri, 4 Oct 2024 08:29:53 -0700 Subject: [PATCH] Clean up. --- lib/debezium/converters/bit.go | 20 +++++++++++++++----- lib/debezium/converters/bit_test.go | 10 +++++++--- sources/postgres/adapter/adapter_test.go | 2 +- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/debezium/converters/bit.go b/lib/debezium/converters/bit.go index 1225c0cb..c65b641c 100644 --- a/lib/debezium/converters/bit.go +++ b/lib/debezium/converters/bit.go @@ -25,8 +25,12 @@ func (b BitConverter) ToField(name string) debezium.Field { } default: return debezium.Field{ - FieldName: name, - Type: debezium.String, + FieldName: name, + DebeziumType: debezium.Bits, + Type: debezium.Bytes, + Parameters: map[string]any{ + "length": b.charMaxLength, + }, } } } @@ -37,9 +41,15 @@ func (b BitConverter) Convert(value any) (any, error) { return nil, err } - switch b.charMaxLength { - case 0: + if b.charMaxLength == 0 { return nil, fmt.Errorf("bit converter failed: invalid char max length") + } + + if len(stringValue) != b.charMaxLength { + return nil, fmt.Errorf("bit converter failed: mismatched char max length, value: %q, length: %d", stringValue, len(stringValue)) + } + + switch b.charMaxLength { case 1: if stringValue == "0" { return false, nil @@ -48,6 +58,6 @@ func (b BitConverter) Convert(value any) (any, error) { } return nil, fmt.Errorf(`string value %q is not in ["0", "1"]`, value) default: - return stringValue, nil + return []byte(stringValue), nil } } diff --git a/lib/debezium/converters/bit_test.go b/lib/debezium/converters/bit_test.go index 40c99fad..fd9c805f 100644 --- a/lib/debezium/converters/bit_test.go +++ b/lib/debezium/converters/bit_test.go @@ -1,6 +1,7 @@ package converters import ( + "github.com/artie-labs/transfer/lib/debezium" "github.com/stretchr/testify/assert" "testing" ) @@ -10,7 +11,9 @@ func TestBitConverter_ToField(t *testing.T) { // char size not specified field := NewBitConverter(0).ToField("foo") assert.Equal(t, "foo", field.FieldName) - assert.Equal(t, "string", string(field.Type)) + assert.Equal(t, "bytes", string(field.Type)) + assert.Equal(t, map[string]interface{}{"length": 0}, field.Parameters) + assert.Equal(t, debezium.Bits, field.DebeziumType) } { // char max size 1 @@ -22,7 +25,8 @@ func TestBitConverter_ToField(t *testing.T) { // char max size 5 field := NewBitConverter(5).ToField("foo") assert.Equal(t, "foo", field.FieldName) - assert.Equal(t, "string", string(field.Type)) + assert.Equal(t, "bytes", string(field.Type)) + assert.Equal(t, debezium.Bits, field.DebeziumType) } } @@ -63,6 +67,6 @@ func TestBitConverter_Convert(t *testing.T) { converter := NewBitConverter(5) value, err := converter.Convert("hello") assert.NoError(t, err) - assert.Equal(t, "hello", value.(string)) + assert.Equal(t, []byte("hello"), value) } } diff --git a/sources/postgres/adapter/adapter_test.go b/sources/postgres/adapter/adapter_test.go index f370249e..f4ebd7a3 100644 --- a/sources/postgres/adapter/adapter_test.go +++ b/sources/postgres/adapter/adapter_test.go @@ -298,7 +298,7 @@ func TestValueConverterForType_Convert(t *testing.T) { actualValue, actualErr := converter.Convert("10101") assert.NoError(t, actualErr) - assert.Equal(t, "10101", actualValue) + assert.Equal(t, []byte("10101"), actualValue) } }