mirror of
https://github.com/spf13/viper
synced 2024-12-22 03:27:03 +00:00
fix: gocritic lint issues
This commit is contained in:
parent
3a23b80b11
commit
f0c4ccd6cd
10 changed files with 49 additions and 34 deletions
|
@ -7,6 +7,16 @@ linters-settings:
|
||||||
- standard
|
- standard
|
||||||
- default
|
- default
|
||||||
- prefix(github.com/spf13/viper)
|
- prefix(github.com/spf13/viper)
|
||||||
|
gocritic:
|
||||||
|
# Enable multiple checks by tags. See "Tags" section in https://github.com/go-critic/go-critic#usage.
|
||||||
|
enabled-tags:
|
||||||
|
- diagnostic
|
||||||
|
- experimental
|
||||||
|
- opinionated
|
||||||
|
- performance
|
||||||
|
- style
|
||||||
|
disabled-checks:
|
||||||
|
- importShadow
|
||||||
golint:
|
golint:
|
||||||
min-confidence: 0
|
min-confidence: 0
|
||||||
goimports:
|
goimports:
|
||||||
|
@ -22,6 +32,7 @@ linters:
|
||||||
- exhaustive
|
- exhaustive
|
||||||
- exportloopref
|
- exportloopref
|
||||||
- gci
|
- gci
|
||||||
|
- gocritic
|
||||||
- godot
|
- godot
|
||||||
- gofmt
|
- gofmt
|
||||||
- gofumpt
|
- gofumpt
|
||||||
|
@ -63,7 +74,6 @@ linters:
|
||||||
# - gochecknoinits
|
# - gochecknoinits
|
||||||
# - gocognit
|
# - gocognit
|
||||||
# - goconst
|
# - goconst
|
||||||
# - gocritic
|
|
||||||
# - gocyclo
|
# - gocyclo
|
||||||
# - gosec
|
# - gosec
|
||||||
# - gosimple
|
# - gosimple
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
// flattenAndMergeMap recursively flattens the given map into a new map
|
// flattenAndMergeMap recursively flattens the given map into a new map
|
||||||
// Code is based on the function with the same name in the main package.
|
// Code is based on the function with the same name in the main package.
|
||||||
// TODO: move it to a common place.
|
// TODO: move it to a common place.
|
||||||
func flattenAndMergeMap(shadow map[string]any, m map[string]any, prefix string, delimiter string) map[string]any {
|
func flattenAndMergeMap(shadow, m map[string]any, prefix, delimiter string) map[string]any {
|
||||||
if shadow != nil && prefix != "" && shadow[prefix] != nil {
|
if shadow != nil && prefix != "" && shadow[prefix] != nil {
|
||||||
// prefix is shadowed => nothing more to flatten
|
// prefix is shadowed => nothing more to flatten
|
||||||
return shadow
|
return shadow
|
||||||
|
|
|
@ -19,7 +19,7 @@ type Codec struct {
|
||||||
LoadOptions LoadOptions
|
LoadOptions LoadOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Codec) Encode(v map[string]any) ([]byte, error) {
|
func (c *Codec) Encode(v map[string]any) ([]byte, error) {
|
||||||
cfg := ini.Empty()
|
cfg := ini.Empty()
|
||||||
ini.PrettyFormat = false
|
ini.PrettyFormat = false
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ func (c Codec) Encode(v map[string]any) ([]byte, error) {
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Codec) Decode(b []byte, v map[string]any) error {
|
func (c *Codec) Decode(b []byte, v map[string]any) error {
|
||||||
cfg := ini.Empty(c.LoadOptions)
|
cfg := ini.Empty(c.LoadOptions)
|
||||||
|
|
||||||
err := cfg.Append(b)
|
err := cfg.Append(b)
|
||||||
|
@ -90,7 +90,7 @@ func (c Codec) Decode(b []byte, v map[string]any) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Codec) keyDelimiter() string {
|
func (c *Codec) keyDelimiter() string {
|
||||||
if c.KeyDelimiter == "" {
|
if c.KeyDelimiter == "" {
|
||||||
return "."
|
return "."
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ func deepSearch(m map[string]any, path []string) map[string]any {
|
||||||
// flattenAndMergeMap recursively flattens the given map into a new map
|
// flattenAndMergeMap recursively flattens the given map into a new map
|
||||||
// Code is based on the function with the same name in the main package.
|
// Code is based on the function with the same name in the main package.
|
||||||
// TODO: move it to a common place.
|
// TODO: move it to a common place.
|
||||||
func flattenAndMergeMap(shadow map[string]any, m map[string]any, prefix string, delimiter string) map[string]any {
|
func flattenAndMergeMap(shadow, m map[string]any, prefix, delimiter string) map[string]any {
|
||||||
if shadow != nil && prefix != "" && shadow[prefix] != nil {
|
if shadow != nil && prefix != "" && shadow[prefix] != nil {
|
||||||
// prefix is shadowed => nothing more to flatten
|
// prefix is shadowed => nothing more to flatten
|
||||||
return shadow
|
return shadow
|
||||||
|
|
|
@ -42,7 +42,7 @@ func deepSearch(m map[string]any, path []string) map[string]any {
|
||||||
// flattenAndMergeMap recursively flattens the given map into a new map
|
// flattenAndMergeMap recursively flattens the given map into a new map
|
||||||
// Code is based on the function with the same name in the main package.
|
// Code is based on the function with the same name in the main package.
|
||||||
// TODO: move it to a common place.
|
// TODO: move it to a common place.
|
||||||
func flattenAndMergeMap(shadow map[string]any, m map[string]any, prefix string, delimiter string) map[string]any {
|
func flattenAndMergeMap(shadow, m map[string]any, prefix, delimiter string) map[string]any {
|
||||||
if shadow != nil && prefix != "" && shadow[prefix] != nil {
|
if shadow != nil && prefix != "" && shadow[prefix] != nil {
|
||||||
// prefix is shadowed => nothing more to flatten
|
// prefix is shadowed => nothing more to flatten
|
||||||
return shadow
|
return shadow
|
||||||
|
|
|
@ -55,6 +55,7 @@ func (n *discardHandler) Enabled(_ context.Context, _ slog.Level) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//nolint:gocritic // hugeParam: _ is heavy (288 bytes); consider passing it by pointer
|
||||||
func (n *discardHandler) Handle(_ context.Context, _ slog.Record) error {
|
func (n *discardHandler) Handle(_ context.Context, _ slog.Record) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,7 +97,7 @@ func overrideFromLayer(l layer, assert *assert.Assertions, firstPath string, fir
|
||||||
v := New()
|
v := New()
|
||||||
firstKeys := strings.Split(firstPath, v.keyDelim)
|
firstKeys := strings.Split(firstPath, v.keyDelim)
|
||||||
if assert == nil ||
|
if assert == nil ||
|
||||||
len(firstKeys) == 0 || len(firstKeys[0]) == 0 {
|
len(firstKeys) == 0 || firstKeys[0] == "" {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ func overrideFromLayer(l layer, assert *assert.Assertions, firstPath string, fir
|
||||||
|
|
||||||
// Override and check new value
|
// Override and check new value
|
||||||
secondKeys := strings.Split(secondPath, v.keyDelim)
|
secondKeys := strings.Split(secondPath, v.keyDelim)
|
||||||
if len(secondKeys) == 0 || len(secondKeys[0]) == 0 {
|
if len(secondKeys) == 0 || secondKeys[0] == "" {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
v.Set(secondPath, secondValue)
|
v.Set(secondPath, secondValue)
|
||||||
|
@ -129,7 +129,7 @@ func overrideFromLayer(l layer, assert *assert.Assertions, firstPath string, fir
|
||||||
// configuration map of the given layer, and that the final value equals the one given.
|
// configuration map of the given layer, and that the final value equals the one given.
|
||||||
func deepCheckValue(assert *assert.Assertions, v *Viper, l layer, keys []string, value any) {
|
func deepCheckValue(assert *assert.Assertions, v *Viper, l layer, keys []string, value any) {
|
||||||
if assert == nil || v == nil ||
|
if assert == nil || v == nil ||
|
||||||
len(keys) == 0 || len(keys[0]) == 0 {
|
len(keys) == 0 || keys[0] == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ func (rc remoteConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error)
|
||||||
return bytes.NewReader(resp), nil
|
return bytes.NewReader(resp), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
|
func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (responseCh <-chan *viper.RemoteResponse, quitCh chan bool) {
|
||||||
cm, err := getConfigManager(rp)
|
cm, err := getConfigManager(rp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
17
viper.go
17
viper.go
|
@ -345,7 +345,7 @@ func (v *Viper) resetEncoding() {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
codec := ini.Codec{
|
codec := &ini.Codec{
|
||||||
KeyDelimiter: v.keyDelim,
|
KeyDelimiter: v.keyDelim,
|
||||||
LoadOptions: v.iniLoadOptions,
|
LoadOptions: v.iniLoadOptions,
|
||||||
}
|
}
|
||||||
|
@ -957,7 +957,8 @@ func (v *Viper) Sub(key string) *Viper {
|
||||||
}
|
}
|
||||||
|
|
||||||
if reflect.TypeOf(data).Kind() == reflect.Map {
|
if reflect.TypeOf(data).Kind() == reflect.Map {
|
||||||
subv.parents = append(v.parents, strings.ToLower(key))
|
subv.parents = append([]string(nil), v.parents...)
|
||||||
|
subv.parents = append(subv.parents, strings.ToLower(key))
|
||||||
subv.automaticEnvApplied = v.automaticEnvApplied
|
subv.automaticEnvApplied = v.automaticEnvApplied
|
||||||
subv.envPrefix = v.envPrefix
|
subv.envPrefix = v.envPrefix
|
||||||
subv.envKeyReplacer = v.envKeyReplacer
|
subv.envKeyReplacer = v.envKeyReplacer
|
||||||
|
@ -1434,7 +1435,7 @@ func readAsCSV(val string) ([]string, error) {
|
||||||
func stringToStringConv(val string) any {
|
func stringToStringConv(val string) any {
|
||||||
val = strings.Trim(val, "[]")
|
val = strings.Trim(val, "[]")
|
||||||
// An empty string would cause an empty map
|
// An empty string would cause an empty map
|
||||||
if len(val) == 0 {
|
if val == "" {
|
||||||
return map[string]any{}
|
return map[string]any{}
|
||||||
}
|
}
|
||||||
r := csv.NewReader(strings.NewReader(val))
|
r := csv.NewReader(strings.NewReader(val))
|
||||||
|
@ -1458,7 +1459,7 @@ func stringToStringConv(val string) any {
|
||||||
func stringToIntConv(val string) any {
|
func stringToIntConv(val string) any {
|
||||||
val = strings.Trim(val, "[]")
|
val = strings.Trim(val, "[]")
|
||||||
// An empty string would cause an empty map
|
// An empty string would cause an empty map
|
||||||
if len(val) == 0 {
|
if val == "" {
|
||||||
return map[string]any{}
|
return map[string]any{}
|
||||||
}
|
}
|
||||||
ss := strings.Split(val, ",")
|
ss := strings.Split(val, ",")
|
||||||
|
@ -1506,13 +1507,13 @@ func (v *Viper) SetEnvKeyReplacer(r *strings.Replacer) {
|
||||||
|
|
||||||
// RegisterAlias creates an alias that provides another accessor for the same key.
|
// RegisterAlias creates an alias that provides another accessor for the same key.
|
||||||
// This enables one to change a name without breaking the application.
|
// This enables one to change a name without breaking the application.
|
||||||
func RegisterAlias(alias string, key string) { v.RegisterAlias(alias, key) }
|
func RegisterAlias(alias, key string) { v.RegisterAlias(alias, key) }
|
||||||
|
|
||||||
func (v *Viper) RegisterAlias(alias string, key string) {
|
func (v *Viper) RegisterAlias(alias, key string) {
|
||||||
v.registerAlias(alias, strings.ToLower(key))
|
v.registerAlias(alias, strings.ToLower(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Viper) registerAlias(alias string, key string) {
|
func (v *Viper) registerAlias(alias, key string) {
|
||||||
alias = strings.ToLower(alias)
|
alias = strings.ToLower(alias)
|
||||||
if alias != key && alias != v.realKey(key) {
|
if alias != key && alias != v.realKey(key) {
|
||||||
_, exists := v.aliases[alias]
|
_, exists := v.aliases[alias]
|
||||||
|
@ -2181,6 +2182,8 @@ func (v *Viper) SetConfigPermissions(perm os.FileMode) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// IniLoadOptions sets the load options for ini parsing.
|
// IniLoadOptions sets the load options for ini parsing.
|
||||||
|
//
|
||||||
|
//nolint:gocritic // hugeParam: in is heavy (114 bytes); consider passing it by pointer
|
||||||
func IniLoadOptions(in ini.LoadOptions) Option {
|
func IniLoadOptions(in ini.LoadOptions) Option {
|
||||||
return optionFunc(func(v *Viper) {
|
return optionFunc(func(v *Viper) {
|
||||||
v.iniLoadOptions = in
|
v.iniLoadOptions = in
|
||||||
|
|
|
@ -234,17 +234,15 @@ func initIni() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// initDirs makes directories for testing.
|
// initDirs makes directories for testing.
|
||||||
func initDirs(t *testing.T) (string, string) {
|
func initDirs(t *testing.T) (root, config string) {
|
||||||
var (
|
testDirs := []string{`a a`, `b`, `C_`}
|
||||||
testDirs = []string{`a a`, `b`, `C_`}
|
config = `improbable`
|
||||||
config = `improbable`
|
|
||||||
)
|
|
||||||
|
|
||||||
if runtime.GOOS != "windows" {
|
if runtime.GOOS != "windows" {
|
||||||
testDirs = append(testDirs, `d\d`)
|
testDirs = append(testDirs, `d\d`)
|
||||||
}
|
}
|
||||||
|
|
||||||
root := t.TempDir()
|
root = t.TempDir()
|
||||||
|
|
||||||
for _, dir := range testDirs {
|
for _, dir := range testDirs {
|
||||||
innerDir := filepath.Join(root, dir)
|
innerDir := filepath.Join(root, dir)
|
||||||
|
@ -428,7 +426,7 @@ func TestReadInConfig(t *testing.T) {
|
||||||
file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
_, err = file.Write([]byte(`key: value`))
|
_, err = file.WriteString(`key: value`)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
file.Close()
|
file.Close()
|
||||||
|
@ -453,7 +451,7 @@ func TestReadInConfig(t *testing.T) {
|
||||||
file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
_, err = file.Write([]byte(`key: value`))
|
_, err = file.WriteString(`key: value`)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
file.Close()
|
file.Close()
|
||||||
|
@ -936,7 +934,8 @@ func TestUnmarshalWithDecoderOptions(t *testing.T) {
|
||||||
if raw == "" {
|
if raw == "" {
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
return m, json.Unmarshal([]byte(raw), &m)
|
err := json.Unmarshal([]byte(raw), &m)
|
||||||
|
return m, err
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
|
|
||||||
|
@ -2343,12 +2342,12 @@ func doTestCaseInsensitive(t *testing.T, typ, config string) {
|
||||||
assert.Equal(t, 5, cast.ToInt(Get("ef.lm.p.q")))
|
assert.Equal(t, 5, cast.ToInt(Get("ef.lm.p.q")))
|
||||||
}
|
}
|
||||||
|
|
||||||
func newViperWithConfigFile(t *testing.T) (*Viper, string) {
|
func newViperWithConfigFile(t *testing.T) (v *Viper, configFile string) {
|
||||||
watchDir := t.TempDir()
|
watchDir := t.TempDir()
|
||||||
configFile := path.Join(watchDir, "config.yaml")
|
configFile = path.Join(watchDir, "config.yaml")
|
||||||
err := os.WriteFile(configFile, []byte("foo: bar\n"), 0o640)
|
err := os.WriteFile(configFile, []byte("foo: bar\n"), 0o640)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
v := New()
|
v = New()
|
||||||
v.SetConfigFile(configFile)
|
v.SetConfigFile(configFile)
|
||||||
err = v.ReadInConfig()
|
err = v.ReadInConfig()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -2356,8 +2355,8 @@ func newViperWithConfigFile(t *testing.T) (*Viper, string) {
|
||||||
return v, configFile
|
return v, configFile
|
||||||
}
|
}
|
||||||
|
|
||||||
func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
|
func newViperWithSymlinkedConfigFile(t *testing.T) (v *Viper, watchDir, configFile string) {
|
||||||
watchDir := t.TempDir()
|
watchDir = t.TempDir()
|
||||||
dataDir1 := path.Join(watchDir, "data1")
|
dataDir1 := path.Join(watchDir, "data1")
|
||||||
err := os.Mkdir(dataDir1, 0o777)
|
err := os.Mkdir(dataDir1, 0o777)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -2368,11 +2367,11 @@ func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
|
||||||
// now, symlink the tm `data1` dir to `data` in the baseDir
|
// now, symlink the tm `data1` dir to `data` in the baseDir
|
||||||
os.Symlink(dataDir1, path.Join(watchDir, "data"))
|
os.Symlink(dataDir1, path.Join(watchDir, "data"))
|
||||||
// and link the `<watchdir>/datadir1/config.yaml` to `<watchdir>/config.yaml`
|
// and link the `<watchdir>/datadir1/config.yaml` to `<watchdir>/config.yaml`
|
||||||
configFile := path.Join(watchDir, "config.yaml")
|
configFile = path.Join(watchDir, "config.yaml")
|
||||||
os.Symlink(path.Join(watchDir, "data", "config.yaml"), configFile)
|
os.Symlink(path.Join(watchDir, "data", "config.yaml"), configFile)
|
||||||
t.Logf("Config file location: %s\n", path.Join(watchDir, "config.yaml"))
|
t.Logf("Config file location: %s\n", path.Join(watchDir, "config.yaml"))
|
||||||
// init Viper
|
// init Viper
|
||||||
v := New()
|
v = New()
|
||||||
v.SetConfigFile(configFile)
|
v.SetConfigFile(configFile)
|
||||||
err = v.ReadInConfig()
|
err = v.ReadInConfig()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -2612,6 +2611,8 @@ func BenchmarkGetBoolFromMap(b *testing.B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip some tests on Windows that kept failing when Windows was added to the CI as a target.
|
// Skip some tests on Windows that kept failing when Windows was added to the CI as a target.
|
||||||
|
//
|
||||||
|
//nolint:gocritic // sloppyTestFuncName
|
||||||
func skipWindows(t *testing.T) {
|
func skipWindows(t *testing.T) {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
t.Skip("Skip test on Windows")
|
t.Skip("Skip test on Windows")
|
||||||
|
|
Loading…
Reference in a new issue