Add support for configuring the JSON unmarshal function used when querying SELECT_JSON statements.

This commit is contained in:
go-jet 2025-03-11 13:33:39 +01:00
parent 7ab44bc61c
commit 1db6b12074
6 changed files with 74 additions and 3 deletions

View file

@ -2,6 +2,7 @@ package postgres
import (
"context"
"github.com/bytedance/sonic"
"github.com/go-jet/jet/v2/internal/testutils"
"github.com/go-jet/jet/v2/internal/utils/ptr"
. "github.com/go-jet/jet/v2/postgres"
@ -225,10 +226,24 @@ func BenchmarkJoinEverythingJSON(b *testing.B) {
}
}
func BenchmarkJoinEverythingJsonSonic(b *testing.B) {
useJsonUnmarshalFunc(sonic.Unmarshal, func() {
for i := 0; i < b.N; i++ {
testJoinEverythingJSON(b)
}
})
}
func TestJoinEverythingJSON(t *testing.T) {
testJoinEverythingJSON(t)
}
func TestJoinEverythingJSONSonic(t *testing.T) {
useJsonUnmarshalFunc(sonic.Unmarshal, func() {
testJoinEverythingJSON(t)
})
}
func testJoinEverythingJSON(t require.TestingT) {
manager := Employee.AS("Manager")

View file

@ -3,6 +3,7 @@ package postgres
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"github.com/go-jet/jet/v2/qrm"
"github.com/go-jet/jet/v2/stmtcache"
@ -108,6 +109,16 @@ func allowUnusedColumns(f func()) {
f()
}
func useJsonUnmarshalFunc(unmarshalJson func(data []byte, v any) error, f func()) {
defer func() {
qrm.GlobalConfig.JsonUnmarshalFunc = json.Unmarshal
}()
qrm.GlobalConfig.JsonUnmarshalFunc = unmarshalJson
f()
}
var loggedSQL string
var loggedSQLArgs []interface{}
var loggedDebugSQL string

View file

@ -1,6 +1,7 @@
package postgres
import (
"github.com/bytedance/sonic"
"github.com/go-jet/jet/v2/internal/testutils"
. "github.com/go-jet/jet/v2/postgres"
"github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/northwind/model"
@ -110,6 +111,21 @@ func TestNorthwindJoinEverythingJson(t *testing.T) {
testNorthwindJoinEverythingJson(t)
}
func BenchmarkTestNorthwindJoinEverythingSonicJson(b *testing.B) {
useJsonUnmarshalFunc(sonic.Unmarshal, func() {
for i := 0; i < b.N; i++ {
testNorthwindJoinEverythingJson(b)
}
})
}
// uncomment when bug is fixed: https://github.com/bytedance/sonic/issues/774
//func TestNorthwindJoinEverythingJsonSonic(t *testing.T) {
// useJsonUnmarshalFunc(sonic.Unmarshal, func() {
// testNorthwindJoinEverythingJson(t)
// })
//}
func testNorthwindJoinEverythingJson(t require.TestingT) {
stmt := SELECT_JSON_ARR(