-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Postgres] Support
BIT VARYING
(#510)
- Loading branch information
Showing
7 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package converters | ||
|
||
import ( | ||
"fmt" | ||
"github.com/artie-labs/transfer/lib/debezium" | ||
"github.com/artie-labs/transfer/lib/typing" | ||
) | ||
|
||
// BitVaryingConverter - Precision here is optional, if it's not specified - then it's infinite. | ||
// If it's specified, then it's the maximum number of bits that can be stored. | ||
type BitVaryingConverter struct { | ||
optionalCharMaxLength int | ||
} | ||
|
||
func NewBitVaryingConverter(optionalCharMaxLength int) BitVaryingConverter { | ||
return BitVaryingConverter{optionalCharMaxLength: optionalCharMaxLength} | ||
} | ||
|
||
func (BitVaryingConverter) ToField(name string) debezium.Field { | ||
return debezium.Field{ | ||
FieldName: name, | ||
DebeziumType: debezium.Bits, | ||
Type: debezium.Bytes, | ||
} | ||
} | ||
|
||
func (b BitVaryingConverter) Convert(value any) (any, error) { | ||
stringValue, err := typing.AssertType[string](value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if b.optionalCharMaxLength > 0 && len(stringValue) > b.optionalCharMaxLength { | ||
return nil, fmt.Errorf("bit varying converter failed: value exceeds char max length, value: %q, length: %d", stringValue, len(stringValue)) | ||
} | ||
|
||
for _, char := range stringValue { | ||
if char != '0' && char != '1' { | ||
return nil, fmt.Errorf("invalid binary string %q: contains non-binary characters", stringValue) | ||
} | ||
} | ||
|
||
return stringToByteA(stringValue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package converters | ||
|
||
import ( | ||
"github.com/artie-labs/transfer/lib/debezium" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestBitVaryingConverter_ToField(t *testing.T) { | ||
{ | ||
// char size not specified | ||
field := NewBitVaryingConverter(0).ToField("foo") | ||
assert.Equal(t, "foo", field.FieldName) | ||
assert.Equal(t, "bytes", string(field.Type)) | ||
assert.Equal(t, debezium.Bits, field.DebeziumType) | ||
assert.Nil(t, field.Parameters) | ||
} | ||
{ | ||
// char max size 1 | ||
field := NewBitVaryingConverter(1).ToField("foo") | ||
assert.Equal(t, "foo", field.FieldName) | ||
assert.Equal(t, "bytes", string(field.Type)) | ||
assert.Nil(t, field.Parameters) | ||
} | ||
{ | ||
// char max size 5 | ||
field := NewBitVaryingConverter(5).ToField("foo") | ||
assert.Equal(t, "foo", field.FieldName) | ||
assert.Equal(t, "bytes", string(field.Type)) | ||
assert.Equal(t, debezium.Bits, field.DebeziumType) | ||
assert.Nil(t, field.Parameters) | ||
} | ||
} | ||
|
||
func TestBitVaryingConverter_Convert(t *testing.T) { | ||
{ | ||
// char size not specified | ||
_, err := BitVaryingConverter{}.Convert("foo") | ||
assert.ErrorContains(t, err, `invalid binary string "foo": contains non-binary characters`) | ||
} | ||
{ | ||
// char max size 1 | ||
converter := NewBitVaryingConverter(1) | ||
{ | ||
// Invalid value - wrong type | ||
_, err := converter.Convert(1234) | ||
assert.ErrorContains(t, err, "expected type string, got int") | ||
} | ||
{ | ||
// Valid value - 0 | ||
value, err := converter.Convert("0") | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte{}, value) | ||
} | ||
{ | ||
// Valid value - 1 | ||
value, err := converter.Convert("1") | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte{1}, value) | ||
} | ||
{ | ||
// Invalid value - 2 | ||
_, err := converter.Convert("2") | ||
assert.ErrorContains(t, err, `invalid binary string "2": contains non-binary characters`) | ||
} | ||
} | ||
{ | ||
// char max size - 5 | ||
{ | ||
// Length not matching, but it's fine. | ||
converter := NewBitVaryingConverter(8) | ||
value, err := converter.Convert("101111") | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte{47}, value) | ||
} | ||
{ | ||
// Invalid, value contains non 0s and 1s | ||
converter := NewBitVaryingConverter(5) | ||
_, err := converter.Convert("1011a") | ||
assert.ErrorContains(t, err, "invalid binary string") | ||
} | ||
{ | ||
// Valid | ||
converter := NewBitVaryingConverter(5) | ||
value, err := converter.Convert("10101") | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte{21}, value) | ||
} | ||
{ | ||
// Valid #2 | ||
converter := NewBitVaryingConverter(5) | ||
value, err := converter.Convert("10011") | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte{19}, value) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters