Documentation ¶
Index ¶
- Constants
- Variables
- type EnvVarsLoaderFunc
- type EnvVarsMap
- type ParsedFieldGetter
- type ParserCtx
- func (p *ParserCtx) AddParserFunc(rType reflect.Type, fn ParserFunc)
- func (p *ParserCtx) AddValidationError(key, value string)
- func (p *ParserCtx) AddValidatorFunc(validatorKey string, fn ValidatorFunc)
- func (p *ParserCtx) GetEnvPrefix() string
- func (p *ParserCtx) GetEnvPrefixDelim() string
- func (p *ParserCtx) GetEnvSliceDelim() string
- func (p *ParserCtx) GetEnvVarsMap() EnvVarsMap
- func (p *ParserCtx) GetParserFuncMap() ParserFuncMap
- func (p *ParserCtx) GetTagName() string
- func (p *ParserCtx) GetValidationErrors() validationErrorMap
- func (p *ParserCtx) HasValidationErrors() bool
- func (p *ParserCtx) SetEnvPrefix(envPrefix string) error
- func (p *ParserCtx) SetEnvPrefixDelim(delim string) error
- func (p *ParserCtx) SetEnvSliceDelim(delim string) error
- func (p *ParserCtx) SetEnvVarsLoaderFunc(envVarsLoadFunc EnvVarsLoaderFunc) error
- func (p *ParserCtx) SetParserFuncMap(fnMap ParserFuncMap) error
- func (p *ParserCtx) SetTagName(tagName string) error
- func (p *ParserCtx) SetValidatorFuncsMap(fnMap ValidatorFuncsMap) error
- type ParserCtxAccessor
- type ParserCtxFuncSetter
- func AddParserFunc(rType reflect.Type, fn ParserFunc) ParserCtxFuncSetter
- func AddValidatorFunc(validatorKey string, fn ValidatorFunc) ParserCtxFuncSetter
- func SetEnvPrefix(envPrefix string) ParserCtxFuncSetter
- func SetEnvPrefixDelim(delim string) ParserCtxFuncSetter
- func SetEnvSliceDelim(delim string) ParserCtxFuncSetter
- func SetEnvVarsLoaderFunc(fn EnvVarsLoaderFunc) ParserCtxFuncSetter
- func SetParserFuncMap(fnMap ParserFuncMap) ParserCtxFuncSetter
- func SetTagName(tagName string) ParserCtxFuncSetter
- func SetValidatorFuncsMap(fnMap ValidatorFuncsMap) ParserCtxFuncSetter
- type ParserCtxGetter
- type ParserCtxSetter
- type ParserFunc
- type ParserFuncMap
- type ValidatorFunc
- type ValidatorFuncs
- type ValidatorFuncsMap
Constants ¶
const DefaultEnvPrefixDelim = "_"
const DefaultEnvSliceDelim = ","
const DefaultTagName = "env"
Variables ¶
var ErrNestedNotStruct = func(rValue reflect.Value) error { return errorx.New(fmt.Sprintf("field: %v is not a struct but has nested tag option", rValue.Elem().Type().String())) }
var ErrNotAStructPtr = errorx.New("env: expected a pointer to a Struct")
ErrNotAStructPtr is returned if you pass something that is not a pointer to a Struct to Parse.
Functions ¶
This section is empty.
Types ¶
type EnvVarsLoaderFunc ¶
type EnvVarsLoaderFunc func() EnvVarsMap
type EnvVarsMap ¶
func (EnvVarsMap) GetLength ¶
func (e EnvVarsMap) GetLength() int
func (EnvVarsMap) Set ¶
func (e EnvVarsMap) Set(key, value string)
type ParsedFieldGetter ¶
type ParsedFieldGetter interface { GetStructField() reflect.StructField GetEnvValue() string GetEnvFound() bool GetEnvKey() string // contains filtered or unexported methods }
type ParserCtx ¶
type ParserCtx struct {
// contains filtered or unexported fields
}
func (*ParserCtx) AddParserFunc ¶
func (p *ParserCtx) AddParserFunc(rType reflect.Type, fn ParserFunc)
func (*ParserCtx) AddValidationError ¶
func (*ParserCtx) AddValidatorFunc ¶
func (p *ParserCtx) AddValidatorFunc(validatorKey string, fn ValidatorFunc)
func (*ParserCtx) GetEnvPrefix ¶
func (*ParserCtx) GetEnvPrefixDelim ¶
func (*ParserCtx) GetEnvSliceDelim ¶
func (*ParserCtx) GetEnvVarsMap ¶
func (p *ParserCtx) GetEnvVarsMap() EnvVarsMap
func (*ParserCtx) GetParserFuncMap ¶
func (p *ParserCtx) GetParserFuncMap() ParserFuncMap
func (*ParserCtx) GetTagName ¶
func (*ParserCtx) GetValidationErrors ¶
func (p *ParserCtx) GetValidationErrors() validationErrorMap
func (*ParserCtx) HasValidationErrors ¶
func (*ParserCtx) SetEnvPrefix ¶
func (*ParserCtx) SetEnvPrefixDelim ¶
func (*ParserCtx) SetEnvSliceDelim ¶
func (*ParserCtx) SetEnvVarsLoaderFunc ¶
func (p *ParserCtx) SetEnvVarsLoaderFunc(envVarsLoadFunc EnvVarsLoaderFunc) error
func (*ParserCtx) SetParserFuncMap ¶
func (p *ParserCtx) SetParserFuncMap(fnMap ParserFuncMap) error
func (*ParserCtx) SetTagName ¶
func (*ParserCtx) SetValidatorFuncsMap ¶
func (p *ParserCtx) SetValidatorFuncsMap(fnMap ValidatorFuncsMap) error
type ParserCtxAccessor ¶
type ParserCtxAccessor interface { ParserCtxGetter ParserCtxSetter }
type ParserCtxFuncSetter ¶
type ParserCtxFuncSetter func(setter ParserCtxSetter) error
func AddParserFunc ¶
func AddParserFunc(rType reflect.Type, fn ParserFunc) ParserCtxFuncSetter
AddParserFunc adds a ParserFunc. This does not reset teh ParserFuncMap for the given ParserCtxSetter but will override a ParserFunc that's already defined for the given rType.
func AddValidatorFunc ¶
func AddValidatorFunc(validatorKey string, fn ValidatorFunc) ParserCtxFuncSetter
AddValidatorFunc adds a validator to the givne ParserCtxSetter's ValidatorFuncsMap. This does not reset the ValidatorFuncsMap but will override any existing validator that matches the validatorKey.
func SetEnvPrefix ¶
func SetEnvPrefix(envPrefix string) ParserCtxFuncSetter
SetEnvPrefix sets the prefix that will be appending to the env var name in the struct field. This is useful when an existing struct is reused but the env var defined in the struct field ha a prefix.
func SetEnvPrefixDelim ¶
func SetEnvPrefixDelim(delim string) ParserCtxFuncSetter
SetEnvPrefixDelim sets the delimiter used when appending a prefix to the existing env vars defined in a struct field. Defaault is DefaultEnvPrefixDelim
func SetEnvSliceDelim ¶
func SetEnvSliceDelim(delim string) ParserCtxFuncSetter
SetEnvSliceDelim defines the delimiter used to split a collection value in a given ENV var value.
func SetEnvVarsLoaderFunc ¶
func SetEnvVarsLoaderFunc(fn EnvVarsLoaderFunc) ParserCtxFuncSetter
SetEnvVarsLoaderFunc sets the func that is used ot load the env vars in to the EnvVarsMap
func SetParserFuncMap ¶
func SetParserFuncMap(fnMap ParserFuncMap) ParserCtxFuncSetter
SetParserFuncMap sets the ParserFuncMap using the fnMap. This overrides the ParserFuncMap for the given ParserCtx. This should only if it's not desired to use the parsers already defined in this package.
func SetTagName ¶
func SetTagName(tagName string) ParserCtxFuncSetter
SetTagName set the tag name that will be used to for the struct field
func SetValidatorFuncsMap ¶
func SetValidatorFuncsMap(fnMap ValidatorFuncsMap) ParserCtxFuncSetter
SetValidatorFuncsMap sets the ValidatorFuncsMap for the given ParserCtxSetter. This should only be used if it's not desired to use the defined validators in the package and which to override ALL of them.
type ParserCtxGetter ¶
type ParserCtxGetter interface { // GetTagName returns the tag name used to check the tag values for a // given struct field GetTagName() string // GetEnvPrefix returns the ENV prefix to be appended to the ENV name // defined in the struct tag GetEnvPrefix() string // GetEnvPrefixDelima reeturns the ENV prefix delimiter that is used // to join the ENV prefix and the ENV name GetEnvPrefixDelim() string // GetEnvSliceDelim returns the delimiter used to split the values of // a collection for a given ENV var GetEnvSliceDelim() string // GetParserFuncMap returns ParserFuncMap that will be used to parse // the different types for a given struct field GetParserFuncMap() ParserFuncMap // GetEnvVarsMaps returns the environemnt varialbes that was mapped to // EnvVarsMap GetEnvVarsMap() EnvVarsMap // GetValidationErrors returns the validation errors that were added // when validation failed for a given struct field. GetValidationErrors() validationErrorMap // HasValidationErrors returns a boolean if the validationErrorMap has // a length of greater than 0 HasValidationErrors() bool }
func Parse ¶
func Parse(v interface{}, setterFuncs ...ParserCtxFuncSetter) (ParserCtxGetter, error)
Parse parses a struct containing `env` tags and loads its values from environment variables.
type ParserCtxSetter ¶
type ParserCtxSetter interface { // SetTagName sets the tagname that will be used to read the tag values // for a given struct field. SetTagName(tagName string) error // SetEnvVarsLoaderFunc sets the env var loader func that will be used // to set the EnvVarsMap taht will hold the environmental variables SetEnvVarsLoaderFunc(fn EnvVarsLoaderFunc) error // SetEnvPrefix sets the env prefix SetEnvPrefix(envPrefix string) error // SetEnvPrefixDelim sets the env prefix delimiter SetEnvPrefixDelim(delim string) error // SetEnvSliceDelima sets the delimiter for the ENV var that contains // a collection SetEnvSliceDelim(delim string) error // SetParserFuncMap sets the ParserFuncMap using the fnMap. This overrides the // ParserFuncMap for the given ParserCtx. This should only if it's not desired // to use the parsers already defined in this package. SetParserFuncMap(fnMap ParserFuncMap) error // AddParserFunc adds a ParserFunc. This does not reset teh ParserFuncMap for // the given ParserCtxSetter but will override a ParserFunc that's already // defined for the given rType. AddParserFunc(rType reflect.Type, fn ParserFunc) // SetValidatorFuncsMap sets the ValidatorFuncsMap for the given // ParserCtxSetter. This should only be used if it's not desired to use the // defined validators in the package and which to override ALL of them. SetValidatorFuncsMap(fnMap ValidatorFuncsMap) error // AddValidatorFunc adds a validator to the givne ParserCtxSetter's // ValidatorFuncsMap. This does not reset the ValidatorFuncsMap but will // override any existing validator that matches the validatorKey. AddValidatorFunc(validatorKey string, fn ValidatorFunc) // AddValidationError adds the validation error that was generated // when a validation failed. AddValidationError(key, value string) }
type ParserFunc ¶
ParserFunc defines the signature of a function that can be used within `CustomParsers`.
type ParserFuncMap ¶
type ParserFuncMap map[string]ParserFunc
func (ParserFuncMap) Add ¶
func (p ParserFuncMap) Add(rType reflect.Type, pFunc ParserFunc)
func (ParserFuncMap) Get ¶
func (p ParserFuncMap) Get(rType reflect.Type) ParserFunc
func (ParserFuncMap) GetLength ¶
func (p ParserFuncMap) GetLength() int
type ValidatorFunc ¶
type ValidatorFunc func(parserCtx ParserCtxAccessor, parsedField ParsedFieldGetter) error
ValidatorFunc defines the signature of the function that will validate the v. It is expected to return an error if it fails the validation
type ValidatorFuncs ¶
type ValidatorFuncs []ValidatorFunc
func (ValidatorFuncs) GetLength ¶
func (v ValidatorFuncs) GetLength() int
type ValidatorFuncsMap ¶
type ValidatorFuncsMap map[string]ValidatorFunc
func (ValidatorFuncsMap) Add ¶
func (v ValidatorFuncsMap) Add(key string, fn ValidatorFunc)
func (ValidatorFuncsMap) GetLength ¶
func (v ValidatorFuncsMap) GetLength() int