-
Notifications
You must be signed in to change notification settings - Fork 2k
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 using ENV without binding #688
Comments
I was able to work around this limitation by first marshaling my structs to a format viper could read and then using that data as a config. Note the package app
import (
"bytes"
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"
)
type APIConfig struct {
RootURL string `yaml:"rootUrl" mapstructure:"rootUrl"`
}
type HostPortConfig struct {
Host string `yaml:"host" mapstructure:"host"`
Port int `yaml:"port" mapstructure:"port"`
}
type AppConfig struct {
MyApi *APIConfig `yaml:"myapi" mapstructure:"myapi"`
SrvAddr *HostPortConfig `yaml:"srvaddr" mapstructure:"srvaddr"`
}
var Config *AppConfig
func init() {
Config = new(AppConfig)
Config.New() // Initializes with some default values (definition not shown)
// Marshal the struct into a format viper can read
bs, err := yaml.Marshal(Config)
if err != nil {
panic("unable to marshal config to YAML")
}
// No bindings here!
viper.AutomaticEnv()
// Hydrate viper with fields
viper.SetConfigType("yaml")
viper.ReadConfig(bytes.NewBuffer(bs))
// Now this call will take into account the env as well
viper.Unmarshal(Config)
} |
I guess viper was created to be independent from format, in your case it is .yaml. So you can't use .toml for example without changing of code. But thanks for comment anyway. |
Please see and follow the issue above. ☝️ |
2 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi all,
Having this code:
and running in this way:
I got config with empty
ServerAddr
. If I remove comment from binding line it works but why viper couldn't get it frommapstructure
tag itself?Please let me know if I doing something wrong.
The text was updated successfully, but these errors were encountered: