diff --git a/staging/src/k8s.io/apimachinery/third_party/forked/golang/json/BUILD b/staging/src/k8s.io/apimachinery/third_party/forked/golang/json/BUILD index cf64e1f539a..9754f28e932 100644 --- a/staging/src/k8s.io/apimachinery/third_party/forked/golang/json/BUILD +++ b/staging/src/k8s.io/apimachinery/third_party/forked/golang/json/BUILD @@ -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(["**"]), diff --git a/staging/src/k8s.io/apimachinery/third_party/forked/golang/json/fields.go b/staging/src/k8s.io/apimachinery/third_party/forked/golang/json/fields.go index ac6d9cb96b1..006972ecafd 100644 --- a/staging/src/k8s.io/apimachinery/third_party/forked/golang/json/fields.go +++ b/staging/src/k8s.io/apimachinery/third_party/forked/golang/json/fields.go @@ -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 diff --git a/staging/src/k8s.io/apimachinery/third_party/forked/golang/json/fields_test.go b/staging/src/k8s.io/apimachinery/third_party/forked/golang/json/fields_test.go new file mode 100644 index 00000000000..04d8cebd9b2 --- /dev/null +++ b/staging/src/k8s.io/apimachinery/third_party/forked/golang/json/fields_test.go @@ -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") + } +}