Documentation
¶
Overview ¶
Package flinn provides a declarative, type-safe configuration loader for Go. It resolves values from environment variables and structured (JSON/TOML) files, with support for defaults, and validation.
Core Concepts ¶
- Loader: Orchestrates loading from multiple sources.
- Field: Definition for a single configuration value, created by String, Int, etc.
- Source: An interface for providing values from a structured source (e.g., a JSON file).
- Group: A collection of fields that share a namespace for file paths and env prefixes.
Values are resolved in the following order of precedence (highest to lowest):
- Environment variable (if enabled for the field or loader)
- Source file
- Default value (if set)
If one or more fields fail to resolve or validate, loading returns a FieldErrors collection so every problem is reported at once.
Index ¶
- type ConfigItem
- type Field
- type FieldError
- type FieldErrors
- type Group
- type Loader
- type LoaderOption
- type NumericField
- func (f *NumericField[T]) AddValidator(fn func(T) error) *NumericField[T]
- func (f *NumericField[T]) Default(v T) *NumericField[T]
- func (f *NumericField[T]) Env(key string) *NumericField[T]
- func (f *NumericField[T]) FileKey(key string) *NumericField[T]
- func (f *NumericField[T]) Max(v T) *NumericField[T]
- func (f *NumericField[T]) Min(v T) *NumericField[T]
- func (f *NumericField[T]) Required() *NumericField[T]
- type Source
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConfigItem ¶ added in v0.1.4
type ConfigItem interface {
// contains filtered or unexported methods
}
ConfigItem is a general node in a configuration schema. It is implemented by both Field and Group.
func DefineSchema ¶ added in v0.1.4
func DefineSchema(fields ...ConfigItem) []ConfigItem
DefineSchema groups configuration items into a slice. It is a syntactic sugar to create a slice of ConfigItem for Loader.Load.
type Field ¶
type Field[T comparable] struct { // contains filtered or unexported fields }
Field represents a single configuration leaf node that parses values into type T. It holds configuration for environment variable keys, file keys, default values, and validation rules.
func Bool ¶
Bool creates a Field that parses boolean values. It accepts the same formats as strconv.ParseBool (e.g., "true", "1", "false", "0").
func (*Field[T]) AddValidator ¶ added in v0.1.4
AddValidator adds a custom validation function to the field. Validators are run after the value is parsed and assigned to the destination.
func (*Field[T]) Default ¶ added in v0.1.4
Default is a Field option to set a default value for a field. This value will be used if other sources (env, file) do not provide a value.
func (*Field[T]) Env ¶ added in v0.1.4
Env is a Field option to set an environment variable name to load a value from.
func (*Field[T]) FileKey ¶ added in v0.1.4
FileKey overrides the key used when looking up this field in a config source. The default is the snake_case conversion of the field name.
type FieldError ¶
type FieldError struct {
Path string // dot-separated path, e.g., "database.port"
Rule string // "required", "parse", "resolve", "validate", "type", "min", "max"
Value any // the offending value, nil if absent
Msg string
}
FieldError is a single error for a specific configuration field.
type FieldErrors ¶
type FieldErrors []FieldError
FieldErrors is a collection of FieldError values. It implements the error interface and provides a formatted string of all collected errors.
func (FieldErrors) Error ¶
func (e FieldErrors) Error() string
Error returns a string representation of all collected field errors, one per line.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group represents a collection of configuration items under a named scope. It can be used to model nested structures in configuration files.
func FieldsGroup ¶ added in v0.1.4
func FieldsGroup(name string, children ...ConfigItem) *Group
FieldsGroup creates a new Group that wraps multiple configuration items under a named scope. It is used to represent nested configuration structures.
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader is the main object to configure and load configuration values.
func NewLoader ¶
func NewLoader(opts ...LoaderOption) *Loader
NewLoader returns a new Loader instance.
func (*Loader) Load ¶
func (l *Loader) Load(fields []ConfigItem) error
Load populates the configuration based on the provided fields. Each field is resolved sequentially, with environment variables taking precedence over other sources. It returns a FieldErrors collection if any errors occur.
type LoaderOption ¶
type LoaderOption func(*Loader)
LoaderOption is a function type used to configure a Loader.
func WithAutoEnv ¶
func WithAutoEnv() LoaderOption
WithAutoEnv enables automatic resolution of environment variables based on field names. If Env() is not explicitly called on a field, the environment variable name will be derived from the field's path (e.g., "DATABASE_PORT"). Without this option, only fields that have explicit Env() option will be loaded from environment variables.
func WithEnvPrefix ¶
func WithEnvPrefix(envPrefix string) LoaderOption
WithEnvPrefix sets a global prefix applied to all environment variable names, both auto-generated keys and explicit names set via Field.Env. The prefix is joined with [joinEnvPrefix].
func WithLogger ¶
func WithLogger(logger *slog.Logger) LoaderOption
WithLogger sets the logger used by the loader for debugging and warnings.
func WithSource ¶
func WithSource(source Source) LoaderOption
WithSource is a loader option that sets the configuration source (e.g., JSONSource) for the loader.
type NumericField ¶ added in v0.1.4
NumericField is a specialized Field for ordered types (integers, floats) that supports additional range-based validators like Min and Max.
func Float ¶
func Float(name string, dest *float64) *NumericField[float64]
Float creates a configuration Field that parses a floating-point value. It returns a NumericField, allowing for range-based validation (Min, Max).
func Int ¶
func Int(name string, dest *int) *NumericField[int]
Int creates a configuration leaf Field that parses string values as base-10 integers. It returns a NumericField, allowing for range-based validation (Min, Max).
func (*NumericField[T]) AddValidator ¶ added in v0.1.4
func (f *NumericField[T]) AddValidator(fn func(T) error) *NumericField[T]
AddValidator delegates to the embedded Field.AddValidator and returns f for chaining.
func (*NumericField[T]) Default ¶ added in v0.1.4
func (f *NumericField[T]) Default(v T) *NumericField[T]
Default delegates to the embedded Field.Default and returns f for chaining.
func (*NumericField[T]) Env ¶ added in v0.1.4
func (f *NumericField[T]) Env(key string) *NumericField[T]
Env delegates to the embedded Field.Env and returns f for chaining.
func (*NumericField[T]) FileKey ¶ added in v0.1.4
func (f *NumericField[T]) FileKey(key string) *NumericField[T]
FileKey delegates to the embedded Field.FileKey and returns f for chaining.
func (*NumericField[T]) Max ¶ added in v0.1.4
func (f *NumericField[T]) Max(v T) *NumericField[T]
Max adds a validator that ensures the field value is less than or equal to v.
func (*NumericField[T]) Min ¶ added in v0.1.4
func (f *NumericField[T]) Min(v T) *NumericField[T]
Min adds a validator that ensures the field value is greater than or equal to v.
func (*NumericField[T]) Required ¶ added in v0.1.4
func (f *NumericField[T]) Required() *NumericField[T]
Required delegates to the embedded Field.Required and returns f for chaining.
type Source ¶
type Source interface {
// Get retrieves a configuration value at the given path.
// path is a sequence of key segments corresponding to nested positions
// (e.g., ["database", "host"]).
// Returns the raw string value, true when found, or an error on retrieval failure.
// When the key is absent, return ("", false, nil).
Get(path []string) (string, bool, error)
}
Source is the interface for configuration backends. Implementations provide values from structured sources such as JSON or TOML files.
func NewJSONSource ¶
NewJSONSource reads and parses the JSON file at the given path. The root of the JSON document must be an object. Returns an error if the file cannot be read or is not valid JSON.