goenvconf

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: Apache-2.0 Imports: 10 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

View Source
const (
	// ErrCodeParseEnvFailed is the error code when parsing environment variable failed.
	ErrCodeParseEnvFailed = "ParseEnvFailed"
)

Variables

View Source
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 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 ParseBoolSliceFromString added in v0.6.0

func ParseBoolSliceFromString(input string) ([]bool, error)

ParseBoolSliceFromString parses a boolean slice from a comma-separated string.

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 ParseFloatSliceFromString added in v0.6.0

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

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

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

ParseStringMapFromString parses a string map from a string with format:

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

func ParseStringSliceFromString added in v0.6.0

func ParseStringSliceFromString(input string) []string

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 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.

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

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.

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

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.

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 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.

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

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.

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

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.

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

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.

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.

Jump to

Keyboard shortcuts

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