-
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] Aligning array values to Debezium (#591)
- Loading branch information
Showing
9 changed files
with
240 additions
and
44 deletions.
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
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 |
---|---|---|
@@ -1,24 +1,70 @@ | ||
package converters | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/artie-labs/transfer/lib/debezium" | ||
) | ||
|
||
type ArrayConverter struct{} | ||
func NewArrayConverter(elementType string) ArrayConverter { | ||
var json bool | ||
for _, el := range []string{"json", "jsonb"} { | ||
if strings.EqualFold(elementType, el) { | ||
json = true | ||
break | ||
} | ||
} | ||
|
||
return ArrayConverter{json: json} | ||
} | ||
|
||
type ArrayConverter struct { | ||
json bool | ||
} | ||
|
||
func (a ArrayConverter) ToField(name string) debezium.Field { | ||
var item *debezium.Item | ||
if a.json { | ||
item = &debezium.Item{ | ||
DebeziumType: debezium.JSON, | ||
} | ||
} | ||
|
||
func (ArrayConverter) ToField(name string) debezium.Field { | ||
return debezium.Field{ | ||
FieldName: name, | ||
Type: debezium.Array, | ||
FieldName: name, | ||
Type: debezium.Array, | ||
ItemsMetadata: item, | ||
} | ||
} | ||
|
||
func (ArrayConverter) Convert(value any) (any, error) { | ||
func (a ArrayConverter) Convert(value any) (any, error) { | ||
arrayValue, ok := value.([]any) | ||
if !ok { | ||
return nil, fmt.Errorf("expected []any got %T with value: %v", value, value) | ||
} | ||
|
||
if a.json { | ||
// If json is enabled, we should parse the array elements to JSON strings | ||
var elements []any | ||
for _, el := range arrayValue { | ||
switch el.(type) { | ||
case string: | ||
// Already JSON string, so we can skip the marshalling | ||
elements = append(elements, el) | ||
default: | ||
parsedValue, err := json.Marshal(el) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal value: %v", err) | ||
} | ||
|
||
elements = append(elements, string(parsedValue)) | ||
} | ||
} | ||
|
||
return elements, nil | ||
} | ||
|
||
return arrayValue, nil | ||
} |
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,83 @@ | ||
package converters | ||
|
||
import ( | ||
"github.com/artie-labs/transfer/lib/debezium" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestArrayConverter_ToField(t *testing.T) { | ||
{ | ||
// String | ||
converter := NewArrayConverter("string") | ||
field := converter.ToField("name") | ||
assert.Equal(t, debezium.Field{ | ||
FieldName: "name", | ||
Type: debezium.Array, | ||
ItemsMetadata: nil, | ||
}, field) | ||
} | ||
{ | ||
// json[] | ||
converter := NewArrayConverter("json") | ||
field := converter.ToField("name") | ||
assert.Equal(t, debezium.Field{ | ||
FieldName: "name", | ||
Type: debezium.Array, | ||
ItemsMetadata: &debezium.Item{ | ||
DebeziumType: debezium.JSON, | ||
}, | ||
}, field) | ||
} | ||
{ | ||
// jsonb[] | ||
converter := NewArrayConverter("jsonb") | ||
field := converter.ToField("name") | ||
assert.Equal(t, debezium.Field{ | ||
FieldName: "name", | ||
Type: debezium.Array, | ||
ItemsMetadata: &debezium.Item{ | ||
DebeziumType: debezium.JSON, | ||
}, | ||
}, field) | ||
} | ||
} | ||
|
||
func TestArrayConverter(t *testing.T) { | ||
{ | ||
// Array of strings | ||
list := []any{"a", "b", "c"} | ||
converter := NewArrayConverter("string") | ||
converted, err := converter.Convert(list) | ||
assert.NoError(t, err) | ||
|
||
returnedValue, err := converter.ToField("name").ParseValue(converted) | ||
assert.NoError(t, err) | ||
assert.Equal(t, list, returnedValue) | ||
} | ||
{ | ||
// Array of jsonb[] | ||
listOfObjects := []any{map[string]any{"a": "b"}, map[string]any{"c": "d"}, []any{"e", "f"}} | ||
listOfJsonStrings := []any{`{"a": "b"}`, `{"c": "d"}`, `["e", "f"]`} | ||
{ | ||
// Invalid - item type is JSON objects | ||
converter := NewArrayConverter("jsonb") | ||
converted, err := converter.Convert(listOfObjects) | ||
assert.NoError(t, err) | ||
|
||
returnedValue, err := converter.ToField("name").ParseValue(converted) | ||
assert.NoError(t, err) | ||
assert.Equal(t, listOfObjects, returnedValue) | ||
} | ||
{ | ||
// Valid - item type is JSON strings | ||
converter := NewArrayConverter("jsonb") | ||
converted, err := converter.Convert(listOfJsonStrings) | ||
assert.NoError(t, err) | ||
|
||
returnedValue, err := converter.ToField("name").ParseValue(converted) | ||
assert.NoError(t, err) | ||
assert.Equal(t, listOfObjects, returnedValue) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.