Skip to content
Open
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 viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ func DecodeHook(hook mapstructure.DecodeHookFunc) DecoderConfigOption {
}
}

// DecodeSquash returns a DecoderConfigOption that configures whether
// embedded/anonymous fields are squashed into the parent structure during decoding.
func DecodeSquash(squash bool) DecoderConfigOption {
return func(c *mapstructure.DecoderConfig) {
c.Squash = squash
}
}

// Viper is a prioritized configuration registry. It
// maintains a set of configuration sources, fetches
// values to populate those, and provides them according
Expand Down
17 changes: 17 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,23 @@ func TestUnmarshalWithDecoderOptions(t *testing.T) {
}, &C)
}

func TestUnmarshalEmbeddedUnexportedStruct(t *testing.T) {
v := New()
v.Set("field", "value")

type unexportedConfig struct {
Field string
}

type config struct {
unexportedConfig
}

var cfg config
require.NoError(t, v.Unmarshal(&cfg, DecodeSquash(true)))
assert.Equal(t, "value", cfg.Field)
}

func TestUnmarshalWithAutomaticEnv(t *testing.T) {
t.Setenv("PORT", "1313")
t.Setenv("NAME", "Steve")
Expand Down