From 0ed67c9e41dcfc3eef6953ca63082454c189443b Mon Sep 17 00:00:00 2001 From: Alexander Zielenski Date: Fri, 13 Oct 2023 14:05:47 -0700 Subject: [PATCH] cleanup: use swtich in CachedDeepEqual and add more comments --- .../apiserver/pkg/cel/common/equality.go | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/staging/src/k8s.io/apiserver/pkg/cel/common/equality.go b/staging/src/k8s.io/apiserver/pkg/cel/common/equality.go index 57e107db891..e08103fc8a2 100644 --- a/staging/src/k8s.io/apiserver/pkg/cel/common/equality.go +++ b/staging/src/k8s.io/apiserver/pkg/cel/common/equality.go @@ -159,9 +159,18 @@ func (r *CorrelatedObject) CachedDeepEqual() (res bool) { oldAsArray, oldIsArray := r.OldValue.([]interface{}) newAsArray, newIsArray := r.Value.([]interface{}) - if oldIsArray != newIsArray { + oldAsMap, oldIsMap := r.OldValue.(map[string]interface{}) + newAsMap, newIsMap := r.Value.(map[string]interface{}) + + // If old and new are not the same type, they are not equal + if (oldIsArray != newIsArray) || oldIsMap != newIsMap { return false - } else if oldIsArray { + } + + // Objects are known to be same type of (map, slice, or primitive) + switch { + case oldIsArray: + // Both arrays case. oldIsArray == newIsArray if len(oldAsArray) != len(newAsArray) { return false } @@ -185,14 +194,8 @@ func (r *CorrelatedObject) CachedDeepEqual() (res bool) { } return true - } - - oldAsMap, oldIsMap := r.OldValue.(map[string]interface{}) - newAsMap, newIsMap := r.Value.(map[string]interface{}) - - if oldIsMap != newIsMap { - return false - } else if oldIsMap { + case oldIsMap: + // Both maps case. oldIsMap == newIsMap if len(oldAsMap) != len(newAsMap) { return false } @@ -210,9 +213,11 @@ func (r *CorrelatedObject) CachedDeepEqual() (res bool) { } return true - } - return reflect.DeepEqual(r.OldValue, r.Value) + default: + // Primitive: use reflect.DeepEqual + return reflect.DeepEqual(r.OldValue, r.Value) + } } // Key returns the child of the receiver with the given name.