Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Debezium] Array item type safety #1082

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/debezium/converters/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,16 @@ func (a Array) Convert(value any) (any, error) {

convertedElements := make([]any, len(elements))
for i, element := range elements {
if castedElement, ok := element.(string); ok {
switch castedElement := element.(type) {
case string:
var obj any
if err := json.Unmarshal([]byte(castedElement), &obj); err != nil {
return nil, err
}

convertedElements[i] = obj
default:
return nil, fmt.Errorf("expected string, got %T, value '%v'", element, element)
}
}

Expand Down
21 changes: 17 additions & 4 deletions lib/debezium/converters/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,22 @@ func TestArray_Convert(t *testing.T) {
}
{
// Array of JSON objects
value, err := NewArray(true).Convert([]any{"{\"body\": \"they are on to us\", \"sender\": \"pablo\"}"})
assert.NoError(t, err)
assert.Len(t, value.([]any), 1)
assert.Equal(t, map[string]any{"body": "they are on to us", "sender": "pablo"}, value.([]any)[0])
{
// Invalid json
_, err := NewArray(true).Convert([]any{"hello"})
assert.ErrorContains(t, err, "invalid character 'h' looking for beginning of value")
}
{
// Invalid data type
_, err := NewArray(true).Convert([]any{123})
assert.ErrorContains(t, err, "expected string, got int, value '123'")
}
{
// Valid
value, err := NewArray(true).Convert([]any{"{\"body\": \"they are on to us\", \"sender\": \"pablo\"}"})
assert.NoError(t, err)
assert.Len(t, value.([]any), 1)
assert.Equal(t, map[string]any{"body": "they are on to us", "sender": "pablo"}, value.([]any)[0])
}
}
}
Loading