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

Unmarshal() does not use the env variable if the config file has no that key #1236

Closed
2 tasks done
zzycn003 opened this issue Oct 13, 2021 · 3 comments
Closed
2 tasks done
Labels
kind/enhancement New feature or request resolution/duplicate This issue or pull request already exists

Comments

@zzycn003
Copy link

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

  1. with config file, has no a key for poolSize
  2. the poolSize is injected with environment
  3. call viperh.Unmarshal

NOTE: only when the poolSize is defined in config file, the env override works

Additional Information

No response

@zzycn003 zzycn003 added the kind/bug Something isn't working label Oct 13, 2021
@github-actions
Copy link

👋 Thanks for reporting!

A maintainer will take a look at your issue shortly. 👀

In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues.

⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9

📣 If you've already given us your feedback, you can still help by spreading the news,
either by sharing the above link or telling people about this on Twitter:

https://twitter.com/sagikazarmark/status/1306904078967074816

Thank you! ❤️

@vorlif
Copy link

vorlif commented Oct 21, 2021

[x] I have searched the issue tracker for an issue that matches the one I want to file, without success.

I have the same problem and it is similar to that in

#188 #1012 #935 #761 #688 #464

Since the issue has existed since 2016, we probably have to hope that it will be resolved in Viper v2.

@sagikazarmark sagikazarmark added kind/enhancement New feature or request resolution/duplicate This issue or pull request already exists and removed kind/bug Something isn't working labels Dec 9, 2021
@sagikazarmark
Copy link
Collaborator

Please see #761 for a detailed analysis and potential workarounds. Closing as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request resolution/duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants