mirror of https://github.com/k3s-io/k3s
Merge pull request #50707 from diegs/json-ptr
Automatic merge from submit-queue (batch tested with PRs 50213, 50707, 49502, 51230, 50848) Fix forkedjson.LookupPatchMetadata for pointers. **What this PR does / why we need it**: Fixes a bug in `forkedjson.LookupPatchMetadata`. It is triggered when called with some API objects such as the `Selector` field (a pointer) in https://godoc.org/k8s.io/api/extensions/v1beta1#DeploymentSpec. The provided test case fails without the lines added to `fields.go`. **Which issue this PR fixes** N/A **Special notes for your reviewer**: **Release note**: ```release-note NONE ```pull/6/head
commit
b9425ded2e
|
@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"])
|
|||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
|
@ -10,6 +11,12 @@ go_library(
|
|||
srcs = ["fields.go"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["fields_test.go"],
|
||||
library = ":go_default_library",
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
|
|
|
@ -28,6 +28,9 @@ const (
|
|||
// TODO: fix the returned errors to be introspectable.
|
||||
func LookupPatchMetadata(t reflect.Type, jsonField string) (
|
||||
elemType reflect.Type, patchStrategies []string, patchMergeKey string, e error) {
|
||||
if t.Kind() == reflect.Ptr {
|
||||
t = t.Elem()
|
||||
}
|
||||
if t.Kind() == reflect.Map {
|
||||
elemType = t.Elem()
|
||||
return
|
||||
|
|
30
staging/src/k8s.io/apimachinery/third_party/forked/golang/json/fields_test.go
vendored
Normal file
30
staging/src/k8s.io/apimachinery/third_party/forked/golang/json/fields_test.go
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLookupPtrToStruct(t *testing.T) {
|
||||
type Elem struct {
|
||||
Key string
|
||||
Value string
|
||||
}
|
||||
type Outer struct {
|
||||
Inner []Elem `json:"inner" patchStrategy:"merge" patchMergeKey:"key"`
|
||||
}
|
||||
outer := &Outer{}
|
||||
elemType, patchStrategies, patchMergeKey, err := LookupPatchMetadata(reflect.TypeOf(outer), "inner")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if elemType != reflect.TypeOf([]Elem{}) {
|
||||
t.Errorf("elemType = %v, want: %v", elemType, reflect.TypeOf([]Elem{}))
|
||||
}
|
||||
if !reflect.DeepEqual(patchStrategies, []string{"merge"}) {
|
||||
t.Errorf("patchStrategies = %v, want: %v", patchStrategies, []string{"merge"})
|
||||
}
|
||||
if patchMergeKey != "key" {
|
||||
t.Errorf("patchMergeKey = %v, want: %v", patchMergeKey, "key")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue