Documentation ¶
Overview ¶
Package env implements pulling environment variables into a struct.
This file is based on code from caarlos0/env, available at github.com/caarlos0/env
Copyright (c) 2015-2024 Carlos Alexandro Becker Licensed under the MIT License. See the LICENSE file in the original project for details.
Modifications made by Kenzo Spaulding, 2024
Index ¶
- Variables
- 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 FieldParams
- type OnSetFn
- type Options
- type ParserFunc
Constants ¶
This section is empty.
Variables ¶
var ( ErrParseValue = errors.New("unable to parse") ErrStructPtr = errors.New("not struct pointer error") ErrNoSupportedTagOption = errors.New("tag option not supported") ErrVarIsNotSet = errors.New("required environment variable is not set") ErrEmptyVar = errors.New("environment variable should not be empty") ErrLoadFileContent = errors.New("could not load content of file from variable") ErrNoParser = errors.New("no parser found") )
Functions ¶
func Parse ¶
func Parse(v interface{}) error
Parse parses a struct containing `env` tags and loads its values from environment variables.
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.
Types ¶
type FieldParams ¶
type FieldParams struct { OwnKey string Key string DefaultValue string HasDefaultValue bool Required bool LoadFile bool Unset bool NotEmpty bool Expand bool Init bool Ignored 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 Options ¶
type Options struct { // Environment keys and values that will be accessible for the service. Environment map[string]string // TagName specifies another tag name to use rather than the default 'env'. TagName string // PrefixTagName specifies another prefix tag name to use rather than the default 'envPrefix'. PrefixTagName string // DefaultValueTagName specifies another default tag name to use rather than the default 'envDefault'. DefaultValueTagName string // RequiredIfNoDef automatically sets all fields 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 every 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 ParserFunc ¶
ParserFunc defines the signature of a function that can be used within `Options`' `FuncMap`.