2018-07-25 04:17:02 -04:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/modern-go/reflect2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValDecoder is an internal type registered to cache as needed.
|
|
|
|
// Don't confuse jsoniter.ValDecoder with json.Decoder.
|
|
|
|
// For json.Decoder's adapter, refer to jsoniter.AdapterDecoder(todo link).
|
|
|
|
//
|
|
|
|
// Reflection on type to create decoders, which is then cached
|
|
|
|
// Reflection on value is avoided as we can, as the reflect.Value itself will allocate, with following exceptions
|
|
|
|
// 1. create instance of new value, for example *int will need a int to be allocated
|
|
|
|
// 2. append to slice, if the existing cap is not enough, allocate will be done using Reflect.New
|
|
|
|
// 3. assignment to map, both key and value will be reflect.Value
|
|
|
|
// For a simple struct binding, it will be reflect.Value free and allocation free
|
|
|
|
type ValDecoder interface {
|
|
|
|
Decode(ptr unsafe.Pointer, iter *Iterator)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValEncoder is an internal type registered to cache as needed.
|
|
|
|
// Don't confuse jsoniter.ValEncoder with json.Encoder.
|
|
|
|
// For json.Encoder's adapter, refer to jsoniter.AdapterEncoder(todo godoc link).
|
|
|
|
type ValEncoder interface {
|
|
|
|
IsEmpty(ptr unsafe.Pointer) bool
|
|
|
|
Encode(ptr unsafe.Pointer, stream *Stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
type checkIsEmpty interface {
|
|
|
|
IsEmpty(ptr unsafe.Pointer) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type ctx struct {
|
|
|
|
*frozenConfig
|
|
|
|
prefix string
|
|
|
|
encoders map[reflect2.Type]ValEncoder
|
|
|
|
decoders map[reflect2.Type]ValDecoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ctx) caseSensitive() bool {
|
|
|
|
if b.frozenConfig == nil {
|
|
|
|
// default is case-insensitive
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return b.frozenConfig.caseSensitive
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ctx) append(prefix string) *ctx {
|
|
|
|
return &ctx{
|
|
|
|
frozenConfig: b.frozenConfig,
|
|
|
|
prefix: b.prefix + " " + prefix,
|
|
|
|
encoders: b.encoders,
|
|
|
|
decoders: b.decoders,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadVal copy the underlying JSON into go interface, same as json.Unmarshal
|
|
|
|
func (iter *Iterator) ReadVal(obj interface{}) {
|
[20.10] vendor: github.com/json-iterator/go v1.1.12 for Go 1.18 compatibility
full diff: https://github.com/json-iterator/go/compare/0ff49de124c6f76f8494e194af75bde0f1a49a29...024077e996b048517130b21ea6bf12aa23055d3d
Fixes a nil-pointer exception on go 1.18;
```
=== FAIL: cli/context/kubernetes TestSaveLoadContexts (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x40fcbc]
goroutine 19 [running]:
testing.tRunner.func1.2({0xa7e080, 0x1073930})
/usr/local/go/src/testing/testing.go:1389 +0x24e
testing.tRunner.func1()
/usr/local/go/src/testing/testing.go:1392 +0x39f
panic({0xa7e080, 0x1073930})
/usr/local/go/src/runtime/panic.go:838 +0x207
reflect.mapiternext(0x40?)
/usr/local/go/src/runtime/map.go:1378 +0x19
github.com/docker/cli/vendor/github.com/modern-go/reflect2.(*UnsafeMapIterator).UnsafeNext(0x8?)
/go/src/github.com/docker/cli/vendor/github.com/modern-go/reflect2/unsafe_map.go:136 +0x32
github.com/docker/cli/vendor/github.com/json-iterator/go.(*sortKeysMapEncoder).Encode(0xc000478930, 0xc0000ca3a8, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_map.go:293 +0x335
github.com/docker/cli/vendor/github.com/json-iterator/go.(*placeholderEncoder).Encode(0xc00046c898?, 0x95d787?, 0xc0000bae58?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:327 +0x22
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structFieldEncoder).Encode(0xc000482630, 0xa2790c?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:110 +0x56
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structEncoder).Encode(0xc000482780, 0xb3a599?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:158 +0x652
github.com/docker/cli/vendor/github.com/json-iterator/go.(*placeholderEncoder).Encode(0xc00046ca10?, 0x95d787?, 0xc0000bae58?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:327 +0x22
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structFieldEncoder).Encode(0xc0004829f0, 0xa0fd11?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:110 +0x56
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structEncoder).Encode(0xc000482a50, 0x40aa15?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:158 +0x652
github.com/docker/cli/vendor/github.com/json-iterator/go.(*sliceEncoder).Encode(0xc00047e198, 0xc0003a83c8, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_slice.go:38 +0x2bb
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structFieldEncoder).Encode(0xc0004837a0, 0xa12e12?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:110 +0x56
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structEncoder).Encode(0xc000483890, 0x0?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:158 +0x652
github.com/docker/cli/vendor/github.com/json-iterator/go.(*OptionalEncoder).Encode(0xc0003b6be0?, 0x0?, 0x0?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_optional.go:74 +0xa4
github.com/docker/cli/vendor/github.com/json-iterator/go.(*onePtrEncoder).Encode(0xc000471e30, 0xc0003a8370, 0xc000460720?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:214 +0x82
github.com/docker/cli/vendor/github.com/json-iterator/go.(*Stream).WriteVal(0xc0000bae40, {0xabe4a0, 0xc0003a8370})
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:93 +0x158
github.com/docker/cli/vendor/github.com/json-iterator/go.(*frozenConfig).Marshal(0xc0003b6be0, {0xabe4a0, 0xc0003a8370})
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/config.go:299 +0xc9
github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/json.(*Serializer).Encode(0xc00043aee0?, {0xc375c0?, 0xc0003a8370?}, {0xc339e0, 0xc000460210})
/go/src/github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/json/json.go:310 +0x6d
github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/versioning.(*codec).Encode(0xc0000f8480, {0xc37570?, 0xc0000bacc0}, {0xc339e0, 0xc000460210})
/go/src/github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/versioning/versioning.go:231 +0x926
github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime.Encode({0x7f48b36ce5c0, 0xc0000f8480}, {0xc37570, 0xc0000bacc0})
/go/src/github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/codec.go:46 +0x64
github.com/docker/cli/vendor/k8s.io/client-go/tools/clientcmd.Write(...)
/go/src/github.com/docker/cli/vendor/k8s.io/client-go/tools/clientcmd/loader.go:469
github.com/docker/cli/cli/context/kubernetes.TestSaveLoadContexts(0xc0004561a0?)
/go/src/github.com/docker/cli/cli/context/kubernetes/endpoint_test.go:75 +0xf0a
testing.tRunner(0xc0004561a0, 0xb89ea0)
/usr/local/go/src/testing/testing.go:1439 +0x102
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:1486 +0x35f
```
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-08-19 10:12:25 -04:00
|
|
|
depth := iter.depth
|
2018-07-25 04:17:02 -04:00
|
|
|
cacheKey := reflect2.RTypeOf(obj)
|
|
|
|
decoder := iter.cfg.getDecoderFromCache(cacheKey)
|
|
|
|
if decoder == nil {
|
|
|
|
typ := reflect2.TypeOf(obj)
|
[20.10] vendor: github.com/json-iterator/go v1.1.12 for Go 1.18 compatibility
full diff: https://github.com/json-iterator/go/compare/0ff49de124c6f76f8494e194af75bde0f1a49a29...024077e996b048517130b21ea6bf12aa23055d3d
Fixes a nil-pointer exception on go 1.18;
```
=== FAIL: cli/context/kubernetes TestSaveLoadContexts (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x40fcbc]
goroutine 19 [running]:
testing.tRunner.func1.2({0xa7e080, 0x1073930})
/usr/local/go/src/testing/testing.go:1389 +0x24e
testing.tRunner.func1()
/usr/local/go/src/testing/testing.go:1392 +0x39f
panic({0xa7e080, 0x1073930})
/usr/local/go/src/runtime/panic.go:838 +0x207
reflect.mapiternext(0x40?)
/usr/local/go/src/runtime/map.go:1378 +0x19
github.com/docker/cli/vendor/github.com/modern-go/reflect2.(*UnsafeMapIterator).UnsafeNext(0x8?)
/go/src/github.com/docker/cli/vendor/github.com/modern-go/reflect2/unsafe_map.go:136 +0x32
github.com/docker/cli/vendor/github.com/json-iterator/go.(*sortKeysMapEncoder).Encode(0xc000478930, 0xc0000ca3a8, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_map.go:293 +0x335
github.com/docker/cli/vendor/github.com/json-iterator/go.(*placeholderEncoder).Encode(0xc00046c898?, 0x95d787?, 0xc0000bae58?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:327 +0x22
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structFieldEncoder).Encode(0xc000482630, 0xa2790c?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:110 +0x56
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structEncoder).Encode(0xc000482780, 0xb3a599?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:158 +0x652
github.com/docker/cli/vendor/github.com/json-iterator/go.(*placeholderEncoder).Encode(0xc00046ca10?, 0x95d787?, 0xc0000bae58?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:327 +0x22
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structFieldEncoder).Encode(0xc0004829f0, 0xa0fd11?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:110 +0x56
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structEncoder).Encode(0xc000482a50, 0x40aa15?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:158 +0x652
github.com/docker/cli/vendor/github.com/json-iterator/go.(*sliceEncoder).Encode(0xc00047e198, 0xc0003a83c8, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_slice.go:38 +0x2bb
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structFieldEncoder).Encode(0xc0004837a0, 0xa12e12?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:110 +0x56
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structEncoder).Encode(0xc000483890, 0x0?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:158 +0x652
github.com/docker/cli/vendor/github.com/json-iterator/go.(*OptionalEncoder).Encode(0xc0003b6be0?, 0x0?, 0x0?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_optional.go:74 +0xa4
github.com/docker/cli/vendor/github.com/json-iterator/go.(*onePtrEncoder).Encode(0xc000471e30, 0xc0003a8370, 0xc000460720?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:214 +0x82
github.com/docker/cli/vendor/github.com/json-iterator/go.(*Stream).WriteVal(0xc0000bae40, {0xabe4a0, 0xc0003a8370})
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:93 +0x158
github.com/docker/cli/vendor/github.com/json-iterator/go.(*frozenConfig).Marshal(0xc0003b6be0, {0xabe4a0, 0xc0003a8370})
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/config.go:299 +0xc9
github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/json.(*Serializer).Encode(0xc00043aee0?, {0xc375c0?, 0xc0003a8370?}, {0xc339e0, 0xc000460210})
/go/src/github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/json/json.go:310 +0x6d
github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/versioning.(*codec).Encode(0xc0000f8480, {0xc37570?, 0xc0000bacc0}, {0xc339e0, 0xc000460210})
/go/src/github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/versioning/versioning.go:231 +0x926
github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime.Encode({0x7f48b36ce5c0, 0xc0000f8480}, {0xc37570, 0xc0000bacc0})
/go/src/github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/codec.go:46 +0x64
github.com/docker/cli/vendor/k8s.io/client-go/tools/clientcmd.Write(...)
/go/src/github.com/docker/cli/vendor/k8s.io/client-go/tools/clientcmd/loader.go:469
github.com/docker/cli/cli/context/kubernetes.TestSaveLoadContexts(0xc0004561a0?)
/go/src/github.com/docker/cli/cli/context/kubernetes/endpoint_test.go:75 +0xf0a
testing.tRunner(0xc0004561a0, 0xb89ea0)
/usr/local/go/src/testing/testing.go:1439 +0x102
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:1486 +0x35f
```
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-08-19 10:12:25 -04:00
|
|
|
if typ == nil || typ.Kind() != reflect.Ptr {
|
2018-07-25 04:17:02 -04:00
|
|
|
iter.ReportError("ReadVal", "can only unmarshal into pointer")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
decoder = iter.cfg.DecoderOf(typ)
|
|
|
|
}
|
|
|
|
ptr := reflect2.PtrOf(obj)
|
|
|
|
if ptr == nil {
|
|
|
|
iter.ReportError("ReadVal", "can not read into nil pointer")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
decoder.Decode(ptr, iter)
|
[20.10] vendor: github.com/json-iterator/go v1.1.12 for Go 1.18 compatibility
full diff: https://github.com/json-iterator/go/compare/0ff49de124c6f76f8494e194af75bde0f1a49a29...024077e996b048517130b21ea6bf12aa23055d3d
Fixes a nil-pointer exception on go 1.18;
```
=== FAIL: cli/context/kubernetes TestSaveLoadContexts (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x40fcbc]
goroutine 19 [running]:
testing.tRunner.func1.2({0xa7e080, 0x1073930})
/usr/local/go/src/testing/testing.go:1389 +0x24e
testing.tRunner.func1()
/usr/local/go/src/testing/testing.go:1392 +0x39f
panic({0xa7e080, 0x1073930})
/usr/local/go/src/runtime/panic.go:838 +0x207
reflect.mapiternext(0x40?)
/usr/local/go/src/runtime/map.go:1378 +0x19
github.com/docker/cli/vendor/github.com/modern-go/reflect2.(*UnsafeMapIterator).UnsafeNext(0x8?)
/go/src/github.com/docker/cli/vendor/github.com/modern-go/reflect2/unsafe_map.go:136 +0x32
github.com/docker/cli/vendor/github.com/json-iterator/go.(*sortKeysMapEncoder).Encode(0xc000478930, 0xc0000ca3a8, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_map.go:293 +0x335
github.com/docker/cli/vendor/github.com/json-iterator/go.(*placeholderEncoder).Encode(0xc00046c898?, 0x95d787?, 0xc0000bae58?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:327 +0x22
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structFieldEncoder).Encode(0xc000482630, 0xa2790c?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:110 +0x56
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structEncoder).Encode(0xc000482780, 0xb3a599?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:158 +0x652
github.com/docker/cli/vendor/github.com/json-iterator/go.(*placeholderEncoder).Encode(0xc00046ca10?, 0x95d787?, 0xc0000bae58?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:327 +0x22
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structFieldEncoder).Encode(0xc0004829f0, 0xa0fd11?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:110 +0x56
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structEncoder).Encode(0xc000482a50, 0x40aa15?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:158 +0x652
github.com/docker/cli/vendor/github.com/json-iterator/go.(*sliceEncoder).Encode(0xc00047e198, 0xc0003a83c8, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_slice.go:38 +0x2bb
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structFieldEncoder).Encode(0xc0004837a0, 0xa12e12?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:110 +0x56
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structEncoder).Encode(0xc000483890, 0x0?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:158 +0x652
github.com/docker/cli/vendor/github.com/json-iterator/go.(*OptionalEncoder).Encode(0xc0003b6be0?, 0x0?, 0x0?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_optional.go:74 +0xa4
github.com/docker/cli/vendor/github.com/json-iterator/go.(*onePtrEncoder).Encode(0xc000471e30, 0xc0003a8370, 0xc000460720?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:214 +0x82
github.com/docker/cli/vendor/github.com/json-iterator/go.(*Stream).WriteVal(0xc0000bae40, {0xabe4a0, 0xc0003a8370})
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:93 +0x158
github.com/docker/cli/vendor/github.com/json-iterator/go.(*frozenConfig).Marshal(0xc0003b6be0, {0xabe4a0, 0xc0003a8370})
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/config.go:299 +0xc9
github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/json.(*Serializer).Encode(0xc00043aee0?, {0xc375c0?, 0xc0003a8370?}, {0xc339e0, 0xc000460210})
/go/src/github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/json/json.go:310 +0x6d
github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/versioning.(*codec).Encode(0xc0000f8480, {0xc37570?, 0xc0000bacc0}, {0xc339e0, 0xc000460210})
/go/src/github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/versioning/versioning.go:231 +0x926
github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime.Encode({0x7f48b36ce5c0, 0xc0000f8480}, {0xc37570, 0xc0000bacc0})
/go/src/github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/codec.go:46 +0x64
github.com/docker/cli/vendor/k8s.io/client-go/tools/clientcmd.Write(...)
/go/src/github.com/docker/cli/vendor/k8s.io/client-go/tools/clientcmd/loader.go:469
github.com/docker/cli/cli/context/kubernetes.TestSaveLoadContexts(0xc0004561a0?)
/go/src/github.com/docker/cli/cli/context/kubernetes/endpoint_test.go:75 +0xf0a
testing.tRunner(0xc0004561a0, 0xb89ea0)
/usr/local/go/src/testing/testing.go:1439 +0x102
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:1486 +0x35f
```
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-08-19 10:12:25 -04:00
|
|
|
if iter.depth != depth {
|
|
|
|
iter.ReportError("ReadVal", "unexpected mismatched nesting")
|
|
|
|
return
|
|
|
|
}
|
2018-07-25 04:17:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteVal copy the go interface into underlying JSON, same as json.Marshal
|
|
|
|
func (stream *Stream) WriteVal(val interface{}) {
|
|
|
|
if nil == val {
|
|
|
|
stream.WriteNil()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cacheKey := reflect2.RTypeOf(val)
|
|
|
|
encoder := stream.cfg.getEncoderFromCache(cacheKey)
|
|
|
|
if encoder == nil {
|
|
|
|
typ := reflect2.TypeOf(val)
|
|
|
|
encoder = stream.cfg.EncoderOf(typ)
|
|
|
|
}
|
|
|
|
encoder.Encode(reflect2.PtrOf(val), stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *frozenConfig) DecoderOf(typ reflect2.Type) ValDecoder {
|
|
|
|
cacheKey := typ.RType()
|
|
|
|
decoder := cfg.getDecoderFromCache(cacheKey)
|
|
|
|
if decoder != nil {
|
|
|
|
return decoder
|
|
|
|
}
|
|
|
|
ctx := &ctx{
|
|
|
|
frozenConfig: cfg,
|
|
|
|
prefix: "",
|
|
|
|
decoders: map[reflect2.Type]ValDecoder{},
|
|
|
|
encoders: map[reflect2.Type]ValEncoder{},
|
|
|
|
}
|
|
|
|
ptrType := typ.(*reflect2.UnsafePtrType)
|
|
|
|
decoder = decoderOfType(ctx, ptrType.Elem())
|
|
|
|
cfg.addDecoderToCache(cacheKey, decoder)
|
|
|
|
return decoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func decoderOfType(ctx *ctx, typ reflect2.Type) ValDecoder {
|
|
|
|
decoder := getTypeDecoderFromExtension(ctx, typ)
|
|
|
|
if decoder != nil {
|
|
|
|
return decoder
|
|
|
|
}
|
|
|
|
decoder = createDecoderOfType(ctx, typ)
|
|
|
|
for _, extension := range extensions {
|
|
|
|
decoder = extension.DecorateDecoder(typ, decoder)
|
|
|
|
}
|
2018-09-26 07:26:19 -04:00
|
|
|
decoder = ctx.decoderExtension.DecorateDecoder(typ, decoder)
|
|
|
|
for _, extension := range ctx.extraExtensions {
|
2018-07-25 04:17:02 -04:00
|
|
|
decoder = extension.DecorateDecoder(typ, decoder)
|
|
|
|
}
|
|
|
|
return decoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDecoderOfType(ctx *ctx, typ reflect2.Type) ValDecoder {
|
|
|
|
decoder := ctx.decoders[typ]
|
|
|
|
if decoder != nil {
|
|
|
|
return decoder
|
|
|
|
}
|
|
|
|
placeholder := &placeholderDecoder{}
|
|
|
|
ctx.decoders[typ] = placeholder
|
|
|
|
decoder = _createDecoderOfType(ctx, typ)
|
|
|
|
placeholder.decoder = decoder
|
|
|
|
return decoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func _createDecoderOfType(ctx *ctx, typ reflect2.Type) ValDecoder {
|
|
|
|
decoder := createDecoderOfJsonRawMessage(ctx, typ)
|
|
|
|
if decoder != nil {
|
|
|
|
return decoder
|
|
|
|
}
|
|
|
|
decoder = createDecoderOfJsonNumber(ctx, typ)
|
|
|
|
if decoder != nil {
|
|
|
|
return decoder
|
|
|
|
}
|
|
|
|
decoder = createDecoderOfMarshaler(ctx, typ)
|
|
|
|
if decoder != nil {
|
|
|
|
return decoder
|
|
|
|
}
|
|
|
|
decoder = createDecoderOfAny(ctx, typ)
|
|
|
|
if decoder != nil {
|
|
|
|
return decoder
|
|
|
|
}
|
|
|
|
decoder = createDecoderOfNative(ctx, typ)
|
|
|
|
if decoder != nil {
|
|
|
|
return decoder
|
|
|
|
}
|
|
|
|
switch typ.Kind() {
|
|
|
|
case reflect.Interface:
|
|
|
|
ifaceType, isIFace := typ.(*reflect2.UnsafeIFaceType)
|
|
|
|
if isIFace {
|
|
|
|
return &ifaceDecoder{valType: ifaceType}
|
|
|
|
}
|
|
|
|
return &efaceDecoder{}
|
|
|
|
case reflect.Struct:
|
|
|
|
return decoderOfStruct(ctx, typ)
|
|
|
|
case reflect.Array:
|
|
|
|
return decoderOfArray(ctx, typ)
|
|
|
|
case reflect.Slice:
|
|
|
|
return decoderOfSlice(ctx, typ)
|
|
|
|
case reflect.Map:
|
|
|
|
return decoderOfMap(ctx, typ)
|
|
|
|
case reflect.Ptr:
|
|
|
|
return decoderOfOptional(ctx, typ)
|
|
|
|
default:
|
|
|
|
return &lazyErrorDecoder{err: fmt.Errorf("%s%s is unsupported type", ctx.prefix, typ.String())}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *frozenConfig) EncoderOf(typ reflect2.Type) ValEncoder {
|
|
|
|
cacheKey := typ.RType()
|
|
|
|
encoder := cfg.getEncoderFromCache(cacheKey)
|
|
|
|
if encoder != nil {
|
|
|
|
return encoder
|
|
|
|
}
|
|
|
|
ctx := &ctx{
|
|
|
|
frozenConfig: cfg,
|
|
|
|
prefix: "",
|
|
|
|
decoders: map[reflect2.Type]ValDecoder{},
|
|
|
|
encoders: map[reflect2.Type]ValEncoder{},
|
|
|
|
}
|
|
|
|
encoder = encoderOfType(ctx, typ)
|
|
|
|
if typ.LikePtr() {
|
|
|
|
encoder = &onePtrEncoder{encoder}
|
|
|
|
}
|
|
|
|
cfg.addEncoderToCache(cacheKey, encoder)
|
|
|
|
return encoder
|
|
|
|
}
|
|
|
|
|
|
|
|
type onePtrEncoder struct {
|
|
|
|
encoder ValEncoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *onePtrEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
|
|
|
return encoder.encoder.IsEmpty(unsafe.Pointer(&ptr))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *onePtrEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
|
|
|
encoder.encoder.Encode(unsafe.Pointer(&ptr), stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
func encoderOfType(ctx *ctx, typ reflect2.Type) ValEncoder {
|
|
|
|
encoder := getTypeEncoderFromExtension(ctx, typ)
|
|
|
|
if encoder != nil {
|
|
|
|
return encoder
|
|
|
|
}
|
|
|
|
encoder = createEncoderOfType(ctx, typ)
|
|
|
|
for _, extension := range extensions {
|
|
|
|
encoder = extension.DecorateEncoder(typ, encoder)
|
|
|
|
}
|
2018-09-26 07:26:19 -04:00
|
|
|
encoder = ctx.encoderExtension.DecorateEncoder(typ, encoder)
|
|
|
|
for _, extension := range ctx.extraExtensions {
|
2018-07-25 04:17:02 -04:00
|
|
|
encoder = extension.DecorateEncoder(typ, encoder)
|
|
|
|
}
|
|
|
|
return encoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func createEncoderOfType(ctx *ctx, typ reflect2.Type) ValEncoder {
|
|
|
|
encoder := ctx.encoders[typ]
|
|
|
|
if encoder != nil {
|
|
|
|
return encoder
|
|
|
|
}
|
|
|
|
placeholder := &placeholderEncoder{}
|
|
|
|
ctx.encoders[typ] = placeholder
|
|
|
|
encoder = _createEncoderOfType(ctx, typ)
|
|
|
|
placeholder.encoder = encoder
|
|
|
|
return encoder
|
|
|
|
}
|
|
|
|
func _createEncoderOfType(ctx *ctx, typ reflect2.Type) ValEncoder {
|
|
|
|
encoder := createEncoderOfJsonRawMessage(ctx, typ)
|
|
|
|
if encoder != nil {
|
|
|
|
return encoder
|
|
|
|
}
|
|
|
|
encoder = createEncoderOfJsonNumber(ctx, typ)
|
|
|
|
if encoder != nil {
|
|
|
|
return encoder
|
|
|
|
}
|
|
|
|
encoder = createEncoderOfMarshaler(ctx, typ)
|
|
|
|
if encoder != nil {
|
|
|
|
return encoder
|
|
|
|
}
|
|
|
|
encoder = createEncoderOfAny(ctx, typ)
|
|
|
|
if encoder != nil {
|
|
|
|
return encoder
|
|
|
|
}
|
|
|
|
encoder = createEncoderOfNative(ctx, typ)
|
|
|
|
if encoder != nil {
|
|
|
|
return encoder
|
|
|
|
}
|
|
|
|
kind := typ.Kind()
|
|
|
|
switch kind {
|
|
|
|
case reflect.Interface:
|
|
|
|
return &dynamicEncoder{typ}
|
|
|
|
case reflect.Struct:
|
|
|
|
return encoderOfStruct(ctx, typ)
|
|
|
|
case reflect.Array:
|
|
|
|
return encoderOfArray(ctx, typ)
|
|
|
|
case reflect.Slice:
|
|
|
|
return encoderOfSlice(ctx, typ)
|
|
|
|
case reflect.Map:
|
|
|
|
return encoderOfMap(ctx, typ)
|
|
|
|
case reflect.Ptr:
|
|
|
|
return encoderOfOptional(ctx, typ)
|
|
|
|
default:
|
|
|
|
return &lazyErrorEncoder{err: fmt.Errorf("%s%s is unsupported type", ctx.prefix, typ.String())}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type lazyErrorDecoder struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (decoder *lazyErrorDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
|
|
|
if iter.WhatIsNext() != NilValue {
|
|
|
|
if iter.Error == nil {
|
|
|
|
iter.Error = decoder.err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
iter.Skip()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type lazyErrorEncoder struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *lazyErrorEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
|
|
|
if ptr == nil {
|
|
|
|
stream.WriteNil()
|
|
|
|
} else if stream.Error == nil {
|
|
|
|
stream.Error = encoder.err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *lazyErrorEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type placeholderDecoder struct {
|
|
|
|
decoder ValDecoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (decoder *placeholderDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
|
|
|
decoder.decoder.Decode(ptr, iter)
|
|
|
|
}
|
|
|
|
|
|
|
|
type placeholderEncoder struct {
|
|
|
|
encoder ValEncoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *placeholderEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
|
|
|
encoder.encoder.Encode(ptr, stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *placeholderEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
|
|
|
return encoder.encoder.IsEmpty(ptr)
|
|
|
|
}
|