From b0c0a5edc592a6bbf66e361ac16d46e523a802fe Mon Sep 17 00:00:00 2001 From: Jiahui Feng Date: Wed, 11 Jan 2023 10:08:11 -0800 Subject: [PATCH] make structural type use OpenAPI lib. Kubernetes-commit: e11ea145eaa9c3261a3a6c3ef8c9c5ff29ad5e56 --- pkg/cel/openapi/maplist.go | 20 ++++++++++---------- pkg/cel/openapi/maplist_test.go | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pkg/cel/openapi/maplist.go b/pkg/cel/openapi/maplist.go index d018fc8a1..ff67c9eed 100644 --- a/pkg/cel/openapi/maplist.go +++ b/pkg/cel/openapi/maplist.go @@ -23,12 +23,12 @@ import ( "k8s.io/kube-openapi/pkg/validation/spec" ) -// mapList provides a "lookup by key" operation for lists (arrays) with x-kubernetes-list-type=map. -type mapList interface { - // get returns the first element having given key, for all - // x-kubernetes-list-map-keys, to the provided object. If the provided object isn't itself a valid mapList element, +// MapList provides a "lookup by key" operation for lists (arrays) with x-kubernetes-list-type=map. +type MapList interface { + // Get returns the first element having given key, for all + // x-kubernetes-list-map-keys, to the provided object. If the provided object isn't itself a valid MapList element, // get returns nil. - get(interface{}) interface{} + Get(interface{}) interface{} } type keyStrategy interface { @@ -91,10 +91,10 @@ func (ks *multiKeyStrategy) CompositeKeyFor(obj map[string]interface{}) (interfa return delimited.String(), true } -// emptyMapList is a mapList containing no elements. +// emptyMapList is a MapList containing no elements. type emptyMapList struct{} -func (emptyMapList) get(interface{}) interface{} { +func (emptyMapList) Get(interface{}) interface{} { return nil } @@ -107,7 +107,7 @@ type mapListImpl struct { unkeyedItems []interface{} } -func (a *mapListImpl) get(obj interface{}) interface{} { +func (a *mapListImpl) Get(obj interface{}) interface{} { mobj, ok := obj.(map[string]interface{}) if !ok { return nil @@ -162,10 +162,10 @@ func makeKeyStrategy(sts *spec.Schema) keyStrategy { } } -// makeMapList returns a queryable interface over the provided x-kubernetes-list-type=map +// MakeMapList returns a queryable interface over the provided x-kubernetes-list-type=map // keyedItems. If the provided schema is _not_ an array with x-kubernetes-list-type=map, returns an // empty mapList. -func makeMapList(sts *spec.Schema, items []interface{}) (rv mapList) { +func MakeMapList(sts *spec.Schema, items []interface{}) (rv MapList) { if !sts.Type.Contains("array") || getXListType(sts) != "map" || len(getXListMapKeys(sts)) == 0 { return emptyMapList{} } diff --git a/pkg/cel/openapi/maplist_test.go b/pkg/cel/openapi/maplist_test.go index 134a0f2cd..62d303d15 100644 --- a/pkg/cel/openapi/maplist_test.go +++ b/pkg/cel/openapi/maplist_test.go @@ -296,11 +296,11 @@ func TestMapList(t *testing.T) { }, } { t.Run(tc.name, func(t *testing.T) { - mapList := makeMapList(tc.sts, tc.items) + mapList := MakeMapList(tc.sts, tc.items) for _, warmUp := range tc.warmUpQueries { - mapList.get(warmUp) + mapList.Get(warmUp) } - actual := mapList.get(tc.query) + actual := mapList.Get(tc.query) if !reflect.DeepEqual(tc.expected, actual) { t.Errorf("got: %v, expected %v", actual, tc.expected) }