Documentation ¶
Index ¶
- func Parse(v interface{}, opts ...Options) error
- func ParseWithFuncs(v interface{}, funcMap map[reflect.Type]ParserFunc, 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 ¶
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"` 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 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"` } os.Setenv("HOME", "/tmp/fakehome") var cfg config if err := Parse(&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}
func ParseWithFuncs ¶
func ParseWithFuncs(v interface{}, funcMap map[reflect.Type]ParserFunc, opts ...Options) error
ParseWithFuncs is the same as `Parse` except it also allows the user to pass in custom parsers.
Example ¶
type thing struct { desc string } type conf struct { Thing thing `env:"THING"` } os.Setenv("THING", "my thing") c := conf{} err := ParseWithFuncs(&c, 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 ¶ added in v7.1.0
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 ¶ added in v7.1.0
func (e AggregateError) Error() string
func (AggregateError) Is ¶ added in v7.1.0
func (e AggregateError) Is(err error) bool
Is conforms with errors.Is.
type EmptyEnvVarError ¶ added in v7.1.0
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 ¶ added in v7.1.0
func (e EmptyEnvVarError) Error() string
type EnvVarIsNotSetError ¶ added in v7.1.0
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 ¶ added in v7.1.0
func (e EnvVarIsNotSetError) Error() string
type LoadFileContentError ¶ added in v7.1.0
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 ¶ added in v7.1.0
func (e LoadFileContentError) Error() string
type NoParserError ¶ added in v7.1.0
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 ¶ added in v7.1.0
func (e NoParserError) Error() string
type NoSupportedTagOptionError ¶ added in v7.1.0
type NoSupportedTagOptionError struct {
Tag string
}
This error occurs when the given tag is not supported In-built supported tags: "", "file", "required", "unset", "notEmpty", "envDefault", "envExpand", "envSeparator" How to create a custom tag: https://github.com/caarlos0/env#changing-default-tag-name
func (NoSupportedTagOptionError) Error ¶ added in v7.1.0
func (e NoSupportedTagOptionError) Error() string
type NotStructPtrError ¶ added in v7.1.0
type NotStructPtrError struct{}
The error occurs when pass something that is not a pointer to a Struct to Parse
func (NotStructPtrError) Error ¶ added in v7.1.0
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 // contains filtered or unexported fields }
Options for the parser.
type ParseError ¶ added in v7.1.0
The error occurs when it's impossible to convert the value for given type.
func (ParseError) Error ¶ added in v7.1.0
func (e ParseError) Error() string
type ParseValueError ¶ added in v7.1.0
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 ¶ added in v7.1.0
func (e ParseValueError) Error() string
type ParserFunc ¶
ParserFunc defines the signature of a function that can be used within `CustomParsers`.