Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental flag for bind struct #1854

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions experimental.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package viper

// ExperimentalBindStruct tells Viper to use the new bind struct feature.
func ExperimentalBindStruct() Option {
return optionFunc(func(v *Viper) {
v.experimentalBindStruct = true
})
}
8 changes: 5 additions & 3 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ type Viper struct {
encoderRegistry *encoding.EncoderRegistry
decoderRegistry *encoding.DecoderRegistry

experimentalFinder bool
experimentalFinder bool
experimentalBindStruct bool
}

// New returns an initialized Viper instance.
Expand All @@ -219,6 +220,7 @@ func New() *Viper {
v.resetEncoding()

v.experimentalFinder = features.Finder
v.experimentalBindStruct = features.BindStruct

return v
}
Expand Down Expand Up @@ -985,7 +987,7 @@ func Unmarshal(rawVal any, opts ...DecoderConfigOption) error {
func (v *Viper) Unmarshal(rawVal any, opts ...DecoderConfigOption) error {
keys := v.AllKeys()

if features.BindStruct {
if v.experimentalBindStruct {
// TODO: make this optional?
structKeys, err := v.decodeStructKeys(rawVal, opts...)
if err != nil {
Expand Down Expand Up @@ -1079,7 +1081,7 @@ func (v *Viper) UnmarshalExact(rawVal any, opts ...DecoderConfigOption) error {

keys := v.AllKeys()

if features.BindStruct {
if v.experimentalBindStruct {
// TODO: make this optional?
structKeys, err := v.decodeStructKeys(rawVal, opts...)
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/spf13/viper/internal/features"
"github.com/spf13/viper/internal/testutil"
)

Expand Down Expand Up @@ -1067,10 +1066,6 @@ func TestUnmarshalWithDecoderOptions(t *testing.T) {
}

func TestUnmarshalWithAutomaticEnv(t *testing.T) {
if !features.BindStruct {
t.Skip("binding struct is not enabled")
}

t.Setenv("PORT", "1313")
t.Setenv("NAME", "Steve")
t.Setenv("DURATION", "1s1ms")
Expand Down Expand Up @@ -1104,7 +1099,7 @@ func TestUnmarshalWithAutomaticEnv(t *testing.T) {
Flag bool `mapstructure:"flag"`
}

v := New()
v := NewWithOptions(ExperimentalBindStruct())
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv()

Expand Down