mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 05:27:21 +00:00
Merge pull request #96323 from msscaroso/fix-lint-runtime-serializer-json
Fix go lint on folder apimachinery/pkg/runtime/serializer/json
This commit is contained in:
commit
55f95bc893
@ -252,7 +252,6 @@ staging/src/k8s.io/apimachinery/pkg/conversion
|
|||||||
staging/src/k8s.io/apimachinery/pkg/runtime
|
staging/src/k8s.io/apimachinery/pkg/runtime
|
||||||
staging/src/k8s.io/apimachinery/pkg/runtime/schema
|
staging/src/k8s.io/apimachinery/pkg/runtime/schema
|
||||||
staging/src/k8s.io/apimachinery/pkg/runtime/serializer
|
staging/src/k8s.io/apimachinery/pkg/runtime/serializer
|
||||||
staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json
|
|
||||||
staging/src/k8s.io/apimachinery/pkg/runtime/serializer/protobuf
|
staging/src/k8s.io/apimachinery/pkg/runtime/serializer/protobuf
|
||||||
staging/src/k8s.io/apimachinery/pkg/runtime/serializer/recognizer
|
staging/src/k8s.io/apimachinery/pkg/runtime/serializer/recognizer
|
||||||
staging/src/k8s.io/apimachinery/pkg/runtime/serializer/streaming
|
staging/src/k8s.io/apimachinery/pkg/runtime/serializer/streaming
|
||||||
|
@ -631,7 +631,7 @@ func BenchmarkDecodeIntoJSONCodecGenConfigCompatibleWithStandardLibrary(b *testi
|
|||||||
}
|
}
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
iter := json.CaseSensitiveJsonIterator()
|
iter := json.CaseSensitiveJSONIterator()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
obj := v1.Pod{}
|
obj := v1.Pod{}
|
||||||
if err := iter.Unmarshal(encoded[i%width], &obj); err != nil {
|
if err := iter.Unmarshal(encoded[i%width], &obj); err != nil {
|
||||||
|
@ -25,7 +25,7 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime/serializer/json"
|
"k8s.io/apimachinery/pkg/runtime/serializer/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
var encodingjson = json.CaseSensitiveJsonIterator()
|
var encodingjson = json.CaseSensitiveJSONIterator()
|
||||||
|
|
||||||
// GetObjectMeta does conversion of JSON to ObjectMeta. It first tries json.Unmarshal into a metav1.ObjectMeta
|
// GetObjectMeta does conversion of JSON to ObjectMeta. It first tries json.Unmarshal into a metav1.ObjectMeta
|
||||||
// type. If that does not work and dropMalformedFields is true, it does field-by-field best-effort conversion
|
// type. If that does not work and dropMalformedFields is true, it does field-by-field best-effort conversion
|
||||||
|
@ -47,7 +47,7 @@ func TestGroupVersionUnmarshalJSON(t *testing.T) {
|
|||||||
t.Errorf("JSON codec failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)
|
t.Errorf("JSON codec failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)
|
||||||
}
|
}
|
||||||
// test the json-iterator codec
|
// test the json-iterator codec
|
||||||
iter := json.CaseSensitiveJsonIterator()
|
iter := json.CaseSensitiveJSONIterator()
|
||||||
if err := iter.Unmarshal(c.input, &result); err != nil {
|
if err := iter.Unmarshal(c.input, &result); err != nil {
|
||||||
t.Errorf("json-iterator codec failed to unmarshal input '%v': %v", c.input, err)
|
t.Errorf("json-iterator codec failed to unmarshal input '%v': %v", c.input, err)
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ func TestVerbsJsonIterUnmarshalJSON(t *testing.T) {
|
|||||||
{`{"verbs":["delete"]}`, APIResource{Verbs: Verbs([]string{"delete"})}},
|
{`{"verbs":["delete"]}`, APIResource{Verbs: Verbs([]string{"delete"})}},
|
||||||
}
|
}
|
||||||
|
|
||||||
iter := json.CaseSensitiveJsonIterator()
|
iter := json.CaseSensitiveJSONIterator()
|
||||||
for i, c := range cases {
|
for i, c := range cases {
|
||||||
var result APIResource
|
var result APIResource
|
||||||
if err := iter.Unmarshal([]byte(c.input), &result); err != nil {
|
if err := iter.Unmarshal([]byte(c.input), &result); err != nil {
|
||||||
|
@ -96,6 +96,7 @@ type SerializerOptions struct {
|
|||||||
Strict bool
|
Strict bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serializer handles encoding versioned objects into the proper JSON form
|
||||||
type Serializer struct {
|
type Serializer struct {
|
||||||
meta MetaFactory
|
meta MetaFactory
|
||||||
options SerializerOptions
|
options SerializerOptions
|
||||||
@ -144,10 +145,10 @@ func (customNumberDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CaseSensitiveJsonIterator returns a jsoniterator API that's configured to be
|
// CaseSensitiveJSONIterator returns a jsoniterator API that's configured to be
|
||||||
// case-sensitive when unmarshalling, and otherwise compatible with
|
// case-sensitive when unmarshalling, and otherwise compatible with
|
||||||
// the encoding/json standard library.
|
// the encoding/json standard library.
|
||||||
func CaseSensitiveJsonIterator() jsoniter.API {
|
func CaseSensitiveJSONIterator() jsoniter.API {
|
||||||
config := jsoniter.Config{
|
config := jsoniter.Config{
|
||||||
EscapeHTML: true,
|
EscapeHTML: true,
|
||||||
SortMapKeys: true,
|
SortMapKeys: true,
|
||||||
@ -159,10 +160,10 @@ func CaseSensitiveJsonIterator() jsoniter.API {
|
|||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
// StrictCaseSensitiveJsonIterator returns a jsoniterator API that's configured to be
|
// StrictCaseSensitiveJSONIterator returns a jsoniterator API that's configured to be
|
||||||
// case-sensitive, but also disallows unknown fields when unmarshalling. It is compatible with
|
// case-sensitive, but also disallows unknown fields when unmarshalling. It is compatible with
|
||||||
// the encoding/json standard library.
|
// the encoding/json standard library.
|
||||||
func StrictCaseSensitiveJsonIterator() jsoniter.API {
|
func StrictCaseSensitiveJSONIterator() jsoniter.API {
|
||||||
config := jsoniter.Config{
|
config := jsoniter.Config{
|
||||||
EscapeHTML: true,
|
EscapeHTML: true,
|
||||||
SortMapKeys: true,
|
SortMapKeys: true,
|
||||||
@ -179,8 +180,8 @@ func StrictCaseSensitiveJsonIterator() jsoniter.API {
|
|||||||
// from outside. Still does not protect from package level jsoniter.Register*() functions - someone calling them
|
// from outside. Still does not protect from package level jsoniter.Register*() functions - someone calling them
|
||||||
// in some other library will mess with every usage of the jsoniter library in the whole program.
|
// in some other library will mess with every usage of the jsoniter library in the whole program.
|
||||||
// See https://github.com/json-iterator/go/issues/265
|
// See https://github.com/json-iterator/go/issues/265
|
||||||
var caseSensitiveJsonIterator = CaseSensitiveJsonIterator()
|
var caseSensitiveJSONIterator = CaseSensitiveJSONIterator()
|
||||||
var strictCaseSensitiveJsonIterator = StrictCaseSensitiveJsonIterator()
|
var strictCaseSensitiveJSONIterator = StrictCaseSensitiveJSONIterator()
|
||||||
|
|
||||||
// gvkWithDefaults returns group kind and version defaulting from provided default
|
// gvkWithDefaults returns group kind and version defaulting from provided default
|
||||||
func gvkWithDefaults(actual, defaultGVK schema.GroupVersionKind) schema.GroupVersionKind {
|
func gvkWithDefaults(actual, defaultGVK schema.GroupVersionKind) schema.GroupVersionKind {
|
||||||
@ -236,7 +237,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
|
|||||||
types, _, err := s.typer.ObjectKinds(into)
|
types, _, err := s.typer.ObjectKinds(into)
|
||||||
switch {
|
switch {
|
||||||
case runtime.IsNotRegisteredError(err), isUnstructured:
|
case runtime.IsNotRegisteredError(err), isUnstructured:
|
||||||
if err := caseSensitiveJsonIterator.Unmarshal(data, into); err != nil {
|
if err := caseSensitiveJSONIterator.Unmarshal(data, into); err != nil {
|
||||||
return nil, actual, err
|
return nil, actual, err
|
||||||
}
|
}
|
||||||
return into, actual, nil
|
return into, actual, nil
|
||||||
@ -260,7 +261,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
|
|||||||
return nil, actual, err
|
return nil, actual, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := caseSensitiveJsonIterator.Unmarshal(data, obj); err != nil {
|
if err := caseSensitiveJSONIterator.Unmarshal(data, obj); err != nil {
|
||||||
return nil, actual, err
|
return nil, actual, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -285,7 +286,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
|
|||||||
// due to that a matching field doesn't exist in the object. hence we can return a typed strictDecoderError,
|
// due to that a matching field doesn't exist in the object. hence we can return a typed strictDecoderError,
|
||||||
// the actual error is that the object contains unknown field.
|
// the actual error is that the object contains unknown field.
|
||||||
strictObj := obj.DeepCopyObject()
|
strictObj := obj.DeepCopyObject()
|
||||||
if err := strictCaseSensitiveJsonIterator.Unmarshal(altered, strictObj); err != nil {
|
if err := strictCaseSensitiveJSONIterator.Unmarshal(altered, strictObj); err != nil {
|
||||||
return nil, actual, runtime.NewStrictDecodingError(err.Error(), string(originalData))
|
return nil, actual, runtime.NewStrictDecodingError(err.Error(), string(originalData))
|
||||||
}
|
}
|
||||||
// Always return the same object as the non-strict serializer to avoid any deviations.
|
// Always return the same object as the non-strict serializer to avoid any deviations.
|
||||||
@ -302,7 +303,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
|
|||||||
|
|
||||||
func (s *Serializer) doEncode(obj runtime.Object, w io.Writer) error {
|
func (s *Serializer) doEncode(obj runtime.Object, w io.Writer) error {
|
||||||
if s.options.Yaml {
|
if s.options.Yaml {
|
||||||
json, err := caseSensitiveJsonIterator.Marshal(obj)
|
json, err := caseSensitiveJSONIterator.Marshal(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -315,7 +316,7 @@ func (s *Serializer) doEncode(obj runtime.Object, w io.Writer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if s.options.Pretty {
|
if s.options.Pretty {
|
||||||
data, err := caseSensitiveJsonIterator.MarshalIndent(obj, "", " ")
|
data, err := caseSensitiveJSONIterator.MarshalIndent(obj, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -123,7 +123,7 @@ func testcases() []testcase {
|
|||||||
var decoders = map[string]func([]byte, interface{}) error{
|
var decoders = map[string]func([]byte, interface{}) error{
|
||||||
"gojson": gojson.Unmarshal,
|
"gojson": gojson.Unmarshal,
|
||||||
"utiljson": utiljson.Unmarshal,
|
"utiljson": utiljson.Unmarshal,
|
||||||
"jsoniter": CaseSensitiveJsonIterator().Unmarshal,
|
"jsoniter": CaseSensitiveJSONIterator().Unmarshal,
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJSONLimits(t *testing.T) {
|
func TestJSONLimits(t *testing.T) {
|
||||||
|
@ -60,21 +60,21 @@ type DecodableSpec struct {
|
|||||||
func (d *testDecodable) GetObjectKind() schema.ObjectKind { return d }
|
func (d *testDecodable) GetObjectKind() schema.ObjectKind { return d }
|
||||||
func (d *testDecodable) SetGroupVersionKind(gvk schema.GroupVersionKind) { d.gvk = gvk }
|
func (d *testDecodable) SetGroupVersionKind(gvk schema.GroupVersionKind) { d.gvk = gvk }
|
||||||
func (d *testDecodable) GroupVersionKind() schema.GroupVersionKind { return d.gvk }
|
func (d *testDecodable) GroupVersionKind() schema.GroupVersionKind { return d.gvk }
|
||||||
func (in *testDecodable) DeepCopyObject() runtime.Object {
|
func (d *testDecodable) DeepCopyObject() runtime.Object {
|
||||||
if in == nil {
|
if d == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
out := new(testDecodable)
|
out := new(testDecodable)
|
||||||
in.DeepCopyInto(out)
|
d.DeepCopyInto(out)
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
func (in *testDecodable) DeepCopyInto(out *testDecodable) {
|
func (d *testDecodable) DeepCopyInto(out *testDecodable) {
|
||||||
*out = *in
|
*out = *d
|
||||||
out.Other = in.Other
|
out.Other = d.Other
|
||||||
out.Value = in.Value
|
out.Value = d.Value
|
||||||
out.Spec = in.Spec
|
out.Spec = d.Spec
|
||||||
out.Interface = in.Interface
|
out.Interface = d.Interface
|
||||||
out.gvk = in.gvk
|
out.gvk = d.gvk
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ const (
|
|||||||
MutationAuditAnnotationPrefix = "mutation.webhook.admission.k8s.io/"
|
MutationAuditAnnotationPrefix = "mutation.webhook.admission.k8s.io/"
|
||||||
)
|
)
|
||||||
|
|
||||||
var encodingjson = json.CaseSensitiveJsonIterator()
|
var encodingjson = json.CaseSensitiveJSONIterator()
|
||||||
|
|
||||||
type mutatingDispatcher struct {
|
type mutatingDispatcher struct {
|
||||||
cm *webhookutil.ClientManager
|
cm *webhookutil.ClientManager
|
||||||
|
@ -24,7 +24,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// hold a single instance of the case-sensitive decoder
|
// hold a single instance of the case-sensitive decoder
|
||||||
var caseSensitiveJsonIterator = json.CaseSensitiveJsonIterator()
|
var caseSensitiveJsonIterator = json.CaseSensitiveJSONIterator()
|
||||||
|
|
||||||
// metadataValidatingDecoder wraps a decoder and additionally ensures metadata schema fields decode before returning an unstructured object
|
// metadataValidatingDecoder wraps a decoder and additionally ensures metadata schema fields decode before returning an unstructured object
|
||||||
type metadataValidatingDecoder struct {
|
type metadataValidatingDecoder struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user