Skip to content

Commit

Permalink
Clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Oct 4, 2024
1 parent b25545a commit a559052
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
20 changes: 15 additions & 5 deletions lib/debezium/converters/bit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
}
Expand All @@ -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
Expand All @@ -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
}
}
10 changes: 7 additions & 3 deletions lib/debezium/converters/bit_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package converters

import (
"github.com/artie-labs/transfer/lib/debezium"
"github.com/stretchr/testify/assert"
"testing"
)
Expand All @@ -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
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion sources/postgres/adapter/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
}
Expand Down

0 comments on commit a559052

Please sign in to comment.