How goccy/go-yaml works
1. This repository is a full YAML toolkit for Go, not just a serializer. The public API lives at the repo root and exposes marshal/unmarshal, decoding, encoding, YAMLPath, validation, and error formatting. Underneath, the code is organized as a layered pipeline from raw bytes to tokens, AST, reflection-based object mapping, and formatted output.
2. The lowest input boundary is the scanner/lexer split. The scanner package handles character-stream concerns such as position tracking, context, and syntax/error detection. The lexer package turns that stream into tokens. This is the first major parsing boundary and the place where line/column-aware diagnostics begin.
3. The parser package consumes those tokens and builds a YAML AST. The ast package defines the node model used for preserving YAML structure, including comments, anchors, aliases, tags, scalar forms, and document structure. That AST is central to the library’s reversible transformations and to features that need to preserve YAML fidelity instead of collapsing everything into generic maps.
4. The encode/decode layer is the main data-mapping subsystem. decode.go and encode.go sit at the root and connect YAML documents to Go values via reflection, struct tags, and options. This layer is what powers yaml.Unmarshal/Yaml.Marshal-style behavior, including custom marshaler/unmarshaler hooks, json-tag compatibility, and anchor/alias handling.
5. Struct mapping is a distinct subsystem rather than being embedded inside the parser. struct.go, path.go, option.go, stdlib_quote.go, and related helpers manage tag resolution, field selection, quoting, anchor naming, merge keys, and other high-level conversion rules. This is where YAML semantics are adapted to Go types and where most user-facing configuration knobs land.
6. Error handling is intentionally structured. error.go plus internal/errors and scanner/error.go centralize rich diagnostics, while internal/format formats messages for display. The public API can emit location-aware and optionally colorized errors, and the printer/color helpers support terminal-friendly output. This means errors are not just propagated; they are rendered as a user-facing subsystem.
7. YAMLPath is another major feature boundary. The path.go package lets callers select, read, filter, replace, and annotate nodes within a YAML document using path expressions. It operates on parsed YAML rather than raw text, so it depends on the AST/parser side of the system and feeds back into formatting or validation workflows.
8. Validation is integrated but kept separate from parsing. validate.go and testdata/validate_test.go show that parsed/mapped values can be checked with validation rules and then reported with source-aware diagnostics. The architecture therefore has a second-stage semantic validation path after syntactic parsing and type mapping.
9. The printer subsystem is responsible for turning parsed or transformed YAML back into human-readable form, including colored output. printer/printer.go and printer/color.go are useful for command-line tools, diagnostics, and reversible formatting workflows. This is separate from encoding because it focuses on presentation of YAML content and errors, not Go object mapping.
10. The root package acts as the façade that binds all these subsystems into one API surface. It wires options, context management, decoding/encoding helpers, path utilities, and error formatting into a single import path. That makes the public boundary small while allowing the internals to stay specialized.
11. There is a standalone CLI tool in cmd/ycat. It is its own Go module and uses the library to print YAML with syntax coloring. Architecturally, it is a thin consumer of the core packages rather than a separate implementation.
12. The docs/playground directory is a browser-based demo and debugging surface, not part of the core library. It combines a small Go WASM backend with a TypeScript/Vite frontend, plus a worker-based execution path. Its role is to visualize parsing and YAML behavior interactively, which is useful when explaining or validating the library’s output.
13. Testing is a major part of the architecture. Beyond unit tests, testdata contains a large YAML test suite corpus with expected outputs and errors. That suite is used to validate parser correctness, edge cases, and spec coverage, making conformance a first-class concern of the repository.
14. In diagram terms, the main flow is: source YAML enters scanner/lexer, becomes tokens, then AST via parser, then either maps into Go values through encode/decode or is queried/transformed via YAMLPath, and finally is rendered back through printer/encoder or emitted as diagnostics. The primary boundary is between syntax handling and semantic object mapping, with AST preserved as the bridge for round-tripping and advanced transformations.
Main entry points: yaml.go yaml.go · parser.go Parser · printer.go Printer · ast.go AST
Components 21
Core API
Public façade and high-level YAML operations
- yaml.go · Top-level entrypoint exposing marshal, unmarshal, path, validation, and formatting helpers
yaml.go - Decode · Converts YAML documents into Go values
decode.go - Encode · Converts Go values back into YAML output
encode.go - Options · Shared configuration for parsing, mapping, and output behavior
option.go - Context · Shared execution context used across API layers
context.go
Parsing Pipeline
Byte stream to YAML AST
- Scanner · Character-stream boundary with position tracking and syntax detection
scanner/scanner.go - Lexer · Transforms scanned input into YAML tokens
lexer/lexer.go - Parser · Consumes tokens and builds YAML documents and nodes
parser/parser.go - AST · Preserves YAML structure, comments, anchors, aliases, tags, and document shape
ast/ast.go - Token · Token definitions shared between lexer and parser
token/token.go
Mapping & Paths
Object conversion and YAMLPath features
- Struct Map · Resolves tags, field selection, quoting, anchors, and merge semantics for Go types
struct.go - YAMLPath · Selects, filters, replaces, and annotates nodes within parsed YAML
path.go - Quote Rules · Standard-library quoting helpers used during encoding and field rendering
stdlib_quote.go - Validate · Runs rule-based validation on parsed or mapped values
validate.go
Diagnostics
Error formatting and human-facing output
- Errors · Central error types and formatting for source-aware messages
error.go - Format · Formats diagnostics for display, including human-readable output
internal/format/format.go - Printer · Renders YAML or transformed content in readable form
printer/printer.go - Color · Color and terminal styling helpers for diagnostics and printed YAML
printer/color.go
Tools & Demo
CLI and browser playground consumers
- ycat · Standalone command-line consumer that prints YAML with coloring
cmd/ycat/ycat.go - Playground · Browser-based demo for interactive parsing and YAML visualization
docs/playground/src/App.tsx
Test Corpus
Primary validation and conformance assets
- YAML Suite · Large spec-driven corpus of inputs, outputs, and expected errors
testdata/yaml-test-suite
Connections 25
- yaml.go → Decode: exposes · Public API routes document loading into decoding
- yaml.go → Encode: exposes · Public API routes Go value emission into encoding
- yaml.go → YAMLPath: exposes · Facade publishes YAMLPath operations
- yaml.go → Validate: exposes · Facade publishes validation entrypoints
- yaml.go → Options: configures · Shared options shape top-level behavior
- yaml.go → Context: uses · Root package threads execution context through operations
- Decode → Parser: consumes · Decoding begins from parsed YAML documents
- Encode → Struct Map: uses · Encoding maps Go structs and values into YAML nodes
- Decode → Struct Map: uses · Decoding maps YAML content into Go fields and types
- Struct Map → Options: reads · Field resolution and conversion depend on user options
- YAMLPath → AST: operates on · YAMLPath manipulates parsed YAML structure rather than raw text
- Validate → AST: checks · Validation works on parsed or mapped YAML structure
- Scanner → Lexer: feeds · Scanned character stream is tokenized by the lexer
- Lexer → Token: produces · Lexer emits reusable token definitions
- Lexer → Parser: feeds · Tokens are consumed by the parser
- Parser → AST: builds · Parser constructs the YAML AST and document model
- Parser → Errors: reports · Parser and scanner diagnostics flow into structured errors
- Errors → Format: formats · Error structures are rendered for display
- Printer → Color: styles · Printer uses terminal coloring for readable output
- Printer → AST: renders · Printer turns AST content back into human-readable YAML
- ycat → Printer: uses · CLI delegates formatting and display to printer
- Playground → Parser: visualizes · Demo backend parses YAML for interactive inspection
- Playground → Printer: shows · Demo presents formatted YAML and diagnostics
- YAML Suite → Parser: validates · Corpus exercises parser correctness and edge cases
- YAML Suite → Encode: validates · Corpus checks round-trip and emission behavior