mirror of
https://github.com/spf13/viper
synced 2024-12-22 03:27:03 +00:00
Review changes
This commit is contained in:
parent
f0c4ccd6cd
commit
36a38682ba
6 changed files with 19 additions and 20 deletions
|
@ -13,10 +13,10 @@ linters-settings:
|
||||||
- diagnostic
|
- diagnostic
|
||||||
- experimental
|
- experimental
|
||||||
- opinionated
|
- opinionated
|
||||||
- performance
|
|
||||||
- style
|
- style
|
||||||
disabled-checks:
|
disabled-checks:
|
||||||
- importShadow
|
- importShadow
|
||||||
|
- unnamedResult
|
||||||
golint:
|
golint:
|
||||||
min-confidence: 0
|
min-confidence: 0
|
||||||
goimports:
|
goimports:
|
||||||
|
|
|
@ -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 "."
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,6 @@ 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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) (responseCh <-chan *viper.RemoteResponse, quitCh chan bool) {
|
func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
|
||||||
cm, err := getConfigManager(rp)
|
cm, err := getConfigManager(rp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
4
viper.go
4
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,
|
||||||
}
|
}
|
||||||
|
@ -2182,8 +2182,6 @@ 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,15 +234,17 @@ func initIni() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// initDirs makes directories for testing.
|
// initDirs makes directories for testing.
|
||||||
func initDirs(t *testing.T) (root, config string) {
|
func initDirs(t *testing.T) (string, string) {
|
||||||
testDirs := []string{`a a`, `b`, `C_`}
|
var (
|
||||||
config = `improbable`
|
testDirs = []string{`a a`, `b`, `C_`}
|
||||||
|
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)
|
||||||
|
@ -2342,12 +2344,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) (v *Viper, configFile string) {
|
func newViperWithConfigFile(t *testing.T) (*Viper, 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)
|
||||||
|
@ -2355,8 +2357,8 @@ func newViperWithConfigFile(t *testing.T) (v *Viper, configFile string) {
|
||||||
return v, configFile
|
return v, configFile
|
||||||
}
|
}
|
||||||
|
|
||||||
func newViperWithSymlinkedConfigFile(t *testing.T) (v *Viper, watchDir, configFile string) {
|
func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, 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)
|
||||||
|
@ -2367,11 +2369,11 @@ func newViperWithSymlinkedConfigFile(t *testing.T) (v *Viper, watchDir, configFi
|
||||||
// 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)
|
||||||
|
|
Loading…
Reference in a new issue