transform

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package transform provides composable, post-merge / pre-decode transformations on the merged configuration tree.

FastConf's reload pipeline is conceptually:

discover → decode → merge/patch → [TRANSFORMERS] → decodeInto(*T)
                                                  → validate → publish

A Transformer is a pure function `func(map[string]any) error` that may mutate the in-place merged map before it is decoded into the user's strongly-typed struct. Built-in transformers cover the most common cases (defaults, env interpolation, key aliasing, deletion). Users can also write their own.

Transformers run in declaration order and are wired via `fastconf.WithTransformers(...)`. Failures abort the reload and the previously committed state is preserved (same guarantee as every other stage).

Design notes:

  • Transformers operate on `map[string]any` rather than `*T` so they remain decoupled from the user type and can be reused across multiple Manager[T] instances.
  • Path syntax used by helpers below is dotted: "a.b.c". Numeric indices into slices are NOT supported (config trees are usually small maps; complex array surgery belongs in RFC 6902 patches).
  • All helpers tolerate a nil root map (treated as empty); they never panic on missing intermediate nodes.

Index

Constants

This section is empty.

Variables

View Source
var ErrTransform = errors.New("fastconf/internal/transform")

ErrTransform is returned wrapped by built-in transformers on failure.

Functions

func Wrap

func Wrap(name string, err error) error

Wrap turns a built-in error into a wrapped ErrTransform with the transformer name attached.

Types

type Transformer

type Transformer interface {
	Transform(root map[string]any) error
	Name() string
}

Transformer mutates the merged configuration tree. Returning an error aborts the reload. Implementations MUST be safe to call concurrently with reads of unrelated Manager instances but are guaranteed to be invoked serially within a single reload.

func Aliases

func Aliases(mapping map[string]string) Transformer

Aliases returns a Transformer that rewrites legacy keys to their new home. If the target path already has a value the new world wins and the alias is dropped.

func Defaults

func Defaults(values map[string]any) Transformer

Defaults returns a Transformer that recursively merges the supplied values into the tree, only filling keys that are missing.

func DeletePaths

func DeletePaths(paths ...string) Transformer

DeletePaths returns a Transformer that removes the specified dotted-path keys from the tree. Missing paths are silently ignored.

func EnvSubst

func EnvSubst() Transformer

EnvSubst returns a Transformer that walks every string value in the tree and substitutes occurrences of ${VAR} or ${VAR:-default}.

func EnvSubstWith

func EnvSubstWith(lookup func(string) string) Transformer

EnvSubstWith is like EnvSubst but reads variables through the supplied lookup function.

func SetIfAbsent

func SetIfAbsent(path string, value any) Transformer

SetIfAbsent sets a single dotted-path key only when it has no value.

type TransformerFunc

type TransformerFunc struct {
	NameStr string
	Fn      func(map[string]any) error
}

TransformerFunc adapts a plain function to the Transformer interface.

func (TransformerFunc) Name

func (t TransformerFunc) Name() string

func (TransformerFunc) Transform

func (t TransformerFunc) Transform(root map[string]any) error

Jump to

Keyboard shortcuts

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