From 3268cbdcf41aed03c0b699100cf40dedac3dce5f Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Sat, 20 Jul 2024 15:42:51 +0200 Subject: [PATCH] refactor: replace string slice search with slices package Signed-off-by: Mark Sagi-Kazar --- remote.go | 5 +++-- util.go | 9 --------- viper.go | 11 ++++++----- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/remote.go b/remote.go index 2c07f81..bdde7de 100644 --- a/remote.go +++ b/remote.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "reflect" + "slices" ) // SupportedRemoteProviders are universally supported remote providers. @@ -93,7 +94,7 @@ func AddRemoteProvider(provider, endpoint, path string) error { } func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error { - if !stringInSlice(provider, SupportedRemoteProviders) { + if !slices.Contains(SupportedRemoteProviders, provider) { return UnsupportedRemoteProviderError(provider) } if provider != "" && endpoint != "" { @@ -126,7 +127,7 @@ func AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) err } func (v *Viper) AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error { - if !stringInSlice(provider, SupportedRemoteProviders) { + if !slices.Contains(SupportedRemoteProviders, provider) { return UnsupportedRemoteProviderError(provider) } if provider != "" && endpoint != "" { diff --git a/util.go b/util.go index 9a6a5a8..2a08074 100644 --- a/util.go +++ b/util.go @@ -128,15 +128,6 @@ func absPathify(logger *slog.Logger, inPath string) string { return "" } -func stringInSlice(a string, list []string) bool { - for _, b := range list { - if b == a { - return true - } - } - return false -} - func userHomeDir() string { if runtime.GOOS == "windows" { home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") diff --git a/viper.go b/viper.go index c7b6244..c4f66bb 100644 --- a/viper.go +++ b/viper.go @@ -29,6 +29,7 @@ import ( "os" "path/filepath" "reflect" + "slices" "strconv" "strings" "sync" @@ -460,7 +461,7 @@ func (v *Viper) AddConfigPath(in string) { absin := absPathify(v.logger, in) v.logger.Info("adding path to search paths", "path", absin) - if !stringInSlice(absin, v.configPaths) { + if !slices.Contains(v.configPaths, absin) { v.configPaths = append(v.configPaths, absin) } } @@ -1478,7 +1479,7 @@ func (v *Viper) ReadInConfig() error { return err } - if !stringInSlice(v.getConfigType(), SupportedExts) { + if !slices.Contains(SupportedExts, v.getConfigType()) { return UnsupportedConfigError(v.getConfigType()) } @@ -1509,7 +1510,7 @@ func (v *Viper) MergeInConfig() error { return err } - if !stringInSlice(v.getConfigType(), SupportedExts) { + if !slices.Contains(SupportedExts, v.getConfigType()) { return UnsupportedConfigError(v.getConfigType()) } @@ -1616,7 +1617,7 @@ func (v *Viper) writeConfig(filename string, force bool) error { return fmt.Errorf("config type could not be determined for %s", filename) } - if !stringInSlice(configType, SupportedExts) { + if !slices.Contains(SupportedExts, configType) { return UnsupportedConfigError(configType) } if v.config == nil { @@ -1645,7 +1646,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]any) error { format := strings.ToLower(v.getConfigType()) - if !stringInSlice(format, SupportedExts) { + if !slices.Contains(SupportedExts, format) { return UnsupportedConfigError(format) }