env

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

README

env

The env package loads strongly-typed configuration structs from environment variables, .env files, and JSON files. It supports stage-specific overrides, default values, custom parsers, nested structs, slices, maps, and aggregate error reporting.

Usage Examples

Basic Configuration
package main

import (
    "fmt"
    "log"
    "time"

    "github.com/deepnoodle-ai/wonton/env"
)

type Config struct {
    Host    string        `env:"HOST" envDefault:"localhost"`
    Port    int           `env:"PORT" envDefault:"8080"`
    APIKey  string        `env:"API_KEY,required"`
    Debug   bool          `env:"DEBUG" envDefault:"false"`
    Timeout time.Duration `env:"TIMEOUT" envDefault:"30s"`
}

func main() {
    cfg, err := env.Parse[Config]()
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Server: %s:%d\n", cfg.Host, cfg.Port)
    fmt.Printf("Timeout: %v\n", cfg.Timeout)
}
Loading from .env Files
cfg, err := env.Parse[Config](
    env.WithEnvFile(".env", ".env.local"),
)
if err != nil {
    log.Fatal(err)
}
Stage-Based Configuration
// With stage "PROD", looks for PROD_PORT before PORT
cfg, err := env.Parse[Config](
    env.WithStage("PROD"),
    env.WithEnvFile(".env"),
)
Prefix Support
// All vars prefixed with MYAPP_ (e.g., MYAPP_HOST, MYAPP_PORT)
cfg, err := env.Parse[Config](
    env.WithPrefix("MYAPP"),
)
JSON Configuration Files
type Config struct {
    Host string `json:"host" env:"HOST" envDefault:"localhost"`
    Port int    `json:"port" env:"PORT" envDefault:"8080"`
}

cfg, err := env.Parse[Config](
    env.WithJSONFile("config.json", "config.local.json"),
)
// Environment variables override JSON values
Nested Structs
type Database struct {
    Host     string `env:"HOST" envDefault:"localhost"`
    Port     int    `env:"PORT" envDefault:"5432"`
    Username string `env:"USER,required"`
    Password string `env:"PASSWORD,required,unset"`
}

type Config struct {
    DB Database `envPrefix:"DB_"`
}

// Expects: DB_HOST, DB_PORT, DB_USER, DB_PASSWORD
cfg, err := env.Parse[Config]()
Slices and Maps
type Config struct {
    // Comma-separated: "host1,host2,host3"
    Hosts []string `env:"HOSTS" envSeparator:","`

    // Colon-separated key:value pairs: "key1:val1,key2:val2"
    Labels map[string]string `env:"LABELS"`

    // Custom separators
    Ports []int `env:"PORTS" envSeparator:";"`
}
Custom Parsers
type IPAddr struct{ net.IP }

cfg, err := env.Parse[Config](
    env.WithParser(func(s string) (IPAddr, error) {
        ip := net.ParseIP(s)
        if ip == nil {
            return IPAddr{}, fmt.Errorf("invalid IP: %s", s)
        }
        return IPAddr{ip}, nil
    }),
)
Field Change Notifications
cfg, err := env.Parse[Config](
    env.WithOnSet(func(fieldName, envVar string, value any, isDefault bool) {
        if isDefault {
            fmt.Printf("%s: using default value\n", fieldName)
        } else {
            fmt.Printf("%s: loaded from %s\n", fieldName, envVar)
        }
    }),
)
Loading File Contents
type Config struct {
    // Reads the file path from API_KEY_FILE, then loads file contents
    APIKey string `env:"API_KEY_FILE,file,required"`
}
Variable Expansion
type Config struct {
    // Expands $HOME and other env vars in the value
    LogPath string `env:"LOG_PATH,expand" envDefault:"$HOME/logs/app.log"`
}

API Reference

Configuration Functions
Function Description Returns
Parse[T](opts...) Parses environment into type T (T, error)
Must[T](opts...) Like Parse but panics on error T
ParseInto(v any, opts...) Parses into existing struct pointer error
Options
Function Description
WithPrefix(prefix) Prepends prefix to all env var names
WithStage(stage) Enables stage-based variable resolution
WithEnvFile(files...) Loads .env files in order
WithJSONFile(files...) Loads JSON config files in order
WithEnvironment(map) Uses custom env map instead of os.Environ()
WithTagName(env, default) Changes struct tag names
WithRequiredIfNoDefault() Makes fields without defaults required
WithUseFieldName() Uses field names as env vars when no tag present
WithParser[T](parser) Adds custom parser for type T
WithOnSet(fn) Calls fn when fields are set
WithRequireConfigFile() Errors if no .env or JSON file loaded
Struct Tags
Tag Description Example
env:"VAR" Environment variable name env:"PORT"
env:"VAR,required" Field must be set env:"API_KEY,required"
env:"VAR,notEmpty" Field must be set and non-empty env:"NAME,notEmpty"
env:"VAR,file" Value is file path; read file contents env:"KEY_FILE,file"
env:"VAR,expand" Expand $VAR references in value env:"PATH,expand"
env:"VAR,unset" Unset env var after reading (for secrets) env:"PASSWORD,unset"
envDefault:"value" Default value if not set envDefault:"8080"
envPrefix:"PREFIX_" Prefix for nested struct fields envPrefix:"DB_"
envSeparator:"," Separator for slice/map parsing envSeparator:";"
envKeyValSeparator:":" Separator for map key:value pairs envKeyValSeparator:"="
.env File Functions
Function Description Returns
LoadEnvFile(files...) Loads .env files into os environment error
OverloadEnvFile(files...) Loads .env files, overwriting existing vars error
ReadEnvFile(filename) Reads .env file into map (map[string]string, error)
ParseEnvReader(r) Parses .env format from reader (map[string]string, error)
ParseEnvString(s) Parses .env format string (map[string]string, error)
WriteEnvFile(map, filename) Writes map to .env file (default perms) error
WriteEnvFileWithPerm(map, filename, perm) Writes map to .env file with permissions error
Supported Types
  • Basic types: string, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
  • Time types: time.Duration, time.Location
  • Collections: []T (slices), map[K]V (maps)
  • Any type implementing encoding.TextUnmarshaler
  • Pointer types: *T for any supported type T
Error Types
Type Description
ParseError General parsing error
AggregateError Multiple field errors collected together
FieldError Error parsing a specific field
VarNotSetError Required variable not set
EmptyVarError Variable set but empty when notEmpty required
FileLoadError Error loading file when file option used

.env File Format

The package supports standard .env file syntax:

# Comments start with # or //
HOST=localhost
PORT=8080

# Quoted values
DATABASE_URL="postgres://localhost/mydb"

# Variable expansion (with expand tag)
LOG_PATH=$HOME/logs/app.log

# Export syntax
export API_KEY=secret123

# Empty values
OPTIONAL_FIELD=

# Newlines via escape sequences (double-quoted only)
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA...\n-----END RSA PRIVATE KEY-----"
  • cli - CLI framework that uses env for configuration loading
  • terminal - Terminal utilities for interactive configuration

Implementation Notes

Configuration Precedence (lowest to highest)
  1. envDefault tags - Default values specified in struct tags
  2. JSON files - Unmarshaled directly into struct (later files override earlier ones)
  3. .env files - Merged together (later files override earlier ones)
  4. Environment variables - Process env vars (or custom map via WithEnvironment)

This means environment variables always win, followed by .env files, then JSON, then defaults.

JSON and Zero Values

When using JSON files with envDefault, explicit zero values in JSON (0, false, "") cannot be reliably distinguished from "not set". The parser treats zero values as "not set" and applies the default. To work around this:

  • Use pointer types (*int, *bool) where nil means "not set" and zero is explicit
  • Avoid envDefault for fields where zero is a meaningful value from JSON
  • Use environment variables to override JSON when you need explicit zeros
Other Notes
  • Missing config files are silently skipped unless WithRequireConfigFile() is used
  • Nested structs can use envPrefix tag or auto-generate PREFIX_FIELDNAME_ format
  • Aggregate errors provide detailed information about all parsing failures

Documentation

Overview

Package env provides flexible configuration loading from environment variables, .env files, and JSON files. It supports struct tags for automatic binding, variable prefixes, stage-based configuration, and aggregate error handling.

The package is designed for both simple and complex configuration scenarios, with support for nested structs, slices, maps, custom parsers, and validation. All parsing errors are collected and reported together for better debugging.

Basic usage:

type Config struct {
    Host string `env:"HOST" envDefault:"localhost"`
    Port int    `env:"PORT" envDefault:"8080"`
}

cfg, err := env.Parse[Config]()
if err != nil {
    log.Fatal(err)
}

With options:

cfg, err := env.Parse[Config](
    env.WithPrefix("MYAPP"),
    env.WithEnvFile(".env"),
    env.WithJSONFile("config.json"),
)

Supported struct tag options:

  • env:"VAR" - environment variable name
  • env:"VAR,required" - field must be set
  • env:"VAR,notEmpty" - field must be non-empty
  • env:"VAR,file" - value is a file path, load file contents
  • env:"VAR,expand" - expand $VAR references in value
  • env:"VAR,unset" - unset env var after reading (useful for secrets)
  • envDefault:"value" - default value if not set
  • envPrefix:"PREFIX_" - prefix for nested struct fields
  • envSeparator:"," - separator for slice/map parsing
  • envKeyValSeparator:":" - separator for map key:value pairs

The package supports all basic Go types (string, bool, int, uint, float), time.Duration, time.Location, slices, maps, pointers, and any type implementing encoding.TextUnmarshaler.

Package env provides .env file parsing and manipulation. This file contains functions for reading, parsing, and writing .env format files.

Example

Example demonstrates basic configuration parsing with default values.

type Config struct {
	Host string `env:"HOST" envDefault:"localhost"`
	Port int    `env:"PORT" envDefault:"8080"`
}

// Parse with defaults (no environment variables set)
cfg, err := Parse[Config](WithEnvironment(map[string]string{}))
if err != nil {
	fmt.Println("Error:", err)
	return
}

fmt.Printf("Host: %s, Port: %d\n", cfg.Host, cfg.Port)
Output:

Host: localhost, Port: 8080
Example (CustomParser)

Example_customParser demonstrates using a custom parser for a user-defined type.

type LogLevel int
const (
	Debug LogLevel = iota
	Info
	Warn
	Error
)

type Config struct {
	Level LogLevel `env:"LOG_LEVEL"`
}

env := map[string]string{
	"LOG_LEVEL": "warn",
}

cfg, err := Parse[Config](
	WithEnvironment(env),
	WithParser(func(s string) (LogLevel, error) {
		switch strings.ToLower(s) {
		case "debug":
			return Debug, nil
		case "info":
			return Info, nil
		case "warn":
			return Warn, nil
		case "error":
			return Error, nil
		default:
			return 0, fmt.Errorf("invalid log level: %s", s)
		}
	}),
)
if err != nil {
	fmt.Println("Error:", err)
	return
}

fmt.Printf("Log Level: %d (Warn)\n", cfg.Level)
Output:

Log Level: 2 (Warn)
Example (NestedStructs)

Example_nestedStructs demonstrates configuration with nested structs.

type Database struct {
	Host string `env:"HOST"`
	Port int    `env:"PORT"`
}

type Config struct {
	Database Database `envPrefix:"DB_"`
}

env := map[string]string{
	"DB_HOST": "postgres.example.com",
	"DB_PORT": "5432",
}

cfg, err := Parse[Config](WithEnvironment(env))
if err != nil {
	fmt.Println("Error:", err)
	return
}

fmt.Printf("Database: %s:%d\n", cfg.Database.Host, cfg.Database.Port)
Output:

Database: postgres.example.com:5432
Example (SlicesAndMaps)

Example_slicesAndMaps demonstrates parsing slices and maps from environment variables.

type Config struct {
	Hosts  []string          `env:"HOSTS"`
	Labels map[string]string `env:"LABELS"`
}

env := map[string]string{
	"HOSTS":  "host1,host2,host3",
	"LABELS": "env:prod,region:us-west",
}

cfg, err := Parse[Config](WithEnvironment(env))
if err != nil {
	fmt.Println("Error:", err)
	return
}

fmt.Printf("Hosts: %v\n", cfg.Hosts)
fmt.Printf("Labels: %v\n", cfg.Labels)
Output:

Hosts: [host1 host2 host3]
Labels: map[env:prod region:us-west]
Example (WithPrefix)

Example_withPrefix demonstrates using a prefix for all environment variables.

type Config struct {
	Host string `env:"HOST"`
	Port int    `env:"PORT"`
}

env := map[string]string{
	"MYAPP_HOST": "api.example.com",
	"MYAPP_PORT": "443",
}

cfg, err := Parse[Config](
	WithEnvironment(env),
	WithPrefix("MYAPP"),
)
if err != nil {
	fmt.Println("Error:", err)
	return
}

fmt.Printf("Host: %s, Port: %d\n", cfg.Host, cfg.Port)
Output:

Host: api.example.com, Port: 443

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetErrors

func GetErrors[T error](err error) []T

GetErrors extracts all errors of a specific type from an AggregateError. Returns an empty slice if the error is not an AggregateError or if no matching errors are found.

Example:

varErrors := env.GetErrors[*env.VarNotSetError](err)
for _, e := range varErrors {
    fmt.Printf("Missing: %s\n", e.EnvVar)
}

func HasError

func HasError[T error](err error) bool

HasError checks if an error contains a specific error type using errors.As. It's useful for checking if a particular error type exists in an error chain or within an AggregateError.

Example:

if env.HasError[*env.VarNotSetError](err) {
    fmt.Println("Missing required variables")
}

func LoadEnvFile

func LoadEnvFile(filenames ...string) error

LoadEnvFile loads environment variables from .env files into os.Environ(). Variables that already exist in the environment are NOT overwritten. Use OverloadEnvFile to override existing values.

If no filenames are provided, it defaults to loading ".env" from the current directory. Missing files return an error.

Example:

err := env.LoadEnvFile(".env", ".env.local")
if err != nil {
    log.Fatal(err)
}

func Must

func Must[T any](opts ...Option) T

Must wraps Parse and panics on error. Useful for initialization where configuration errors should be fatal.

Example:

var cfg = env.Must[Config](
    env.WithEnvFile(".env"),
)

func OverloadEnvFile

func OverloadEnvFile(filenames ...string) error

OverloadEnvFile loads environment variables from .env files, overwriting any existing values in os.Environ().

If no filenames are provided, it defaults to loading ".env" from the current directory. Missing files return an error.

Example:

err := env.OverloadEnvFile(".env.production")
if err != nil {
    log.Fatal(err)
}

func Parse

func Parse[T any](opts ...Option) (T, error)

Parse parses environment variables into a struct of type T and returns the populated struct. This is the primary entry point for configuration loading.

The function loads configuration in the following order (later sources override earlier):

  1. envDefault struct tags (lowest priority)
  2. JSON files (if specified with WithJSONFile)
  3. .env files (if specified with WithEnvFile)
  4. Environment variables (highest priority, always take precedence)

Example:

type Config struct {
    Host string `env:"HOST" envDefault:"localhost"`
    Port int    `env:"PORT" envDefault:"8080"`
}

cfg, err := env.Parse[Config](
    env.WithEnvFile(".env"),
    env.WithPrefix("MYAPP"),
)

func ParseEnvReader

func ParseEnvReader(r io.Reader) (map[string]string, error)

ParseEnvReader parses .env format from a reader and returns a map of key-value pairs. Supports standard .env syntax including comments, quoted values, export statements, and both = and : separators.

Example:

file, _ := os.Open("config.env")
defer file.Close()
envVars, err := env.ParseEnvReader(file)

func ParseEnvString

func ParseEnvString(s string) (map[string]string, error)

ParseEnvString parses a .env format string and returns a map of key-value pairs. Useful for parsing inline .env configuration.

Example:

envVars, err := env.ParseEnvString(`
    HOST=localhost
    PORT=8080
    DEBUG=true
`)
Example

ExampleParseEnvString demonstrates parsing .env format from a string.

envData := `
# Database configuration
HOST=localhost
PORT=5432
DB_NAME=myapp

# API Keys
API_KEY="secret-key-here"
`

envVars, err := ParseEnvString(envData)
if err != nil {
	fmt.Println("Error:", err)
	return
}

fmt.Println("HOST:", envVars["HOST"])
fmt.Println("PORT:", envVars["PORT"])
fmt.Println("DB_NAME:", envVars["DB_NAME"])
fmt.Println("API_KEY:", envVars["API_KEY"])
Output:

HOST: localhost
PORT: 5432
DB_NAME: myapp
API_KEY: secret-key-here

func ParseInto

func ParseInto(v any, opts ...Option) error

ParseInto parses environment variables into an existing struct pointer. Unlike Parse, this modifies an existing struct in place, which is useful when you need to preserve unexported fields or embed configuration in a larger struct.

Example:

cfg := Config{internalField: "preserved"}
err := env.ParseInto(&cfg, env.WithEnvFile(".env"))

func ReadEnvFile

func ReadEnvFile(filename string) (map[string]string, error)

ReadEnvFile reads a .env file and returns a map of key-value pairs. Does not modify the environment. Returns an error if the file cannot be opened.

Example:

envVars, err := env.ReadEnvFile(".env")
if err != nil {
    log.Fatal(err)
}
fmt.Println(envVars["DATABASE_URL"])

func WriteEnvFile

func WriteEnvFile(envMap map[string]string, filename string) error

WriteEnvFile writes a map of environment variables to a .env file. Values are automatically quoted when necessary (spaces, special characters, etc.). Escape sequences are applied to quoted values. Uses default file permissions (0666 before umask). For sensitive data, use WriteEnvFileWithPerm with restrictive permissions like 0600.

Example:

envVars := map[string]string{
    "HOST": "localhost",
    "PORT": "8080",
    "MESSAGE": "Hello, World!",
}
err := env.WriteEnvFile(envVars, ".env.output")
Example

ExampleWriteEnvFile demonstrates writing environment variables to a .env file.

envVars := map[string]string{
	"HOST":    "localhost",
	"PORT":    "8080",
	"DEBUG":   "true",
	"MESSAGE": "Hello, World!",
}

// In a real application, you would write to an actual file
// For this example, we'll just demonstrate the function signature
_ = WriteEnvFile(envVars, "/tmp/example.env")

fmt.Println("Environment variables written to file")
Output:

Environment variables written to file

func WriteEnvFileWithPerm added in v0.0.2

func WriteEnvFileWithPerm(envMap map[string]string, filename string, perm os.FileMode) error

WriteEnvFileWithPerm writes a map of environment variables to a .env file with the specified file permissions. Use this for sensitive configuration files that should have restrictive permissions.

Example:

envVars := map[string]string{
    "API_KEY": "secret123",
}
// Only owner can read/write
err := env.WriteEnvFileWithPerm(envVars, ".env.secrets", 0600)

Types

type AggregateError

type AggregateError struct {
	Errors []error
}

AggregateError collects multiple parsing errors that occur during configuration parsing. It implements the error interface and provides unwrapping support for errors.Is and errors.As. When multiple fields fail to parse, all errors are collected and reported together.

func (*AggregateError) Error

func (e *AggregateError) Error() string

func (*AggregateError) Is

func (e *AggregateError) Is(target error) bool

Is implements errors.Is support.

func (*AggregateError) Unwrap

func (e *AggregateError) Unwrap() []error

Unwrap returns the list of errors for errors.Is/As support.

type EmptyVarError

type EmptyVarError struct {
	Field  string // Name of the struct field
	EnvVar string // Environment variable that was empty
}

EmptyVarError indicates an environment variable is set but empty when a non-empty value is required. This occurs when a field is marked with the "notEmpty" tag option.

func (*EmptyVarError) Error

func (e *EmptyVarError) Error() string

type FieldError

type FieldError struct {
	Field  string // Name of the struct field
	EnvVar string // Environment variable name
	Value  string // Value that failed to parse
	Err    error  // Underlying error
}

FieldError represents an error parsing a specific field in the configuration struct. It includes the field name, environment variable name, attempted value, and underlying error.

func (*FieldError) Error

func (e *FieldError) Error() string

func (*FieldError) Unwrap

func (e *FieldError) Unwrap() error

type FileLoadError

type FileLoadError struct {
	Field    string // Name of the struct field
	EnvVar   string // Environment variable containing the file path
	Filename string // Path to the file that failed to load
	Err      error  // Underlying error from file read operation
}

FileLoadError indicates an error loading content from a file. This occurs when a field is marked with the "file" tag option and the file path cannot be read.

func (*FileLoadError) Error

func (e *FileLoadError) Error() string

func (*FileLoadError) Unwrap

func (e *FileLoadError) Unwrap() error

type OnSetFunc

type OnSetFunc func(fieldName, envVar string, value any, isDefault bool)

OnSetFunc is a callback function that is invoked when a field value is set during parsing. It receives the field name, environment variable name, parsed value, and whether the value came from a default.

type Option

type Option func(*Options)

Option is a functional option for configuring the parsing behavior. Use the With* functions to create options for Parse and ParseInto.

func WithEnvFile

func WithEnvFile(files ...string) Option

WithEnvFile adds .env files to load (in order, later overrides earlier).

func WithEnvironment

func WithEnvironment(env map[string]string) Option

WithEnvironment uses a custom environment map instead of os.Environ().

func WithJSONFile

func WithJSONFile(files ...string) Option

WithJSONFile adds JSON config files to load (in order, later overrides earlier).

func WithOnSet

func WithOnSet(fn OnSetFunc) Option

WithOnSet registers a callback that is called whenever a field value is set during parsing. This is useful for logging, debugging, or tracking which configuration values came from defaults vs environment variables.

The callback receives:

  • fieldName: the struct field name
  • envVar: the full environment variable name (with prefix/stage if applicable)
  • value: the parsed value that was set
  • isDefault: true if the value came from envDefault tag, false if from environment

Example:

cfg, err := env.Parse[Config](
    env.WithOnSet(func(field, envVar string, value any, isDefault bool) {
        if isDefault {
            log.Printf("%s using default value", field)
        } else {
            log.Printf("%s loaded from %s", field, envVar)
        }
    }),
)

func WithParser

func WithParser[T any](parser func(string) (T, error)) Option

WithParser adds a custom parser for a specific type T. This allows you to define how to parse environment variable strings into custom types.

Example:

type IPAddr struct{ net.IP }
cfg, err := env.Parse[Config](
    env.WithParser(func(s string) (IPAddr, error) {
        ip := net.ParseIP(s)
        if ip == nil {
            return IPAddr{}, fmt.Errorf("invalid IP")
        }
        return IPAddr{ip}, nil
    }),
)

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets the environment variable prefix.

func WithRequireConfigFile added in v0.0.2

func WithRequireConfigFile() Option

WithRequireConfigFile causes parsing to fail if no config file is loaded. Use this when your application requires configuration from at least one .env or JSON file, but doesn't care which specific file provides it.

func WithRequiredIfNoDefault

func WithRequiredIfNoDefault() Option

WithRequiredIfNoDefault makes fields without defaults required.

func WithStage

func WithStage(stage string) Option

WithStage enables stage-based variable resolution. When set, the parser looks for stage-prefixed variables before falling back to the base variable.

This is useful for multi-environment configurations where some variables differ per environment while others remain the same.

Example: WithStage("PROD") causes PORT to look for PROD_PORT first, then PORT.

func WithTagName

func WithTagName(envTag, defaultTag string) Option

WithTagName sets custom tag names.

func WithUseFieldName

func WithUseFieldName() Option

WithUseFieldName uses field names as env var names when no tag is present.

type Options

type Options struct {
	// Environment is a custom map of environment variables.
	// If nil, os.Environ() is used.
	Environment map[string]string

	// Prefix is prepended to all env var lookups.
	// Example: Prefix="MYAPP" means HOST becomes MYAPP_HOST
	Prefix string

	// Stage enables stage-based variable resolution.
	// If set, looks for STAGE_VARNAME before VARNAME.
	// Example: Stage="PROD" means PORT looks for PROD_PORT first, then PORT.
	Stage string

	// EnvFiles is a list of .env files to load.
	// Later files override earlier ones.
	EnvFiles []string

	// JSONFiles is a list of JSON config files to load.
	// Later files override earlier ones.
	JSONFiles []string

	// TagName is the struct tag to use for env var names (default: "env").
	TagName string

	// DefaultTagName is the tag for default values (default: "default").
	DefaultTagName string

	// RequiredIfNoDefault makes fields without defaults required.
	RequiredIfNoDefault bool

	// UseFieldNameByDefault uses the field name (converted to UPPER_SNAKE_CASE)
	// if no env tag is specified.
	UseFieldNameByDefault bool

	// FuncMap provides custom parsers for specific types.
	FuncMap map[reflect.Type]ParserFunc

	// OnSet is called whenever a field value is set.
	OnSet OnSetFunc

	// RequireConfigFile causes an error if no config file (.env or JSON) is loaded.
	// By default, missing config files are silently skipped.
	RequireConfigFile bool
}

Options configures the parsing behavior for Parse and ParseInto. Options are typically set using functional option helpers like WithPrefix, WithEnvFile, etc., rather than by constructing this struct directly.

type ParseError

type ParseError struct {
	Err error
}

ParseError represents a general parsing error that occurs during configuration loading. It wraps underlying errors for additional context.

func (*ParseError) Error

func (e *ParseError) Error() string

func (*ParseError) Unwrap

func (e *ParseError) Unwrap() error

type ParserFunc

type ParserFunc func(value string) (any, error)

ParserFunc is a function that parses a string value into a typed value. It is used with WithParser to provide custom parsing logic for specific types.

type VarNotSetError

type VarNotSetError struct {
	Field  string // Name of the struct field
	EnvVar string // Environment variable that was not set
}

VarNotSetError indicates a required environment variable is not set. This occurs when a field is marked with the "required" tag option or when WithRequiredIfNoDefault is used and no default value is provided.

func (*VarNotSetError) Error

func (e *VarNotSetError) Error() string

Jump to

Keyboard shortcuts

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