Skip to content

Commit

Permalink
added stringsutil package
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Ángel Ortuño <ortuman@gmail.com>
  • Loading branch information
ortuman committed Dec 13, 2022
1 parent b81688e commit cd2647e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
13 changes: 3 additions & 10 deletions cache/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/golang/snappy"

stringsutil "github.com/grafana/dskit/util/strings"
)

const (
Expand All @@ -33,7 +35,7 @@ func (cfg *CompressionConfig) RegisterFlagsWithPrefix(f *flag.FlagSet, prefix st
}

func (cfg *CompressionConfig) Validate() error {
if cfg.Compression != "" && !strSliceContains(supportedCompressions, cfg.Compression) {
if cfg.Compression != "" && !stringsutil.SliceContains(supportedCompressions, cfg.Compression) {
return errUnsupportedCompression
}
return nil
Expand Down Expand Up @@ -93,12 +95,3 @@ func (s *snappyCache) Fetch(ctx context.Context, keys []string) map[string][]byt
func (s *snappyCache) Name() string {
return s.next.Name()
}

func strSliceContains(slice []string, s string) bool {
for _, v := range slice {
if v == s {
return true
}
}
return false
}
8 changes: 4 additions & 4 deletions ring/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (

"github.com/grafana/dskit/kv"
shardUtil "github.com/grafana/dskit/ring/shard"
"github.com/grafana/dskit/ring/util"
"github.com/grafana/dskit/services"

"github.com/grafana/dskit/flagext"
dsmath "github.com/grafana/dskit/internal/math"
stringsutil "github.com/grafana/dskit/util/strings"
)

const (
Expand Down Expand Up @@ -291,7 +291,7 @@ func (r *Ring) updateRingState(ringDesc *Desc) {
// Filter out all instances belonging to excluded zones.
if len(r.cfg.ExcludedZones) > 0 {
for instanceID, instance := range ringDesc.Ingesters {
if util.StringsContain(r.cfg.ExcludedZones, instance.Zone) {
if stringsutil.SliceContains(r.cfg.ExcludedZones, instance.Zone) {
delete(ringDesc.Ingesters, instanceID)
}
}
Expand Down Expand Up @@ -364,13 +364,13 @@ func (r *Ring) Get(key uint32, op Operation, bufDescs []InstanceDesc, bufHosts,
}

// We want n *distinct* instances && distinct zones.
if util.StringsContain(distinctHosts, info.InstanceID) {
if stringsutil.SliceContains(distinctHosts, info.InstanceID) {
continue
}

// Ignore if the instances don't have a zone set.
if r.cfg.ZoneAwarenessEnabled && info.Zone != "" {
if util.StringsContain(distinctZones, info.Zone) {
if stringsutil.SliceContains(distinctZones, info.Zone) {
continue
}
}
Expand Down
12 changes: 0 additions & 12 deletions ring/util/string_utils.go

This file was deleted.

12 changes: 12 additions & 0 deletions util/strings/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package stringsutil

// SliceContains returns true if the search value is within the list of input values.
func SliceContains(values []string, search string) bool {
for _, v := range values {
if search == v {
return true
}
}

return false
}

0 comments on commit cd2647e

Please sign in to comment.