From 20475d4e9af99ccfdc1cc64391954ff41a0b40e2 Mon Sep 17 00:00:00 2001 From: Jack Francis Date: Fri, 2 May 2025 12:38:10 -0700 Subject: [PATCH 1/3] add Includes filter to SKU type Signed-off-by: Jack Francis --- sku.go | 10 ++++++++++ sku_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/sku.go b/sku.go index 565f82f..baa3752 100644 --- a/sku.go +++ b/sku.go @@ -572,3 +572,13 @@ func (s *SKU) Equal(other *SKU) bool { localErr != nil && otherErr != nil } + +// Includes returns true if the SKU's name is in the list of SKUs. +func (s *SKU) Includes(skuList []SKU) bool { + for _, sku := range skuList { + if s.GetName() == sku.GetName() { + return true + } + } + return false +} diff --git a/sku_test.go b/sku_test.go index 7e91e2e..4296ef7 100644 --- a/sku_test.go +++ b/sku_test.go @@ -517,3 +517,54 @@ func Test_SKU_HasCapabilityInZone(t *testing.T) { }) } } + +// Test_SKU_Includes tests the SKU Includes method +func Test_SKU_Includes(t *testing.T) { + cases := map[string]struct { + skuList []SKU + sku SKU + expect bool + }{ + "empty list should not include": { + skuList: []SKU{}, + sku: SKU{ + Name: to.StringPtr("foo"), + }, + expect: false, + }, + "missing name should not include": { + skuList: []SKU{ + { + Name: to.StringPtr("foo"), + }, + }, + sku: SKU{ + Name: to.StringPtr("bar"), + }, + expect: false, + }, + "name is included": { + skuList: []SKU{ + { + Name: to.StringPtr("foo"), + }, + { + Name: to.StringPtr("bar"), + }, + }, + sku: SKU{ + Name: to.StringPtr("bar"), + }, + expect: true, + }, + } + for name, tc := range cases { + tc := tc + t.Run(name, func(t *testing.T) { + sku := SKU(tc.sku) + if diff := cmp.Diff(tc.expect, sku.Includes(tc.skuList)); diff != "" { + t.Error(diff) + } + }) + } +} From 7935f6adfeee4651c49c68896f0a5795bdac5a5b Mon Sep 17 00:00:00 2001 From: Jack Francis Date: Mon, 5 May 2025 12:17:17 -0700 Subject: [PATCH 2/3] include FilterFn Signed-off-by: Jack Francis --- cache.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cache.go b/cache.go index 5248d81..0981faa 100644 --- a/cache.go +++ b/cache.go @@ -371,5 +371,12 @@ func UnsafeLocationFilter(location string) func(*SKU) bool { } } +// IncludesFilter returns a FilterFn that checks if the SKU is included in the provided list of SKUs. +func IncludesFilter(skuList []SKU) func(*SKU) bool { + return func(s *SKU) bool { + return s.Includes(skuList) + } +} + // MapFn is a convenience type for mapping. type MapFn func(*SKU) SKU From 6b826df348e17ec2135417a9e08f224d90809669 Mon Sep 17 00:00:00 2001 From: Jack Francis Date: Wed, 7 May 2025 08:20:10 -0700 Subject: [PATCH 3/3] s/Includes/MemberOf Signed-off-by: Jack Francis --- cache.go | 2 +- sku.go | 4 ++-- sku_test.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cache.go b/cache.go index 0981faa..90517d3 100644 --- a/cache.go +++ b/cache.go @@ -374,7 +374,7 @@ func UnsafeLocationFilter(location string) func(*SKU) bool { // IncludesFilter returns a FilterFn that checks if the SKU is included in the provided list of SKUs. func IncludesFilter(skuList []SKU) func(*SKU) bool { return func(s *SKU) bool { - return s.Includes(skuList) + return s.MemberOf(skuList) } } diff --git a/sku.go b/sku.go index baa3752..436ad93 100644 --- a/sku.go +++ b/sku.go @@ -573,8 +573,8 @@ func (s *SKU) Equal(other *SKU) bool { otherErr != nil } -// Includes returns true if the SKU's name is in the list of SKUs. -func (s *SKU) Includes(skuList []SKU) bool { +// MemberOf returns true if the SKU's name is in the list of SKUs. +func (s *SKU) MemberOf(skuList []SKU) bool { for _, sku := range skuList { if s.GetName() == sku.GetName() { return true diff --git a/sku_test.go b/sku_test.go index 4296ef7..ec79d0a 100644 --- a/sku_test.go +++ b/sku_test.go @@ -518,7 +518,7 @@ func Test_SKU_HasCapabilityInZone(t *testing.T) { } } -// Test_SKU_Includes tests the SKU Includes method +// Test_SKU_MemberOf tests the SKU MemberOf method func Test_SKU_Includes(t *testing.T) { cases := map[string]struct { skuList []SKU @@ -562,7 +562,7 @@ func Test_SKU_Includes(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { sku := SKU(tc.sku) - if diff := cmp.Diff(tc.expect, sku.Includes(tc.skuList)); diff != "" { + if diff := cmp.Diff(tc.expect, sku.MemberOf(tc.skuList)); diff != "" { t.Error(diff) } })