Documentation
¶
Overview ¶
Package goenvconf contains reusable structures and utilities for configuration with environment variables.
Index ¶
- Constants
- Variables
- func GetOSEnv(s string) (string, error)
- func ParseBoolMapFromString(input string) (map[string]bool, error)
- func ParseBoolSliceFromString(input string) ([]bool, error)
- func ParseFloatMapFromString[T float32 | float64](input string) (map[string]T, error)
- func ParseFloatSliceFromString[T float32 | float64](input string) ([]T, error)
- func ParseIntSliceFromString[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64](input string) ([]T, error)
- func ParseIntegerMapFromString[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64](input string) (map[string]T, error)
- func ParseStringMapFromString(input string) (map[string]string, error)
- func ParseStringSliceFromString(input string) []string
- type EnvAny
- type EnvBool
- type EnvBoolSlice
- type EnvFloat
- type EnvFloatSlice
- type EnvInt
- type EnvIntSlice
- type EnvMapBool
- type EnvMapFloat
- type EnvMapInt
- type EnvMapString
- type EnvString
- type EnvStringSlice
- type GetEnvFunc
- type ParseEnvError
Constants ¶
const (
// ErrCodeParseEnvFailed is the error code when parsing environment variable failed.
ErrCodeParseEnvFailed = "ParseEnvFailed"
)
Variables ¶
var ( // ErrEnvironmentValueRequired occurs when both value and env fields are null or empty. ErrEnvironmentValueRequired = ParseEnvError{ Code: "EmptyEnv", Detail: "require either value or env", } // ErrEnvironmentVariableValueRequired the error that occurs when the value from environment variable is empty. ErrEnvironmentVariableValueRequired = ParseEnvError{ Code: "EmptyVar", Detail: "the environment variable value is empty", } )
Functions ¶
func ParseBoolMapFromString ¶
ParseBoolMapFromString parses a bool map from a string with format:
<key1>=<value1>;<key2>=<value2>
func ParseBoolSliceFromString ¶ added in v0.6.0
ParseBoolSliceFromString parses a boolean slice from a comma-separated string.
func ParseFloatMapFromString ¶
ParseFloatMapFromString parses a float map from a string with format:
<key1>=<value1>;<key2>=<value2>
func ParseFloatSliceFromString ¶ added in v0.6.0
ParseFloatSliceFromString parses a floating-point number slice from a comma-separated string.
func ParseIntSliceFromString ¶ added in v0.6.0
func ParseIntSliceFromString[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64]( input string, ) ([]T, error)
ParseIntSliceFromString parses an integer slice from a comma-separated string.
func ParseIntegerMapFromString ¶
func ParseIntegerMapFromString[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64]( input string, ) (map[string]T, error)
ParseIntegerMapFromString parses an integer map from a string with format:
<key1>=<value1>;<key2>=<value2>
func ParseStringMapFromString ¶
ParseStringMapFromString parses a string map from a string with format:
<key1>=<value1>;<key2>=<value2>
func ParseStringSliceFromString ¶ added in v0.6.0
ParseStringSliceFromString parses a string slice from a comma-separated string.
Types ¶
type EnvAny ¶ added in v0.2.0
type EnvAny struct {
Value any `` /* 154-byte string literal not displayed */
Variable *string `` /* 152-byte string literal not displayed */
}
EnvAny represents either arbitrary value or an environment reference.
func NewEnvAnyValue ¶ added in v0.2.0
NewEnvAnyValue creates an EnvAny with a literal value.
func NewEnvAnyVariable ¶ added in v0.2.0
NewEnvAnyVariable creates an EnvAny with a variable name.
type EnvBool ¶
type EnvBool struct {
Value *bool `` /* 154-byte string literal not displayed */
Variable *string `` /* 152-byte string literal not displayed */
}
EnvBool represents either a literal boolean or an environment reference.
func NewEnvBool ¶
NewEnvBool creates an EnvBool instance.
func NewEnvBoolValue ¶
NewEnvBoolValue creates an EnvBool with a literal value.
func NewEnvBoolVariable ¶
NewEnvBoolVariable creates an EnvBool with a variable name.
func (EnvBool) GetCustom ¶ added in v0.4.0
func (ev EnvBool) GetCustom(getFunc GetEnvFunc) (bool, error)
GetCustom gets literal value or from system environment with custom function.
func (EnvBool) GetOrDefault ¶
GetOrDefault returns the default value if the environment value is empty.
type EnvBoolSlice ¶ added in v0.6.0
type EnvBoolSlice struct {
Value []bool `` /* 154-byte string literal not displayed */
Variable *string `` /* 152-byte string literal not displayed */
}
EnvBoolSlice represents either a literal boolean slice or an environment reference.
func NewEnvBoolSlice ¶ added in v0.6.0
func NewEnvBoolSlice(env string, value []bool) EnvBoolSlice
NewEnvBoolSlice creates an EnvBoolSlice instance.
func NewEnvBoolSliceValue ¶ added in v0.6.0
func NewEnvBoolSliceValue(value []bool) EnvBoolSlice
NewEnvBoolSliceValue creates an EnvBoolSlice with a literal value.
func NewEnvBoolSliceVariable ¶ added in v0.6.0
func NewEnvBoolSliceVariable(name string) EnvBoolSlice
NewEnvBoolSliceVariable creates an EnvBoolSlice with a variable name.
func (EnvBoolSlice) Equal ¶ added in v0.6.0
func (ev EnvBoolSlice) Equal(target EnvBoolSlice) bool
Equal checks if this instance equals the target value.
func (EnvBoolSlice) Get ¶ added in v0.6.0
func (ev EnvBoolSlice) Get() ([]bool, error)
Get gets literal value or from system environment.
func (EnvBoolSlice) GetCustom ¶ added in v0.6.0
func (ev EnvBoolSlice) GetCustom(getFunc GetEnvFunc) ([]bool, error)
GetCustom gets literal value or from system environment by a custom function.
func (EnvBoolSlice) IsZero ¶ added in v0.6.0
func (ev EnvBoolSlice) IsZero() bool
IsZero checks if the instance is empty.
type EnvFloat ¶
type EnvFloat struct {
Value *float64 `` /* 154-byte string literal not displayed */
Variable *string `` /* 152-byte string literal not displayed */
}
EnvFloat represents either a literal floating point number or an environment reference.
func NewEnvFloat ¶
NewEnvFloat creates an EnvFloat instance.
func NewEnvFloatValue ¶
NewEnvFloatValue creates an EnvFloat with a literal value.
func NewEnvFloatVariable ¶
NewEnvFloatVariable creates an EnvFloat with a variable name.
func (EnvFloat) GetCustom ¶ added in v0.4.0
func (ev EnvFloat) GetCustom(getFunc GetEnvFunc) (float64, error)
GetCustom gets literal value or from system environment by a custom function.
func (EnvFloat) GetOrDefault ¶
GetOrDefault returns the default value if the environment value is empty.
type EnvFloatSlice ¶ added in v0.6.0
type EnvFloatSlice struct {
Value []float64 `` /* 154-byte string literal not displayed */
Variable *string `` /* 152-byte string literal not displayed */
}
EnvFloatSlice represents either a literal floating-point number slice or an environment reference.
func NewEnvFloatSlice ¶ added in v0.6.0
func NewEnvFloatSlice(env string, value []float64) EnvFloatSlice
NewEnvFloatSlice creates an EnvFloatSlice instance.
func NewEnvFloatSliceValue ¶ added in v0.6.0
func NewEnvFloatSliceValue(value []float64) EnvFloatSlice
NewEnvFloatSliceValue creates an EnvFloatSlice with a literal value.
func NewEnvFloatSliceVariable ¶ added in v0.6.0
func NewEnvFloatSliceVariable(name string) EnvFloatSlice
NewEnvFloatSliceVariable creates an EnvFloatSlice with a variable name.
func (EnvFloatSlice) Equal ¶ added in v0.6.0
func (ev EnvFloatSlice) Equal(target EnvFloatSlice) bool
Equal checks if this instance equals the target value.
func (EnvFloatSlice) Get ¶ added in v0.6.0
func (ev EnvFloatSlice) Get() ([]float64, error)
Get gets literal value or from system environment.
func (EnvFloatSlice) GetCustom ¶ added in v0.6.0
func (ev EnvFloatSlice) GetCustom(getFunc GetEnvFunc) ([]float64, error)
GetCustom gets literal value or from system environment by a custom function.
func (EnvFloatSlice) IsZero ¶ added in v0.6.0
func (ev EnvFloatSlice) IsZero() bool
IsZero checks if the instance is empty.
type EnvInt ¶
type EnvInt struct {
Value *int64 `` /* 154-byte string literal not displayed */
Variable *string `` /* 152-byte string literal not displayed */
}
EnvInt represents either a literal integer or an environment reference.
func NewEnvIntValue ¶
NewEnvIntValue creates an EnvInt with a literal value.
func NewEnvIntVariable ¶
NewEnvIntVariable creates an EnvInt with a variable name.
func (EnvInt) GetCustom ¶ added in v0.4.0
func (ev EnvInt) GetCustom(getFunc GetEnvFunc) (int64, error)
GetCustom gets literal value or from system environment by a custom function.
func (EnvInt) GetOrDefault ¶
GetOrDefault returns the default value if the environment value is empty.
type EnvIntSlice ¶ added in v0.6.0
type EnvIntSlice struct {
Value []int64 `` /* 154-byte string literal not displayed */
Variable *string `` /* 152-byte string literal not displayed */
}
EnvIntSlice represents either a literal integer slice or an environment reference.
func NewEnvIntSlice ¶ added in v0.6.0
func NewEnvIntSlice(env string, value []int64) EnvIntSlice
NewEnvIntSlice creates an EnvIntSlice instance.
func NewEnvIntSliceValue ¶ added in v0.6.0
func NewEnvIntSliceValue(value []int64) EnvIntSlice
NewEnvIntSliceValue creates an EnvIntSlice with a literal value.
func NewEnvIntSliceVariable ¶ added in v0.6.0
func NewEnvIntSliceVariable(name string) EnvIntSlice
NewEnvIntSliceVariable creates an EnvIntSlice with a variable name.
func (EnvIntSlice) Equal ¶ added in v0.6.0
func (ev EnvIntSlice) Equal(target EnvIntSlice) bool
Equal checks if this instance equals the target value.
func (EnvIntSlice) Get ¶ added in v0.6.0
func (ev EnvIntSlice) Get() ([]int64, error)
Get gets literal value or from system environment.
func (EnvIntSlice) GetCustom ¶ added in v0.6.0
func (ev EnvIntSlice) GetCustom(getFunc GetEnvFunc) ([]int64, error)
GetCustom gets literal value or from system environment by a custom function.
func (EnvIntSlice) IsZero ¶ added in v0.6.0
func (ev EnvIntSlice) IsZero() bool
IsZero checks if the instance is empty.
type EnvMapBool ¶
type EnvMapBool struct {
Value map[string]bool `` /* 154-byte string literal not displayed */
Variable *string `` /* 152-byte string literal not displayed */
}
EnvMapBool represents either a literal bool map or an environment reference.
func NewEnvMapBool ¶
func NewEnvMapBool(env string, value map[string]bool) EnvMapBool
NewEnvMapBool creates an EnvMapBool instance.
func NewEnvMapBoolValue ¶
func NewEnvMapBoolValue(value map[string]bool) EnvMapBool
NewEnvMapBoolValue creates an EnvMapBool with a literal value.
func NewEnvMapBoolVariable ¶
func NewEnvMapBoolVariable(name string) EnvMapBool
NewEnvMapBoolVariable creates an EnvMapBool with a variable name.
func (EnvMapBool) Equal ¶ added in v0.5.0
func (ev EnvMapBool) Equal(target EnvMapBool) bool
Equal checks if this instance equals the target value.
func (EnvMapBool) Get ¶
func (ev EnvMapBool) Get() (map[string]bool, error)
Get gets literal value or from system environment.
func (EnvMapBool) GetCustom ¶ added in v0.4.0
func (ev EnvMapBool) GetCustom(getFunc GetEnvFunc) (map[string]bool, error)
GetCustom gets literal value or from system environment by a custom function.
func (EnvMapBool) IsZero ¶ added in v0.3.0
func (ev EnvMapBool) IsZero() bool
IsZero checks if the instance is empty.
type EnvMapFloat ¶
type EnvMapFloat struct {
Value map[string]float64 `` /* 154-byte string literal not displayed */
Variable *string `` /* 152-byte string literal not displayed */
}
EnvMapFloat represents either a literal float map or an environment reference.
func NewEnvMapFloat ¶
func NewEnvMapFloat(env string, value map[string]float64) EnvMapFloat
NewEnvMapFloat creates an EnvMapFloat instance.
func NewEnvMapFloatValue ¶
func NewEnvMapFloatValue(value map[string]float64) EnvMapFloat
NewEnvMapFloatValue creates an EnvMapFloat with a literal value.
func NewEnvMapFloatVariable ¶
func NewEnvMapFloatVariable(name string) EnvMapFloat
NewEnvMapFloatVariable creates an EnvMapFloat with a variable name.
func (EnvMapFloat) Equal ¶ added in v0.5.0
func (ev EnvMapFloat) Equal(target EnvMapFloat) bool
Equal checks if this instance equals the target value.
func (EnvMapFloat) Get ¶
func (ev EnvMapFloat) Get() (map[string]float64, error)
Get gets literal value or from system environment.
func (EnvMapFloat) GetCustom ¶ added in v0.4.0
func (ev EnvMapFloat) GetCustom(getFunc GetEnvFunc) (map[string]float64, error)
GetCustom gets literal value or from system environment by a custom function.
func (EnvMapFloat) IsZero ¶ added in v0.3.0
func (ev EnvMapFloat) IsZero() bool
IsZero checks if the instance is empty.
type EnvMapInt ¶
type EnvMapInt struct {
Value map[string]int64 `` /* 154-byte string literal not displayed */
Variable *string `` /* 152-byte string literal not displayed */
}
EnvMapInt represents either a literal int map or an environment reference.
func NewEnvMapInt ¶
NewEnvMapInt creates an EnvMapInt instance.
func NewEnvMapIntValue ¶
NewEnvMapIntValue creates an EnvMapInt with a literal value.
func NewEnvMapIntVariable ¶
NewEnvMapIntVariable creates an EnvMapInt with a variable name.
type EnvMapString ¶
type EnvMapString struct {
Value map[string]string `` /* 154-byte string literal not displayed */
Variable *string `` /* 152-byte string literal not displayed */
}
EnvMapString represents either a literal string map or an environment reference.
func NewEnvMapString ¶
func NewEnvMapString(env string, value map[string]string) EnvMapString
NewEnvMapString creates an EnvMapString instance.
func NewEnvMapStringValue ¶
func NewEnvMapStringValue(value map[string]string) EnvMapString
NewEnvMapStringValue creates an EnvMapString with a literal value.
func NewEnvMapStringVariable ¶
func NewEnvMapStringVariable(name string) EnvMapString
NewEnvMapStringVariable creates an EnvMapString with a variable name.
func (EnvMapString) Equal ¶ added in v0.5.0
func (ev EnvMapString) Equal(target EnvMapString) bool
Equal checks if this instance equals the target value.
func (EnvMapString) Get ¶
func (ev EnvMapString) Get() (map[string]string, error)
Get gets literal value or from system environment.
func (EnvMapString) GetCustom ¶ added in v0.4.0
func (ev EnvMapString) GetCustom(getFunc GetEnvFunc) (map[string]string, error)
GetCustom gets literal value or from system environment by a custom function.
func (EnvMapString) IsZero ¶ added in v0.3.0
func (ev EnvMapString) IsZero() bool
IsZero checks if the instance is empty.
type EnvString ¶
type EnvString struct {
Value *string `` /* 154-byte string literal not displayed */
Variable *string `` /* 152-byte string literal not displayed */
}
EnvString represents either a literal string or an environment reference.
func NewEnvString ¶
NewEnvString creates an EnvString instance.
func NewEnvStringValue ¶
NewEnvStringValue creates an EnvString with a literal value.
func NewEnvStringVariable ¶
NewEnvStringVariable creates an EnvString with a variable name.
func (EnvString) GetCustom ¶ added in v0.4.0
func (ev EnvString) GetCustom(getFunc GetEnvFunc) (string, error)
GetCustom gets literal value or from system environment by a custom function.
func (EnvString) GetOrDefault ¶
GetOrDefault returns the default value if the environment value is empty.
type EnvStringSlice ¶ added in v0.6.0
type EnvStringSlice struct {
Value []string `` /* 154-byte string literal not displayed */
Variable *string `` /* 152-byte string literal not displayed */
}
EnvStringSlice represents either a literal string slice or an environment reference.
func NewEnvStringSlice ¶ added in v0.6.0
func NewEnvStringSlice(env string, value []string) EnvStringSlice
NewEnvStringSlice creates an EnvStringSlice instance.
func NewEnvStringSliceValue ¶ added in v0.6.0
func NewEnvStringSliceValue(value []string) EnvStringSlice
NewEnvStringSliceValue creates an EnvStringSlice with a literal value.
func NewEnvStringSliceVariable ¶ added in v0.6.0
func NewEnvStringSliceVariable(name string) EnvStringSlice
NewEnvStringSliceVariable creates an EnvStringSlice with a variable name.
func (EnvStringSlice) Equal ¶ added in v0.6.0
func (ev EnvStringSlice) Equal(target EnvStringSlice) bool
Equal checks if this instance equals the target value.
func (EnvStringSlice) Get ¶ added in v0.6.0
func (ev EnvStringSlice) Get() ([]string, error)
Get gets literal value or from system environment.
func (EnvStringSlice) GetCustom ¶ added in v0.6.0
func (ev EnvStringSlice) GetCustom(getFunc GetEnvFunc) ([]string, error)
GetCustom gets literal value or from system environment by a custom function.
func (EnvStringSlice) IsZero ¶ added in v0.6.0
func (ev EnvStringSlice) IsZero() bool
IsZero checks if the instance is empty.
type GetEnvFunc ¶ added in v0.4.0
GetEnvFunc abstracts a custom function to get the value of an environment variable.
func OSEnvGetter ¶ added in v0.4.1
func OSEnvGetter(_ context.Context) GetEnvFunc
OSEnvGetter wraps the GetOSEnv function with context.
type ParseEnvError ¶ added in v0.6.0
type ParseEnvError struct {
Code string `json:"code" jsonschema:"enum=EmptyEnv,enum=EmptyVar,enum=ParseEnvFailed"`
Detail string `json:"detail"`
Hint string `json:"hint,omitempty"`
}
ParseEnvError structures a detailed error for parsed env.
func NewParseEnvFailedError ¶ added in v0.6.0
func NewParseEnvFailedError(detail string, hint string) ParseEnvError
NewParseEnvFailedError creates a ParseEnvError for parsing env variable errors.
func (ParseEnvError) Error ¶ added in v0.6.0
func (pee ParseEnvError) Error() string
Error returns the error message.