mirror of
https://github.com/spf13/viper
synced 2025-04-27 07:47:17 +00:00
Compare commits
3 commits
master
...
remote/v1.
Author | SHA1 | Date | |
---|---|---|---|
|
8bf8e1d6c5 | ||
|
9568cfcfd6 | ||
|
fd05140cd6 |
4 changed files with 43 additions and 19 deletions
|
@ -6,7 +6,7 @@ replace github.com/spf13/viper => ../
|
|||
|
||||
require (
|
||||
github.com/sagikazarmark/crypt v0.26.0
|
||||
github.com/spf13/viper v1.20.0-alpha.6
|
||||
github.com/spf13/viper v1.20.1
|
||||
)
|
||||
|
||||
require (
|
||||
|
@ -56,8 +56,8 @@ require (
|
|||
github.com/sagikazarmark/locafero v0.7.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||
github.com/spf13/afero v1.12.0 // indirect
|
||||
github.com/spf13/cast v1.7.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/spf13/cast v1.7.1 // indirect
|
||||
github.com/spf13/pflag v1.0.6 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
go.etcd.io/etcd/api/v3 v3.5.15 // indirect
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect
|
||||
|
|
|
@ -256,10 +256,10 @@ github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9yS
|
|||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||
github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
|
||||
github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4=
|
||||
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
|
||||
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
|
||||
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
||||
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
|
|
32
viper.go
32
viper.go
|
@ -1535,27 +1535,29 @@ func (v *Viper) MergeInConfig() error {
|
|||
func ReadConfig(in io.Reader) error { return v.ReadConfig(in) }
|
||||
|
||||
func (v *Viper) ReadConfig(in io.Reader) error {
|
||||
if v.configType == "" {
|
||||
return errors.New("cannot decode configuration: config type is not set")
|
||||
config := make(map[string]any)
|
||||
|
||||
err := v.unmarshalReader(in, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
v.config = make(map[string]any)
|
||||
return v.unmarshalReader(in, v.config)
|
||||
v.config = config
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MergeConfig merges a new configuration with an existing config.
|
||||
func MergeConfig(in io.Reader) error { return v.MergeConfig(in) }
|
||||
|
||||
func (v *Viper) MergeConfig(in io.Reader) error {
|
||||
if v.configType == "" {
|
||||
return errors.New("cannot decode configuration: config type is not set")
|
||||
}
|
||||
config := make(map[string]any)
|
||||
|
||||
cfg := make(map[string]any)
|
||||
if err := v.unmarshalReader(in, cfg); err != nil {
|
||||
if err := v.unmarshalReader(in, config); err != nil {
|
||||
return err
|
||||
}
|
||||
return v.MergeConfigMap(cfg)
|
||||
|
||||
return v.MergeConfigMap(config)
|
||||
}
|
||||
|
||||
// MergeConfigMap merges the configuration from the map given with an existing config.
|
||||
|
@ -1662,15 +1664,21 @@ func (v *Viper) writeConfig(filename string, force bool) error {
|
|||
}
|
||||
|
||||
func (v *Viper) unmarshalReader(in io.Reader, c map[string]any) error {
|
||||
format := strings.ToLower(v.getConfigType())
|
||||
if format == "" {
|
||||
return errors.New("cannot decode configuration: unable to determine config type")
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(in)
|
||||
|
||||
format := strings.ToLower(v.getConfigType())
|
||||
|
||||
// TODO: remove this once SupportedExts is deprecated/removed
|
||||
if !slices.Contains(SupportedExts, format) {
|
||||
return UnsupportedConfigError(format)
|
||||
}
|
||||
|
||||
// TODO: return [UnsupportedConfigError] if the registry does not contain the format
|
||||
// TODO: consider deprecating this error type
|
||||
decoder, err := v.decoderRegistry.Decoder(format)
|
||||
if err != nil {
|
||||
return ConfigParseError{err}
|
||||
|
|
|
@ -1542,6 +1542,14 @@ func TestReadConfig(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestReadConfigWithSetConfigFile(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigFile("config.yaml") // Dummy value to infer config type from file extension
|
||||
err := v.ReadConfig(bytes.NewBuffer(yamlMergeExampleSrc))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 45000, v.GetInt("hello.pop"))
|
||||
}
|
||||
|
||||
func TestIsSet(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("yaml")
|
||||
|
@ -2059,6 +2067,14 @@ func TestMergeConfig(t *testing.T) {
|
|||
assert.Equal(t, "bar", v.GetString("fu"))
|
||||
}
|
||||
|
||||
func TestMergeConfigWithSetConfigFile(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigFile("config.yaml") // Dummy value to infer config type from file extension
|
||||
err := v.MergeConfig(bytes.NewBuffer(yamlMergeExampleSrc))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 45000, v.GetInt("hello.pop"))
|
||||
}
|
||||
|
||||
func TestMergeConfigOverrideType(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("json")
|
||||
|
|
Loading…
Add table
Reference in a new issue