-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Closed
Labels
kind/enhancementNew feature or requestNew feature or requestresolution/duplicateThis issue or pull request already existsThis issue or pull request already exists
Description
Preflight Checklist
- I have searched the issue tracker for an issue that matches the one I want to file, without success.
- I am not looking for support or already pursued the available support channels without success.
Viper Version
1.9.0
Go Version
1.16
Config Source
Environment variables, Files
Format
YAML
Repl.it link
No response
Code reproducing the issue
type Redis struct {
Addrs string `mapstructure:"addrs"`
PoolSize int `mapstructure:"poolSize"`
}
var (
cfgFile string
cfg Redis
)
// the config file as below
/**
{
"addrs": "127.0.0.1:6379"
// NOTE: the config file does not contains the key for poolSize, poolSize is injected from env
}
*/
/**
env is POOLSIZE=100
*/
func main() {
viper.SetConfigFile(cfgFile)
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
if err := viper.ReadInConfig(); err != nil {
return fmt.Errorf("failed to load config file %s: %v", cfgFile, err)
}
if err := viper.Unmarshal(&cfg); err != nil {
return fmt.Errorf("failed to marshal config file %s: %v", cfgFile, err)
}
}
Expected Behavior
cfg.PoolSize == 100
Actual Behavior
cfg.PoolSize == 0
Steps To Reproduce
- with config file, has no a key for poolSize
- the poolSize is injected with environment
- call viperh.Unmarshal
NOTE: only when the poolSize is defined in config file, the env override works
Additional Information
No response
Metadata
Metadata
Assignees
Labels
kind/enhancementNew feature or requestNew feature or requestresolution/duplicateThis issue or pull request already existsThis issue or pull request already exists