Documentation ¶
Index ¶
- func Parse(v interface{}) error
- func ParseWithOptions(v interface{}, opts Options) error
- type AggregateError
- type EmptyEnvVarError
- type EnvVarIsNotSetError
- 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"` 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 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 ParseWithOptions ¶
Parse 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/caarlos0/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/caarlos0/env#required-fields
func (EnvVarIsNotSetError) Error ¶
func (e EnvVarIsNotSetError) Error() string
type LoadFileContentError ¶
This error occurs when it's impossible to load the value from the file Read about From file feature: https://github.com/caarlos0/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/caarlos0/env#supported-types-and-defaults How to create a custom parser: https://github.com/caarlos0/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/caarlos0/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. UseFieldNameByDefault bool // Custom parse functions for different types. FuncMap map[reflect.Type]ParserFunc }
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/caarlos0/env#supported-types-and-defaults How to create a custom parser: https://github.com/caarlos0/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`.