Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
7c5b8f6a
Commit
7c5b8f6a
authored
Oct 05, 2015
by
Alex Robinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #14985 from deads2k/fix-anon-struct
fix patch for anonymous struct fields
parents
66cbacc9
c4ca26f8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
3 deletions
+64
-3
resthandler_test.go
pkg/apiserver/resthandler_test.go
+51
-0
fields.go
third_party/forked/json/fields.go
+13
-3
No files found.
pkg/apiserver/resthandler_test.go
0 → 100644
View file @
7c5b8f6a
/*
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
))
}
}
third_party/forked/json/fields.go
View file @
7c5b8f6a
...
@@ -44,9 +44,12 @@ func LookupPatchMetadata(t reflect.Type, jsonField string) (reflect.Type, string
...
@@ -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 {
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment