Documentation ¶
Index ¶
- func Must[T any](t T, err error) T
- func Parse(v interface{}) error
- func ParseAs[T any]() (T, error)
- func ParseAsWithOptions[T any](opts Options) (T, error)
- func ParseWithOptions(v interface{}, opts Options) error
- func ToMap(env []string) map[string]string
- type AggregateError
- type EmptyEnvVarError
- type EnvVarIsNotSetError
- type FieldParams
- type LoadFileContentError
- type NoParserError
- type NoSupportedTagOptionError
- type NotStructPtrError
- type OnSetFn
- type Options
- type ParseError
- type ParseValueError
- type ParserFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
func Parse(v interface{}) error
Parse parses a struct containing `env` tags and loads its values from environment variables.
Example ¶
type inner struct { Foo string `env:"FOO" envDefault:"foobar"` } type config struct { Home string `env:"HOME,required"` Port int `env:"PORT" envDefault:"3000"` IsProduction bool `env:"PRODUCTION"` TempFolder string `env:"TEMP_FOLDER,expand" envDefault:"${HOME}/.tmp"` StringInts map[string]int `env:"MAP_STRING_INT" envDefault:"k1:1,k2:2"` Inner inner } os.Setenv("HOME", "/tmp/fakehome") var cfg config if err := Parse(&cfg); err != nil { fmt.Println("failed:", err) } fmt.Printf("%+v", cfg)
Output: {Home:/tmp/fakehome Port:3000 IsProduction:false TempFolder:/tmp/fakehome/.tmp StringInts:map[k1:1 k2:2] Inner:{Foo:foobar}}
Example (Defaults) ¶
type config struct { A string `env:"FOO" envDefault:"foo"` B string `env:"FOO"` } // env FOO is not set cfg := config{ A: "A", B: "B", } if err := Parse(&cfg); err != nil { fmt.Println("failed:", err) } fmt.Printf("%+v", cfg)
Output: {A:foo B:B}
Example (OnSet) ¶
type config struct { Home string `env:"HOME,required"` Port int `env:"PORT" envDefault:"3000"` IsProduction bool `env:"PRODUCTION"` NoEnvTag bool Inner struct{} `envPrefix:"INNER_"` } os.Setenv("HOME", "/tmp/fakehome") var cfg config if err := ParseWithOptions(&cfg, Options{ OnSet: func(tag string, value interface{}, isDefault bool) { fmt.Printf("Set %s to %v (default? %v)\n", tag, value, isDefault) }, }); err != nil { fmt.Println("failed:", err) } fmt.Printf("%+v", cfg)
Output: Set HOME to /tmp/fakehome (default? false) Set PORT to 3000 (default? true) Set PRODUCTION to (default? false) {Home:/tmp/fakehome Port:3000 IsProduction:false NoEnvTag:false Inner:{}}
func ParseAs ¶
ParseAs parses the given struct type containing `env` tags and loads its values from environment variables.
func ParseAsWithOptions ¶
ParseWithOptions parses the given struct type containing `env` tags and loads its values from environment variables.
func ParseWithOptions ¶
ParseWithOptions parses a struct containing `env` tags and loads its values from environment variables.
Example ¶
type thing struct { desc string } type conf struct { Thing thing `env:"THING"` } os.Setenv("THING", "my thing") c := conf{} err := ParseWithOptions(&c, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(thing{}): func(v string) (interface{}, error) { return thing{desc: v}, nil }, }}) if err != nil { fmt.Println(err) } fmt.Println(c.Thing.desc)
Output: my thing
Types ¶
type AggregateError ¶
type AggregateError struct {
Errors []error
}
An aggregated error wrapper to combine gathered errors. This allows either to display all errors or convert them individually List of the available errors ParseError NotStructPtrError NoParserError NoSupportedTagOptionError EnvVarIsNotSetError EmptyEnvVarError LoadFileContentError ParseValueError
func (AggregateError) Error ¶
func (e AggregateError) Error() string
type EmptyEnvVarError ¶
type EmptyEnvVarError struct {
Key string
}
This error occurs when the variable which must be not empty is existing but has an empty value Read about not empty fields: https://github.com/0xc000022070/env#not-empty-fields
func (EmptyEnvVarError) Error ¶
func (e EmptyEnvVarError) Error() string
type EnvVarIsNotSetError ¶
type EnvVarIsNotSetError struct {
Key string
}
This error occurs when the required variable is not set Read about required fields: https://github.com/0xc000022070/env#required-fields
func (EnvVarIsNotSetError) Error ¶
func (e EnvVarIsNotSetError) Error() string
type FieldParams ¶
type FieldParams struct { OwnKey string Key string DefaultValue string HasDefaultValue bool Required bool LoadFile bool Unset bool NotEmpty bool Expand bool }
FieldParams contains information about parsed field tags.
func GetFieldParams ¶
func GetFieldParams(v interface{}) ([]FieldParams, error)
GetFieldParams parses a struct containing `env` tags and returns information about tags it found.
func GetFieldParamsWithOptions ¶
func GetFieldParamsWithOptions(v interface{}, opts Options) ([]FieldParams, error)
GetFieldParamsWithOptions parses a struct containing `env` tags and returns information about tags it found.
type LoadFileContentError ¶
This error occurs when it's impossible to load the value from the file Read about From file feature: https://github.com/0xc000022070/env#from-file
func (LoadFileContentError) Error ¶
func (e LoadFileContentError) Error() string
type NoParserError ¶
This error occurs when there is no parser provided for given type Supported types and defaults: https://github.com/0xc000022070/env#supported-types-and-defaults How to create a custom parser: https://github.com/0xc000022070/env#custom-parser-funcs
func (NoParserError) Error ¶
func (e NoParserError) Error() string
type NoSupportedTagOptionError ¶
type NoSupportedTagOptionError struct {
Tag string
}
This error occurs when the given tag is not supported In-built supported tags: "", "file", "required", "unset", "notEmpty", "expand", "envDefault", "envSeparator" How to create a custom tag: https://github.com/0xc000022070/env#changing-default-tag-name
func (NoSupportedTagOptionError) Error ¶
func (e NoSupportedTagOptionError) Error() string
type NotStructPtrError ¶
type NotStructPtrError struct{}
The error occurs when pass something that is not a pointer to a Struct to Parse
func (NotStructPtrError) Error ¶
func (e NotStructPtrError) Error() string
type Options ¶
type Options struct { // Environment keys and values that will be accessible for the service. Environment map[string]string // TagName specifies another tagname to use rather than the default env. TagName string // RequiredIfNoDef automatically sets all env as required if they do not // declare 'envDefault'. RequiredIfNoDef bool // OnSet allows to run a function when a value is set. OnSet OnSetFn // Prefix define a prefix for each key. Prefix string // UseFieldNameByDefault defines whether or not env should use the field // name by default if the `env` key is missing. // Note that the field name will be "converted" to conform with environment // variable names conventions. UseFieldNameByDefault bool // Custom parse functions for different types. FuncMap map[reflect.Type]ParserFunc // contains filtered or unexported fields }
Options for the parser.
type ParseError ¶
The error occurs when it's impossible to convert the value for given type.
func (ParseError) Error ¶
func (e ParseError) Error() string
type ParseValueError ¶
This error occurs when it's impossible to convert value using given parser Supported types and defaults: https://github.com/0xc000022070/env#supported-types-and-defaults How to create a custom parser: https://github.com/0xc000022070/env#custom-parser-funcs
func (ParseValueError) Error ¶
func (e ParseValueError) Error() string
type ParserFunc ¶
ParserFunc defines the signature of a function that can be used within `CustomParsers`.