config

package
v4.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package config provides helpers for populating configuration structs from environment variables, .env files, and JSON files. It builds on the caarlos0/env struct-tag conventions (`env:`, `envPrefix:`, `envDefault:`, ...) already used throughout platform-go's per-package config subpackages, giving callers a single, application-agnostic place to mount those tags onto their config values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyEnvironmentVariables

func ApplyEnvironmentVariables(cfg any, opts ...Option) error

ApplyEnvironmentVariables populates cfg (a non-nil pointer to a struct) from environment variables using the caarlos0/env struct tags, following that library's standard semantics:

  • A field whose env var is set is assigned that value, overriding any value the field already held (e.g. one decoded from a file).
  • A field with an envDefault whose env var is unset is assigned the default, which likewise overrides any pre-existing value. Because of this, when layering env vars on top of a decoded config, a field carrying an envDefault always ends up at either its env value or its default — never a value that came only from the file.
  • A field with no env var and no envDefault is left untouched.

func LoadFromDotEnvFile

func LoadFromDotEnvFile[T any](path string, opts ...Option) (*T, error)

LoadFromDotEnvFile loads the .env file at path into the process environment and then builds a *T from the environment. godotenv does not override variables already present in the process, so real environment values still take precedence over values in the file.

func LoadFromEnvironment

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

LoadFromEnvironment builds a *T populated entirely from environment variables.

func LoadFromJSONFile

func LoadFromJSONFile[T any](path string, opts ...Option) (*T, error)

LoadFromJSONFile decodes the JSON file at path into a *T, then overlays environment variables on top of it via ApplyEnvironmentVariables. A set env var takes precedence over the file value. Note the caarlos0/env caveat documented on ApplyEnvironmentVariables: a field carrying an envDefault whose env var is unset is reset to that default even if the file supplied a value, so give such fields their env var (or no envDefault) when the file should win.

func LoadFromTOMLFile

func LoadFromTOMLFile[T any](path string, opts ...Option) (*T, error)

LoadFromTOMLFile behaves like LoadFromJSONFile but decodes a TOML file (via BurntSushi/toml). TOML keys map to struct fields by their `toml:` tag, falling back to a case-insensitive field-name match.

func LoadFromYAMLFile

func LoadFromYAMLFile[T any](path string, opts ...Option) (*T, error)

LoadFromYAMLFile behaves like LoadFromJSONFile but decodes a YAML file (via gopkg.in/yaml.v3). YAML keys map to struct fields by their `yaml:` tag, falling back to the lower-cased field name.

func ResolveDotEnvPath

func ResolveDotEnvPath(baseDir, filename string) (string, error)

ResolveDotEnvPath joins baseDir and filename and returns the result if that file exists. A missing file yields "" (and a nil error) so callers can treat "no .env present" as "skip loading" rather than a failure; any other stat error is returned.

func Validate

func Validate(ctx context.Context, cfg any) error

Validate runs cfg's context-aware validation if cfg implements ozzo-validation's ValidatableWithContext; otherwise it is a no-op. It is a convenience for validating a freshly loaded config, particularly one built solely from the environment where there is no file baseline to fall back on.

Types

type OnSetFunc

type OnSetFunc func(tag string, value any, isDefault bool)

OnSetFunc is invoked for each field the parser populates from the environment. It mirrors caarlos0/env's OnSet hook and is handy for debug logging which variables were applied. Wire it to any logger via WithOnSet.

type Option

type Option func(*options)

Option configures how environment variables are applied.

func WithOnSet

func WithOnSet(fn OnSetFunc) Option

WithOnSet registers a hook invoked for each field populated from the environment. Passing nil is a no-op.

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets a prefix prepended to every env var key the parser reads (e.g. "MYAPP_"). Nested envPrefix struct tags are appended after it.

Jump to

Keyboard shortcuts

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