Over the weekend, I added some options I wanted for a challenge I’m utilizing dotconfig on. Required fields, optionally available fields, and defaults:
sort NewFeaturesDemo struct {
// New required choice in struct tag will drive
// non-zero values.
APIVersion float64 `env:"API_VERSION,required"`
// New optionally available choice in struct tag is not going to throw
// error if key's lacking. Like `encoding/json` will
// simply default to zero worth if lacking.
IsDev bool `env:"IS_DEV,optionally available"`
// New default tag will set a default worth if the
// ENV variable is unset.
Greeting string `env:"GREETING" default:"Hello!"`
}
I additionally obtained check protection to 100% and cleaned up the repo. I launched v1 (thanks for the nudge @Karl) and dedicated to not making any breaking modifications from right here on:
I believe “required” is so far as I need to go into validation for this bundle. Larger-level validation ought to most likely be dealt with in your config init. Right here’s a real-world instance the place I’m validating a JWT signing secret:
func (c *Config) Validate() error {
information, err := base64.StdEncoding.DecodeString(c.jwtSigningSecretInBase64)
if err != nil {
return errors.New("could not base64 decode your JWT signing secret. Wish to generate one rapidly? go run github.com/DeanPDX/jwt-secret@newest and choose HS384. Paste the worth into .env")
}
c.JWTSigningSecret = information
return nil
}
Anyway, ship me a pull request or difficulty when you so need! Hopefully any individual else finds this handy.