Skip to content

Commit

Permalink
fix patch for anonymous struct fields
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed Oct 2, 2015
1 parent 7007481 commit c4ca26f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
51 changes: 51 additions & 0 deletions pkg/apiserver/resthandler_test.go
Original file line number Diff line number Diff line change
@@ -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))
}

}
16 changes: 13 additions & 3 deletions third_party/forked/json/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ func LookupPatchMetadata(t reflect.Type, jsonField string) (reflect.Type, string
}
}
if f != nil {
// Find the reflect.Value of the most preferential
// struct field.
// Find the reflect.Value of the most preferential struct field.
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")
patchMergeKey := tjf.Tag.Get("patchMergeKey")
return tjf.Type, patchStrategy, patchMergeKey, nil
Expand All @@ -60,13 +63,20 @@ type field struct {
nameBytes []byte // []byte(name)
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
typ reflect.Type
omitEmpty 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 {
f.nameBytes = []byte(f.name)
f.equalFold = foldFunc(f.nameBytes)
Expand Down

0 comments on commit c4ca26f

Please sign in to comment.