Documentation
¶
Index ¶
- func CustomTypeParser[T any](p CustomTypeParserFunc[T]) (reflect.Type, func(v string) (interface{}, error))
- func Get[T any](envName string, opts ...GetterOption[T]) T
- func GetList[T any](envName string, opts ...GetterOption[T]) []T
- func LoadFromFile(filenames ...string) error
- func Parse(v any, opts ...ParseOptions) error
- func ParseWithConfig(v any, pc *ParseConfig) error
- type CustomTypeParserFunc
- type GetterOption
- func WithCustomParser[T any](parser func(string) (T, error)) GetterOption[T]
- func WithDefaultListValue[T any](values []T) GetterOption[T]
- func WithDefaultValue[T any](value T) GetterOption[T]
- func WithEnum[T any](values []T) GetterOption[T]
- func WithListSeparator[T any](separator string) GetterOption[T]
- type ParseConfig
- type ParseOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CustomTypeParser ¶ added in v1.2.0
func CustomTypeParser[T any](p CustomTypeParserFunc[T]) (reflect.Type, func(v string) (interface{}, error))
CustomTypeParser is a helper function to create a custom parser.
func Get ¶ added in v1.2.0
func Get[T any](envName string, opts ...GetterOption[T]) T
Get retrieves the value of the environment variable with the given name. It converts the value to the type T. If the environment variable is not defined, it returns the default value or zero. If the environment variable is defined but cannot be converted to the type T, it returns the default value or zero.
POINTERS ARE NOT SUPPORTED. If T is a pointer, it ALWAYS returns nil.
func GetList ¶ added in v1.2.0
func GetList[T any](envName string, opts ...GetterOption[T]) []T
GetGetList retrieves a list of values from the environment variable with the given name. The values are expected to be separated by commas. It converts each value to the type T. If the environment variable is not defined, it returns an empty list. If the environment variable is defined but cannot be converted to the type T, it returns an empty list.
POINTERS ARE NOT SUPPORTED. If T is a pointer, it ALWAYS returns an empty list.
func LoadFromFile ¶ added in v1.2.0
LoadFromFile loads the environment variables from the given files. By default, it loads from the .env file.
func Parse ¶ added in v1.2.0
func Parse(v any, opts ...ParseOptions) error
Parse parses the environment variables into the given struct.
func ParseWithConfig ¶ added in v1.2.0
func ParseWithConfig(v any, pc *ParseConfig) error
ParseWithConfig parses the environment variables into the given struct. It uses a config for the parsing.
Types ¶
type CustomTypeParserFunc ¶ added in v1.2.0
CustomTypeParserFunc is a generic function that can parse a string into a custom type.
type GetterOption ¶ added in v1.2.0
type GetterOption[T any] func(cfg *getterConfig[T])
func WithCustomParser ¶ added in v1.2.0
func WithCustomParser[T any](parser func(string) (T, error)) GetterOption[T]
WithCustomParser sets a custom parser to convert the environment variable to the type T.
func WithDefaultListValue ¶ added in v1.2.0
func WithDefaultListValue[T any](values []T) GetterOption[T]
WithDefaultListValue sets the default value to return if the environment variable is not defined or if its value is an empty list.
func WithDefaultValue ¶ added in v1.2.0
func WithDefaultValue[T any](value T) GetterOption[T]
WithDefaultValue sets the default value to return if the environment variable is not defined or if its value is zero.
func WithEnum ¶ added in v1.2.0
func WithEnum[T any](values []T) GetterOption[T]
WithEnum sets the enum values to validate the environment variable against.
func WithListSeparator ¶ added in v1.2.0
func WithListSeparator[T any](separator string) GetterOption[T]
WithListSeparator sets the separator for the list of values.
type ParseConfig ¶ added in v1.2.0
type ParseConfig struct {
// contains filtered or unexported fields
}
ParseConfig is a configuration for the Parser.
func (*ParseConfig) AddCustomTypeParser ¶ added in v1.2.0
func (c *ParseConfig) AddCustomTypeParser(r reflect.Type, pf func(v string) (interface{}, error)) *ParseConfig
AddCustomTypeParser adds a custom type parser to the config.
func (*ParseConfig) AddPrefix ¶ added in v1.2.0
func (c *ParseConfig) AddPrefix(prefix string) *ParseConfig
AddPrefix adds a prefix to the config.
type ParseOptions ¶ added in v1.2.0
type ParseOptions func(pc *ParseConfig)
ParseOptions is an option for the Parse.
func FromFiles ¶ added in v1.2.0
func FromFiles(filenames ...string) ParseOptions
FromFiles loads the environment variables from the given files. Same as LoadFromFile.
func WithCustomTypeParser ¶ added in v1.2.0
func WithCustomTypeParser[T any](p CustomTypeParserFunc[T]) ParseOptions
WithCustomTypeParser adds a custom type parser to the config. Same as ParseConfig.AddCustomTypeParser.
func WithPrefix ¶ added in v1.2.0
func WithPrefix(prefix string) ParseOptions
WithPrefix sets a prefix for the environment variable keys.