Documentation
¶
Overview ¶
Package goenvconf contains reusable structures and utilities for configuration with environment variables.
Index ¶
- Variables
- func GetOSEnv(s string) (string, error)
- func ParseBoolMapFromString(input string) (map[string]bool, error)
- func ParseFloatMapFromString[T float32 | float64](input string) (map[string]T, error)
- func ParseIntMapFromString(input string) (map[string]int, 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)
- type EnvAny
- type EnvBool
- type EnvFloat
- func (ev EnvFloat) Equal(target EnvFloat) bool
- func (ev EnvFloat) Get() (float64, error)
- func (ev EnvFloat) GetCustom(getFunc GetEnvFunc) (float64, error)
- func (ev EnvFloat) GetOrDefault(defaultValue float64) (float64, error)
- func (ev EnvFloat) IsZero() bool
- func (ev *EnvFloat) UnmarshalJSON(b []byte) error
- type EnvInt
- type EnvMapBool
- type EnvMapFloat
- type EnvMapInt
- type EnvMapString
- type EnvString
- func (ev EnvString) Equal(target EnvString) bool
- func (ev EnvString) Get() (string, error)
- func (ev EnvString) GetCustom(getFunc GetEnvFunc) (string, error)
- func (ev EnvString) GetOrDefault(defaultValue string) (string, error)
- func (ev EnvString) IsZero() bool
- func (ev *EnvString) UnmarshalJSON(b []byte) error
- type GetEnvFunc
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEnvironmentValueRequired occurs when both value and env fields are null or empty. ErrEnvironmentValueRequired = errors.New("require either value or env") // ErrEnvironmentVariableValueRequired the error happens when the value from environment variable is empty. ErrEnvironmentVariableValueRequired = errors.New("the environment variable value is empty") // ErrParseStringFailed is the error when failed to parse a string to another type. ErrParseStringFailed = errors.New("ParseStringFailed") )
Functions ¶
func ParseBoolMapFromString ¶
ParseBoolMapFromString parses a bool map from a string with format:
<key1>=<value1>;<key2>=<value2>
func ParseFloatMapFromString ¶
ParseFloatMapFromString parses a float map from a string with format:
<key1>=<value1>;<key2>=<value2>
func ParseIntMapFromString ¶
ParseIntMapFromString parses an integer map from a string with format:
<key1>=<value1>;<key2>=<value2>
Types ¶
type EnvAny ¶ added in v0.2.0
type EnvAny struct {
Value any `json:"value,omitempty" jsonschema:"anyof_required=value" mapstructure:"value" yaml:"value,omitempty"`
Variable *string `json:"env,omitempty" jsonschema:"anyof_required=env" mapstructure:"env" yaml:"env,omitempty"`
}
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.
func (EnvAny) GetCustom ¶ added in v0.4.0
func (ev EnvAny) GetCustom(getFunc GetEnvFunc) (any, error)
GetCustom gets literal value or from system environment by a custom function.
func (*EnvAny) UnmarshalJSON ¶ added in v0.2.0
UnmarshalJSON implements json.Unmarshaler.
type EnvBool ¶
type EnvBool struct {
Value *bool `json:"value,omitempty" jsonschema:"anyof_required=value" mapstructure:"value" yaml:"value,omitempty"`
Variable *string `json:"env,omitempty" jsonschema:"anyof_required=env" mapstructure:"env" yaml:"env,omitempty"`
}
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.
func (*EnvBool) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type EnvFloat ¶
type EnvFloat struct {
Value *float64 `json:"value,omitempty" jsonschema:"anyof_required=value" mapstructure:"value" yaml:"value,omitempty"`
Variable *string `json:"env,omitempty" jsonschema:"anyof_required=env" mapstructure:"env" yaml:"env,omitempty"`
}
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.
func (*EnvFloat) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type EnvInt ¶
type EnvInt struct {
Value *int64 `json:"value,omitempty" jsonschema:"anyof_required=value" mapstructure:"value" yaml:"value,omitempty"`
Variable *string `json:"env,omitempty" jsonschema:"anyof_required=env" mapstructure:"env" yaml:"env,omitempty"`
}
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.
func (*EnvInt) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type EnvMapBool ¶
type EnvMapBool struct {
Value map[string]bool `json:"value,omitempty" jsonschema:"anyof_required=value" mapstructure:"value" yaml:"value,omitempty"`
Variable *string `json:"env,omitempty" jsonschema:"anyof_required=env" mapstructure:"env" yaml:"env,omitempty"`
}
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.
func (*EnvMapBool) UnmarshalJSON ¶
func (ev *EnvMapBool) UnmarshalJSON(b []byte) error
UnmarshalJSON implements json.Unmarshaler.
type EnvMapFloat ¶
type EnvMapFloat struct {
Value map[string]float64 `json:"value,omitempty" jsonschema:"anyof_required=value" mapstructure:"value" yaml:"value,omitempty"`
Variable *string `json:"env,omitempty" jsonschema:"anyof_required=env" mapstructure:"env" yaml:"env,omitempty"`
}
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.
func (*EnvMapFloat) UnmarshalJSON ¶
func (ev *EnvMapFloat) UnmarshalJSON(b []byte) error
UnmarshalJSON implements json.Unmarshaler.
type EnvMapInt ¶
type EnvMapInt struct {
Value map[string]int64 `json:"value,omitempty" jsonschema:"anyof_required=value" mapstructure:"value" yaml:"value,omitempty"`
Variable *string `json:"env,omitempty" jsonschema:"anyof_required=env" mapstructure:"env" yaml:"env,omitempty"`
}
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.
func (EnvMapInt) GetCustom ¶ added in v0.4.0
func (ev EnvMapInt) GetCustom(getFunc GetEnvFunc) (map[string]int64, error)
GetCustom gets literal value or from system environment by a custom function.
func (*EnvMapInt) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type EnvMapString ¶
type EnvMapString struct {
Value map[string]string `json:"value,omitempty" jsonschema:"anyof_required=value" mapstructure:"value" yaml:"value,omitempty"`
Variable *string `json:"env,omitempty" jsonschema:"anyof_required=env" mapstructure:"env" yaml:"env,omitempty"`
}
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.
func (*EnvMapString) UnmarshalJSON ¶
func (ev *EnvMapString) UnmarshalJSON(b []byte) error
UnmarshalJSON implements json.Unmarshaler.
type EnvString ¶
type EnvString struct {
Value *string `json:"value,omitempty" jsonschema:"anyof_required=value" mapstructure:"value" yaml:"value,omitempty"`
Variable *string `json:"env,omitempty" jsonschema:"anyof_required=env" mapstructure:"env" yaml:"env,omitempty"`
}
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.
func (*EnvString) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
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.