hooks

command
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

README

Hooks example

Demonstrates all three hook points in one pipeline, showing the exact order they run.

What it shows

  • WithTransform: map stage, inspect and mutate the raw key-value map before binding
  • WithValidator: cross-field, validate relationships between fields (TLS cert + key consistency)
  • OnBound[T]: struct stage, post-binding normalization (lowercase the log level)

Pipeline order

Load sources -> WithTransform -> WithValidator -> WithBinding -> OnBound[T]

Run

cd examples/hooks && go run .

Tests

cd examples/hooks && go test -v

The tests cover:

  • Happy path with all hooks running
  • WithTransform overriding log level for production
  • WithValidator rejecting TLS enabled without cert/key files
  • OnBound normalizing the log level to lowercase

Key ideas

  1. Hook points are ordered: transforms run before validators, which run before binding, which runs before OnBound.
  2. OnBound[T] is type-safe: passing a mismatched OnBound[Other] is a compile error, not a runtime panic.
  3. WithValidator operates on the raw *Values map, making cross-field checks easy without Go struct reflection.

Documentation

Overview

Package main demonstrates all three hook points in one pipeline.

Pipeline order:

  1. WithTransform — map stage: override log level for production.
  2. WithValidator — cross-field: TLS cert + key must both be set when TLS is enabled.
  3. WithBinding — decode map into App struct.
  4. OnBound[App] — struct stage: normalize Logging.Level to lowercase.

Jump to

Keyboard shortcuts

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