diff --git a/go.mod b/go.mod index bb2e376..3566f6f 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e044ce1..9e73e3d 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/qrm/utill.go b/qrm/utill.go index 1775fda..84f4ee8 100644 --- a/qrm/utill.go +++ b/qrm/utill.go @@ -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 diff --git a/tests/postgres/json_test.go b/tests/postgres/json_test.go new file mode 100644 index 0000000..f9b8fe3 --- /dev/null +++ b/tests/postgres/json_test.go @@ -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) +}