From 2d6de216b892b88ddf1fda4821418b796644a1cc Mon Sep 17 00:00:00 2001 From: Kevin D'souza Date: Sat, 26 Jul 2025 09:25:34 +0530 Subject: [PATCH] feat: move to a more robust way of checking if element in slice (#4001) --- object/user.go | 2 +- util/slice.go | 14 ++------------ util/validation.go | 4 ++-- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/object/user.go b/object/user.go index 1fdcec6b..e3e52426 100644 --- a/object/user.go +++ b/object/user.go @@ -807,7 +807,7 @@ func UpdateUser(id string, user *User, columns []string, isAdmin bool) (bool, er columns = append(columns, "deleted_time") } - if util.ContainsString(columns, "groups") { + if util.InSlice(columns, "groups") { _, err := userEnforcer.UpdateGroupsForUser(user.GetId(), user.Groups) if err != nil { return false, err diff --git a/util/slice.go b/util/slice.go index 83add773..e929f93c 100644 --- a/util/slice.go +++ b/util/slice.go @@ -14,7 +14,7 @@ package util -import "sort" +import "slices" func DeleteVal(values []string, val string) []string { newValues := []string{} @@ -38,18 +38,8 @@ func ReplaceVal(values []string, oldVal string, newVal string) []string { return newValues } -func ContainsString(values []string, val string) bool { - sort.Strings(values) - return sort.SearchStrings(values, val) != len(values) -} - func InSlice(slice []string, elem string) bool { - for _, val := range slice { - if val == elem { - return true - } - } - return false + return slices.Contains(slice, elem) } func ReturnAnyNotEmpty(strs ...string) string { diff --git a/util/validation.go b/util/validation.go index 5d32e6cc..08314fe6 100644 --- a/util/validation.go +++ b/util/validation.go @@ -54,10 +54,10 @@ func IsPhoneValid(phone string, countryCode string) bool { } func IsPhoneAllowInRegin(countryCode string, allowRegions []string) bool { - if ContainsString(allowRegions, "All") { + if InSlice(allowRegions, "All") { return true } - return ContainsString(allowRegions, countryCode) + return InSlice(allowRegions, countryCode) } func IsRegexp(s string) (bool, error) {