$ show pelletier/go-toml
This repository provides a Go library (`github.com/pelletier/go-toml/v2`) for reading and writing TOML documents, conforming to TOML v1.1.0. It offers a two-phase architecture: a low-level iterative AST parser in the `unstable` package, and higher-level `Marshal`/`Unmarshal` functions with configurable streaming `Encoder`/`Decoder` that support strict mode, custom marshalers, and struct tags. The library is dependency-free (only standard library) and includes three CLI tools: `tomll` (formatter), `tomljson` (TOML to JSON), and `jsontoml` (JSON to TOML). The codebase demonstrates strong engineering practices: pooled encoder/decoder states, cached struct plans via `sync.Map`, word-at-a-time scanning for performance, a fused generic decode path, and comprehensive test coverage including auto-generated conformance tests from the upstream toml-test suite. It also includes an experimental document-editing package (`unstable/edit`) that preserves comments and layout via byte-range indexing and splice-based mutations.
The LLM identified 3 files worth highlighting for reuse:
A generic CLI program wrapper that handles stdin, file, and in-place editing modes with flag parsing. It is used by all three CLI tools (tomll, tomljson, jsontoml) and can be extracted to create new CLI tools that process file contents with minimal boilerplate. The pattern demonstrates clean separation of concerns and reusable I/O handling.
A `SeenTracker` that efficiently detects duplicate keys in TOML documents using a pre-sized array of slots, avoiding map overhead. It is designed to be O(1) and is tightly coupled to the AST structure, but the underlying pattern of a fixed-size slot tracker with collision detection is broadly applicable to any problem requiring duplicate detection with bounded memory and fast lookups. The file includes tests ensuring the entry size stays within 48 bytes.
A layout-preserving TOML document editor that operates on a byte-range index of the original document. It provides `Set`, `Delete`, `Get`, `Comment`, and `SetComment` operations by computing splice edits and reapplying them. This is a high-craftsmanship component that could be extracted into a standalone library for editing any structured text format where byte-level edits must preserve formatting. It demonstrates advanced use of byte slices, gap buffers, and efficient mutation.