codec-mapping

command
v0.6.0 Latest Latest
Warning

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

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

Documentation

Overview

Package main demonstrates three patterns for reusing and transforming codecs without repeating constraint definitions.

Pattern 1 — Shared field codec variables:

Define portCodec and hostCodec once. Embed them in multiple struct codecs
(AppConfig, ProxyConfig). Constraints live in one place; all structs benefit.

Pattern 2 — Sub-codec direct reuse:

dbCodec is defined independently with its own constraints. It is embedded
in AppConfig and also passed directly to newDBModule — no re-definition,
no constraint duplication.

Pattern 3a — MapCodecSafe (infallible decode):

type Port uint16 wraps portCodec. The port range constraint defined on
portCodec runs automatically in the mapped codec's Decode path.
Encode is trusted (Refine constraints are decode-only by design).

Pattern 3b — MapCodecValidated (fallible mapping + post-decode validation):

DBConfig is mapped to a DSN string. The to function can fail (malformed
host), and cb.Validate enforces DBConfig constraints after the mapping.

Run with: go run ./examples/codec-mapping

Jump to

Keyboard shortcuts

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