From 8292c3eb20e150f4f1bba4524ca6d7b0d52f9e20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Nieto?= Date: Thu, 16 Jun 2022 18:56:08 -0500 Subject: [PATCH 1/2] fix panic on json.RawMessage parse --- adapter/postgresql/helper_test.go | 1 + adapter/postgresql/postgresql_test.go | 6 ++++++ internal/sqlbuilder/convert.go | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/adapter/postgresql/helper_test.go b/adapter/postgresql/helper_test.go index 04654e71..e553df17 100644 --- a/adapter/postgresql/helper_test.go +++ b/adapter/postgresql/helper_test.go @@ -194,6 +194,7 @@ func (h *Helper) TearUp() error { , integer_array integer[] , string_array text[] , jsonb_map jsonb + , raw_jsonb_map jsonb , integer_array_ptr integer[] , string_array_ptr text[] diff --git a/adapter/postgresql/postgresql_test.go b/adapter/postgresql/postgresql_test.go index 7d3708e5..a2f54f80 100644 --- a/adapter/postgresql/postgresql_test.go +++ b/adapter/postgresql/postgresql_test.go @@ -25,6 +25,7 @@ import ( "context" "database/sql" "database/sql/driver" + "encoding/json" "fmt" "math/rand" "strings" @@ -229,6 +230,8 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) { StringArray StringArray `db:"string_array,stringarray"` JSONBMap JSONBMap `db:"jsonb_map"` + RawJSONBMap json.RawMessage `db:"raw_jsonb_map"` + PGTypeInline `db:",inline"` PGTypeAutoInline `db:",inline"` @@ -329,6 +332,9 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) { AutoJSONBMapInteger: map[string]interface{}{"a": 12.0, "b": 13.0}, }, }, + PGType{ + RawJSONBMap: json.RawMessage(`{"foo": "bar"}`), + }, PGType{ IntegerValue: integerValue, StringValue: stringValue, diff --git a/internal/sqlbuilder/convert.go b/internal/sqlbuilder/convert.go index 50f344c9..37901f61 100644 --- a/internal/sqlbuilder/convert.go +++ b/internal/sqlbuilder/convert.go @@ -57,7 +57,7 @@ func toInterfaceArguments(value interface{}) (args []interface{}, isSlice bool) // Byte slice gets transformed into a string. if v.Type().Elem().Kind() == reflect.Uint8 { - return []interface{}{string(value.([]byte))}, false + return []interface{}{string(v.Bytes())}, false } total = v.Len() From a54cb1e0c130a52ff4dbcef3c8df0b9cf9f281bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Nieto?= Date: Sun, 3 Jul 2022 19:45:16 -0500 Subject: [PATCH 2/2] fix json.RawMessage test --- adapter/postgresql/postgresql_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adapter/postgresql/postgresql_test.go b/adapter/postgresql/postgresql_test.go index a2f54f80..8431998d 100644 --- a/adapter/postgresql/postgresql_test.go +++ b/adapter/postgresql/postgresql_test.go @@ -230,7 +230,7 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) { StringArray StringArray `db:"string_array,stringarray"` JSONBMap JSONBMap `db:"jsonb_map"` - RawJSONBMap json.RawMessage `db:"raw_jsonb_map"` + RawJSONBMap *json.RawMessage `db:"raw_jsonb_map,omitempty"` PGTypeInline `db:",inline"` @@ -278,6 +278,7 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) { integerArrayValue := Int64Array{1, 2, 3, 4} stringArrayValue := StringArray{"a", "b", "c"} jsonbMapValue := JSONBMap{"Hello": "World"} + rawJSONBMap := json.RawMessage(`{"foo": "bar"}`) testValue := "Hello world!" @@ -333,7 +334,7 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) { }, }, PGType{ - RawJSONBMap: json.RawMessage(`{"foo": "bar"}`), + RawJSONBMap: &rawJSONBMap, }, PGType{ IntegerValue: integerValue,