From 5ad3a7e4cb8a01980731b58dc23296433e4a455c Mon Sep 17 00:00:00 2001 From: Tyson Cung Date: Sat, 8 Nov 2025 10:12:53 +0800 Subject: [PATCH] Cleanup: replace custom contains() with slices.Contains() This change removes duplicate contains() implementations and uses the standard library slices.Contains() function instead. Changes: - Remove contains[T comparable]() from protocol/protocol.go - Remove contains[T comparable]() from reconciler/bucket.go - Replace contains() usage in protocol/s3.go with slices.Contains() - Replace contains() usage in reconciler/bucket.go with slices.Contains() Fixes #176 --- internal/protocol/protocol.go | 11 ----------- internal/protocol/s3.go | 3 ++- sidecar/internal/reconciler/bucket.go | 13 ++----------- 3 files changed, 4 insertions(+), 23 deletions(-) diff --git a/internal/protocol/protocol.go b/internal/protocol/protocol.go index 91e6defa..ca01a964 100644 --- a/internal/protocol/protocol.go +++ b/internal/protocol/protocol.go @@ -73,14 +73,3 @@ type RpcApiTranslator[RpcType any, ApiType comparable] interface { // Validate checks that user-domain API fields meet requirements and expectations. Validate(map[ApiType]string, cosiapi.BucketAccessAuthenticationType) error } - -// contains is a helper that returns true if the given `list` contains the item `key`. -// Useful for a variety of Validate() implementations. -func contains[T comparable](list []T, key T) bool { - for _, i := range list { - if i == key { - return true - } - } - return false -} diff --git a/internal/protocol/s3.go b/internal/protocol/s3.go index 612617a5..1743a07d 100644 --- a/internal/protocol/s3.go +++ b/internal/protocol/s3.go @@ -18,6 +18,7 @@ package protocol import ( "fmt" + "slices" cosiapi "sigs.k8s.io/container-object-storage-interface/client/apis/objectstorage/v1alpha2" cosiproto "sigs.k8s.io/container-object-storage-interface/proto" @@ -111,7 +112,7 @@ func (S3BucketInfoTranslator) Validate( } as := vars[cosiapi.BucketInfoVar_S3_AddressingStyle] - if !contains(validS3AddressingStyles, as) { + if !slices.Contains(validS3AddressingStyles, as) { errs = append(errs, fmt.Sprintf("S3 addressing style %q must be one of %v", as, validS3AddressingStyles)) } diff --git a/sidecar/internal/reconciler/bucket.go b/sidecar/internal/reconciler/bucket.go index 746f41d3..a07202ca 100644 --- a/sidecar/internal/reconciler/bucket.go +++ b/sidecar/internal/reconciler/bucket.go @@ -19,6 +19,7 @@ package reconciler import ( "context" "fmt" + "slices" "time" "github.com/go-logr/logr" @@ -366,7 +367,7 @@ func validateDriverSupportsProtocols(driver DriverInfo, required []*cosiproto.Ob func validateBucketSupportsProtocols(supported, required []cosiapi.ObjectProtocol) error { unsupported := []string{} for _, req := range required { - if !contains(supported, req) { + if !slices.Contains(supported, req) { unsupported = append(unsupported, string(req)) } } @@ -386,13 +387,3 @@ func mergeApiInfoIntoStringMap[T cosiapi.BucketInfoVar | cosiapi.CredentialVar]( target[string(k)] = v } } - -// contains returns true if the given `list` contains the item `key`. -func contains[T comparable](list []T, key T) bool { - for _, i := range list { - if i == key { - return true - } - } - return false -}