Over the weekend, I added some features I needed for a project I’m using dotconfig on. Required fields, optional fields, and defaults:
type NewFeaturesDemo struct {
// New required option in struct tag will force
// non-zero values.
APIVersion float64 `env:"API_VERSION,required"`
// New optional option in struct tag will not throw
// error if key is missing. Like `encoding/json` will
// just default to zero value if missing.
IsDev bool `env:"IS_DEV,optional"`
// New default tag will set a default value if the
// ENV variable is unset.
Greeting string `env:"GREETING" default:"Hi!"`
}
I also got test coverage to 100% and cleaned up the repo. I released v1 (thanks for the nudge @Karl) and committed to not making any breaking changes from here on:
I think “required” is as far as I want to go into validation for this package. Higher-level validation should probably be handled in your config init. Here’s a real-world example where I am validating a JWT signing secret:
func (c *Config) Validate() error {
data, err := base64.StdEncoding.DecodeString(c.jwtSigningSecretInBase64)
if err != nil {
return errors.New("couldn't base64 decode your JWT signing secret. Want to generate one quickly? go run github.com/DeanPDX/jwt-secret@latest and select HS384. Paste the value into .env")
}
c.JWTSigningSecret = data
return nil
}
Anyway, send me a pull request or issue if you so desire! Hopefully somebody else finds this useful.