Merge pull request #561 from venn-city/fix-json-raw-message-scan

Add support for `json.RawMessage` type and update dependencies.
This commit is contained in:
go-jet 2026-02-16 12:54:32 +01:00 committed by GitHub
commit 6ed68f910e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 51 additions and 6 deletions

4
go.mod
View file

@ -15,7 +15,7 @@ require (
// used in tests
require (
github.com/bytedance/sonic v1.13.3
github.com/bytedance/sonic v1.14.0
github.com/google/go-cmp v0.7.0
github.com/pkg/profile v1.7.0
github.com/shopspring/decimal v1.4.0
@ -26,7 +26,7 @@ require (
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/bytedance/sonic/loader v0.2.4 // indirect
github.com/bytedance/sonic/loader v0.3.0 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/fgprof v0.9.3 // indirect

4
go.sum
View file

@ -5,9 +5,13 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030I
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/bytedance/sonic v1.13.3 h1:MS8gmaH16Gtirygw7jV91pDCN33NyMrPbN7qiYhEsF0=
github.com/bytedance/sonic v1.13.3/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ=
github.com/bytedance/sonic v1.14.0/go.mod h1:WoEbx8WTcFJfzCe0hbmyTGrfjt8PzNEBdxlNUO24NhA=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY=
github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA=
github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=

View file

@ -2,14 +2,16 @@ package qrm
import (
"database/sql"
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
"github.com/go-jet/jet/v2/internal/utils/must"
"github.com/go-jet/jet/v2/internal/utils/strslice"
"github.com/go-jet/jet/v2/qrm/internal"
"github.com/google/uuid"
"reflect"
"strings"
"time"
)
var scannerInterfaceType = reflect.TypeOf((*sql.Scanner)(nil)).Elem()
@ -148,6 +150,7 @@ func initializeValueIfNilPtr(value reflect.Value) {
var timeType = reflect.TypeOf(time.Now())
var uuidType = reflect.TypeOf(uuid.New())
var byteArrayType = reflect.TypeOf([]byte(""))
var jsonRawMessageType = reflect.TypeOf(json.RawMessage{})
func isSimpleModelType(objType reflect.Type) bool {
objType = indirectType(objType)
@ -161,7 +164,7 @@ func isSimpleModelType(objType reflect.Type) bool {
return true
}
return objType == timeType || objType == uuidType || objType == byteArrayType
return objType == timeType || objType == uuidType || objType == byteArrayType || objType == jsonRawMessageType
}
// source can't be pointer

View file

@ -0,0 +1,38 @@
package postgres
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
. "github.com/go-jet/jet/v2/postgres"
. "github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/test_sample/table"
)
type AllTypesJsonRawMessageResult struct {
JSON string `alias:"all_types.json"`
JsonbPtr *json.RawMessage `alias:"all_types.jsonb_ptr"`
Jsonb json.RawMessage `alias:"all_types.jsonb"`
}
func TestJsonRawMessage(t *testing.T) {
var dest []AllTypesJsonRawMessageResult
err := SELECT(AllTypes.JSON, AllTypes.JsonbPtr, AllTypes.Jsonb).
FROM(AllTypes).
LIMIT(2).
QueryContext(ctx, db, &dest)
require.NoError(t, err)
require.Len(t, dest, 2)
require.JSONEq(t, allTypesRow0.JSON, dest[0].JSON)
require.JSONEq(t, allTypesRow0.Jsonb, string(dest[0].Jsonb))
require.NotNil(t, dest[0].JsonbPtr)
require.JSONEq(t, *allTypesRow0.JsonbPtr, string(*dest[0].JsonbPtr))
require.JSONEq(t, allTypesRow1.JSON, dest[1].JSON)
require.JSONEq(t, allTypesRow1.Jsonb, string(dest[1].Jsonb))
require.Nil(t, dest[1].JsonbPtr)
}