goloadenv

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2025 License: MIT Imports: 6 Imported by: 0

README

structload

Generic struct loading. Applications include loading of environment variables into structs for program configuration.

tests MIT license Go.Dev reference Go Report Card

Features

  • Struct loading from environment variables
  • Default and optional configuration fields
  • Nested configuration structs
  • Array and list parsing
  • Extensible type parsing

Examples

See https://pkg.go.dev/github.com/munisense/goloadenv for some examples on how to use this library.

License

Released under the MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatString

func FormatString(config interface{}) string

TODO maybe an extra tag that ensures a field is not printed, handy for passwords for example

func LoadEnv

func LoadEnv(config interface{}) error

LoadEnv loads environment variables into the provided config struct. It uses the "env" struct tag to determine which environment variable corresponds to each field. If an environment variable is not found, and it does not have a default value provided in the tag, it returns an error.

Example:

type DBConfig struct {
  Host     string `env:"DB_HOST"`
  Port     int    `env:"DB_PORT"`
  User     string `env:"DB_USER;default:user"`
  Password string `env:"DB_PASSWORD;default:password"`
}

type Config struct {
  Port     float64 `env:"PORT;default:8080"`
  LogLevel string  `env:"LOG_LEVEL;optional"`
  DB       DBConfig
}

func LoadConfig() error {
  cfg := Config{}
  err := config.LoadEnv(&cfg)
  if err != nil {
    return err
  }
  fmt.Printf("Config: %+v\n", cfg)
  return nil
}

func main() {
  err := godotenv.Load(".env")
  if err != nil {
    	return cfg, fmt.Errorf("could not load environment files: %w", err)
  }
  err := LoadConfig()
  if err != nil {
    fmt.Printf("Error loading config: %v\n", err)
    return
  }
}

TODO: allow for format string defaults, function return defaults?

func RegisterEnvType

func RegisterEnvType[T EnvTypeInterface]()

func SetOracle added in v1.0.0

func SetOracle(oracleFunc func(string) string)

func UnmarshalEnvSlogLevel

func UnmarshalEnvSlogLevel(string string) (interface{}, error)

Types

type EnvNotFoundError

type EnvNotFoundError struct {
	Env string
}

EnvNotFoundError represents an error when an expected environment variable is not found.

func (*EnvNotFoundError) Error

func (e *EnvNotFoundError) Error() string

Error returns a string representation of the EnvNotFoundError.

type EnvParseError

type EnvParseError struct {
	// contains filtered or unexported fields
}

func (*EnvParseError) Error

func (e *EnvParseError) Error() string

type EnvType

type EnvType func(string) (interface{}, error)

type EnvTypeInterface

type EnvTypeInterface interface {
	UnmarshalEnv(string) (interface{}, error)
}

Jump to

Keyboard shortcuts

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