mirror of https://github.com/spf13/viper.git
feat: add experimental flag for bind struct
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
parent
96ad74f6ae
commit
d1e18b2551
|
@ -3,6 +3,6 @@ package viper
|
||||||
// ExperimentalBindStruct tells Viper to use the new bind struct feature.
|
// ExperimentalBindStruct tells Viper to use the new bind struct feature.
|
||||||
func ExperimentalBindStruct() Option {
|
func ExperimentalBindStruct() Option {
|
||||||
return optionFunc(func(v *Viper) {
|
return optionFunc(func(v *Viper) {
|
||||||
v.experimentalFinder = true
|
v.experimentalBindStruct = true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
6
viper.go
6
viper.go
|
@ -196,6 +196,7 @@ type Viper struct {
|
||||||
decoderRegistry *encoding.DecoderRegistry
|
decoderRegistry *encoding.DecoderRegistry
|
||||||
|
|
||||||
experimentalFinder bool
|
experimentalFinder bool
|
||||||
|
experimentalBindStruct bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns an initialized Viper instance.
|
// New returns an initialized Viper instance.
|
||||||
|
@ -219,6 +220,7 @@ func New() *Viper {
|
||||||
v.resetEncoding()
|
v.resetEncoding()
|
||||||
|
|
||||||
v.experimentalFinder = features.Finder
|
v.experimentalFinder = features.Finder
|
||||||
|
v.experimentalBindStruct = features.BindStruct
|
||||||
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
@ -1002,7 +1004,7 @@ func Unmarshal(rawVal any, opts ...DecoderConfigOption) error {
|
||||||
func (v *Viper) Unmarshal(rawVal any, opts ...DecoderConfigOption) error {
|
func (v *Viper) Unmarshal(rawVal any, opts ...DecoderConfigOption) error {
|
||||||
keys := v.AllKeys()
|
keys := v.AllKeys()
|
||||||
|
|
||||||
if features.BindStruct {
|
if v.experimentalBindStruct {
|
||||||
// TODO: make this optional?
|
// TODO: make this optional?
|
||||||
structKeys, err := v.decodeStructKeys(rawVal, opts...)
|
structKeys, err := v.decodeStructKeys(rawVal, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1096,7 +1098,7 @@ func (v *Viper) UnmarshalExact(rawVal any, opts ...DecoderConfigOption) error {
|
||||||
|
|
||||||
keys := v.AllKeys()
|
keys := v.AllKeys()
|
||||||
|
|
||||||
if features.BindStruct {
|
if v.experimentalBindStruct {
|
||||||
// TODO: make this optional?
|
// TODO: make this optional?
|
||||||
structKeys, err := v.decodeStructKeys(rawVal, opts...)
|
structKeys, err := v.decodeStructKeys(rawVal, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -29,7 +29,6 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/spf13/viper/internal/features"
|
|
||||||
"github.com/spf13/viper/internal/testutil"
|
"github.com/spf13/viper/internal/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1067,10 +1066,6 @@ func TestUnmarshalWithDecoderOptions(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshalWithAutomaticEnv(t *testing.T) {
|
func TestUnmarshalWithAutomaticEnv(t *testing.T) {
|
||||||
if !features.BindStruct {
|
|
||||||
t.Skip("binding struct is not enabled")
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Setenv("PORT", "1313")
|
t.Setenv("PORT", "1313")
|
||||||
t.Setenv("NAME", "Steve")
|
t.Setenv("NAME", "Steve")
|
||||||
t.Setenv("DURATION", "1s1ms")
|
t.Setenv("DURATION", "1s1ms")
|
||||||
|
@ -1104,7 +1099,7 @@ func TestUnmarshalWithAutomaticEnv(t *testing.T) {
|
||||||
Flag bool `mapstructure:"flag"`
|
Flag bool `mapstructure:"flag"`
|
||||||
}
|
}
|
||||||
|
|
||||||
v := New()
|
v := NewWithOptions(ExperimentalBindStruct())
|
||||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||||
v.AutomaticEnv()
|
v.AutomaticEnv()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue