Skip to content

Commit 6ed68f9

Browse files
authored
Merge pull request #561 from venn-city/fix-json-raw-message-scan
Add support for `json.RawMessage` type and update dependencies.
2 parents 7b56e1a + 89dda70 commit 6ed68f9

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515

1616
// used in tests
1717
require (
18-
github.com/bytedance/sonic v1.13.3
18+
github.com/bytedance/sonic v1.14.0
1919
github.com/google/go-cmp v0.7.0
2020
github.com/pkg/profile v1.7.0
2121
github.com/shopspring/decimal v1.4.0
@@ -26,7 +26,7 @@ require (
2626

2727
require (
2828
filippo.io/edwards25519 v1.1.0 // indirect
29-
github.com/bytedance/sonic/loader v0.2.4 // indirect
29+
github.com/bytedance/sonic/loader v0.3.0 // indirect
3030
github.com/cloudwego/base64x v0.1.5 // indirect
3131
github.com/davecgh/go-spew v1.1.1 // indirect
3232
github.com/felixge/fgprof v0.9.3 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030I
55
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
66
github.com/bytedance/sonic v1.13.3 h1:MS8gmaH16Gtirygw7jV91pDCN33NyMrPbN7qiYhEsF0=
77
github.com/bytedance/sonic v1.13.3/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
8+
github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ=
9+
github.com/bytedance/sonic v1.14.0/go.mod h1:WoEbx8WTcFJfzCe0hbmyTGrfjt8PzNEBdxlNUO24NhA=
810
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
911
github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY=
1012
github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
13+
github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA=
14+
github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
1115
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
1216
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
1317
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=

qrm/utill.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ package qrm
22

33
import (
44
"database/sql"
5+
"encoding/json"
56
"fmt"
7+
"reflect"
8+
"strings"
9+
"time"
10+
611
"github.com/go-jet/jet/v2/internal/utils/must"
712
"github.com/go-jet/jet/v2/internal/utils/strslice"
813
"github.com/go-jet/jet/v2/qrm/internal"
914
"github.com/google/uuid"
10-
"reflect"
11-
"strings"
12-
"time"
1315
)
1416

1517
var scannerInterfaceType = reflect.TypeOf((*sql.Scanner)(nil)).Elem()
@@ -148,6 +150,7 @@ func initializeValueIfNilPtr(value reflect.Value) {
148150
var timeType = reflect.TypeOf(time.Now())
149151
var uuidType = reflect.TypeOf(uuid.New())
150152
var byteArrayType = reflect.TypeOf([]byte(""))
153+
var jsonRawMessageType = reflect.TypeOf(json.RawMessage{})
151154

152155
func isSimpleModelType(objType reflect.Type) bool {
153156
objType = indirectType(objType)
@@ -161,7 +164,7 @@ func isSimpleModelType(objType reflect.Type) bool {
161164
return true
162165
}
163166

164-
return objType == timeType || objType == uuidType || objType == byteArrayType
167+
return objType == timeType || objType == uuidType || objType == byteArrayType || objType == jsonRawMessageType
165168
}
166169

167170
// source can't be pointer

tests/postgres/json_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package postgres
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
. "github.com/go-jet/jet/v2/postgres"
10+
. "github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/test_sample/table"
11+
)
12+
13+
type AllTypesJsonRawMessageResult struct {
14+
JSON string `alias:"all_types.json"`
15+
JsonbPtr *json.RawMessage `alias:"all_types.jsonb_ptr"`
16+
Jsonb json.RawMessage `alias:"all_types.jsonb"`
17+
}
18+
19+
func TestJsonRawMessage(t *testing.T) {
20+
var dest []AllTypesJsonRawMessageResult
21+
22+
err := SELECT(AllTypes.JSON, AllTypes.JsonbPtr, AllTypes.Jsonb).
23+
FROM(AllTypes).
24+
LIMIT(2).
25+
QueryContext(ctx, db, &dest)
26+
27+
require.NoError(t, err)
28+
require.Len(t, dest, 2)
29+
30+
require.JSONEq(t, allTypesRow0.JSON, dest[0].JSON)
31+
require.JSONEq(t, allTypesRow0.Jsonb, string(dest[0].Jsonb))
32+
require.NotNil(t, dest[0].JsonbPtr)
33+
require.JSONEq(t, *allTypesRow0.JsonbPtr, string(*dest[0].JsonbPtr))
34+
35+
require.JSONEq(t, allTypesRow1.JSON, dest[1].JSON)
36+
require.JSONEq(t, allTypesRow1.Jsonb, string(dest[1].Jsonb))
37+
require.Nil(t, dest[1].JsonbPtr)
38+
}

0 commit comments

Comments
 (0)