Open
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.
- I have checked the troubleshooting guide for my problem, without success.
Viper Version
1.16.0
Go Version
1.19.4
Config Source
Environment variables, Files
Format
YAML
Repl.it link
No response
Code reproducing the issue
package main
import (
"fmt"
"log"
"os"
"github.com/spf13/viper"
)
type Config struct {
WorkerCount int `mapstructure:"workercount"`
Database DatabaseConfig `mapstructure:"database"`
}
type DatabaseConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Database string `mapstructure:"database"`
}
func main() {
os.Setenv("WORKERCOUNT", "7")
os.Setenv("DATABASE_PASSWORD", "MyPassword")
viper.SetConfigName("config")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("reading config: %w", err))
}
viper.AutomaticEnv()
log.Println("workercount:", viper.Get("workercount")) // expecting 7, got 7
subViper := viper.Sub("database")
subViper.SetEnvPrefix("database")
subViper.AutomaticEnv()
log.Println("database_password:", subViper.Get("password")) // expecting "MyPassword", got "1234"
}
Expected Behavior
The value of the password of the database is supposed to be taken from the environment variable, set at the beginning of the main()
. I should have MyPassword
.
Actual Behavior
I have the value from the configuration file and not the one from the environment variable: admin
Steps To Reproduce
- I used this
config.yaml
:
workercount: 4
database:
host: localhost
port: 3306
username: admin
password: admin
database: 1234
- Simply executed the code above in v1.16.0.
Additional Information
It works with viper:v1.15.0
, but it doesn't with viper:v1.16.0
.
Activity