# goccy/go-yaml architecture

> Architecture diagram of the GitHub repository goccy/go-yaml, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/goccy/go-yaml
- Repository: https://github.com/goccy/go-yaml

## Overview

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`](https://github.com/goccy/go-yaml/blob/master/yaml.go) (yaml.go), [`parser/parser.go`](https://github.com/goccy/go-yaml/blob/master/parser/parser.go) (Parser), [`printer/printer.go`](https://github.com/goccy/go-yaml/blob/master/printer/printer.go) (Printer), [`ast/ast.go`](https://github.com/goccy/go-yaml/blob/master/ast/ast.go) (AST)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core API"]
  node_yaml["yaml.go<br/>facade API<br/>[yaml.go]"]
  node_decode["Decode<br/>object mapping<br/>[decode.go]"]
  node_encode["Encode<br/>object mapping<br/>[encode.go]"]
  node_options["Options<br/>config rules<br/>[option.go]"]
  node_context["Context<br/>runtime state<br/>[context.go]"]
end

subgraph group_parse["Parsing Pipeline"]
  node_scanner["Scanner<br/>char stream<br/>[scanner.go]"]
  node_lexer["Lexer<br/>tokenizer<br/>[lexer.go]"]
  node_parser["Parser<br/>ast builder<br/>[parser.go]"]
  node_ast["AST<br/>yaml model<br/>[ast.go]"]
  node_token["Token<br/>[token.go]"]
end

subgraph group_mapping["Mapping & Paths"]
  node_structmap["Struct Map<br/>field mapping<br/>[struct.go]"]
  node_path["YAMLPath<br/>path query<br/>[path.go]"]
  node_stdlibquote["Quote Rules<br/>scalar quoting<br/>[stdlib_quote.go]"]
  node_validate["Validate<br/>semantic check<br/>[validate.go]"]
end

subgraph group_diag["Diagnostics"]
  node_errfmt["Errors<br/>diagnostics<br/>[error.go]"]
  node_format["Format<br/>message rendering<br/>[format.go]"]
  node_printer["Printer<br/>yaml output<br/>[printer.go]"]
  node_color["Color<br/>terminal styling<br/>[color.go]"]
end

subgraph group_tools["Tools & Demo"]
  node_ycat["ycat<br/>CLI tool<br/>[ycat.go]"]
  node_playground["Playground<br/>web demo<br/>[App.tsx]"]
end

subgraph group_tests["Test Corpus"]
  node_suite[("YAML Suite<br/>conformance corpus")]
end

node_yaml -->|"exposes"| node_decode
node_yaml -->|"exposes"| node_encode
node_yaml -->|"exposes"| node_path
node_yaml -->|"exposes"| node_validate
node_yaml -->|"configures"| node_options
node_yaml -->|"uses"| node_context
node_decode -->|"consumes"| node_parser
node_encode -->|"uses"| node_structmap
node_decode -->|"uses"| node_structmap
node_structmap -->|"reads"| node_options
node_path -->|"operates on"| node_ast
node_validate -->|"checks"| node_ast
node_scanner -->|"feeds"| node_lexer
node_lexer -->|"produces"| node_token
node_lexer -->|"feeds"| node_parser
node_parser -->|"builds"| node_ast
node_parser -->|"reports"| node_errfmt
node_errfmt -->|"formats"| node_format
node_printer -->|"styles"| node_color
node_printer -->|"renders"| node_ast
node_ycat -->|"uses"| node_printer
node_playground -->|"visualizes"| node_parser
node_playground -->|"shows"| node_printer
node_suite -->|"validates"| node_parser
node_suite -->|"validates"| node_encode

click node_yaml "https://github.com/goccy/go-yaml/blob/master/yaml.go"
click node_decode "https://github.com/goccy/go-yaml/blob/master/decode.go"
click node_encode "https://github.com/goccy/go-yaml/blob/master/encode.go"
click node_options "https://github.com/goccy/go-yaml/blob/master/option.go"
click node_context "https://github.com/goccy/go-yaml/blob/master/context.go"
click node_scanner "https://github.com/goccy/go-yaml/blob/master/scanner/scanner.go"
click node_lexer "https://github.com/goccy/go-yaml/blob/master/lexer/lexer.go"
click node_parser "https://github.com/goccy/go-yaml/blob/master/parser/parser.go"
click node_ast "https://github.com/goccy/go-yaml/blob/master/ast/ast.go"
click node_token "https://github.com/goccy/go-yaml/blob/master/token/token.go"
click node_structmap "https://github.com/goccy/go-yaml/blob/master/struct.go"
click node_path "https://github.com/goccy/go-yaml/blob/master/path.go"
click node_stdlibquote "https://github.com/goccy/go-yaml/blob/master/stdlib_quote.go"
click node_validate "https://github.com/goccy/go-yaml/blob/master/validate.go"
click node_errfmt "https://github.com/goccy/go-yaml/blob/master/error.go"
click node_format "https://github.com/goccy/go-yaml/blob/master/internal/format/format.go"
click node_printer "https://github.com/goccy/go-yaml/blob/master/printer/printer.go"
click node_color "https://github.com/goccy/go-yaml/blob/master/printer/color.go"
click node_ycat "https://github.com/goccy/go-yaml/blob/master/cmd/ycat/ycat.go"
click node_playground "https://github.com/goccy/go-yaml/blob/master/docs/playground/src/App.tsx"
click node_suite "https://github.com/goccy/go-yaml/tree/master/testdata/yaml-test-suite"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_yaml,node_decode,node_encode,node_options,node_context toneBlue
class node_scanner,node_lexer,node_parser,node_ast,node_token toneAmber
class node_structmap,node_path,node_stdlibquote,node_validate toneMint
class node_errfmt,node_format,node_printer,node_color toneRose
class node_ycat,node_playground toneIndigo
class node_suite toneTeal
```

## Components

### Core API

Public façade and high-level YAML operations

- [yaml.go](https://github.com/goccy/go-yaml/blob/master/yaml.go): facade API · Top-level entrypoint exposing marshal, unmarshal, path, validation, and formatting helpers · `yaml.go`
- [Decode](https://github.com/goccy/go-yaml/blob/master/decode.go): object mapping · Converts YAML documents into Go values · `decode.go`
- [Encode](https://github.com/goccy/go-yaml/blob/master/encode.go): object mapping · Converts Go values back into YAML output · `encode.go`
- [Options](https://github.com/goccy/go-yaml/blob/master/option.go): config rules · Shared configuration for parsing, mapping, and output behavior · `option.go`
- [Context](https://github.com/goccy/go-yaml/blob/master/context.go): runtime state · Shared execution context used across API layers · `context.go`

### Parsing Pipeline

Byte stream to YAML AST

- [Scanner](https://github.com/goccy/go-yaml/blob/master/scanner/scanner.go): char stream · Character-stream boundary with position tracking and syntax detection · `scanner/scanner.go`
- [Lexer](https://github.com/goccy/go-yaml/blob/master/lexer/lexer.go): tokenizer · Transforms scanned input into YAML tokens · `lexer/lexer.go`
- [Parser](https://github.com/goccy/go-yaml/blob/master/parser/parser.go): ast builder · Consumes tokens and builds YAML documents and nodes · `parser/parser.go`
- [AST](https://github.com/goccy/go-yaml/blob/master/ast/ast.go): yaml model · Preserves YAML structure, comments, anchors, aliases, tags, and document shape · `ast/ast.go`
- [Token](https://github.com/goccy/go-yaml/blob/master/token/token.go): syntax tokens · Token definitions shared between lexer and parser · `token/token.go`

### Mapping & Paths

Object conversion and YAMLPath features

- [Struct Map](https://github.com/goccy/go-yaml/blob/master/struct.go): field mapping · Resolves tags, field selection, quoting, anchors, and merge semantics for Go types · `struct.go`
- [YAMLPath](https://github.com/goccy/go-yaml/blob/master/path.go): path query · Selects, filters, replaces, and annotates nodes within parsed YAML · `path.go`
- [Quote Rules](https://github.com/goccy/go-yaml/blob/master/stdlib_quote.go): scalar quoting · Standard-library quoting helpers used during encoding and field rendering · `stdlib_quote.go`
- [Validate](https://github.com/goccy/go-yaml/blob/master/validate.go): semantic check · Runs rule-based validation on parsed or mapped values · `validate.go`

### Diagnostics

Error formatting and human-facing output

- [Errors](https://github.com/goccy/go-yaml/blob/master/error.go): diagnostics · Central error types and formatting for source-aware messages · `error.go`
- [Format](https://github.com/goccy/go-yaml/blob/master/internal/format/format.go): message rendering · Formats diagnostics for display, including human-readable output · `internal/format/format.go`
- [Printer](https://github.com/goccy/go-yaml/blob/master/printer/printer.go): yaml output · Renders YAML or transformed content in readable form · `printer/printer.go`
- [Color](https://github.com/goccy/go-yaml/blob/master/printer/color.go): terminal styling · Color and terminal styling helpers for diagnostics and printed YAML · `printer/color.go`

### Tools & Demo

CLI and browser playground consumers

- [ycat](https://github.com/goccy/go-yaml/blob/master/cmd/ycat/ycat.go): CLI tool · Standalone command-line consumer that prints YAML with coloring · `cmd/ycat/ycat.go`
- [Playground](https://github.com/goccy/go-yaml/blob/master/docs/playground/src/App.tsx): web demo · Browser-based demo for interactive parsing and YAML visualization · `docs/playground/src/App.tsx`

### Test Corpus

Primary validation and conformance assets

- [YAML Suite](https://github.com/goccy/go-yaml/tree/master/testdata/yaml-test-suite): conformance corpus · Large spec-driven corpus of inputs, outputs, and expected errors · `testdata/yaml-test-suite`

## Connections

- 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
