From 2a3138b6c91ce937be7a96fff8db28e5d817ec28 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Fri, 13 Dec 2024 21:05:49 -0800 Subject: [PATCH] Clean up. --- lib/debezium/converters/basic.go | 5 ++++- lib/debezium/converters/basic_test.go | 21 +++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/debezium/converters/basic.go b/lib/debezium/converters/basic.go index b361c5656..b262b70d5 100644 --- a/lib/debezium/converters/basic.go +++ b/lib/debezium/converters/basic.go @@ -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) } } diff --git a/lib/debezium/converters/basic_test.go b/lib/debezium/converters/basic_test.go index 8b65298e7..72eb56535 100644 --- a/lib/debezium/converters/basic_test.go +++ b/lib/debezium/converters/basic_test.go @@ -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]) + } } }