mirror of https://github.com/k3s-io/k3s
fix patch for anonymous struct fields
parent
7007481f00
commit
c4ca26f84d
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package apiserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/kubernetes/pkg/api"
|
||||||
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||||
|
)
|
||||||
|
|
||||||
|
type testPatchType struct {
|
||||||
|
unversioned.TypeMeta `json:",inline"`
|
||||||
|
|
||||||
|
testPatchSubType `json:",inline"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type testPatchSubType struct {
|
||||||
|
StringField string `json:"theField"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*testPatchType) IsAnAPIObject() {}
|
||||||
|
|
||||||
|
func TestPatchAnonymousField(t *testing.T) {
|
||||||
|
originalJS := `{"kind":"testPatchType","theField":"my-value"}`
|
||||||
|
patch := `{"theField": "changed!"}`
|
||||||
|
expectedJS := `{"kind":"testPatchType","theField":"changed!"}`
|
||||||
|
|
||||||
|
actualBytes, err := getPatchedJS(string(api.StrategicMergePatchType), []byte(originalJS), []byte(patch), &testPatchType{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if string(actualBytes) != expectedJS {
|
||||||
|
t.Errorf("expected %v, got %v", expectedJS, string(actualBytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -44,9 +44,12 @@ func LookupPatchMetadata(t reflect.Type, jsonField string) (reflect.Type, string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if f != nil {
|
if f != nil {
|
||||||
// Find the reflect.Value of the most preferential
|
// Find the reflect.Value of the most preferential struct field.
|
||||||
// struct field.
|
|
||||||
tjf := t.Field(f.index[0])
|
tjf := t.Field(f.index[0])
|
||||||
|
// we must navigate down all the anonymously included structs in the chain
|
||||||
|
for i := 1; i < len(f.index); i++ {
|
||||||
|
tjf = tjf.Type.Field(f.index[i])
|
||||||
|
}
|
||||||
patchStrategy := tjf.Tag.Get("patchStrategy")
|
patchStrategy := tjf.Tag.Get("patchStrategy")
|
||||||
patchMergeKey := tjf.Tag.Get("patchMergeKey")
|
patchMergeKey := tjf.Tag.Get("patchMergeKey")
|
||||||
return tjf.Type, patchStrategy, patchMergeKey, nil
|
return tjf.Type, patchStrategy, patchMergeKey, nil
|
||||||
|
@ -60,13 +63,20 @@ type field struct {
|
||||||
nameBytes []byte // []byte(name)
|
nameBytes []byte // []byte(name)
|
||||||
equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent
|
equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent
|
||||||
|
|
||||||
tag bool
|
tag bool
|
||||||
|
// index is the sequence of indexes from the containing type fields to this field.
|
||||||
|
// it is a slice because anonymous structs will need multiple navigation steps to correctly
|
||||||
|
// resolve the proper fields
|
||||||
index []int
|
index []int
|
||||||
typ reflect.Type
|
typ reflect.Type
|
||||||
omitEmpty bool
|
omitEmpty bool
|
||||||
quoted bool
|
quoted bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f field) String() string {
|
||||||
|
return fmt.Sprintf("{name: %s, type: %v, tag: %v, index: %v, omitEmpty: %v, quoted: %v}", f.name, f.typ, f.tag, f.index, f.omitEmpty, f.quoted)
|
||||||
|
}
|
||||||
|
|
||||||
func fillField(f field) field {
|
func fillField(f field) field {
|
||||||
f.nameBytes = []byte(f.name)
|
f.nameBytes = []byte(f.name)
|
||||||
f.equalFold = foldFunc(f.nameBytes)
|
f.equalFold = foldFunc(f.nameBytes)
|
||||||
|
|
Loading…
Reference in New Issue