From 0e79c4bd2a4ae314daa340fc37daa1160709b740 Mon Sep 17 00:00:00 2001 From: tanhuaan Date: Wed, 30 Jul 2025 16:32:57 +0800 Subject: [PATCH] refactor: use slices.Contains to simplify code Signed-off-by: tanhuaan --- util/sprig/defaults.go | 8 ++------ util/util.go | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/util/sprig/defaults.go b/util/sprig/defaults.go index c5c14308..0d3056b9 100644 --- a/util/sprig/defaults.go +++ b/util/sprig/defaults.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "reflect" + "slices" "strings" ) @@ -95,12 +96,7 @@ func coalesce(v ...any) any { // Returns: // - bool: True if all values are non-empty, false otherwise func all(v ...any) bool { - for _, val := range v { - if empty(val) { - return false - } - } - return true + return !slices.ContainsFunc(v, empty) } // anyNonEmpty checks if at least one value in a list is non-empty. diff --git a/util/util.go b/util/util.go index 73b227af..66e9dfd6 100644 --- a/util/util.go +++ b/util/util.go @@ -12,6 +12,7 @@ import ( "net/netip" "os" "regexp" + "slices" "strconv" "strings" "sync" @@ -49,12 +50,7 @@ func FileExists(filename string) bool { // Contains returns true if needle is contained in haystack func Contains[T comparable](haystack []T, needle T) bool { - for _, s := range haystack { - if s == needle { - return true - } - } - return false + return slices.Contains(haystack, needle) } // ContainsIP returns true if any one of the of prefixes contains the ip.