envar

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2023 License: GPL-3.0 Imports: 10 Imported by: 0

README

envar

This package allows parsing ENV vars into struct fields using a tag that can be optionally defined apart from the default (env).

Usage

See parser_test.go for full examples.

TODO:

  • - implement ENV VAR expansion. This is already started but will perhaps take a different approach.

Documentation

Index

Constants

View Source
const DefaultEnvPrefixDelim = "_"
View Source
const DefaultEnvSliceDelim = ","
View Source
const DefaultTagName = "env"

Variables

View Source
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()))
}
View Source
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

type EnvVarsMap map[string]string

func (EnvVarsMap) Get

func (e EnvVarsMap) Get(key string) (string, bool)

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 (p *ParserCtx) AddValidationError(key, value string)

func (*ParserCtx) AddValidatorFunc

func (p *ParserCtx) AddValidatorFunc(validatorKey string, fn ValidatorFunc)

func (*ParserCtx) GetEnvPrefix

func (p *ParserCtx) GetEnvPrefix() string

func (*ParserCtx) GetEnvPrefixDelim

func (p *ParserCtx) GetEnvPrefixDelim() string

func (*ParserCtx) GetEnvSliceDelim

func (p *ParserCtx) GetEnvSliceDelim() string

func (*ParserCtx) GetEnvVarsMap

func (p *ParserCtx) GetEnvVarsMap() EnvVarsMap

func (*ParserCtx) GetParserFuncMap

func (p *ParserCtx) GetParserFuncMap() ParserFuncMap

func (*ParserCtx) GetTagName

func (p *ParserCtx) GetTagName() string

func (*ParserCtx) GetValidationErrors

func (p *ParserCtx) GetValidationErrors() validationErrorMap

func (*ParserCtx) HasValidationErrors

func (p *ParserCtx) HasValidationErrors() bool

func (*ParserCtx) SetEnvPrefix

func (p *ParserCtx) SetEnvPrefix(envPrefix string) error

func (*ParserCtx) SetEnvPrefixDelim

func (p *ParserCtx) SetEnvPrefixDelim(delim string) error

func (*ParserCtx) SetEnvSliceDelim

func (p *ParserCtx) SetEnvSliceDelim(delim string) error

func (*ParserCtx) SetEnvVarsLoaderFunc

func (p *ParserCtx) SetEnvVarsLoaderFunc(envVarsLoadFunc EnvVarsLoaderFunc) error

func (*ParserCtx) SetParserFuncMap

func (p *ParserCtx) SetParserFuncMap(fnMap ParserFuncMap) error

func (*ParserCtx) SetTagName

func (p *ParserCtx) SetTagName(tagName string) error

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

type ParserFunc func(v string) (interface{}, error)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL