refactor: use slices.Contains to simplify code

Signed-off-by: hardlydearly <799511800@qq.com>
This commit is contained in:
hardlydearly 2025-04-30 10:43:26 +08:00 committed by Ayoub Mrini
parent 59874fd89c
commit ba4b058b7a
7 changed files with 22 additions and 48 deletions

View File

@ -30,6 +30,7 @@ import (
goregexp "regexp" //nolint:depguard // The Prometheus client library requires us to pass a regexp from this package.
"runtime"
"runtime/debug"
"slices"
"strconv"
"strings"
"sync"
@ -1921,10 +1922,8 @@ func (p *rwProtoMsgFlagParser) Set(opt string) error {
if err := t.Validate(); err != nil {
return err
}
for _, prev := range *p.msgs {
if prev == t {
return fmt.Errorf("duplicated %v flag value, got %v already", t, *p.msgs)
}
if slices.Contains(*p.msgs, t) {
return fmt.Errorf("duplicated %v flag value, got %v already", t, *p.msgs)
}
*p.msgs = append(*p.msgs, t)
return nil

View File

@ -21,6 +21,7 @@ import (
"net/url"
"os"
"path/filepath"
"slices"
"sort"
"strconv"
"strings"
@ -1109,13 +1110,11 @@ func (v *AlertmanagerAPIVersion) UnmarshalYAML(unmarshal func(interface{}) error
return err
}
for _, supportedVersion := range SupportedAlertmanagerAPIVersions {
if *v == supportedVersion {
return nil
}
if !slices.Contains(SupportedAlertmanagerAPIVersions, *v) {
return fmt.Errorf("expected Alertmanager api version to be one of %v but got %v", SupportedAlertmanagerAPIVersions, *v)
}
return fmt.Errorf("expected Alertmanager api version to be one of %v but got %v", SupportedAlertmanagerAPIVersions, *v)
return nil
}
const (

View File

@ -20,6 +20,7 @@ import (
"log/slog"
"os"
"reflect"
"slices"
"strings"
"sync"
"time"
@ -210,18 +211,9 @@ func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
if _, ok := allowedSelectors[c.Role]; !ok {
return fmt.Errorf("invalid role: %q, expecting one of: pod, service, endpoints, endpointslice, node or ingress", c.Role)
}
var allowed bool
for _, role := range allowedSelectors[c.Role] {
if role == string(selector.Role) {
allowed = true
break
}
}
if !allowed {
if !slices.Contains(allowedSelectors[c.Role], string(selector.Role)) {
return fmt.Errorf("%s role supports only %s selectors", c.Role, strings.Join(allowedSelectors[c.Role], ", "))
}
_, err := fields.ParseSelector(selector.Field)
if err != nil {
return err

View File

@ -167,10 +167,8 @@ func (b *Builder) Del(ns ...string) *Builder {
// Keep removes all labels from the base except those with the given names.
func (b *Builder) Keep(ns ...string) *Builder {
b.base.Range(func(l Label) {
for _, n := range ns {
if l.Name == n {
return
}
if slices.Contains(ns, l.Name) {
return
}
b.del = append(b.del, l.Name)
})

View File

@ -95,12 +95,7 @@ func (m *FastRegexMatcher) compileMatchStringFunction() func(string) bool {
return func(s string) bool {
if len(m.setMatches) != 0 {
for _, match := range m.setMatches {
if match == s {
return true
}
}
return false
return slices.Contains(m.setMatches, s)
}
if m.prefix != "" && !strings.HasPrefix(s, m.prefix) {
return false
@ -771,16 +766,11 @@ func (m *equalMultiStringSliceMatcher) setMatches() []string {
func (m *equalMultiStringSliceMatcher) Matches(s string) bool {
if m.caseSensitive {
for _, v := range m.values {
if s == v {
return true
}
}
} else {
for _, v := range m.values {
if strings.EqualFold(s, v) {
return true
}
return slices.Contains(m.values, s)
}
for _, v := range m.values {
if strings.EqualFold(s, v) {
return true
}
}
return false

View File

@ -20,6 +20,7 @@ import (
"math"
"os"
"path"
"slices"
"sort"
"strconv"
"sync"
@ -1008,11 +1009,8 @@ func TestMetricsUpdate(t *testing.T) {
var metrics int
for _, m := range ms {
s := m.GetName()
for _, n := range metricNames {
if s == n {
metrics += len(m.Metric)
break
}
if slices.Contains(metricNames, s) {
metrics += len(m.Metric)
}
}
return metrics

View File

@ -599,10 +599,8 @@ func Intersect(its ...Postings) Postings {
if len(its) == 1 {
return its[0]
}
for _, p := range its {
if p == EmptyPostings() {
return EmptyPostings()
}
if slices.Contains(its, EmptyPostings()) {
return EmptyPostings()
}
return newIntersectPostings(its...)