cleanup: consolidate util strings contain functions (#5304)

* Consolidate util strings contain functions

* fix test
pull/5335/head
Travis Patterson 3 years ago committed by GitHub
parent cf419d1cb8
commit 1925d77f65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      clients/pkg/logentry/stages/timestamp.go
  2. 2
      clients/pkg/promtail/targets/kafka/target_syncer.go
  3. 10
      pkg/util/string.go
  4. 4
      pkg/util/string_test.go

@ -78,7 +78,7 @@ func validateTimestampConfig(cfg *TimestampConfig) (parser, error) {
if cfg.ActionOnFailure == nil { if cfg.ActionOnFailure == nil {
cfg.ActionOnFailure = util.StringRef(TimestampActionOnFailureDefault) cfg.ActionOnFailure = util.StringRef(TimestampActionOnFailureDefault)
} else { } else {
if !util.StringSliceContains(TimestampActionOnFailureOptions, *cfg.ActionOnFailure) { if !util.StringsContain(TimestampActionOnFailureOptions, *cfg.ActionOnFailure) {
return nil, fmt.Errorf(ErrInvalidActionOnFailure, TimestampActionOnFailureOptions) return nil, fmt.Errorf(ErrInvalidActionOnFailure, TimestampActionOnFailureOptions)
} }
} }

@ -159,7 +159,7 @@ func withSASLAuthentication(cfg sarama.Config, authCfg scrapeconfig.KafkaAuthent
sarama.SASLTypeSCRAMSHA256, sarama.SASLTypeSCRAMSHA256,
sarama.SASLTypePlaintext, sarama.SASLTypePlaintext,
} }
if !util.StringSliceContains(supportedMechanism, string(authCfg.SASLConfig.Mechanism)) { if !util.StringsContain(supportedMechanism, string(authCfg.SASLConfig.Mechanism)) {
return nil, fmt.Errorf("error unsupported sasl mechanism: %s", authCfg.SASLConfig.Mechanism) return nil, fmt.Errorf("error unsupported sasl mechanism: %s", authCfg.SASLConfig.Mechanism)
} }

@ -10,16 +10,6 @@ func StringRef(value string) *string {
return &value return &value
} }
func StringSliceContains(slice []string, value string) bool {
for _, item := range slice {
if item == value {
return true
}
}
return false
}
// SnakeCase converts given string `s` into `snake_case`. // SnakeCase converts given string `s` into `snake_case`.
func SnakeCase(s string) string { func SnakeCase(s string) string {
var buf bytes.Buffer var buf bytes.Buffer

@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestStringSliceContains(t *testing.T) { func TestStringsContain(t *testing.T) {
t.Parallel() t.Parallel()
tests := map[string]struct { tests := map[string]struct {
@ -32,7 +32,7 @@ func TestStringSliceContains(t *testing.T) {
t.Run(testName, func(t *testing.T) { t.Run(testName, func(t *testing.T) {
t.Parallel() t.Parallel()
actual := StringSliceContains(testData.inputSlice, testData.inputValue) actual := StringsContain(testData.inputSlice, testData.inputValue)
assert.Equal(t, testData.expected, actual) assert.Equal(t, testData.expected, actual)
}) })
} }

Loading…
Cancel
Save