Documentation
¶
Overview ¶
Package fig provides configuration loading from environment variables and secret providers.
fig uses struct tags to declare configuration sources:
type Config struct {
Host string `env:"DB_HOST" default:"localhost"`
Port int `env:"DB_PORT" default:"5432"`
Password string `secret:"db/password"`
Timeout time.Duration `env:"TIMEOUT" default:"30s"`
Debug bool `env:"DEBUG"`
Tags []string `env:"TAGS"`
Name string `env:"NAME" required:"true"`
}
Resolution order: secret -> env -> default -> zero value.
Supported Types ¶
- string
- int, int8, int16, int32, int64
- uint, uint8, uint16, uint32, uint64
- float32, float64
- bool
- time.Duration
- []string (comma-separated)
- any type implementing encoding.TextUnmarshaler
Secret Providers ¶
Pass a secret provider when loading:
fig.Load(&cfg, vault.New(...))
Validation ¶
If the config struct implements Validator, Validate() is called after loading:
func (c *Config) Validate() error {
if c.Port <= 0 {
return errors.New("port must be positive")
}
return nil
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrRequired indicates a required field was not set. ErrRequired = errors.New("fig: required field not set") // ErrInvalidType indicates a value could not be converted to the target type. ErrInvalidType = errors.New("fig: invalid type conversion") // ErrNotStruct indicates Load was called with a non-struct type. ErrNotStruct = errors.New("fig: type must be a struct") // ErrSecretNotFound indicates a secret was not found in the provider. ErrSecretNotFound = errors.New("fig: secret not found") )
Sentinel errors for configuration loading.
Functions ¶
func Load ¶
func Load[T any](cfg *T, provider ...SecretProvider) error
Load populates a struct from environment variables, secrets, and defaults. An optional SecretProvider can be passed for secret tag resolution.
func LoadContext ¶
func LoadContext[T any](ctx context.Context, cfg *T, provider ...SecretProvider) error
LoadContext populates a struct with context support for secret provider timeouts. An optional SecretProvider can be passed for secret tag resolution.
Types ¶
type FieldError ¶
FieldError wraps an error with field context.
func (*FieldError) Error ¶
func (e *FieldError) Error() string
func (*FieldError) Unwrap ¶
func (e *FieldError) Unwrap() error