codecs

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

Codecs example

Shows how to use explicit codecs for both reading and writing configuration.

What it shows

  • WithFileAs(path, codec.JSON): load a JSON file explicitly (no extension guessing)
  • WithFileAs(path, codec.TOML): load a TOML override on top of it
  • WithFileDumperAs(path, codec.YAML): dump the merged effective configuration to a YAML file

Files

  • app.json: base configuration in JSON format
  • overrides.toml: overrides in TOML format; listen.port here wins over the JSON value
  • effective-config.yaml: written on Dump (not committed)

Run

cd examples/codecs && go run .

Output: app=formats-demo listen.port=4000 meta.region=local

A file effective-config.yaml is also written with the fully merged state.

Tests

cd examples/codecs && go test -v

Key ideas

  1. WithFileAs lets you load any format regardless of file extension.
  2. Source order is priority order. Later sources win on conflicting keys.
  3. WithFileDumperAs writes the fully merged map, not the last-loaded file. Useful for debugging or snapshotting effective configuration.

Documentation

Overview

Package main shows codec usage end-to-end: JSON + TOML as input sources, merged YAML written out via WithFileDumperAs.

Sources (lowest to highest priority):

  1. app.json — loaded with WithFileAs and codec.JSON
  2. overrides.toml — loaded with WithFileAs and codec.TOML (wins on conflicts)

After Load, Dump writes the merged state to effective-config.yaml.

Jump to

Keyboard shortcuts

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