forked from TrueCloudLab/frostfs-node
[#493] node/config: Change Sub implementation
In some cases viper doesn't interpret `section.value` as a subsection with `section` name, but value is value still can be accessed through full pathname. Fix `Config.Sub` method implementation in order to always interpret configuration like described above as a subsection. From now method never returns nil, therefore an additional check has been removed from the `Value` method. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
e5c014bbfb
commit
1aa88159ca
3 changed files with 17 additions and 15 deletions
|
@ -1,14 +1,17 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/spf13/viper"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Sub returns sub-section of the Config by name.
|
// Sub returns subsection of the Config by name.
|
||||||
|
//
|
||||||
|
// Returns nil if subsection if missing.
|
||||||
func (x *Config) Sub(name string) *Config {
|
func (x *Config) Sub(name string) *Config {
|
||||||
return (*Config)(
|
return &Config{
|
||||||
(*viper.Viper)(x).Sub(name),
|
v: x.v,
|
||||||
)
|
path: append(x.path, name),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value returns configuration value by name.
|
// Value returns configuration value by name.
|
||||||
|
@ -20,9 +23,5 @@ func (x *Config) Sub(name string) *Config {
|
||||||
//
|
//
|
||||||
// Returns nil if config is nil.
|
// Returns nil if config is nil.
|
||||||
func (x *Config) Value(name string) interface{} {
|
func (x *Config) Value(name string) interface{} {
|
||||||
if x != nil {
|
return x.v.Get(strings.Join(append(x.path, name), separator))
|
||||||
return (*viper.Viper)(x).Get(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,9 +21,6 @@ func TestConfigCommon(t *testing.T) {
|
||||||
|
|
||||||
const nonExistentSub = "non-existent sub-section"
|
const nonExistentSub = "non-existent sub-section"
|
||||||
|
|
||||||
sub = c.Sub(nonExistentSub)
|
|
||||||
require.Nil(t, sub)
|
|
||||||
|
|
||||||
val = c.Sub(nonExistentSub).Value("value")
|
val = c.Sub(nonExistentSub).Value("value")
|
||||||
require.Nil(t, val)
|
require.Nil(t, val)
|
||||||
})
|
})
|
||||||
|
|
|
@ -13,7 +13,11 @@ import (
|
||||||
// Sub-trees are named configuration sub-sections,
|
// Sub-trees are named configuration sub-sections,
|
||||||
// leaves are named configuration values.
|
// leaves are named configuration values.
|
||||||
// Names are of string type.
|
// Names are of string type.
|
||||||
type Config viper.Viper
|
type Config struct {
|
||||||
|
v *viper.Viper
|
||||||
|
|
||||||
|
path []string
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
separator = "."
|
separator = "."
|
||||||
|
@ -49,5 +53,7 @@ func New(_ Prm, opts ...Option) *Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (*Config)(v)
|
return &Config{
|
||||||
|
v: v,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue