I use Viper https://github.com/spf13/viper for managing project configurations in my GO app, and also for Unmarshaling configuration values to a struct.
var config c.Configuration // Configuration is my configuration struct
err := viper.Unmarshal(&config)
When I miss some configs in .yml config file it doesn't throw any error (as I had guessed) during Unmarshaling.
So how can I force to have all configs implemented? I want to see errors if any field from struct doesn't have value in yaml.
Viper is a complete configuration solution for Go applications including 12-Factor apps. It is designed to work within an application, and can handle all types of configuration needs and formats. It supports: setting defaults. reading from JSON, TOML, YAML, HCL, envfile and Java properties config files.
Viper is a very popular Golang package for this purpose. It can find, load, and unmarshal values from a config file. It supports many types of files, such as JSON, TOML, YAML, ENV, or INI. It can also read values from environment variables or command-line flags.
Installing Viper is similar to installing any package in Go. The first step is to initialize the Go mod file. The best way to do this is to initialize the folder with git init . Next, set up the git origin using git remote add origin ORIGIN_URL , then initialize the project with go mod init .
First, we call viper.AddConfigPath () to tell Viper the location of the config file. In this case, the location is given by the input path argument. Next, we call viper.SetConfigName () to tell Viper to look for a config file with a specific name. Our config file is app.env, so its name is app.
There are many cases when we need to set up defaults for the application or load config variables from different file types, and this is where using Viper can be extremely useful. It can even live-read config variables, work with flags, and enable you to marshal and unmarshal.
There are many libraries used to manage configuration in go, this time in this story we will discuss a simple way to read yml configuration files with a viper. 2. The configuration file looks like this 3. Then install library 4. This is full code of how it’s look 5. Then run project This example only reads from the config file anyway …
Due to the way Viper works, this cannot load from *only* env vars, so the config file is still needed and the env vars can override it. See spf13/viper#584 To help protect users from this issue, this change also adds verification that essential config settings have been configured.
You can integrate validator package along with viper, so that you can check for any missing configuration. Attaching code snippet and config screenshot of my working code.
package config
import (
"github.com/go-playground/validator/v10"
"github.com/spf13/viper"
"log"
)
type Configuration struct {
Server struct {
Application string `yaml:"application" validate:"required"`
} `yaml:"server"`
}
var config Configuration
func GetConfig() *Configuration {
return &config
}
func init() {
vp := viper.New()
vp.SetConfigName("config") // name of config file (without extension)
vp.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
vp.AddConfigPath(".")
if err := vp.ReadInConfig(); err!=nil {
log.Fatalf("Read error %v", err)
}
if err := vp.Unmarshal(&config); err!=nil {
log.Fatalf("unable to unmarshall the config %v", err)
}
validate := validator.New()
if err := validate.Struct(&config); err!=nil{
log.Fatalf("Missing required attributes %v\n", err)
}
}
My property screenshot
Missing property error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With