Documentation
¶
Index ¶
- Constants
- func Init[ConfigStruct any]() (c ConfigStruct, err error)
- func InitGlobal[ConfigStruct any]() ConfigStruct
- func Load[T any](ptr *T) error
- func Lookup[T any](key string, opts ...LookupOption) (T, bool, error)
- func LookupFieldEnvNames(sf reflect.StructField) ([]string, bool)
- func ReflectTryLoad(ptr reflect.Value) error
- func SetLookup[T any](set *Set, ptr *T, key string, opts ...LookupOption)
- type LookupOption
- type ParserFunc
- type Set
Examples ¶
Constants ¶
const ( ErrInvalidType errorkit.Error = "ErrInvalidType" ErrInvalidValue errorkit.Error = "ErrInvalidValue" )
const ErrMissingEnvironmentVariable errorkit.Error = "ErrMissingEnvironmentVariable"
Variables ¶
This section is empty.
Functions ¶
func Init ¶ added in v0.271.0
Init is a syntax sugar for Load[T](&v). It can be easily used for global variable initialisation
Example ¶
package main import ( "go.llib.dev/frameless/pkg/env" ) func main() { type MyConfig struct { MyRequirement bool `env:"ENV_KEY_1" required:"true"` } var conf, err = env.Init[MyConfig]() _, _ = conf, err }
func InitGlobal ¶ added in v0.271.0
func InitGlobal[ConfigStruct any]() ConfigStruct
InitGlobal is designed to be used as part of your global variable initialisation.
Example ¶
package main import ( "go.llib.dev/frameless/pkg/env" ) func main() { type MyConfig struct { MyRequirement bool `env:"ENV_KEY_1" required:"true"` } var conf = env.InitGlobal[MyConfig]() _ = conf }
func Load ¶
Example ¶
package main import ( "time" "go.llib.dev/frameless/pkg/env" ) func main() { type ExampleAppConfig struct { Foo string `env:"FOO"` Bar time.Duration `env:"BAR" default:"1h5m"` Baz int `env:"BAZ" required:"true"` } var c ExampleAppConfig if err := env.Load(&c); err != nil { _ = err } }
Example (Enum) ¶
package main import ( "go.llib.dev/frameless/pkg/env" ) func main() { type ExampleAppConfig struct { Foo string `env:"FOO" enum:"foo;bar;baz;" default:"foo"` } var c ExampleAppConfig if err := env.Load(&c); err != nil { _ = err } }
Example (WithDefaultValue) ¶
package main import () func main() { type ExampleAppConfig struct { Foo string `env:"FOO" default:"foo"` } }
Example (WithEnvKeyBackwardCompatibility) ¶
package main import ( "go.llib.dev/frameless/pkg/env" ) func main() { type ExampleAppConfig struct { // Foo supports both FOO env key and also OLDFOO env key Foo string `env:"FOO,OLDFOO"` } var c ExampleAppConfig if err := env.Load(&c); err != nil { _ = err } }
Example (WithTimeLayout) ¶
package main import ( "time" ) func main() { type ExampleAppConfig struct { Foo time.Time `env:"FOO" layout:"2006-01-02"` } }
func Lookup ¶
func Lookup[T any](key string, opts ...LookupOption) (T, bool, error)
Example ¶
package main import ( "go.llib.dev/frameless/pkg/env" ) func main() { val, ok, err := env.Lookup[string]("FOO", env.DefaultValue("foo")) _, _, _ = val, ok, err }
func LookupFieldEnvNames ¶ added in v0.278.0
func LookupFieldEnvNames(sf reflect.StructField) ([]string, bool)
func ReflectTryLoad ¶ added in v0.278.0
ReflectTryLoad attempts to load configuration struct, but does not produce an error if the overall config is not valid. Instead, it raises a validation issue only when a given struct field's value is present in the environment variables but does not meet the validation requirements for the specified field type/tag.
func SetLookup ¶ added in v0.193.0
func SetLookup[T any](set *Set, ptr *T, key string, opts ...LookupOption)
SetLookup function registers a Lookup within a specified Set. When 'Set.Parse' is invoked, all the registered lookups will be executed. Unlike the 'Lookup' function, 'SetLookup' doesn't permit missing environment variables without a fallback value and will raise it as an issue.
Types ¶
type LookupOption ¶
type LookupOption interface {
// contains filtered or unexported methods
}
func DefaultValue ¶
func DefaultValue(val string) LookupOption
func ListSeparator ¶
func ListSeparator[SEP rune | string](sep SEP) LookupOption
func ParseWith ¶ added in v0.194.0
func ParseWith[T any](parser ParserFunc[T]) LookupOption
Example ¶
package main import ( "fmt" "strings" "go.llib.dev/frameless/pkg/env" ) func main() { // export FOO=foo:baz type Conf struct { Foo string Bar string } parserFunc := func(v string) (Conf, error) { parts := strings.SplitN(v, ":", 1) if len(parts) != 2 { return Conf{}, fmt.Errorf("invalid format") } return Conf{ Foo: parts[0], Bar: parts[1], }, nil } conf, ok, err := env.Lookup[Conf]("FOO", env.ParseWith(parserFunc)) _, _, _ = conf, ok, err }
func Required ¶
func Required() LookupOption
func TimeLayout ¶
func TimeLayout(layout string) LookupOption
type ParserFunc ¶ added in v0.194.0
type Set ¶ added in v0.193.0
type Set struct {
// contains filtered or unexported fields
}
Example ¶
package main import ( "go.llib.dev/frameless/pkg/env" ) func main() { var ( A bool B int C string D []string ) es := &env.Set{} env.SetLookup(es, &A, "A") env.SetLookup(es, &B, "B") env.SetLookup(es, &C, "C", env.DefaultValue("c-default-value")) env.SetLookup(es, &D, "D", env.ListSeparator(",")) if err := es.Parse(); err != nil { panic(err) } }