simpleenv

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2025 License: MIT Imports: 9 Imported by: 0

README

simpleenv

Maps a ENV variables to a struct, implementing several constraints using struct tags

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(envConfig any) error

Load loads environment variables into the given struct and validates the constraints specified in the struct tags

e.g. `env:"ENVIRONMENT;oneof=development,test,staging,production"`
will load the environment variable `ENVIRONMENT` and validate
that it is one of the values in the `oneof` constraint.

valid constraints:
- optional: the environment variable does not need to exist and be set
- oneof: the environment variable must be one of the values in the `oneof` constraint list (separeted by commas)
- min: the environment variable must be greater than or equal to the value in the `min` constraint
- regex: the environment variable must match the regex pattern in the `regex` constraint
- format: the environment variable must match the format in the `format` constraint (only URL is supported)

example:
	type AppEnv struct {
		Environment string `env:"ENVIRONMENT;oneof=development,test,staging,production"`
		Version     float64 `env:"VERSION;"`
		ApiURL      string  `env:"API_URL;;format=URL"`
		Concurrency int     `env:"CONCURRENCY;optional;min=1"`
	}

	appEnv := AppEnv{}
	appenv.Load(&appEnv)

	appEnv.Environment // "development"

Make sure to pass a pointer to the struct, otherwise it will panic Load will return an error if the environment variables are not set (unless marked as optional) or if the value does not match the constraints

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL