minor fix

This commit is contained in:
Gerasimos (Makis) Maropoulos 2022-02-19 14:26:34 +02:00
parent 41026c9209
commit 980c889f8d
No known key found for this signature in database
GPG Key ID: 66FCC29BD385FCA6
3 changed files with 29 additions and 10 deletions

View File

@ -2371,7 +2371,18 @@ type internalJSONDecoder interface {
DisallowUnknownFields()
}
func (options JSONReader) getDecoder(r io.Reader) (internalJSONDecoder, DecodeFunc) {
type unmarshalerContext interface {
// UnmarshalJSON unmarshal json with context support.
UnmarshalJSON(stdContext.Context, []byte) error //lint:ignore stdmethods external pkg.
}
func wrapDecodeFunc(decodeFunc func(interface{}) error) DecodeFunc {
return func(_ stdContext.Context, outPtr interface{}) error {
return decodeFunc(outPtr)
}
}
func (options JSONReader) getDecoder(r io.Reader, outPtr interface{}) (internalJSONDecoder, DecodeFunc) {
var (
decoder internalJSONDecoder
decodeFunc DecodeFunc
@ -2379,13 +2390,24 @@ func (options JSONReader) getDecoder(r io.Reader) (internalJSONDecoder, DecodeFu
if options.Optimize {
dec := gojson.NewDecoder(r)
decodeFunc = dec.DecodeContext
if outPtr != nil {
// If a custom type does not implement the unnmarshal json with context interface
// that is REQUIRED by the gojson, then fallback to the normal gojson decode without context support,
// so we protect compability against existing objects.
if _, supportsContext := outPtr.(unmarshalerContext); supportsContext {
decodeFunc = dec.DecodeContext
} else {
decodeFunc = wrapDecodeFunc(dec.Decode)
}
} else {
decodeFunc = dec.DecodeContext
}
decoder = dec
} else {
dec := json.NewDecoder(r)
decodeFunc = func(_ stdContext.Context, outPtr interface{}) error {
return dec.Decode(outPtr)
}
decodeFunc = wrapDecodeFunc(dec.Decode)
decoder = dec
}
@ -2407,7 +2429,7 @@ func (ctx *Context) ReadJSON(outPtr interface{}, opts ...JSONReader) error {
options = opts[0]
}
_, decodeFunc := options.getDecoder(ctx.request.Body)
_, decodeFunc := options.getDecoder(ctx.request.Body, outPtr)
return decodeFunc(ctx.request.Context(), outPtr)
/*
@ -2440,7 +2462,7 @@ func (ctx *Context) ReadJSONStream(onDecode func(DecodeFunc) error, opts ...JSON
options = opts[0]
}
decoder, decodeFunc := options.getDecoder(ctx.request.Body)
decoder, decodeFunc := options.getDecoder(ctx.request.Body, nil)
if options.ArrayStream {
_, err := decoder.Token() // read open bracket.

1
go.mod
View File

@ -86,7 +86,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/shiyanhui/hero v0.0.2 // indirect
github.com/smartystreets/goconvey v1.7.2 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/tdewolff/parse/v2 v2.5.27 // indirect

2
go.sum generated
View File

@ -209,8 +209,6 @@ github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shirou/gopsutil/v3 v3.22.1 h1:33y31Q8J32+KstqPfscvFwBlNJ6xLaBy4xqBXzlYV5w=
github.com/shirou/gopsutil/v3 v3.22.1/go.mod h1:WapW1AOOPlHyXr+yOyw3uYx36enocrtSoSBy0L5vUHY=
github.com/shiyanhui/hero v0.0.2 h1:RF8fwiIeWbVsdki8LCS905pxLjCQbOz/NcKE0g1ZOJc=
github.com/shiyanhui/hero v0.0.2/go.mod h1:aBlyf5bmklQfvOmVQm5i04lIGFY7t2QYIJdqEMNGJZM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=