config

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package config loads layered JSON configuration and defines the contract a configuration type implements to take part in that load.

The contract

A configuration is a type T whose pointer implements Config: a Merge that overlays another instance's set fields onto the receiver, and a Finalize that applies defaults, reads environment-variable overrides, and validates. The methods stay concretely typed against T — an implementation writes Merge(*T) and Finalize() error against its own type, with no type assertions.

Layered load

Load reads up to four files from a directory, in a fixed precedence, and merges each one that exists onto a zero value of T:

  • a base file (config.json),
  • an environment overlay (config.<env>.json),
  • a secrets file (secrets.json), and
  • a secrets overlay (secrets.<env>.json).

The active environment is the value of the Options.EnvVar variable; when it is empty, both overlays are skipped. A single Options.OverlayPattern produces both overlay names from the base and secrets stems. Every file is optional: a missing file is skipped, so a load with no files present yields a configuration carrying only what Finalize supplies. Any other read error, or malformed JSON, stops the load.

Later sources win: the secrets file overrides the overlay, which overrides the base. Merging onto a zero value is uniform because a set source field always wins over an unset receiver, so the base file is not a special case. Finalize runs once, after every file has been merged, so the environment overrides it reads take precedence over every file.

Ephemeral lifecycle

A configuration exists to initialize subsystems, not to be retained. A caller loads it, constructs subsystems from the values it carries, and discards it; runtime code holds the values it needs, not the configuration.

Environment-variable names

EnvName composes an environment-variable name from a prefix and parts. A capability pairs its configuration with an Env struct holding the variable names its Finalize reads, built with EnvName so a caller can vary the prefix without the capability hard-coding one.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnvName

func EnvName(prefix string, parts ...string) string

func Load

func Load[T any, PT Config[T]](opts Options) (PT, error)

Types

type Config

type Config[T any] interface {
	*T
	Merge(src *T)
	Finalize() error
}

type Duration

type Duration time.Duration

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

func (*Duration) Set

func (d *Duration) Set(value string) error

func (Duration) String

func (d Duration) String() string

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(data []byte) error

type Options

type Options struct {
	Dir            string
	EnvVar         string
	BaseName       string
	SecretsName    string
	OverlayPattern string
}

Jump to

Keyboard shortcuts

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