Просмотр исходного кода

Don't consider empty filters equal (including nil)

I think in the long term, we should consider an empty or equal to an
empty and, just in case it becomes necessary to distinguish them in
the future. I think this also applies to considering an empty or/and
equivalent to nil.

Signed-off-by: Michael Dresser <michaelmdresser@gmail.com>
Michael Dresser 3 лет назад
Родитель
Сommit
75211e0690
2 измененных файлов с 6 добавлено и 48 удалено
  1. 2 44
      pkg/kubecost/allocationfilter.go
  2. 4 4
      pkg/kubecost/allocationfilter_test.go

+ 2 - 44
pkg/kubecost/allocationfilter.go

@@ -105,10 +105,6 @@ type AllocationFilter interface {
 	// Equals returns true if the two AllocationFilters are logically
 	// equivalent.
 	Equals(AllocationFilter) bool
-
-	// empty returns true if the filter isn't filtering anything, i.e. the
-	// filter is a no-op.
-	empty() bool
 }
 
 // AllocationFilterCondition is the lowest-level type of filter. It represents
@@ -150,10 +146,6 @@ func (left AllocationFilterCondition) Equals(right AllocationFilter) bool {
 	return false
 }
 
-func (filter AllocationFilterCondition) empty() bool {
-	return false
-}
-
 // AllocationFilterOr is a set of filters that should be evaluated as a logical
 // OR.
 type AllocationFilterOr struct {
@@ -223,10 +215,7 @@ func (filter AllocationFilterOr) sort() {
 }
 
 func (left AllocationFilterOr) Equals(right AllocationFilter) bool {
-	if left.empty() {
-		return right == nil || right.empty()
-	}
-
+	// The type cast takes care of right == nil as well
 	rightOr, ok := right.(AllocationFilterOr)
 	if !ok {
 		return false
@@ -239,18 +228,6 @@ func (left AllocationFilterOr) Equals(right AllocationFilter) bool {
 	return left.String() == rightOr.String()
 }
 
-func (filter AllocationFilterOr) empty() bool {
-	for _, inner := range filter.Filters {
-		if inner == nil {
-			continue
-		}
-		if !inner.empty() {
-			return false
-		}
-	}
-	return true
-}
-
 // AllocationFilterOr is a set of filters that should be evaluated as a logical
 // AND.
 type AllocationFilterAnd struct {
@@ -304,10 +281,7 @@ func (filter AllocationFilterAnd) sort() {
 }
 
 func (left AllocationFilterAnd) Equals(right AllocationFilter) bool {
-	if left.empty() {
-		return right == nil || right.empty()
-	}
-
+	// The type cast takes care of right == nil as well
 	rightAnd, ok := right.(AllocationFilterAnd)
 	if !ok {
 		return false
@@ -320,18 +294,6 @@ func (left AllocationFilterAnd) Equals(right AllocationFilter) bool {
 	return left.String() == rightAnd.String()
 }
 
-func (filter AllocationFilterAnd) empty() bool {
-	for _, inner := range filter.Filters {
-		if inner == nil {
-			continue
-		}
-		if !inner.empty() {
-			return false
-		}
-	}
-	return true
-}
-
 func (filter AllocationFilterCondition) Matches(a *Allocation) bool {
 	if a == nil {
 		return false
@@ -544,7 +506,3 @@ func (left AllocationFilterNone) Equals(right AllocationFilter) bool {
 	_, ok := right.(AllocationFilterNone)
 	return ok
 }
-
-func (afn AllocationFilterNone) empty() bool {
-	return false
-}

+ 4 - 4
pkg/kubecost/allocationfilter_test.go

@@ -1218,12 +1218,12 @@ func Test_AllocationFilter_Equals(t *testing.T) {
 		{
 			left:     AllocationFilterOr{},
 			right:    nil,
-			expected: true,
+			expected: false,
 		},
 		{
 			left:     AllocationFilterOr{Filters: []AllocationFilter{}},
 			right:    nil,
-			expected: true,
+			expected: false,
 		},
 
 		{
@@ -1449,12 +1449,12 @@ func Test_AllocationFilter_Equals(t *testing.T) {
 		{
 			left:     AllocationFilterAnd{},
 			right:    nil,
-			expected: true,
+			expected: false,
 		},
 		{
 			left:     AllocationFilterAnd{Filters: []AllocationFilter{}},
 			right:    nil,
-			expected: true,
+			expected: false,
 		},
 
 		{