goenvconf

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: Apache-2.0 Imports: 9 Imported by: 18

README

goenvconf

A reusable library for configuration with environment variables.

Documentation

Overview

Package goenvconf contains reusable structures and utilities for configuration with environment variables.

Index

Constants

This section is empty.

Variables

View Source
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 GetOSEnv added in v0.4.1

func GetOSEnv(s string) (string, error)

GetOSEnv implements the GetEnvFunc with OS environment.

func ParseBoolMapFromString

func ParseBoolMapFromString(input string) (map[string]bool, error)

ParseBoolMapFromString parses a bool map from a string with format:

<key1>=<value1>;<key2>=<value2>

func ParseFloatMapFromString

func ParseFloatMapFromString[T float32 | float64](input string) (map[string]T, error)

ParseFloatMapFromString parses a float map from a string with format:

<key1>=<value1>;<key2>=<value2>

func ParseIntMapFromString

func ParseIntMapFromString(input string) (map[string]int, error)

ParseIntMapFromString parses an integer map from a string with format:

<key1>=<value1>;<key2>=<value2>

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

func ParseStringMapFromString(input string) (map[string]string, error)

ParseStringMapFromString parses a string 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 NewEnvAny added in v0.2.0

func NewEnvAny(env string, value any) EnvAny

NewEnvAny creates an EnvAny instance.

func NewEnvAnyValue added in v0.2.0

func NewEnvAnyValue(value any) EnvAny

NewEnvAnyValue creates an EnvAny with a literal value.

func NewEnvAnyVariable added in v0.2.0

func NewEnvAnyVariable(name string) EnvAny

NewEnvAnyVariable creates an EnvAny with a variable name.

func (EnvAny) Equal added in v0.5.0

func (ev EnvAny) Equal(target EnvAny) bool

Equal checks if this instance equals the target value.

func (EnvAny) Get added in v0.2.0

func (ev EnvAny) Get() (any, error)

Get gets literal value or from system environment.

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) IsZero added in v0.3.0

func (ev EnvAny) IsZero() bool

IsZero checks if the instance is empty.

func (*EnvAny) UnmarshalJSON added in v0.2.0

func (ev *EnvAny) UnmarshalJSON(b []byte) error

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

func NewEnvBool(env string, value bool) EnvBool

NewEnvBool creates an EnvBool instance.

func NewEnvBoolValue

func NewEnvBoolValue(value bool) EnvBool

NewEnvBoolValue creates an EnvBool with a literal value.

func NewEnvBoolVariable

func NewEnvBoolVariable(name string) EnvBool

NewEnvBoolVariable creates an EnvBool with a variable name.

func (EnvBool) Equal added in v0.5.0

func (ev EnvBool) Equal(target EnvBool) bool

Equal checks if this instance equals the target value.

func (EnvBool) Get

func (ev EnvBool) Get() (bool, error)

Get gets literal value or from system environment.

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

func (ev EnvBool) GetOrDefault(defaultValue bool) (bool, error)

GetOrDefault returns the default value if the environment value is empty.

func (EnvBool) IsZero added in v0.3.0

func (ev EnvBool) IsZero() bool

IsZero checks if the instance is empty.

func (*EnvBool) UnmarshalJSON

func (ev *EnvBool) UnmarshalJSON(b []byte) error

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

func NewEnvFloat(env string, value float64) EnvFloat

NewEnvFloat creates an EnvFloat instance.

func NewEnvFloatValue

func NewEnvFloatValue(value float64) EnvFloat

NewEnvFloatValue creates an EnvFloat with a literal value.

func NewEnvFloatVariable

func NewEnvFloatVariable(name string) EnvFloat

NewEnvFloatVariable creates an EnvFloat with a variable name.

func (EnvFloat) Equal added in v0.5.0

func (ev EnvFloat) Equal(target EnvFloat) bool

Equal checks if this instance equals the target value.

func (EnvFloat) Get

func (ev EnvFloat) Get() (float64, error)

Get gets literal value or from system environment.

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

func (ev EnvFloat) GetOrDefault(defaultValue float64) (float64, error)

GetOrDefault returns the default value if the environment value is empty.

func (EnvFloat) IsZero added in v0.3.0

func (ev EnvFloat) IsZero() bool

IsZero checks if the instance is empty.

func (*EnvFloat) UnmarshalJSON

func (ev *EnvFloat) UnmarshalJSON(b []byte) error

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 NewEnvInt

func NewEnvInt(env string, value int64) EnvInt

NewEnvInt creates an EnvInt instance.

func NewEnvIntValue

func NewEnvIntValue(value int64) EnvInt

NewEnvIntValue creates an EnvInt with a literal value.

func NewEnvIntVariable

func NewEnvIntVariable(name string) EnvInt

NewEnvIntVariable creates an EnvInt with a variable name.

func (EnvInt) Equal added in v0.5.0

func (ev EnvInt) Equal(target EnvInt) bool

Equal checks if this instance equals the target value.

func (EnvInt) Get

func (ev EnvInt) Get() (int64, error)

Get gets literal value or from system environment.

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

func (ev EnvInt) GetOrDefault(defaultValue int64) (int64, error)

GetOrDefault returns the default value if the environment value is empty.

func (EnvInt) IsZero added in v0.3.0

func (ev EnvInt) IsZero() bool

IsZero checks if the instance is empty.

func (*EnvInt) UnmarshalJSON

func (ev *EnvInt) UnmarshalJSON(b []byte) error

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

func NewEnvMapInt(env string, value map[string]int64) EnvMapInt

NewEnvMapInt creates an EnvMapInt instance.

func NewEnvMapIntValue

func NewEnvMapIntValue(value map[string]int64) EnvMapInt

NewEnvMapIntValue creates an EnvMapInt with a literal value.

func NewEnvMapIntVariable

func NewEnvMapIntVariable(name string) EnvMapInt

NewEnvMapIntVariable creates an EnvMapInt with a variable name.

func (EnvMapInt) Equal added in v0.5.0

func (ev EnvMapInt) Equal(target EnvMapInt) bool

Equal checks if this instance equals the target value.

func (EnvMapInt) Get

func (ev EnvMapInt) Get() (map[string]int64, error)

Get gets literal value or from system environment.

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) IsZero added in v0.3.0

func (ev EnvMapInt) IsZero() bool

IsZero checks if the instance is empty.

func (*EnvMapInt) UnmarshalJSON

func (ev *EnvMapInt) UnmarshalJSON(b []byte) error

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

func NewEnvString(env string, value string) EnvString

NewEnvString creates an EnvString instance.

func NewEnvStringValue

func NewEnvStringValue(value string) EnvString

NewEnvStringValue creates an EnvString with a literal value.

func NewEnvStringVariable

func NewEnvStringVariable(name string) EnvString

NewEnvStringVariable creates an EnvString with a variable name.

func (EnvString) Equal added in v0.5.0

func (ev EnvString) Equal(target EnvString) bool

Equal checks if this instance equals the target value.

func (EnvString) Get

func (ev EnvString) Get() (string, error)

Get gets literal value or from system environment.

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

func (ev EnvString) GetOrDefault(defaultValue string) (string, error)

GetOrDefault returns the default value if the environment value is empty.

func (EnvString) IsZero added in v0.3.0

func (ev EnvString) IsZero() bool

IsZero checks if the instance is empty.

func (*EnvString) UnmarshalJSON

func (ev *EnvString) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type GetEnvFunc added in v0.4.0

type GetEnvFunc func(string) (string, error)

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.

Jump to

Keyboard shortcuts

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