oneyaml

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0, MIT Imports: 28 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

oneyaml-go

Pure-Go YAML 1.2 that edits a file and keeps its comments.

Go Reference CI Go version License

YAML 1.2 with the Core schema by default; the conformance results and the known deviations are under Correctness. Edits change only the value you set, in its own style; comments and formatting elsewhere are kept byte for byte. Nesting, alias expansion, number size and patch work are bounded by default.

Time per task for oneyaml-go and for the strongest Go competitor whose answer passed the benchmark's check, on the real-world files of each task. Each row says how many times faster oneyaml-go was: the competitor's time divided by ours, so higher is better. The method line under the table names the library commit that was measured, the date of the run and the machine it ran on; About this run says which release that commit is. The figures are measured, not bounds, and later code may differ.

Task oneyaml-go time (geometric mean) Strongest competitor Its time (geometric mean) Speed against it (its time ÷ ours; higher is better) Verdict
typed load 121 µs yaml-v2 v2.4.0 234 µs 1.94× faster ▲ win
generic load 48.1 µs yaml-v2 v2.4.0 (6 of 7), yaml-v3 v3.0.5 (1 of 7) 135 µs 2.79× faster ▲ win
dump 9.5 µs yaml-v2 v2.4.0 (5 of 6), yaml-v4 v4.0.0-rc.6 (1 of 6) 114 µs 12.0× faster ▲ win
document parse 86.3 µs yaml-v3 v3.0.5 (2 of 3), yaml-v4 v4.0.0-rc.6 (1 of 3) 415 µs 4.81× faster ▲ win
unchanged emit (write-back of an unedited document; not a rendering speed) 4.31 ns goccy-go-yaml v1.19.2 186 µs 43,100× faster ▲ win
edit script 13.7 µs yaml-v3 v3.0.5 284 µs 20.7× faster ▲ win
patch (RFC 6902) 15.7 µs † (text API) json-patch v5.9.11 † (json-patch: text API, value-only) 92.1 µs 5.87× faster ▲ win

Not in a row's mean: document parse: no competitor answer: compose-wordpress, gha-workflow, openapi-api-with-examples, prometheus-config; unchanged emit (write-back of an unedited document; not a rendering speed): no competitor answer: compose-wordpress, gha-workflow, openapi-api-with-examples, prometheus-config; edit script: no competitor answer: compose-wordpress, gha-workflow, openapi-api-with-examples, prometheus-config; patch (RFC 6902): no competitor answer: gha-workflow.

Each row is the geometric mean, over the real-world workloads the task has, of our time and of the time of the strongest competitor in each workload. Speed is the competitor's time divided by ours: 2× faster means half the time, and a loss is stated as slower. Where several competitors were strongest the column names them all, with how often. A † marks a cell measured differently from the task as specified; the note beside each mark says how. A marked competitor still counts. A workload where our library has no number is listed and the row is marked as partial, never counted as a win. The verdict is never better than the per-workload verdicts allow: a row with a losing workload is marked mixed or loss, and a row with no winning workload is never marked a win. Memory and the count of better, level and worse workloads in each row are on the full page.

Method: full run of 2026-10-16T21:48:45Z. Kit revision 0abba1d8b4f2. oneyaml-go eea67bb6b962; competitors yaml-v3 v3.0.5, yaml-v4 v4.0.0-rc.6, yaml-v2 v2.4.0, sigs-yaml v1.6.0, goccy-go-yaml v1.19.2, palantir-yamlpatch v1.5.0, palantir-yamlpatch-goccy v1.5.0, json-patch v5.9.11. Runtime go1.26.0 (target linux/amd64), measured with testing.B: ns/op from testing.Benchmark at benchtime 1s. Memory: B/op and allocs/op from testing.B (ReportAllocs). AMD Ryzen 9 7950X3D 16-Core Processor (125.0 GiB), Linux 7.0.0-34-generic (x86_64), measured on a shared workstation (32 logical CPUs; load 2.67 before, 4.06 after, at most 11.23 during).

Every workload, the memory figures, what the unchanged-emit row does and does not measure, and how to reproduce the run: Performance.

  • Measured against the other Go YAML libraries. Typed and generic loads, dumps, document parsing, writing an unedited document, edits and patches are each timed against the strongest competitor whose answer passed the benchmark's check. The table above gives the verdict for each task, and Performance has every workload with its time and memory. Rows where no competitor answered correctly have no comparison and are not counted as wins.
  • YAML 1.2 Core schema by default. Plain scalars follow the Core schema, so plain yes is a string, plain 0755 is the integer 755, and quoted "0755" is a string. YAML 1.1 forms come only from the compat packages. Results against the yaml-test-suite, and the known deviations, are under Correctness.
  • Minimal-diff edits. Document.Set rewrites one value in its own style and falls back to the nearest style that can hold the new value. Reading it back with this package always gives exactly the value you set. Plainness follows the Core schema, so a new plain yes is a string here but a boolean to a YAML 1.1 reader; pass YAML11Strings() when such programs also read the file. Rename changes a key in place, and SetHeadComment and SetLineComment write comments. The benchmark checks every edit byte for byte before it times it. See Editing.
  • Patches. RFC 6902 JSON Patch operations, RFC 6901 pointers and RFC 7396 merge patches, applied to a YAML file with YAML's data model: test compares YAML values, so the integer 1 and the float 1.0 differ where RFC 6902 would call them equal, and a write through an alias is refused. A patch applies atomically and keeps the file's layout. See Patches.
  • Bounded by default. Nesting depth, alias expansion, the digits of a number and the work of a patch are bounded unless you pass NoLimits(). Input size, documents per stream and scalar length are bounded only with Untrusted(); select it, and size it with Limits, for input you do not control. Your own hooks run outside these bounds, and the compat packages have no Untrusted() mode. A panic caused by input is treated as a security bug. No unsafe and no cgo. See Limits.
  • Compatibility adapters for yaml.v3 and sigs.k8s.io/yaml. For the common API listed in each migration guide, one import change keeps your call sites; the known differences are listed. See Migrating from yaml.v3 and from sigs.k8s.io/yaml.
  • No dependencies outside the standard library.

Install

go get oneyaml.dev/go@latest

Quick start

Change one value in a file. The comment, the block scalar and every other byte stay as they were. (The examples import fmt, log and oneyaml "oneyaml.dev/go".)

src := []byte(`# deploy settings
name: api
replicas: 2   # raised by the release job
script: |
  make build
`)

doc, err := oneyaml.Parse(src)
if err != nil {
	log.Fatal(err)
}
if err := doc.Set("/replicas", 3); err != nil {
	log.Fatal(err)
}
if err := doc.Set("/script", "make build\nmake test\n"); err != nil {
	log.Fatal(err)
}
fmt.Print(string(doc.Bytes()))
// Output:
// # deploy settings
// name: api
// replicas: 3   # raised by the release job
// script: |
//   make build
//   make test

Reading into a struct works as it does in encoding/json:

src := []byte(`# deploy settings
name: api
replicas: 2
ports: [80, 443]
labels:
  tier: web
`)
var cfg struct {
	Name     string            `yaml:"name"`
	Replicas int               `yaml:"replicas"`
	Ports    []int             `yaml:"ports"`
	Labels   map[string]string `yaml:"labels"`
}
if err := oneyaml.Unmarshal(src, &cfg); err != nil {
	log.Fatal(err)
}
fmt.Println(cfg.Name, cfg.Replicas, cfg.Ports, cfg.Labels)
// Output: api 2 [80 443] map[tier:web]

The package documentation on pkg.go.dev has a runnable example for every feature.

When to use it, and when not to

Use it when you need to change a YAML file that people also edit by hand, apply a JSON Patch to one, read YAML 1.2 with the Core schema, or read input you do not control with Untrusted().

Think twice when:

  • You need comments in any place. The comment setters write a comment above an entry or after a value, and refuse places such as most of the inside of a flow collection (Comments).
  • You need to stream events. The parser is not exposed; Parse and NewDecoder read a whole document.
  • You hold very large files in memory. A Document keeps the text and an index of it, which is an order of magnitude larger than the text for dense input. Set MaxInputBytes, or use Untrusted(), for input from outside.
  • Your data uses a collection as a mapping key and you read it into any. A Go map cannot hold it; a Document can.
  • Your program depends on go-yaml quirks. The compat packages reproduce documented behaviour, not bugs, and cover yaml.v3 and sigs.k8s.io/yaml only.

Comparison says where other libraries are the better choice.

Correctness

Conformance is measured with oneyaml-kit, the conformance kit shared by the oneyaml libraries, against the yaml-test-suite release data-2022-01-17. The rows below are generated. Conformance has the whole report, explains what each column means, including the strict column for invalid input, and lists every known deviation.

Method: kit 0.1.0 (corpus digest b1c5e8ce5ca5, kit revision 0abba1d8b4f2), every case including the slow ones, run 2026-10-16, library revision eea67bb6b962, yaml-test-suite data-2022-01-17 (revision 6e6c296a) plus 605 kit cases. Generated by bin/kit run --slow --adapter oneyaml-go from the kit's result file oneyaml-go.json; do not edit.

Group Cases Loose Strict Strict, corrected Unsupported Strict only Emulated
yaml-test-suite · events · invalid input 94 94/94 0/94 0/94 0 94 0
yaml-test-suite · events · valid input 308 308/308 308/308 – 0 0 0
yaml-test-suite · values (in.json) · valid input 279 278/279 278/279 279/279 0 0 0
yaml-test-suite · events · all 402 402 402/402 308/402 308/402 0 94 0

The library is fuzzed with Go's native fuzzing. The fuzz targets cover Unmarshal, Document reads, edits and patches, and the compat packages compat/yamlv3 and compat/k8syaml. The patch and compat packages are also checked against their targets by differential tests.

Compatibility adapters for yaml.v3 and sigs.k8s.io/yaml

The compat packages are adapters for the common API of their targets, listed in each migration guide. If your code uses that API, changing the import path keeps your call sites; the package name stays yaml.

# gopkg.in/yaml.v3  ->  oneyaml.dev/go/compat/yamlv3
# sigs.k8s.io/yaml  ->  oneyaml.dev/go/compat/k8syaml

Swapping your import does not change the code of your dependencies. A type from another module whose UnmarshalYAML takes gopkg.in/yaml.v3's *yaml.Node cannot be called by compat/yamlv3, so reading into it returns an error that names the type. (yaml.v3 itself would skip such a method and fill the type field by field, without what the method checks.) Read that part through a wrapper type in your own code whose hook the adapter can call, or decode that part with yaml.v3. Hooks you do not own has the details.

They read YAML 1.1 forms the way their targets do, but they are not identical. Known gaps and differences:

  • yaml.v3 comments: comments land on the same nodes as in yaml.v3 in the common layouts, and are written back where yaml.v3 writes them, inside flow collections too. One flow-mapping layout puts a comment on a different node, and where yaml.v3 writes text that does not read back, the adapter writes text that does.
  • yaml.v3 hooks: an UnmarshalYAML or MarshalYAML method of a form the adapter cannot call is an error, where yaml.v3 skips it. That includes a MarshalYAML that returns gopkg.in/yaml.v3's own *yaml.Node (or a yaml.Node of that module anywhere in the value): the error names the type; build the adapter's yaml.Node instead.
  • sigs.k8s.io/yaml: JSONObjectToYAMLObject and the subpackages of sigs.k8s.io/yaml are not provided.
  • Numbers: in compat/yamlv3, a plain 3.5 read into an integer field is a type error instead of being truncated. In compat/k8syaml, a float read into a string field keeps its full precision, where sigs.k8s.io/yaml rounds it to 32 bits.
  • Valid and invalid input: both read valid YAML that their targets refuse, compat/yamlv3 refuses invalid YAML that yaml.v3 accepts, and both return errors where their targets panic.
  • Limits: both always run with the default limits and take no options, so there is no Untrusted() mode. Bound the input size yourself (how).

Each package's documentation lists every difference (yamlv3, k8syaml), and the migration guides have a checklist. New code should use the oneyaml package.

Documentation

You want to Read
Look up a function or type pkg.go.dev
Change a value, rename a key or write a comment, and keep the file's style Editing
Apply a JSON Patch or merge patch Patches
Read input you do not control Limits, Security
Load a configuration file strictly Loading a configuration file strictly
Know what is and is not conformant Conformance
Move from yaml.v3 Migrating from yaml.v3
Move from sigs.k8s.io/yaml Migrating from sigs.k8s.io/yaml
See speed and memory against other Go libraries Performance
Compare with other Go libraries Comparison
See what changed Changelog
Know what is stable and which Go versions are supported Stability
Contribute Contributing

Stability

From 1.0.0 the module follows Go's rules for a stable module, in the spirit of the Go 1 compatibility promise. Within major version 1:

  • Your code keeps compiling and keeps its meaning. Code that uses the exported API of oneyaml.dev/go, oneyaml.dev/go/compat/yamlv3 or oneyaml.dev/go/compat/k8syaml builds with every later 1.x release and behaves as documented. That covers the error types, the sentinel errors you match with errors.Is and errors.As, and the Limit and Category constants.
  • Written bytes stay the same. The bytes that Marshal and an Encoder write with default options do not change, and neither do the bytes that an edit leaves untouched.

A minor release may still make these changes:

  • Additions: new names, methods, struct fields and Limit constants. A new field breaks a struct literal without field names, so write LimitSet, the error types and the other structs of these packages with field names, as in oneyaml.LimitSet{MaxDepth: 50}, and give a switch on Limit a default case.
  • Bug fixes, including refusing input that the YAML 1.2 specification calls invalid, and writing text that reads back as the value written where the output did not. Each such fix is listed in the changelog.
  • Error messages: their text is written for people and may change. Match errors by type, sentinel and constant.
  • The compat packages: where compat/yamlv3 or compat/k8syaml places comments or lays out output differently from yaml.v3 or sigs.k8s.io/yaml, a minor release may bring it closer to its target.

Patch releases only fix bugs. Security fixes go into the latest minor release (security policy).

Any other change that breaks code needs a new major version with its own module path, oneyaml.dev/go/v2, so code that imports oneyaml.dev/go keeps building against 1.x.

Releases before 1.0 could break code in a minor release. The changelog marks each such change, with what to write instead.

Go versions. The minimum is Go 1.24 (go 1.24 in go.mod), and CI tests Go 1.24, 1.25, 1.26 and 1.27. Raising the minimum takes a minor release and a note in the changelog. Go itself supports only its two newest major releases (release policy), so the minimum reaches back past the releases that still get fixes from Go.

This is the only place this policy is stated; other pages link here.

License

Licensed under either of Apache License, Version 2.0 or MIT license, at your option. Unless you state otherwise, any contribution you submit for inclusion is dual licensed as above, without additional terms or conditions.

Documentation

Overview

Package oneyaml reads, writes and edits YAML. It follows YAML 1.2 with the Core schema by default and is pure Go, with no dependencies outside the standard library. The conformance report has the yaml-test-suite results and the known deviations.

The package has two halves. The first is a typed load and dump in the style of encoding/json: Unmarshal, Marshal, NewDecoder and NewEncoder. The second is Document, which holds a whole YAML file and edits it in place. Edits change only the value you set, in its own style; comments and formatting elsewhere are kept byte for byte.

Nesting, alias expansion, number size and patch work are bounded by default; input from outside needs the Untrusted option as well. See "Limits" below.

Install

go get oneyaml.dev/go@latest

The import path is oneyaml.dev/go and the package name is oneyaml. What a release promises, and the supported Go versions, are in the Stability section of the README.

Load

Unmarshal reads the first document of its input into a Go value. Struct fields are matched through the yaml struct tag; with no yaml tag a json tag is used. A document read into any gives map[string]any, []any, string, bool, int, int64, uint64, math/big.Int and float64. Integers are never rounded. Plain scalars follow the YAML 1.2 Core schema: plain yes, no, on and off are strings, plain 0755 is the integer 755, and a quoted scalar such as "0755" is always a string. NewDecoder reads a stream of documents and returns io.EOF at the end.

A type can read its own YAML. The hooks are tried in this order: Unmarshaler, which receives a Node; UnmarshalYAML(func(any) error) error, the older form that yaml.v2, yaml.v3 and goccy/go-yaml accept; encoding.TextUnmarshaler, for a scalar; and encoding/json.Unmarshaler, which receives the value as JSON, a plain number with its digits as written. So types written for encoding/json, such as Kubernetes API types with json tags and UnmarshalJSON methods, load as they are. An UnmarshalYAML method of any other form, such as yaml.v3's UnmarshalYAML(*yaml.Node) error, is reported as a *TypeError rather than skipped, and the rest of the document is still read. A Node is not a decode target; read one with Document.Get.

Edit

Parse returns a Document that keeps every byte of its input. Document.Set assigns a value at an RFC 6901 pointer, which Pointer builds from keys and indices with "/" and "~" escaped: it replaces an existing value, keeping its style when the style can hold the new value and moving to the nearest style that can when it cannot, and it adds a missing key at the end of its mapping in the style of its siblings. Reading the value back always gives exactly the value set. Document.Remove deletes an entry with the comments that belong to it, Document.Rename changes a key in place, and Document.SetHeadComment and Document.SetLineComment write or remove a comment. Document.Bytes and Document.WriteTo write the result, and a Document prints as its text. An edit that cannot be made, such as one that would write through an alias, returns an *EditError and leaves the document unchanged.

An edit makes every Node read before it stale; Node.Err says when a node reads as zero values because it is stale or was not found. A range over Node.Items or Node.Entries may edit the document, as a loop over a map may change the map: it finds its collection again and goes on with fresh nodes. Edits that add or remove entries may make such a loop skip or repeat some; for those, collect the places with Node.Pointer first and edit afterwards.

Document.Get and Document.Decode read values back through the same binder that Unmarshal uses, and Document.Root gives a read-only Node view with kinds, styles, tags, anchors, comments and positions. A read may take options of its own, such as KnownFields, which apply to that call on top of the options the Document was parsed with.

Patch

Document.ApplyPatch applies RFC 6902 JSON Patch operations, addressed by RFC 6901 pointers, to YAML with YAML's data model. The patch is written as JSON or as a YAML block list. test compares YAML values, so the integer 1 and the float 1.0 differ where RFC 6902 would call them equal, and a write through an alias is refused (see the Document.ApplyPatch documentation). Document.Apply takes the same operations as Go values (PatchAdd, PatchReplace and the rest), and Document.ApplyMergePatch applies an RFC 7396 merge patch. A patch is atomic: if any operation fails, the document is unchanged. The edits it makes follow the same rules as Document.Set.

Marshal

Marshal and Encoder write Go values as block-style YAML. A string is written plain only when every common reader, under the Core schema and under YAML 1.1, reads it back as the same string, so "yes", "0755" and "1:30" come out quoted. Edits to a Document judge plainness by the Core schema alone, and follow this stricter rule only with the YAML11Strings option. Output is deterministic: map keys are sorted and struct fields keep their declaration order. A type can choose its own form by implementing Marshaler, encoding.TextMarshaler or encoding/json.Marshaler, tried in that order. A Node is written as the value it holds. The omitempty tag option leaves out zero values, empty collections and structs whose fields are all zero; omitzero leaves out only zero values. Indent sets the indentation.

Limits

Every call that reads input runs under limits on nesting depth, alias expansion, the digits of a number and the work of a patch; see DefaultLimits. They are on unless you pass NoLimits. Input size, scalar size and documents per stream are bounded only with Untrusted, which you should select, and size with Limits, for input from a source you do not control. Options apply in order: Limits changes only the fields it sets, on top of the options before it, and NoBound lifts one bound. So Untrusted(), Limits(LimitSet{MaxDepth: 50}) keeps every untrusted bound and tightens depth. The bound on input size also holds for edits: Document.Set, Document.Rename, the comment setters and patches refuse to make the text longer than it. A limit that is hit is a *LimitError. The Document and decoder paths follow aliases under the same budget. Code in your own hooks runs outside these limits, and the compat packages always use the defaults.

Errors

Errors are values to test with errors.Is and errors.As. Ask what kind of failure it was with errors.Is, which every error type answers, and read the details with errors.As:

Node.Find reports why a key has no value with the same sentinels: ErrNotFound, ErrStaleNode, or ErrAmbiguousKey for a key written more than once in its mapping, which Document.Get refuses too; a search of "<<" merge sources that passes its bound is a *LimitError. Node.Lookup reports each of these as false.

Match errors by type and kind: the text of a message is written for people and may change in any release.

The library contains no unsafe code. A panic caused by input is treated as a security bug; report it as described in the security policy in the repository.

Options

One Option type serves every entry point, the reads of a Document included, and an option that does not apply to a call is ignored: KnownFields makes an unknown struct key an error, MergeKeys reads the "<<" merge key, YAML11Strings makes edits quote strings that YAML 1.1 reads as another type, Indent sets the output indentation, and Limits, Untrusted and NoLimits set the limits. Options apply in the order given, and the last one that sets a thing wins. KnownFields, MergeKeys and YAML11Strings take an optional bool, so KnownFields(false) turns off an earlier KnownFields().

No option allows a repeated mapping key. Unmarshal and Decoder.Decode report one, and so do Document.Get, Document.Decode and Node.Decode when the part they read holds it. Parse itself keeps a document that has one, so that its other parts can still be read and edited; see Parse.

Compatibility

The compatibility adapters oneyaml.dev/go/compat/yamlv3 and oneyaml.dev/go/compat/k8syaml have the names and signatures of gopkg.in/yaml.v3 and sigs.k8s.io/yaml, so code that uses them keeps its call sites after one import change. They read YAML 1.1 forms the way their targets do, which this package never does, and each lists where it differs from its target.

More

The repository README has a quick start, and its docs directory has guides on editing, patches, limits, conformance, migration and a comparison with other Go YAML libraries.

Example (BlockLiteralKeepsStyle)

Case A: the new value ends in a line break, so the block literal keeps its style, its header and its place. Only the line of text changes.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte(`#hello
a: 10

#abc 123
b: |
  this is a test
c: 13
`))
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/b", "this is a test2\n"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
#hello
a: 10

#abc 123
b: |
  this is a test2
c: 13
Example (BlockLiteralStrip)

Case B: the new value has no final line break. Reading the document back must give exactly that value, so the header changes to |- and nothing else does.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte(`#hello
a: 10

#abc 123
b: |
  this is a test
c: 13
`))
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/b", "this is a test2"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))

	var b string
	if err := doc.Get("/b", &b); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%q\n", b)
}
Output:
#hello
a: 10

#abc 123
b: |-
  this is a test2
c: 13
"this is a test2"
Example (CompatBoundedInput)

The compat packages take no options, so bound the input before you hand it over. Read one byte more than the bound, so that input of exactly the bound passes and longer input is refused instead of being cut short.

package main

import (
	"fmt"
	"io"
	"log"
	"strings"

	yaml "oneyaml.dev/go/compat/k8syaml"
)

func main() {
	const maxBytes = 1 << 20
	r := strings.NewReader("replicas: 3\n") // a file or a request body

	data, err := io.ReadAll(io.LimitReader(r, maxBytes+1))
	if err != nil {
		log.Fatal(err)
	}
	if len(data) > maxBytes {
		log.Fatal("input is larger than 1 MiB")
	}
	var cfg struct {
		Replicas int `json:"replicas"`
	}
	if err := yaml.Unmarshal(data, &cfg); err != nil {
		log.Fatal(err)
	}
	fmt.Println(cfg.Replicas)
}
Output:
3
Example (FromYAMLv3Forms)

Three yaml.v3 habits that differ. 0755 is the decimal integer 755, a date is a string in a generic value, and the "<<" key is an ordinary key unless MergeKeys asks for the merge.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	src := []byte("mode: 0755\nwhen: 2001-02-03\nbase: &b {a: 1}\nuse:\n  <<: *b\n")
	var plain, merged map[string]any
	if err := oneyaml.Unmarshal(src, &plain); err != nil {
		log.Fatal(err)
	}
	if err := oneyaml.Unmarshal(src, &merged, oneyaml.MergeKeys()); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%v %q\n", plain["mode"], plain["when"])
	fmt.Println(plain["use"])
	fmt.Println(merged["use"])

	out, err := oneyaml.Marshal(struct{ Tags []string }{})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(out))
}
Output:
755 "2001-02-03"
map[<<:map[a:1]]
map[a:1]
tags: null
Example (FromYAMLv3Node)

The yaml.v3 way to change a value and keep the comments is to walk a yaml.Node tree. A Document does it with a pointer.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	src := "# service settings\nname: api # public name\nreplicas: 2\n"
	doc, err := oneyaml.Parse([]byte(src))
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/replicas", 3); err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
# service settings
name: api # public name
replicas: 3
Example (QuickStart)

The quick start: Set rewrites a value in its own style and leaves the comments and every other byte as they were.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	src := []byte(`# deploy settings
name: api
replicas: 2   # raised by the release job
script: |
  make build
`)

	doc, err := oneyaml.Parse(src)
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/replicas", 3); err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/script", "make build\nmake test\n"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
# deploy settings
name: api
replicas: 3   # raised by the release job
script: |
  make build
  make test
Example (StrictConfig)

A configuration file read strictly: unknown keys are errors, the input is bounded, and every problem is reported with its position.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	type Config struct {
		Name     string `yaml:"name"`
		Replicas int    `yaml:"replicas"`
	}
	src := []byte("name: api\nreplica: 3\nreplicas: two\n")

	// Decode into a fresh value and use it only when there is no error: a read
	// that reports mismatches has still filled the fields it could.
	var cfg Config
	err := oneyaml.Unmarshal(src, &cfg,
		oneyaml.KnownFields(), // a misspelt key is an error, not skipped
		oneyaml.Untrusted(),   // bounds on size, depth, aliases and numbers
		oneyaml.Limits(oneyaml.LimitSet{MaxInputBytes: 1 << 20}), // sized for a config file
	)
	var te *oneyaml.TypeError
	switch {
	case errors.As(err, &te):
		for _, e := range te.All() {
			fmt.Printf("line %d, column %d: %s\n", e.Line, e.Column, e.Msg)
		}
		fmt.Println("unknown field:", errors.Is(err, oneyaml.ErrUnknownField))
	case err != nil:
		// A *SyntaxError has a position too, and a *LimitError names the bound.
		log.Fatal(err)
	default:
		fmt.Println(cfg.Name, cfg.Replicas)
	}
}
Output:
line 2, column 1: field "replica" not found in type oneyaml_test.Config
line 3, column 11: cannot unmarshal !!str `two` into int
unknown field: true

Index

Examples

Constants

View Source
const NoBound = -1

NoBound, used as the value of a LimitSet field, removes the bound for that field alone. Any negative value has the same effect; NoBound gives it a name.

Variables

View Source
var (
	// ErrNotFound means a pointer or path addressed nothing.
	ErrNotFound = errors.New("oneyaml: path not found")
	// ErrNilDocument means a method that changes or decodes a document was called on
	// a nil *Document, which is what a failed Parse returns.
	ErrNilDocument = errors.New("oneyaml: nil Document")
	// ErrAliasWrite means an edit would write through an alias, or, with
	// MergeKeys, into the "<<" merge source a key comes from.
	ErrAliasWrite = errors.New("oneyaml: write through an alias")
	// ErrAmbiguousKey means a key occurs more than once in its mapping, or, with
	// MergeKeys, a "<<" merge brings it in from two places of one source, so
	// Document.Get, Node.Find or an edit cannot choose one value.
	ErrAmbiguousKey = errors.New("oneyaml: ambiguous key")
	// ErrTestFailed means a patch "test" operation did not match.
	ErrTestFailed = errors.New("oneyaml: patch test failed")
	// ErrAnchorInUse means an anchor cannot be removed while an alias uses it.
	ErrAnchorInUse = errors.New("oneyaml: anchor in use")
)

Sentinel errors for the kinds of failure a document operation can report. Match them with errors.Is.

View Source
var (
	// ErrSyntax matches every [*SyntaxError], whatever its Category.
	ErrSyntax = errors.New("oneyaml: syntax error")
	// ErrEncoding matches a SyntaxError of [CategoryEncoding].
	ErrEncoding = errors.New("oneyaml: invalid encoding")
	// ErrDuplicateKey matches a SyntaxError of [CategoryDuplicateKey].
	ErrDuplicateKey = errors.New("oneyaml: duplicate mapping key")
)

Sentinels for the kinds of a *SyntaxError.

View Source
var (
	// ErrLimit matches every [*LimitError], whichever limit it is.
	ErrLimit = errors.New("oneyaml: limit exceeded")
	// ErrMaxDepth matches a LimitError of [LimitMaxDepth].
	ErrMaxDepth = errors.New("oneyaml: nesting depth limit exceeded")
	// ErrMaxAliasNodes matches a LimitError of [LimitMaxAliasNodes].
	ErrMaxAliasNodes = errors.New("oneyaml: alias expansion node limit exceeded")
	// ErrMaxAliasBytes matches a LimitError of [LimitMaxAliasBytes].
	ErrMaxAliasBytes = errors.New("oneyaml: alias expansion size limit exceeded")
	// ErrMaxInputBytes matches a LimitError of [LimitMaxInputBytes].
	ErrMaxInputBytes = errors.New("oneyaml: input size limit exceeded")
	// ErrMaxScalarBytes matches a LimitError of [LimitMaxScalarBytes].
	ErrMaxScalarBytes = errors.New("oneyaml: scalar size limit exceeded")
	// ErrMaxDocuments matches a LimitError of [LimitMaxDocuments].
	ErrMaxDocuments = errors.New("oneyaml: document count limit exceeded")
	// ErrMaxNumberDigits matches a LimitError of [LimitMaxNumberDigits].
	ErrMaxNumberDigits = errors.New("oneyaml: number digit limit exceeded")
	// ErrMaxPatchWorkRatio matches a LimitError of [LimitMaxPatchWorkRatio].
	ErrMaxPatchWorkRatio = errors.New("oneyaml: patch work limit exceeded")
)

Sentinels for the limits of a *LimitError.

View Source
var ErrInvalidComment = errors.New("oneyaml: invalid comment text")

ErrInvalidComment means comment text cannot be written as a comment: it is not valid UTF-8, holds a control character or a line separator other than the line feed, or (for a line comment) holds a line break.

View Source
var ErrInvalidPatch = errors.New("oneyaml: invalid patch")

ErrInvalidPatch means a patch is not well formed: it is not valid JSON, it is not an array of operations, an operation is unknown or lacks a member it needs, or the operation cannot be done at all (moving a value into itself).

View Source
var ErrInvalidPointer = errors.New("oneyaml: invalid JSON pointer")

ErrInvalidPointer means a string is not an RFC 6901 JSON Pointer.

View Source
var ErrKeyExists = errors.New("oneyaml: key already exists")

ErrKeyExists means a rename would give two entries of a mapping the same key.

View Source
var ErrNoPointer = errors.New("oneyaml: node has no pointer")

ErrNoPointer means a Node has no RFC 6901 pointer: it is a mapping key, lies inside a complex key, or its pointer would not lead back to it.

View Source
var ErrNotAnEntry = errors.New("oneyaml: not a mapping entry")

ErrNotAnEntry means a pointer does not address an entry of a mapping, which is what Rename needs.

View Source
var ErrStaleNode = errors.New("oneyaml: node is stale: the document was edited after it was read")

ErrStaleNode means a Node was used after the Document it came from was edited. Fetch the node again after the edit.

View Source
var ErrUndefinedAlias = errors.New("oneyaml: alias has no anchor in its document")

ErrUndefinedAlias means a value to be written holds an alias whose anchor is not in the document it was read from, so there is nothing to write in its place.

View Source
var ErrUnknownField = errors.New("oneyaml: unknown field")

ErrUnknownField is the kind of a *TypeError for a key that no field of the target struct has, which only happens under KnownFields. Match it with errors.Is; the error is found even when it is one of several type errors.

View Source
var ErrUnsupportedEdit = errors.New("oneyaml: unsupported edit")

ErrUnsupportedEdit means the edit is valid but the document layout is one the edit engine does not rewrite. The document is left unchanged.

Functions

func Marshal

func Marshal(v any, opts ...Option) ([]byte, error)

Marshal returns v as a YAML document.

Output is block style with a two-space indent, ends with a newline and has no document marker. Strings are written plain only when every common reader would read them back as the same string; "yes", "0755" and "1:30" are quoted. Map keys are sorted and struct fields follow declaration order, so the same value always gives the same bytes.

Struct fields are named by the yaml tag, or by the json tag when there is no yaml tag, or by the lower-cased field name. The tag options are omitempty, omitzero, inline and flow. A nil map, slice, pointer or interface is written as null.

Types may implement Marshaler, encoding.TextMarshaler or json.Marshaler, tried in that order. The JSON a json.Marshaler returns is written as the equivalent YAML value (a JSON string as a plain string where it is safe, an object as a mapping with sorted keys). A type with a MarshalYAML method that is not MarshalYAML() (any, error) is a *TypeError. A Node is written as its value. A struct field tagged omitempty is left out when it is a struct whose fields are all zero, or whose IsZero method returns true.

time.Time is written as an RFC 3339 timestamp, time.Duration as its String form, *big.Int as an integer, and []byte as a !!binary scalar. A string that is not valid UTF-8 is also written as !!binary, because no other form keeps its bytes; some readers (goccy) return such a scalar as []byte rather than a string.

Marshal reports a *TypeError for a value with no YAML form (channels, functions, complex numbers) and a *LimitError when nesting or a cycle exceeds the depth limit.

Example

Strings that another reader could take for a number, boolean or null are quoted, so the text reads back as the same string everywhere.

package main

import (
	"fmt"

	oneyaml "oneyaml.dev/go"
)

func main() {
	type Server struct {
		Name    string   `yaml:"name"`
		Port    int      `yaml:"port"`
		Enabled bool     `yaml:"enabled"`
		Tags    []string `yaml:"tags,omitempty"`
		Version string   `yaml:"version"`
		Notes   string   `yaml:"notes"`
	}
	out, err := oneyaml.Marshal(Server{
		Name:    "api",
		Port:    8080,
		Enabled: true,
		Tags:    []string{"web", "yes"},
		Version: "1.10",
		Notes:   "first line\nsecond line\n",
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Print(string(out))
}
Output:
name: api
port: 8080
enabled: true
tags:
  - web
  - 'yes'
version: '1.10'
notes: |
  first line
  second line
Example (JsonTags)

Marshal honours json tags when a field has no yaml tag, and writes struct fields in declaration order, where a round trip through JSON sorts them.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	type Container struct {
		Name  string   `json:"name"`
		Image string   `json:"image"`
		Args  []string `json:"args,omitempty"`
	}
	out, err := oneyaml.Marshal(Container{Name: "web", Image: "nginx:1.25", Args: []string{"--port", "8080"}})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(out))
}
Output:
name: web
image: nginx:1.25
args:
  - --port
  - '8080'
Example (Map)

Map keys are written in sorted order, so output is the same on every run.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	out, err := oneyaml.Marshal(map[string]any{
		"zebra": 1,
		"apple": []any{1.5, nil, "x: y"},
		"empty": map[string]any{},
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(out))
}
Output:
apple:
  - 1.5
  - null
  - 'x: y'
empty: {}
zebra: 1
Example (Omitempty)

omitempty leaves out zero values, empty slices, maps and strings, and a struct whose fields are all zero. omitzero leaves out only zero values, so an empty but non-nil slice is kept.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	type Resources struct {
		CPU    string `yaml:"cpu"`
		Memory string `yaml:"memory"`
	}
	out, err := oneyaml.Marshal(struct {
		Name      string    `yaml:"name"`
		Resources Resources `yaml:"resources,omitempty"`
		Args      []string  `yaml:"args,omitempty"`
		Env       []string  `yaml:"env,omitzero"`
	}{Name: "api", Args: []string{}, Env: []string{}})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(out))
}
Output:
name: api
env: []

func Pointer added in v1.0.0

func Pointer(tokens ...string) string

Pointer builds an RFC 6901 pointer from keys and indices, writing "~" as "~0" and "/" as "~1" inside each one. Use it whenever a key comes from data, since keys such as app.kubernetes.io/name hold a slash:

Pointer("metadata", "labels", "app.kubernetes.io/name")
// /metadata/labels/app.kubernetes.io~1name

Pointer() with no tokens is the empty pointer, which addresses the whole document. An index is written as its digits ("0"); "-" names the place after the last item of a sequence, for Set. The result is what Document.Get, Set, Remove, Rename and the patch operations take, and what Node.Pointer returns for the same place.

To find the node a pointer addresses, pass it to Document.Get with a *Node; Node.Pointer gives the pointer of a node you already hold.

Example
package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("metadata:\n  labels:\n    app.kubernetes.io/name: web\n"))
	if err != nil {
		log.Fatal(err)
	}
	ptr := oneyaml.Pointer("metadata", "labels", "app.kubernetes.io/name")
	fmt.Println(ptr)
	if err := doc.Set(ptr, "api"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(doc)
}
Output:
/metadata/labels/app.kubernetes.io~1name
metadata:
  labels:
    app.kubernetes.io/name: api

func Unmarshal

func Unmarshal(data []byte, v any, opts ...Option) error

Unmarshal reads the first YAML document in data into the value v points to.

v must be a non-nil pointer. Structs are filled by key (see the tag rules on Marshal); maps, slices, arrays, pointers, interfaces and the scalar kinds are filled by shape. A plain scalar is read with the YAML 1.2 Core schema, so plain yes is a string and plain 0755 is the integer 755. A quoted scalar, such as "0755", is always a string, and an explicit tag such as !!int or !!str overrides the resolution. A null value leaves a struct field, number or string as it was and clears a pointer, map, slice or interface.

Into an interface{} a mapping becomes map[string]any, or map[any]any when a key is not a string; a sequence becomes []any; an integer becomes int, or uint64 or *big.Int when it does not fit; a float becomes float64; null becomes nil; and everything else is a string, timestamps and !!binary text included. A tag the Core schema does not resolve is ignored.

Types may read themselves. The hooks are tried in this order: Unmarshaler (UnmarshalYAML with a Node), the older form UnmarshalYAML(func(any) error) error that yaml.v2, yaml.v3 and goccy use, encoding.TextUnmarshaler for a scalar, then json.Unmarshaler. A json.Unmarshaler is given the value as JSON text, so types written for encoding/json (Kubernetes quantities, say) need no YAML code; a value JSON cannot hold, such as a mapping with a key that is not a string or .nan, is a *TypeError. Numbers are passed with the digits as written, on their own or inside a mapping or sequence, so 12345678901234567890.5 reaches UnmarshalJSON unrounded; a form JSON cannot spell, such as 0x1F or .5, is rewritten as a JSON number. A type with an UnmarshalYAML method of any other form, yaml.v3's UnmarshalYAML(*yaml.Node) error for one, is a *TypeError and is not filled by its fields. A null value clears a pointer target without calling any hook; of the other targets, only an Unmarshaler is called for a null value. time.Time reads a timestamp, time.Duration its String form, *big.Int any integer, and []byte a !!binary scalar (or the text of any other scalar).

A Node is a view into a Document and is not a target: decoding into one is a *TypeError. Read it with Document.Get(pointer, &node).

A mapping key that occurs twice is an error, judged after resolution, so 1 and 0x1 clash, and so do 1e400 and 10e399, or 1e5 and 1e00005, which are the same number. Two keys that are different numbers but round to the same float64 (1e-400 and 2e-400, or 1e400 and .inf) are a *TypeError that says so, since a map cannot hold them apart. A key is a duplicate even when one of its entries did not fit its target, so a: 1 followed by a: x is a duplicate-key error. All .nan keys are one key, except in a map with string keys, whose keys are compared as text. A key that no field of a struct target has counts too, as do the keys inside a value the target ignores, so a: 1 twice is refused even when the struct has no field a. A repeated key written as an alias is reported at the alias. Merge keys are ordinary keys unless the MergeKeys option is given. Unknown struct keys are skipped unless KnownFields is given.

A \u or \U escape in a double-quoted scalar must name a Unicode scalar value. An escaped UTF-16 surrogate pair, as JSON writes a character past U+FFFF, is one character ("\ud83d\ude00" is U+1F600); a lone or mismatched surrogate, or a value past U+10FFFF, is a *SyntaxError.

Aliases are decoded by replaying the anchored node; the alias limits bound how much that may produce. If data holds more than one document only the first is read; if it holds none, v is left unchanged and the error is nil.

The error is a *SyntaxError for malformed input, a *TypeError for values that do not fit their targets and a *LimitError when a limit is exceeded. A value that does not fit is skipped and its target keeps what it held, and the rest of the document is still read; the *TypeError describes the first mismatch and lists the others in its More field. A nil pointer target stays nil when its value does not fit. At most 100 mismatches are collected; the read stops at the 100th, and fewer are reported when their paths are very long (see TypeError). A syntax error, a limit error or an error from a hook of your own stops the read at once, and the error returned is that error alone: the mismatches collected before it are dropped. A syntax error after the value, such as a stray closing bracket, is reported even when the value did not fit. UTF-16 and UTF-32 input, with or without a byte order mark, is converted to UTF-8 first; the offsets of errors in such input index the UTF-8 form.

Example
src := []byte(`# deploy settings
name: api
replicas: 2
ports: [80, 443]
labels:
  tier: web
`)
var cfg struct {
	Name     string            `yaml:"name"`
	Replicas int               `yaml:"replicas"`
	Ports    []int             `yaml:"ports"`
	Labels   map[string]string `yaml:"labels"`
}
if err := oneyaml.Unmarshal(src, &cfg); err != nil {
	log.Fatal(err)
}
fmt.Println(cfg.Name, cfg.Replicas, cfg.Ports, cfg.Labels)
Output:
api 2 [80 443] map[tier:web]
Example (CollectionKey)

Generic values hold what Go maps can hold. A collection used as a mapping key cannot be a Go map key, so Unmarshal into any refuses it; a Document reads it.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	src := []byte("[a, b]: c\n")
	var v any
	fmt.Println(oneyaml.Unmarshal(src, &v) != nil)

	doc, err := oneyaml.Parse(src)
	if err != nil {
		log.Fatal(err)
	}
	key, value := doc.Root().Entry(0)
	fmt.Println(key.Kind(), key.Len(), value.Value())
}
Output:
true
sequence 2 c
Example (FuncHook)
package main

import (
	"fmt"
	"log"
	"strings"

	oneyaml "oneyaml.dev/go"
)

// Tags reads either a sequence or a comma-separated string. It uses the
// UnmarshalYAML form of yaml.v2, yaml.v3 and goccy, so code written for them
// keeps working.
type Tags []string

func (t *Tags) UnmarshalYAML(unmarshal func(any) error) error {
	var list []string
	if err := unmarshal(&list); err == nil {
		*t = list
		return nil
	}
	var one string
	if err := unmarshal(&one); err != nil {
		return err
	}
	*t = strings.Split(one, ",")
	return nil
}

func main() {
	var doc struct {
		A Tags `yaml:"a"`
		B Tags `yaml:"b"`
	}
	if err := oneyaml.Unmarshal([]byte("a: [x, y]\nb: \"p,q,r\"\n"), &doc); err != nil {
		log.Fatal(err)
	}
	fmt.Println(doc.A, doc.B)
}
Output:
[x y] [p q r]
Example (Generic)
var v any
err := oneyaml.Unmarshal([]byte("a: 1\nb: [x, 2.5, yes, null]\n"), &v)
fmt.Println(v, err)
Output:
map[a:1 b:[x 2.5 yes <nil>]] <nil>
Example (Json)

JSON is YAML, so a JSON document reads with Unmarshal as it is, and integers above 2^53 stay exact.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	var v map[string]any
	if err := oneyaml.Unmarshal([]byte(`{"id": 12345678901234567890, "ok": true}`), &v); err != nil {
		log.Fatal(err)
	}
	fmt.Println(v["id"], v["ok"])
}
Output:
12345678901234567890 true
Example (JsonHook)

A type that implements json.Unmarshaler or json.Marshaler and no YAML hook is read and written through that hook: the YAML value becomes JSON text first.

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"strings"

	oneyaml "oneyaml.dev/go"
)

// Level is written for encoding/json: it reads and writes the words "low" and
// "high" and nothing else.
type Level int

func (l *Level) UnmarshalJSON(b []byte) error {
	var s string
	if err := json.Unmarshal(b, &s); err != nil {
		return err
	}
	switch strings.ToLower(s) {
	case "low":
		*l = 1
	case "high":
		*l = 2
	default:
		return fmt.Errorf("unknown level %q", s)
	}
	return nil
}

func (l Level) MarshalJSON() ([]byte, error) {
	return json.Marshal([]string{"none", "low", "high"}[l])
}

// A type that implements json.Unmarshaler or json.Marshaler and no YAML hook is
// read and written through that hook: the YAML value becomes JSON text first.
func main() {
	var cfg struct {
		Level Level `yaml:"level"`
	}
	if err := oneyaml.Unmarshal([]byte("level: high\n"), &cfg); err != nil {
		log.Fatal(err)
	}
	fmt.Println(cfg.Level)

	out, err := oneyaml.Marshal(cfg)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(out))

	// The error from UnmarshalJSON comes back inside a *TypeError.
	err = oneyaml.Unmarshal([]byte("level: loud\n"), &cfg)
	fmt.Println(err)
}
Output:
2
level: high
oneyaml: line 1, column 8: /level: cannot unmarshal "loud" into oneyaml_test.Level: unknown level "loud"
Example (JsonTags)

Types that carry only json tags, as Kubernetes types do, read without a yaml tag; any yaml tag on a field takes the place of its json tag.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	var c struct {
		Name  string `json:"name"`
		Image string `json:"image,omitempty"`
	}
	if err := oneyaml.Unmarshal([]byte("name: web\nimage: nginx:1.25\n"), &c); err != nil {
		log.Fatal(err)
	}
	fmt.Println(c.Name, c.Image)
}
Output:
web nginx:1.25
Example (TypeError)
var cfg struct {
	Port int `yaml:"port"`
}
err := oneyaml.Unmarshal([]byte("name: api\nport: eighty\n"), &cfg)
var te *oneyaml.TypeError
if errors.As(err, &te) {
	fmt.Printf("%s at line %d, column %d: %s\n", te.Path, te.Line, te.Column, te.Msg)
}
Output:
/port at line 2, column 7: cannot unmarshal !!str `eighty` into int
Example (YesIsAString)

The primary API reads YAML 1.2: yes is a string, not a boolean, so a bool field refuses it. The compat packages read the way their targets do.

package main

import (
	"errors"
	"fmt"

	oneyaml "oneyaml.dev/go"
)

func main() {
	var cfg struct {
		Debug bool `yaml:"debug"`
	}
	err := oneyaml.Unmarshal([]byte("debug: yes\n"), &cfg)
	var te *oneyaml.TypeError
	if errors.As(err, &te) {
		fmt.Println(te.Path, te.Line, te.Column)
	}
}
Output:
/debug 1 8

Types

type Category added in v1.0.0

type Category string

Category is the kind of a SyntaxError. The set is closed: a limit that is hit is a *LimitError, not a syntax error. Switch on it, or match with errors.Is the sentinel ErrSyntax, ErrEncoding or ErrDuplicateKey.

const (
	// CategorySyntax is any violation of the YAML grammar, including characters
	// that YAML does not allow and an alias to an anchor that was never defined.
	CategorySyntax Category = "syntax"
	// CategoryEncoding is input that is not valid UTF-8, UTF-16 or UTF-32.
	CategoryEncoding Category = "encoding"
	// CategoryDuplicateKey is a mapping that has the same key twice. Key,
	// FirstLine and FirstColumn say which key and where it first appeared.
	CategoryDuplicateKey Category = "duplicate-key"
)

The values of SyntaxError.Category.

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

A Decoder reads YAML documents from an input stream, one per call to Decode.

It reads ahead only as far as the end of the current document: a line that starts with "---" or "..." at column 0 ends a document wherever it occurs, since YAML forbids those markers inside any node. The document is then parsed from the buffer, so a Decoder never holds more than one document and the bytes after it.

Example
stream := "name: first\n---\nname: second\n---\nname: third\n"
dec := oneyaml.NewDecoder(strings.NewReader(stream))
for {
	var doc struct {
		Name string `yaml:"name"`
	}
	err := dec.Decode(&doc)
	if err == io.EOF {
		break
	}
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(doc.Name)
}
Output:
first
second
third

func NewDecoder

func NewDecoder(r io.Reader, opts ...Option) *Decoder

NewDecoder returns a Decoder that reads from r. The options are those of Unmarshal.

func (*Decoder) Decode

func (d *Decoder) Decode(v any) error

Decode reads the next document into the value v points to, as Unmarshal does. It returns io.EOF when there are no more documents. After a syntax error or a read error every later call returns the same error; after a *TypeError the next call reads the next document. An empty document decodes as null.

type Document

type Document struct {
	// contains filtered or unexported fields
}

A Document is a YAML stream held losslessly: every byte of the input, including comments, blank lines, indentation and line breaks, comes back from Bytes unless an edit changed it.

The methods address the first document of the stream; Len and Doc reach the others. A Document returned by Doc is a view that shares the stream: an edit through it changes the stream, and every view sees the change.

Edits are exact and minimal. Set writes the new value in the style of the value it replaces when that style can hold it, and falls back to the nearest style that can otherwise; text outside the edited value is not touched. An edit that fails leaves the document as it was. An edit makes every Node read before it stale; Node.Stale reports it. An edit, patch or merge patch that fails is rolled back completely and leaves the Nodes read before it valid. Read nodes again after editing, or collect their pointers first (Node.Pointer). A Document is not safe for concurrent use.

A nil *Document, which is what a failed Parse returns, is safe to call: getters return zero values (Len is 0, Bytes is nil, Root is the zero Node) and methods that decode, edit or patch return an error matching ErrNilDocument.

func Parse

func Parse(data []byte, opts ...Option) (*Document, error)

Parse reads data as a YAML stream and returns it as a Document. The data is copied; later changes to the slice do not affect the Document.

A stream with no documents (empty input) yields a Document of length 0. Syntax errors are reported as *SyntaxError and exceeded limits as *LimitError. A \u or \U escape that is not a Unicode scalar value, such as a lone surrogate, is a *SyntaxError, as it is for Unmarshal; an escaped surrogate pair, as JSON writes, is one character.

A mapping key that occurs twice does not stop Parse, so a file with one can be loaded and its other parts read and edited. Reading a part that holds the repeated key reports it, by the rules of Unmarshal, and a pointer that goes through it is refused with ErrAmbiguousKey.

Example

Parse keeps the whole file. Set rewrites one value in its own style and leaves the comments, the blank lines and every other byte alone.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	src := []byte(`# deploy settings
name: api
replicas: 2   # raised by the release job
script: |
  make build
`)

	doc, err := oneyaml.Parse(src)
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/replicas", 3); err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/script", "make build\nmake test\n"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))

	var replicas int
	if err := doc.Get("/replicas", &replicas); err != nil {
		log.Fatal(err)
	}
	fmt.Println(replicas)
}
Output:
# deploy settings
name: api
replicas: 3   # raised by the release job
script: |
  make build
  make test
3

func (*Document) Apply

func (d *Document) Apply(ops ...PatchOp) error

Apply applies RFC 6902 operations to the document in order, all or nothing, as ApplyPatch does.

Example

Apply takes the same operations as Go values.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("a: 1\nb: 2\n"))
	if err != nil {
		log.Fatal(err)
	}
	err = doc.Apply(
		oneyaml.PatchTest("/a", 1),
		oneyaml.PatchMove("/b", "/c"),
		oneyaml.PatchAdd("/d", []string{"x", "y"}),
	)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
a: 1
c: 2
d:
  - x
  - y
Example (TestByValue)

A "test" operation compares by the YAML data model: 1 is an integer and 1.0 is a float, so they are different.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("n: 1\n"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(doc.Apply(oneyaml.PatchTest("/n", 1)))
	err = doc.Apply(oneyaml.PatchTest("/n", 1.0))
	fmt.Println(errors.Is(err, oneyaml.ErrTestFailed))
}
Output:
<nil>
true

func (*Document) ApplyMergePatch

func (d *Document) ApplyMergePatch(rfc7396 []byte) error

ApplyMergePatch applies an RFC 7396 JSON Merge Patch, given as JSON text or as YAML, to the document. A member of the patch that is null deletes the member of the same name; an object member is merged into the mapping it meets; any other value replaces what is there, and a sequence replaces the whole sequence. A patch that is not an object replaces the whole document.

The patch is atomic: on any error the document is left as it was, byte for byte. Members that the patch adds go at the end of their mapping in the style of their siblings, and comments and layout outside the changed values stay. A null in the target is kept unless the patch names it. Merging an object into a mapping that is an alias is refused (ErrAliasWrite) because it would change every other use of the anchor; an alias to something else is replaced. With MergeKeys, a null member for a key that only a "<<" merge source has is refused (ErrAliasWrite) for the same reason; a member that sets such a key adds a key of the mapping's own.

Text whose first significant byte is "{" or "[" is read as JSON only, with JSON errors, and so is any text that is valid JSON. Other text is read as one YAML document, which may be any value, as a merge patch kept in a YAML file is written, and gives the same result as the JSON form. The refusals are those of ApplyPatch for a YAML patch: aliases, tags outside the core schema, a collection as a key, a key that is not a string (a merge patch is a JSON object, so write `"1"`, not `1`), keys that repeat by value, a scalar with a bad escape and more than one document are all ErrInvalidPatch, a YAML syntax error is reported with its position, and nesting past the depth limit is a *LimitError.

The result may not be longer than a MaxInputBytes limit, the patch text may not be longer than an eighth of it (see LimitSet), and the text its edits have to parse again is bounded (MaxPatchWorkRatio); past a bound the call fails with a *LimitError and changes nothing.

The patch applies to the first document, or to the document a view from Doc selects.

Example

ApplyMergePatch applies an RFC 7396 merge patch: null deletes a member, an object merges into the mapping it meets, and anything else replaces.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte(`# settings
log:
  level: info   # default
  file: app.log
features: [a, b]
`))
	if err != nil {
		log.Fatal(err)
	}
	err = doc.ApplyMergePatch([]byte(`{"log": {"level": "debug", "file": null}, "features": ["c"], "debug": true}`))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
# settings
log:
  level: debug   # default
features: [c]
debug: true
Example (Alias)

Merge patches replace arrays whole and refuse to merge into an alias.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("base: &b {x: 1}\ncopy: *b\n"))
	if err != nil {
		log.Fatal(err)
	}
	err = doc.ApplyMergePatch([]byte(`{"copy": {"x": 2}}`))
	fmt.Println(errors.Is(err, oneyaml.ErrAliasWrite))
}
Output:
true
Example (Yaml)

A merge patch kept as a YAML file is read like its JSON form. Text that starts with { or [ is read as JSON.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte(`name: api
limits:
  cpu: 1   # cores
  mem: 2
`))
	if err != nil {
		log.Fatal(err)
	}
	err = doc.ApplyMergePatch([]byte(`limits:
  cpu: 4
  mem: null
tier: gold
`))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
name: api
limits:
  cpu: 4   # cores
tier: gold

func (*Document) ApplyPatch

func (d *Document) ApplyPatch(rfc6902 []byte) error

ApplyPatch applies JSON Patch operations (RFC 6902) with YAML equality for "test", given as JSON text or as a YAML block list of operations, to the document. The patch is atomic: if any operation fails, including a "test" that does not match, the document is left exactly as it was, byte for byte, and the error is an *EditError that names the operation.

The operations follow the RFC, with these points where YAML needs a decision:

  • Pointers address mapping keys by their scalar text, and a key that occurs twice is ambiguous (ErrAmbiguousKey).
  • Reads follow aliases, including "from" of copy and the paths of "test". Writing below an alias is refused (ErrAliasWrite); replacing the alias node itself is allowed.
  • "test" compares by the YAML data model under the Core schema: 1 differs from 1.0, 0x1F equals 31, a quoted "1" is a string, an explicit core tag such as !!float decides the type of its scalar, and mappings are equal whatever the order of their keys.
  • "copy" writes the value without its anchor, so no anchor is defined twice. An alias inside the copied value stays an alias while its anchor is still defined once, before the destination; otherwise it is written out.
  • "move" refuses an anchored value that aliases still use (ErrAnchorInUse), and a destination inside the value being moved (ErrInvalidPatch).
  • What a patch may write is bounded. The text copies add up to at most the alias bytes limit (ten times the document, never less than 64 MiB by default), and with a MaxInputBytes limit no step may leave a document larger than it; a patch past either bound fails with a *LimitError and changes nothing. The text its edits have to parse again is bounded too (MaxPatchWorkRatio), and with a MaxInputBytes limit the patch text itself may not be longer than an eighth of it (never less than 64 KiB, and never more than the limit). NoLimits removes the bounds.
  • With MergeKeys, "test" reads a key that a "<<" merge brings in, as Get does, and so does the "from" of "copy" (the source stays unchanged). "remove", "replace" and "move" of a key that only a merge source has, and writes below it, are refused (ErrAliasWrite), because they would change the source; "add" of such a key adds a key of the mapping's own.
  • New members go at the end of their mapping, in the style of their siblings; comments outside the edited values stay.

Text whose first significant byte is "[" or "{" is read as JSON only, with JSON errors; so is a flow-style YAML patch such as [{op: add, ...}]. Other text is read as YAML: one document holding a block list of operations with the same members, as a patch kept in a YAML file is written, gives the same result as the JSON form. The non-specific tag "!" is allowed and makes a scalar a string. A YAML patch is refused with ErrInvalidPatch when it uses aliases, a tag outside the core schema, a collection as a key, or keys that repeat by value as the Core schema reads them (1 and 0x1, null and ~); when it holds more than one document; or when it has a scalar the decoder cannot read (a bad escape). A YAML syntax error is reported with its line and column. Text that is neither JSON nor a YAML list fails with the JSON error, and a YAML patch nested past the depth limit fails with a *LimitError.

A patch applies to the first document, or to the document a view from Doc selects.

Example

ApplyPatch applies an RFC 6902 patch in one step. Comments and layout outside the changed values stay, and if any operation fails nothing changes.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte(`# service
name: api
replicas: 2   # raised by the release job
tags: [web]
`))
	if err != nil {
		log.Fatal(err)
	}
	err = doc.ApplyPatch([]byte(`[
		{"op": "test", "path": "/name", "value": "api"},
		{"op": "replace", "path": "/replicas", "value": 3},
		{"op": "add", "path": "/tags/-", "value": "edge"},
		{"op": "copy", "from": "/name", "path": "/title"}
	]`))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))

	// A failing test rejects the whole patch.
	err = doc.ApplyPatch([]byte(`[
		{"op": "replace", "path": "/replicas", "value": 9},
		{"op": "test", "path": "/name", "value": "web"}
	]`))
	var ee *oneyaml.EditError
	fmt.Println(errors.Is(err, oneyaml.ErrTestFailed), errors.As(err, &ee) && ee.Index == 1)
	fmt.Print(string(doc.Bytes()))
}
Output:
# service
name: api
replicas: 3   # raised by the release job
tags: [web, edge]
title: api
true true
# service
name: api
replicas: 3   # raised by the release job
tags: [web, edge]
title: api
Example (AliasRefused)

A patch never writes through an alias, and a refused patch changes nothing, not even the operations before the refused one.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("base: &b\n  x: 1\ncopy: *b\n"))
	if err != nil {
		log.Fatal(err)
	}
	err = doc.ApplyPatch([]byte(`[
		{"op": "replace", "path": "/base/x", "value": 2},
		{"op": "replace", "path": "/copy/x", "value": 3}
	]`))
	fmt.Println(errors.Is(err, oneyaml.ErrAliasWrite))
	fmt.Print(string(doc.Bytes()))
}
Output:
true
base: &b
  x: 1
copy: *b
Example (PointerText)

A pointer token matches a key by its text, so /1 addresses the integer key 1. Complex keys cannot be addressed.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("1: one\n\"2\": two\n"))
	if err != nil {
		log.Fatal(err)
	}
	err = doc.ApplyPatch([]byte(`[
		{"op": "replace", "path": "/1", "value": "uno"},
		{"op": "replace", "path": "/2", "value": "dos"}
	]`))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
1: uno
"2": dos
Example (Stream)

A patch applies to one document of a stream: the one the Document addresses.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("a: 1\n---\na: 1\n"))
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Doc(1).ApplyPatch([]byte(`[{"op": "replace", "path": "/a", "value": 2}]`)); err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
a: 1
---
a: 2
Example (Yaml)

A patch kept as a YAML file (a block list of operations) gives the same result as the JSON form.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("replicas: 2\nimage: nginx:1.25\n"))
	if err != nil {
		log.Fatal(err)
	}
	patch := `
- op: replace
  path: /replicas
  value: 5
- op: add
  path: /labels
  value:
    tier: web
`
	if err := doc.ApplyPatch([]byte(patch)); err != nil {
		log.Fatal(err)
	}
	fmt.Print(doc)
}
Output:
replicas: 5
image: nginx:1.25
labels:
  tier: web
Example (YamlRules)

Four rules follow from applying RFC 6902 to YAML. Two show here: test compares YAML values, and patch text that starts like JSON is read as JSON.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("replicas: 1\n"))
	if err != nil {
		log.Fatal(err)
	}
	// 1 is an integer and 1.0 a float, so this test fails.
	err = doc.ApplyPatch([]byte(`[{"op": "test", "path": "/replicas", "value": 1.0}]`))
	fmt.Println(errors.Is(err, oneyaml.ErrTestFailed))

	// Text that starts with [ is read as JSON, and these keys are not quoted.
	err = doc.ApplyPatch([]byte(`[{op: test, path: /replicas, value: 1}]`))
	fmt.Println(errors.Is(err, oneyaml.ErrInvalidPatch))

	// The same operation as a YAML block list.
	err = doc.ApplyPatch([]byte("- {op: test, path: /replicas, value: 1}\n"))
	fmt.Println(err)
}
Output:
true
true
<nil>

func (*Document) Bytes

func (d *Document) Bytes() []byte

Bytes returns the stream as text. The slice must not be modified. It stays valid after later edits, which build new text instead of changing it.

func (*Document) Decode

func (d *Document) Decode(v any, opts ...Option) error

Decode reads the document into the value v points to, with the rules and errors of Unmarshal: the same binder reads the stored tree that Unmarshal reads from the parser, so the two always agree. Aliases are expanded within the alias and depth limits the Document was parsed with, or those the options of the call set. A Document with no documents leaves v unchanged. If the view addresses a document that does not exist, the error is an *EditError matching ErrNotFound.

Options given to Decode apply to this read only, on top of those the Document was parsed with: Decode(&cfg, KnownFields()) reads strictly from a Document that was parsed without the option. Options that do not concern reading are ignored; see Option.

Example
package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("server:\n  host: example.org\n  ports: [80, 443]\n"))
	if err != nil {
		log.Fatal(err)
	}
	var cfg struct {
		Server struct {
			Host  string `yaml:"host"`
			Ports []int  `yaml:"ports"`
		} `yaml:"server"`
	}
	if err := doc.Decode(&cfg); err != nil {
		log.Fatal(err)
	}
	fmt.Println(cfg.Server.Host, cfg.Server.Ports)
}
Output:
example.org [80 443]

func (*Document) Doc

func (d *Document) Doc(i int) *Document

Doc returns a view of document i of the stream. The view shares the stream with d. If i is out of range every operation on the view reports ErrNotFound.

Example

A stream of several documents is one Document; Doc addresses one of them.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("a: 1\n---\nb: 2\n"))
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Doc(1).Set("/c", 3); err != nil {
		log.Fatal(err)
	}
	fmt.Println(doc.Len())
	fmt.Print(string(doc.Bytes()))
}
Output:
2
a: 1
---
b: 2
c: 3

func (*Document) ExplicitEnd

func (d *Document) ExplicitEnd() bool

ExplicitEnd reports whether the document ends with a "..." marker. It is false for a view of a document that does not exist.

func (*Document) ExplicitStart

func (d *Document) ExplicitStart() bool

ExplicitStart reports whether the document begins with a "---" marker. A document without the marker is the first of its stream, or follows one that ended with "...". It is false for a view of a document that does not exist.

func (*Document) Get

func (d *Document) Get(ptr string, v any, opts ...Option) error

Get reads the value at the RFC 6901 pointer ptr into v, as Decode does for the whole document: v is any target Unmarshal accepts. Aliases are followed, and expanding them is charged to the alias limits. A *TypeError's Path is the pointer ptr followed by the path inside the value.

With MergeKeys, a pointer also finds the keys a "<<" merge brings in, with the precedence Decode gives them: a key the mapping has itself wins, and an earlier merge source wins over a later one. The pointer to the merge key itself still reads the merged source as written. Merge sources are searched depth-first, each mapping once; the keys read count against MaxAliasNodes, and a search past it fails with a *LimitError. Without MergeKeys the pointer addresses only the keys as written.

If v is a *Node it receives the node itself, and the alias at the end of the pointer, if there is one, is not followed.

A pointer through a key that occurs twice in its mapping is refused with an *EditError matching ErrAmbiguousKey. A repeated key inside the value read is reported as Unmarshal reports it. Build a pointer from keys with Pointer.

Options apply to this read only, on top of those the Document was parsed with, as for Decode: Get("/spec", &spec, KnownFields()) reads one part strictly.

Example

Get reads a value by pointer; aliases are followed.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("base: &b\n  port: 8080\nreplica: *b\n"))
	if err != nil {
		log.Fatal(err)
	}
	var port int
	if err := doc.Get("/replica/port", &port); err != nil {
		log.Fatal(err)
	}
	fmt.Println(port)
}
Output:
8080
Example (Node)

A Node is a view into a Document, so it is read with Document.Get, never decoded into. Marshal writes a Node as the value it holds.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	src := []byte("name: api\nspec: {replicas: 2, tier: web}\n")

	var n oneyaml.Node
	err := oneyaml.Unmarshal(src, &n)
	var te *oneyaml.TypeError
	fmt.Println(errors.As(err, &te))

	doc, err := oneyaml.Parse(src)
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Get("/spec", &n); err != nil {
		log.Fatal(err)
	}
	fmt.Println(n.Kind(), n.Len())

	out, err := oneyaml.Marshal(struct {
		Spec oneyaml.Node `yaml:"spec"`
	}{n})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(out))
}
Output:
true
mapping 2
spec:
  replicas: 2
  tier: web
Example (Options)

Options given to Get apply to that read only, on top of the options the Document was parsed with. KnownFields(false) turns off a KnownFields given earlier.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	src := []byte("name: api\nspec:\n  replicas: 2\n  replica: 3\n")
	type Spec struct {
		Replicas int `yaml:"replicas"`
	}

	// Parsed without options, read strictly in one place.
	doc, err := oneyaml.Parse(src)
	if err != nil {
		log.Fatal(err)
	}
	var strict Spec
	err = doc.Get("/spec", &strict, oneyaml.KnownFields())
	fmt.Println(errors.Is(err, oneyaml.ErrUnknownField))

	// Parsed strictly, read loosely in one place.
	doc, err = oneyaml.Parse(src, oneyaml.KnownFields())
	if err != nil {
		log.Fatal(err)
	}
	var loose Spec
	if err := doc.Get("/spec", &loose, oneyaml.KnownFields(false)); err != nil {
		log.Fatal(err)
	}
	fmt.Println(loose.Replicas)
}
Output:
true
2

func (*Document) Len

func (d *Document) Len() int

Len returns the number of documents in the stream.

func (*Document) Remove

func (d *Document) Remove(ptr string) error

Remove deletes the value at ptr, with its key or its place in the sequence. A comment on the line of the entry, a comment directly above it and a comment inside its value go with it. Removing the last entry of a nested collection leaves {} or []. Removing an anchored node that an alias still uses is refused (ErrAnchorInUse). With MergeKeys, removing a key that only a "<<" merge source has is refused (ErrAliasWrite), because it would change the source.

Example

Remove deletes an entry together with the comment on its line, the comment directly above it and any comment inside its value. A comment above the next entry stays.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("a: 1\n# about b\nb: 2 # two\n# about c\nc: 3\n"))
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Remove("/b"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
a: 1
# about c
c: 3
Example (AnchorInUse)

An edit that would leave an alias without its anchor is refused, and the document is unchanged.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("base: &b\n  x: 1\ncopy: *b\n"))
	if err != nil {
		log.Fatal(err)
	}
	err = doc.Remove("/base")
	fmt.Println(errors.Is(err, oneyaml.ErrAnchorInUse))
	fmt.Print(string(doc.Bytes()))
}
Output:
true
base: &b
  x: 1
copy: *b
Example (Collected)

Pointers collected before an edit are addresses: removing an item moves the ones after it. Removing from the end keeps the earlier pointers valid.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("tags:\n  - web\n  - old\n  - api\n  - old\n"))
	if err != nil {
		log.Fatal(err)
	}
	tags, ok := doc.Root().Lookup("tags")
	if !ok {
		log.Fatal("no tags")
	}
	var ptrs []string
	for t := range tags.Items() {
		if t.Value() != "old" {
			continue
		}
		p, err := t.Pointer()
		if err != nil {
			log.Fatal(err)
		}
		ptrs = append(ptrs, p)
	}
	fmt.Println(ptrs)

	// Remove from the end: /tags/3, then /tags/1. In the other order,
	// removing /tags/1 would move "old" from /tags/3 to /tags/2.
	for i := len(ptrs) - 1; i >= 0; i-- {
		if err := doc.Remove(ptrs[i]); err != nil {
			log.Fatal(err)
		}
	}
	fmt.Print(doc)
}
Output:
[/tags/1 /tags/3]
tags:
  - web
  - api

func (*Document) Rename added in v0.2.0

func (d *Document) Rename(ptr, newKey string) error

Rename changes the key of the mapping entry at ptr to newKey, in place. The value, the entry's position in its mapping, its comments and the layout around it stay as they are; only the key text changes. The new key is written in the style of the old one (plain, single- or double-quoted) when that style can hold it, and otherwise in the nearest style that can: a plain key that needs quoting becomes single-quoted, and one with a line break or an unprintable character becomes double-quoted. The text is always read as a string: renaming the key 1 to "2" gives the quoted key '2'. An anchor or tag on the key stays.

After a successful Rename, Get of the parent pointer with newKey gives the old value and the old key is gone. Renaming a key to the text it already has changes nothing.

Rename refuses, and leaves the document unchanged, with:

  • ErrKeyExists when the mapping already has a key reading as newKey;
  • ErrNotAnEntry when ptr is the root or addresses an item of a sequence;
  • ErrNotFound when ptr does not exist;
  • ErrAliasWrite when ptr leads through an alias (renaming an entry whose value is an alias is allowed, as replacing it is);
  • ErrAmbiguousKey when the key at ptr occurs twice;
  • ErrAnchorInUse when the key carries an anchor that an alias later in the document uses, as Remove does, since the alias would read the new key;
  • ErrUnsupportedEdit for a key that is empty, a collection, an alias or a block scalar, for the merge key "<<", for a new key longer than 1000 bytes or not valid UTF-8, and for a tag on the key that would not fit the new text.

The edit parses only the entries around the key when it can, as Set does, and the whole document otherwise. The document a view from Doc selects is the one edited.

Example

Rename changes a key where it stands. The value, the place in the mapping and the comments stay; a key that already exists is refused.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte(`# chart values
image: nginx
replicas: 2 # scaled by the autoscaler
port: 80
`))
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Rename("/replicas", "replicaCount"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(doc)

	err = doc.Rename("/port", "image")
	fmt.Println(errors.Is(err, oneyaml.ErrKeyExists))
}
Output:
# chart values
image: nginx
replicaCount: 2 # scaled by the autoscaler
port: 80
true

func (*Document) Root

func (d *Document) Root() Node

Root returns the root node of the document. The zero Node is returned if the document does not exist.

func (*Document) Set

func (d *Document) Set(ptr string, v any) error

Set replaces the value at the RFC 6901 pointer ptr with v, or adds it when the pointer names a mapping key that does not exist or the end of a sequence ("-" or the length).

v is anything Marshal accepts. The value that is replaced keeps its style (plain, quoted, literal or folded block, flow or block) when that style can hold the new value; otherwise only that value falls back to the nearest style that can. A new member goes at the end of its mapping and copies the style of its siblings. Reading the document back gives exactly v.

Set refuses to write below an alias (ErrAliasWrite) and a key that occurs twice (ErrAmbiguousKey). Replacing the alias node itself is allowed. With MergeKeys, setting a key that only a "<<" merge source has adds a key of the mapping's own, which overrides the merged one; the source is not changed. On any error the document is unchanged and the error is an *EditError.

Example

A string that cannot stay plain is quoted, a value added to a mapping goes at its end in the style of its siblings, and removing the last entry of a nested mapping leaves {}.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("a: 'x'\nb:\n  c: 1\n"))
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/a", "true"); err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/d", "new"); err != nil {
		log.Fatal(err)
	}
	if err := doc.Remove("/b/c"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
a: 'true'
b: {}
d: 'new'
Example (Alias)

Writing below an alias would change every other place that shares the anchored node, so it is refused and nothing changes.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("base: &b\n  x: 1\ncopy: *b\n"))
	if err != nil {
		log.Fatal(err)
	}
	err = doc.Set("/copy/x", 2)
	fmt.Println(errors.Is(err, oneyaml.ErrAliasWrite))
	err = doc.Set("/base/x", 2)
	fmt.Println(err)
	fmt.Print(string(doc.Bytes()))
}
Output:
true
<nil>
base: &b
  x: 2
copy: *b
Example (AnchorsAndTags)

An anchor stays on a replaced value, so every alias reads the new value. A tag stays when the new value reads back as exactly that value under it, and replacing an alias itself writes the new value in its place.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("a: &x 1\nb: *x\nt: !custom foo\n"))
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/a", 2); err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/t", "bar"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))

	var b int
	if err := doc.Get("/b", &b); err != nil {
		log.Fatal(err)
	}
	fmt.Println(b)
	if err := doc.Set("/b", 9); err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
a: &x 2
b: *x
t: !custom bar
2
a: &x 2
b: 9
t: !custom bar
Example (NewKey)

A new key goes at the end of its mapping and copies the style of its siblings. A null that is already there keeps its spelling.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("# ports\nname: 'api'\nport: 80\nnote: ~\n"))
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/owner", "ops"); err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/note", nil); err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/port", 8080); err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
# ports
name: 'api'
port: 8080
note: ~
owner: 'ops'
Example (StyleFallback)

When the existing style cannot hold the new value, only that value moves to the nearest style that can. A plain scalar stays plain when the Core schema reads it back as the same string, and becomes quoted when it would not.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("a: plain\nb: plain\nc: plain\nd: \"quoted\"\n"))
	if err != nil {
		log.Fatal(err)
	}
	for _, edit := range []struct {
		ptr string
		v   any
	}{
		{"/a", "nginx:1.25"},  // Core reads this as a string: it stays plain
		{"/b", "true"},        // Core reads this as a boolean: it is quoted
		{"/c", "two\nlines"},  // a line break needs an escape: double quotes
		{"/d", "now: plain?"}, // a double-quoted scalar stays double-quoted
	} {
		if err := doc.Set(edit.ptr, edit.v); err != nil {
			log.Fatal(err)
		}
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
a: nginx:1.25
b: 'true'
c: "two\nlines"
d: "now: plain?"

func (*Document) SetHeadComment added in v0.2.0

func (d *Document) SetHeadComment(ptr, text string) error

SetHeadComment replaces the comment on the lines directly above the entry or item at ptr (the comment Node.HeadComment reads) by text, or removes it when text is empty. Each line of text without a leading "#" gets "# " in front, a blank line inside the text becomes a bare "#", and the lines are written at the entry's indentation. The root pointer "" sets the comment above the first content line of the document, below a "---" marker line.

A head comment stands on lines of its own, so an entry that shares its line with another (the first key of a mapping that is an item written after "- ") has none: SetHeadComment refuses it with ErrUnsupportedEdit, as it does an entry of a flow collection. Text that is not valid UTF-8 or holds a line separator other than the line feed, or another control character, is refused with ErrInvalidComment. ErrNotFound and ErrAliasWrite are reported as for Set. The document reads back with the same values and only the comment lines changed.

Example

SetHeadComment writes comment lines above an entry at its indentation and replaces the ones that were there; an empty text removes them.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte(`service:
  # old note
  port: 80
  host: example.org
`))
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.SetHeadComment("/service/port", "the public port"); err != nil {
		log.Fatal(err)
	}
	if err := doc.SetHeadComment("/service/host", "first line\nsecond line"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(doc)

	if err := doc.SetHeadComment("/service/host", ""); err != nil {
		log.Fatal(err)
	}
	fmt.Print(doc)
}
Output:
service:
  # the public port
  port: 80
  # first line
  # second line
  host: example.org
service:
  # the public port
  port: 80
  host: example.org

func (*Document) SetLineComment added in v0.2.0

func (d *Document) SetLineComment(ptr, text string) error

SetLineComment replaces the comment after the value at ptr on its line (the comment Node.LineComment reads) by text, or removes it when text is empty. Text without a leading "#" gets "# " in front. Text with a line break is refused with ErrInvalidComment, as is text that is not valid UTF-8 or holds a control character.

The comment goes after the value: after a scalar, after the closing bracket of a flow collection that is the value, after the header of a block scalar ("|", ">-"), and after the colon or dash of an entry whose value is a block collection or empty. A node inside a flow collection has a place only when nothing, not even a comma, follows it on its line, as for the last item of a collection written one item per line; otherwise, and for the root block collection and a value that something else follows on its line, SetLineComment refuses with ErrUnsupportedEdit. ErrNotFound and ErrAliasWrite are reported as for Set. The document reads back with the same values and only the comment changed.

Example

SetLineComment writes the comment after a value on its line, after the header of a block scalar, or after the colon of an entry whose value is a collection.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte(`replicas: 2
script: |
  make build
env:
  mode: prod
`))
	if err != nil {
		log.Fatal(err)
	}
	for ptr, text := range map[string]string{
		"/replicas": "scaled by the autoscaler",
		"/script":   "run by CI",
		"/env":      "per environment",
	} {
		if err := doc.SetLineComment(ptr, text); err != nil {
			log.Fatal(err)
		}
	}
	fmt.Print(doc)

	replicas, _ := doc.Root().Lookup("replicas")
	fmt.Println(replicas.LineComment())
}
Output:
replicas: 2 # scaled by the autoscaler
script: | # run by CI
  make build
env: # per environment
  mode: prod
# scaled by the autoscaler

func (*Document) String added in v0.2.0

func (d *Document) String() string

String returns the text of the stream, so that printing a *Document prints its YAML. A nil Document gives "".

Example

A Document prints as its text and a Node as its source text.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("name: api # the service\nports: [80, 443]\n"))
	if err != nil {
		log.Fatal(err)
	}
	ports, _ := doc.Root().Lookup("ports")
	fmt.Println(ports)
	fmt.Print(doc)
}
Output:
[80, 443]
name: api # the service
ports: [80, 443]

func (*Document) TagDirectives

func (d *Document) TagDirectives() []TagDirective

TagDirectives returns the %TAG directives written before the document, in order. Directives belong to one document: those of an earlier document are not included.

Example
package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("%YAML 1.2\n%TAG !e! tag:example.com,2000:\n---\na: 1\n...\n---\nb: 2\n"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(doc.Doc(0).ExplicitStart(), doc.Doc(0).ExplicitEnd(), doc.Doc(0).TagDirectives())
	fmt.Println(doc.Doc(0).Version())
	fmt.Println(doc.Doc(1).ExplicitStart(), doc.Doc(1).ExplicitEnd(), doc.Doc(1).TagDirectives())
	fmt.Println(doc.Doc(1).Version())
}
Output:
true true [{!e! tag:example.com,2000:}]
1 2 true
true false []
0 0 false

func (*Document) Version

func (d *Document) Version() (major, minor int, ok bool)

Version returns the version of a %YAML directive the document carries. ok is false when it has none, in which case the document is read as YAML 1.2.

func (*Document) WriteTo

func (d *Document) WriteTo(w io.Writer) (int64, error)

WriteTo writes the stream to w.

Example

Document writes the bytes it holds to any io.Writer.

package main

import (
	"fmt"
	"log"
	"strings"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("# kept\na: 1\n"))
	if err != nil {
		log.Fatal(err)
	}
	var sb strings.Builder
	n, err := doc.WriteTo(&sb)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(n, " ", sb.String())
}
Output:
12 # kept
a: 1

type EditError

type EditError struct {
	// Op is the operation, for example "replace".
	Op string
	// Pointer is the RFC 6901 pointer the operation addressed. For "move" and
	// "copy" it is the destination.
	Pointer string
	// From is the source pointer of a "move" or "copy".
	From string
	// Patch reports that the operation was part of a patch, and Index is its
	// zero-based position in that patch.
	Patch bool
	Index int
	// Kind is one of the sentinel errors above, or another cause.
	Kind error
}

EditError reports a failed document edit or patch operation.

Example

An edit that fails returns an *EditError that says what was attempted.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("a: 1\n"))
	if err != nil {
		log.Fatal(err)
	}
	err = doc.Set("/a/b/c", 2)
	var ee *oneyaml.EditError
	if errors.As(err, &ee) {
		fmt.Println(ee.Op, ee.Pointer, errors.Is(err, oneyaml.ErrNotFound))
	}
}
Output:
set /a/b/c true

func (*EditError) Error

func (e *EditError) Error() string

Error describes the operation that failed and why. The package prefix appears once, and an empty pointer is left out.

func (*EditError) Unwrap

func (e *EditError) Unwrap() error

Unwrap lets errors.Is match Kind.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

An Encoder writes YAML documents to an output stream.

Example

An Encoder writes several documents to one stream, separated by "---".

package main

import (
	"log"
	"os"

	oneyaml "oneyaml.dev/go"
)

func main() {
	enc := oneyaml.NewEncoder(os.Stdout)
	if err := enc.Encode(map[string]any{"kind": "first"}); err != nil {
		log.Fatal(err)
	}
	if err := enc.Encode([]int{1, 2}); err != nil {
		log.Fatal(err)
	}
	if err := enc.Close(); err != nil {
		log.Fatal(err)
	}
}
Output:
kind: first
---
- 1
- 2

func NewEncoder

func NewEncoder(w io.Writer, opts ...Option) *Encoder

NewEncoder returns an Encoder that writes to w.

func (*Encoder) Close

func (e *Encoder) Close() error

Close ends the stream. It does not close the underlying writer. Encode returns an error after Close.

func (*Encoder) Encode

func (e *Encoder) Encode(v any) error

Encode writes v as the next document of the stream. Documents after the first are preceded by a "---" line. Each document is written to the underlying writer with a single Write call, and nothing is written if v cannot be encoded.

type Limit added in v1.0.0

type Limit string

Limit names one of the bounds of a LimitSet by the name of its field. Its values are the Limit constants, and each has a sentinel that errors.Is matches, such as ErrMaxDepth for LimitMaxDepth.

const (
	// LimitMaxDepth is the deepest nesting of collections.
	LimitMaxDepth Limit = "MaxDepth"
	// LimitMaxAliasNodes is the nodes that alias expansion may produce.
	LimitMaxAliasNodes Limit = "MaxAliasNodes"
	// LimitMaxAliasBytes is the scalar bytes that alias expansion may produce.
	// A patch that copies more than this much text also reports it.
	LimitMaxAliasBytes Limit = "MaxAliasBytes"
	// LimitMaxInputBytes is the largest input accepted. A patch that is too long,
	// and an edit or patch that would make the document too large, also report
	// it, as does the fixed 2 GiB bound on the size of a Document.
	LimitMaxInputBytes Limit = "MaxInputBytes"
	// LimitMaxScalarBytes is the longest single scalar accepted.
	LimitMaxScalarBytes Limit = "MaxScalarBytes"
	// LimitMaxDocuments is the most documents accepted in one stream.
	LimitMaxDocuments Limit = "MaxDocuments"
	// LimitMaxNumberDigits is the most digits of a number that is converted.
	LimitMaxNumberDigits Limit = "MaxNumberDigits"
	// LimitMaxPatchWorkRatio is the text that the edits of one patch may read again.
	LimitMaxPatchWorkRatio Limit = "MaxPatchWorkRatio"
)

The values of LimitError.Limit, one for each field of LimitSet.

type LimitError

type LimitError struct {
	// Limit is the bound that was exceeded, named by its [LimitSet] field. Switch
	// on it rather than on What.
	Limit Limit
	// What says in words what the bound applies to, such as "alias expansion
	// nodes" or "patch output size", for messages. Its wording may change.
	What string
	// Max is the bound that was exceeded.
	Max int
	// Option names the option and the [LimitSet] field that raise the bound, in
	// the form "Limits (MaxDepth)"; NoLimits removes every bound. It is empty for
	// a bound that no option changes: the nesting of a patch value (10,000
	// levels), the nesting of typed Go values (100,000 levels) and the 2 GiB size
	// of a Document. It is also empty in the errors of the compat packages, which
	// take no limit options.
	Option string
	// Path is the RFC 6901 pointer to where the limit was hit: the value being
	// read (alias expansion, scalar size, number digits, the nesting of typed
	// values), or, for most patch limits, the pointer of the operation that hit
	// it. It is empty when there is no single value to point at: every limit
	// Parse reports, the size of the input, the number of documents, nesting that
	// the parser stopped at, the nesting of a patch value, a search of "<<"
	// merge sources, and the size of the text that an edit or a merge patch would
	// leave. A limit hit by a decode inside an UnmarshalYAML hook comes back as
	// the error the hook returned, wrapped in a [*TypeError] at the hook's value,
	// and errors.As on it finds a copy whose Path is the pointer in the whole
	// document, as in a [*TypeError]. The read does not change the error the hook
	// holds: inside the hook, and if the hook keeps it and returns it from a later
	// read, its Path is relative to the Node the hook decoded. The text of such an
	// error, if the hook wrapped it, keeps the hook's own wording, which may name
	// a position inside the hook's value.
	Path string
	// contains filtered or unexported fields
}

LimitError reports that a limit was exceeded.

Match it with errors.Is against ErrLimit for any limit or, for one, the sentinel such as ErrMaxDepth; read the fields with errors.As.

Example

A limit that is hit matches ErrLimit and the sentinel of that one limit, and Limit names it for a switch.

package main

import (
	"errors"
	"fmt"
	"strings"

	oneyaml "oneyaml.dev/go"
)

func main() {
	deep := strings.Repeat("[", 100) + strings.Repeat("]", 100)
	err := oneyaml.Unmarshal([]byte(deep), new(any), oneyaml.Limits(oneyaml.LimitSet{MaxDepth: 50}))

	fmt.Println(errors.Is(err, oneyaml.ErrLimit), errors.Is(err, oneyaml.ErrMaxDepth), errors.Is(err, oneyaml.ErrMaxInputBytes))
	var le *oneyaml.LimitError
	if errors.As(err, &le) {
		switch le.Limit {
		case oneyaml.LimitMaxDepth:
			fmt.Println("too deep:", le.Max, le.Option)
		}
	}
}
Output:
true true false
too deep: 50 Limits (MaxDepth)

func (*LimitError) Error

func (e *LimitError) Error() string

Error names the limit, where it was hit and the option that raises it.

func (*LimitError) Is added in v1.0.0

func (e *LimitError) Is(target error) bool

Is reports whether target is ErrLimit or the sentinel of the Limit of e.

type LimitSet

type LimitSet struct {
	// MaxDepth is the deepest nesting of collections.
	MaxDepth int
	// MaxAliasNodes is the number of nodes that alias expansion may produce.
	// Left at its default, the bound is raised to ten times the number of nodes
	// the document itself holds (two times with Untrusted), so a large document
	// that reuses its anchors is not refused. A value you set with Limits
	// applies exactly as given.
	MaxAliasNodes int
	// MaxAliasBytes is the number of scalar bytes that alias expansion may
	// produce. When left at zero it is raised with the input size in the same way
	// as MaxAliasNodes.
	MaxAliasBytes int
	// MaxInputBytes is the largest input accepted. The text of one patch may be
	// at most an eighth of it (never less than 64 KiB, never more than the limit
	// itself): a patch is read into a list of operations that takes far more
	// memory than the same amount of document.
	MaxInputBytes int
	// MaxScalarBytes is the longest single scalar accepted.
	MaxScalarBytes int
	// MaxDocuments is the most documents accepted in one stream.
	MaxDocuments int
	// MaxNumberDigits is the most digits (counting the whole text of a float)
	// that an integer or float scalar may have when it is converted to a
	// number, whether into a big.Int, a float, or a generic value. Turning a
	// very long decimal into a big.Int takes time that grows with the square of
	// its length, so a few megabytes of digits could otherwise keep a call busy
	// for minutes. A longer number makes the call fail with a *LimitError.
	// A plain number used as a mapping key is converted to find repeated keys,
	// even in a part of the input the target ignores, so it is limited too.
	// Reading the scalar as a string, or keeping its text in a Node or Document,
	// is never limited because no conversion happens.
	MaxNumberDigits int
	// MaxPatchWorkRatio bounds the text that the edits of one patch (ApplyPatch,
	// Apply or ApplyMergePatch) may have to read again, as a multiple of the
	// document plus the patch, with 16 MiB on top. Most edits touch a small
	// region, but some re-read the whole document, so a long patch against a large
	// document could otherwise cost time that grows with the square of its size.
	// A patch that goes past the bound fails with a *LimitError and changes
	// nothing.
	MaxPatchWorkRatio int
}

LimitSet bounds the work and memory a call may spend.

A field left at zero is not changed: it keeps the bound given by the options before Limits (the value DefaultLimits gives it if there are none, which is no bound for the fields that have none by default). So setting one field adds that bound without lifting the others, and Untrusted(), Limits(LimitSet{MaxDepth: 50}) tightens the depth of the untrusted preset. Set a field to NoBound, or any negative value, to remove its bound, or pass NoLimits to remove them all.

MaxDepth and MaxNumberDigits also apply when writing (the latter to numbers a MarshalJSON method returns); the other limits apply to reading.

func DefaultLimits

func DefaultLimits() LimitSet

DefaultLimits returns the limits that apply when no limit option is given: generous bounds that real documents do not reach but that stop alias bombs and runaway nesting.

Example

DefaultLimits applies when no limit option is given. UntrustedLimits is what the Untrusted option applies. A zero field means the bound is not applied.

package main

import (
	"fmt"

	oneyaml "oneyaml.dev/go"
)

func main() {
	fmt.Printf("%+v\n", oneyaml.DefaultLimits())
	fmt.Printf("%+v\n", oneyaml.UntrustedLimits())
}
Output:
{MaxDepth:10000 MaxAliasNodes:1000000 MaxAliasBytes:67108864 MaxInputBytes:0 MaxScalarBytes:0 MaxDocuments:0 MaxNumberDigits:100000 MaxPatchWorkRatio:256}
{MaxDepth:1000 MaxAliasNodes:100000 MaxAliasBytes:8388608 MaxInputBytes:67108864 MaxScalarBytes:16777216 MaxDocuments:10000 MaxNumberDigits:4300 MaxPatchWorkRatio:32}

func UntrustedLimits

func UntrustedLimits() LimitSet

UntrustedLimits returns the tighter limits that the Untrusted option applies.

type Marshaler

type Marshaler interface {
	MarshalYAML() (any, error)
}

Marshaler is implemented by types that choose their own YAML form. The value MarshalYAML returns is written in place of the receiver; it may be any value Marshal accepts, including another Marshaler.

Example
package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

type celsius float64

// MarshalYAML lets a type choose its own YAML form.
func (c celsius) MarshalYAML() (any, error) { return fmt.Sprintf("%.1fC", float64(c)), nil }

func main() {
	out, err := oneyaml.Marshal(map[string]any{"temp": celsius(21.5)})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(out))
}
Output:
temp: 21.5C

type Node

type Node struct {
	// contains filtered or unexported fields
}

A Node is a read-only view of one node of a Document. It is a small value and is cheap to copy. A Node becomes stale when the Document is edited: Stale then reports true, methods that return an error report ErrStaleNode, and the others return zero values ("", 0, false, an invalid Node). A zero value looks like a real answer: Value of a stale Node is "", the same as an empty scalar. Err tells the two apart, so a program that keeps Nodes across edits can check n.Err() == nil before it trusts what an accessor returned, or after a loop that read many values. To edit while reading, collect the pointers first (see Pointer) and edit afterwards, or read the nodes again after each edit.

Node is comparable and can be a map key. Two Nodes are equal when they are the same node of the same state of the same Document, so a Node read twice without an edit in between is equal to itself, and a Node read before an edit is never equal to the Node for the same place read after it: the first is stale. An edit that is refused, or a patch that is rolled back, changes nothing and keeps the Nodes equal. To compare two values, compare Value, or Decode both.

Example

A Node is a read-only view of the document: kind, style, text, anchors, tags, comments and position.

package main

import (
	"fmt"
	"log"
	"strings"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("# the name\nname: api # service\nitems: [1, two]\n"))
	if err != nil {
		log.Fatal(err)
	}
	root := doc.Root()
	name, _ := root.Lookup("name")
	line, col := name.Position()
	fmt.Println(name.Value(), name.Tag(), name.LineComment(), line, col)
	fmt.Println(strings.TrimSpace(name.HeadComment()))
	items, _ := root.Lookup("items")
	fmt.Println(items.Style() == oneyaml.FlowStyle, items.Len(), items.Item(1).Value())
}
Output:
api tag:yaml.org,2002:str # service 2 7
# the name
true 2 two

func (Node) AliasName

func (n Node) AliasName() string

AliasName returns the anchor an alias node refers to, or "".

func (Node) Anchor

func (n Node) Anchor() string

Anchor returns the anchor name of the node without "&", or "".

func (Node) Decode

func (n Node) Decode(v any, opts ...Option) error

Decode reads the node and everything below it into the value v points to, as Document.Decode does for a whole document. A *TypeError's Path is relative to the node. An alias inside the subtree is expanded even when its anchor lies outside it. A Node made stale by an edit reports an *EditError matching ErrStaleNode; the zero Node reports one matching ErrNotFound. Options apply to this read only, as for Document.Decode.

Inside an UnmarshalYAML hook, the read that called the hook is already held to its limits, and options given here may only tighten them: for each bound the stricter of the caller's and the option's applies, so NoLimits, or a larger bound, does not lift what the caller was given. KnownFields and MergeKeys apply as given. A Node used outside a hook, or kept past the call, is a read of its own and takes its options in both directions, as Document.Decode does.

Example

Decode on a node reads that part of the document; an alias inside it is expanded even when its anchor is defined outside.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("defaults: &d {retries: 3}\njob:\n  settings: *d\n"))
	if err != nil {
		log.Fatal(err)
	}
	job, _ := doc.Root().Lookup("job")
	var v map[string]map[string]int
	if err := job.Decode(&v); err != nil {
		log.Fatal(err)
	}
	fmt.Println(v)
}
Output:
map[settings:map[retries:3]]

func (Node) Entries

func (n Node) Entries() iter.Seq2[Node, Node]

Entries returns the key and value of each entry of a mapping in order. It yields nothing for any other node.

The loop may edit the Document, as a loop over a map may change the map. After an edit the nodes already yielded are stale, but the loop goes on: it finds the mapping again by the pointer it had before the latest edit and yields the entry at the next position, as fresh nodes. An edit that inserts or removes items of a sequence on the way to this mapping moves the pointer with it. The loop ends if the mapping is replaced or removed, is no longer a mapping, or has no pointer (see Node.Pointer). Any number of loops over one Document may run and edit at once, and a loop that a panic leaves does not affect the others. A loop stepped through iter.Pull and never stopped keeps its place, and the edits made on that Document afterwards get slower; call the stop function.

Entries added at the end during the loop are yielded, so a body that adds an entry on every pass never ends. Entries added or removed before the current one may make the loop skip entries or see some twice, as Go leaves open for a map: to add or remove while ranging, collect the pointers first (Node.Pointer), finish the loop, then edit.

Example (Edit)

Ranging over a collection and setting each value as the loop goes.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("images:\n  web: nginx:1.25\n  db: postgres:15\n"))
	if err != nil {
		log.Fatal(err)
	}
	imgs, ok := doc.Root().Lookup("images")
	if !ok {
		log.Fatal("no images")
	}
	for k, v := range imgs.Entries() {
		// Each Set makes the nodes read so far stale, but the loop goes on with fresh ones.
		if err := doc.Set(oneyaml.Pointer("images", k.Value()), v.Value()+"-patched"); err != nil {
			log.Fatal(err)
		}
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
images:
  web: nginx:1.25-patched
  db: postgres:15-patched

func (Node) Entry

func (n Node) Entry(i int) (key, value Node)

Entry returns the key and value of entry i of a mapping. Looping over i is linear in the length; Entries is the shorter way to write that loop.

func (Node) Err added in v0.3.0

func (n Node) Err() error

Err reports whether n can be read: it is nil for a valid Node, an error matching ErrStaleNode when the Document was edited after n was read, and one matching ErrNotFound for the zero Node. Like bufio.Scanner.Err, it is the check to make when an accessor's zero value could mean either "empty" or "unreadable": Value, Kind, Len, Item, Entry, Lookup, Tag, Anchor, Span, Source, the comment getters and the iterators all return zero values (or nothing) for a Node that is not valid. Err allocates only when it returns an error.

Example
package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("title: ''\nsize: 3\n"))
	if err != nil {
		log.Fatal(err)
	}
	var title oneyaml.Node
	if err := doc.Get("/title", &title); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%q %v\n", title.Value(), title.Err())

	if err := doc.Set("/size", 4); err != nil {
		log.Fatal(err)
	}
	// The edit made title stale. Value is "" again, as for an empty scalar,
	// and only Err says the node can no longer be read.
	fmt.Printf("%q %v\n", title.Value(), errors.Is(title.Err(), oneyaml.ErrStaleNode))
}
Output:
"" <nil>
"" true

func (Node) ExplicitTag

func (n Node) ExplicitTag() (tag string, ok bool)

ExplicitTag returns the tag written on the node, resolved: the handle is expanded by the %TAG directives of the node's document, "!<uri>" gives the uri, and ASCII percent escapes are decoded exactly as the parser does (non-ASCII escapes stay as written). The non-specific tag "!" is returned as "!". ok is false when no tag was written, which Tag cannot tell apart from a tag the Core schema supplies.

Example
package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("%TAG !e! tag:example.com,2000:\n--- !e!point\nx: !!str 1\ny: 2\n"))
	if err != nil {
		log.Fatal(err)
	}
	root := doc.Root()
	tag, ok := root.ExplicitTag()
	fmt.Println(tag, ok, root.TagAsWritten())
	x, _ := root.Lookup("x")
	fmt.Println(x.ExplicitTag())
	y, _ := root.Lookup("y")
	// Tag gives the tag the Core schema assigns; ExplicitTag says none was written.
	fmt.Println(y.Tag())
	fmt.Println(y.ExplicitTag())
}
Output:
tag:example.com,2000:point true !e!point
tag:yaml.org,2002:str true
tag:yaml.org,2002:int
 false

func (Node) Find added in v0.3.0

func (n Node) Find(key string) (Node, error)

Find is Lookup with the reason for a miss. It returns the value of the entry of a mapping whose key has the scalar text key, and a nil error. The error is:

  • an error matching ErrNotFound when the key is absent, or n is not a mapping, or n is the zero Node;
  • an error matching ErrStaleNode when the Document was edited after n was read;
  • an error matching ErrAmbiguousKey when the mapping has the key more than once, or when a merge brings the key in from two places of one source. Document.Get refuses such a key the same way; Node.Entries still yields every entry;
  • a *LimitError when, with MergeKeys, the search of the "<<" merge sources read more keys than MaxAliasNodes allows. The key may or may not exist; Document.Get reports the same refusal.

The name is a verb that returns (value, error), the Go shape for an operation that can fail for several reasons (like os.Open); Lookup keeps the comma-ok shape of a map read, which has room for only one reason. A found key costs what Lookup costs.

Example
package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	// With MergeKeys, a search through merge sources is bounded by MaxAliasNodes.
	// Find tells a refused search from an absent key; Lookup reports both as false.
	src := "base: &base {a: 1, b: 2, c: 3, d: 4}\nuse:\n  <<: *base\n  own: 5\n"
	limits := oneyaml.DefaultLimits()
	limits.MaxAliasNodes = 2
	doc, err := oneyaml.Parse([]byte(src), oneyaml.MergeKeys(), oneyaml.Limits(limits))
	if err != nil {
		log.Fatal(err)
	}
	var use oneyaml.Node
	if err := doc.Get("/use", &use); err != nil {
		log.Fatal(err)
	}
	own, err := use.Find("own")
	fmt.Println(own.Value(), err)
	// Neither "missing" nor "d" is a key of use itself, so both need the merge
	// search, which reads more keys than the bound allows. The answer is the
	// *LimitError, not ErrNotFound: whether the key exists is unknown.
	_, err = use.Find("missing")
	fmt.Println(errors.Is(err, oneyaml.ErrNotFound), errors.As(err, new(*oneyaml.LimitError)))
	_, err = use.Find("d")
	fmt.Println(errors.Is(err, oneyaml.ErrNotFound), errors.As(err, new(*oneyaml.LimitError)))
}
Output:
5 <nil>
false true
false true
Example (AfterEdit)

A Node is a view of the document as it was when it was read. After an edit, look it up again; Find says why a lookup failed, where Lookup only says false.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("spec:\n  replicas: 2 # set by CI\n"))
	if err != nil {
		log.Fatal(err)
	}
	spec, err := doc.Root().Find("spec")
	if err != nil {
		log.Fatal(err)
	}
	replicas, err := spec.Find("replicas")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(replicas.Value())

	if err := doc.Set("/spec/replicas", 3); err != nil {
		log.Fatal(err)
	}

	// spec was read before the edit, so it is stale.
	_, err = spec.Find("replicas")
	fmt.Println(errors.Is(err, oneyaml.ErrStaleNode))

	// Look it up again from the root.
	if spec, err = doc.Root().Find("spec"); err != nil {
		log.Fatal(err)
	}
	if replicas, err = spec.Find("replicas"); err != nil {
		log.Fatal(err)
	}
	fmt.Println(replicas.Value(), replicas.LineComment())
}
Output:
2
true
3 # set by CI

func (Node) HeadComment

func (n Node) HeadComment() string

HeadComment returns the comment lines directly above the entry the node belongs to (no blank line between), trimmed as yaml.v3 does: each line without its indentation and with its "#", the lines joined by line feeds, no line break at the end. It is "" when there is none. Use SetHeadComment to change it.

func (Node) IsValid

func (n Node) IsValid() bool

IsValid reports whether n refers to a node of its document. It is false for the zero Node and for a stale Node.

func (Node) Item

func (n Node) Item(i int) Node

Item returns item i of a sequence, or an invalid Node. Looping over i is linear in the length; Items is the shorter way to write that loop.

func (Node) Items

func (n Node) Items() iter.Seq[Node]

Items returns the items of a sequence in order. It yields nothing for any other node.

The loop may edit the Document, as a loop over a map may change the map. After an edit the nodes already yielded are stale, but the loop goes on: it finds the sequence again by the pointer it had before the latest edit and yields the item at the next position, as a fresh node. An edit that inserts or removes items of a sequence on the way to this one moves the pointer with it. The loop ends if the sequence is replaced or removed, is no longer a sequence, or has no pointer (see Node.Pointer). Any number of loops over one Document may run and edit at once, and a loop that a panic leaves does not affect the others. A loop stepped through iter.Pull and never stopped keeps its place, and the edits made on that Document afterwards get slower; call the stop function.

Items appended during the loop are yielded, so a body that appends on every pass never ends. Items inserted or removed before the current one may make the loop skip items or see some twice, as Go leaves open for a map: to add or remove while ranging, collect the pointers first (Node.Pointer), finish the loop, then edit.

Example
package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("- a\n- [b, c]\n- {d: e}\n"))
	if err != nil {
		log.Fatal(err)
	}
	for item := range doc.Root().Items() {
		fmt.Println(item.Kind())
	}
	doc, err = oneyaml.Parse([]byte("x: 1\ny: [2]\n"))
	if err != nil {
		log.Fatal(err)
	}
	for k, v := range doc.Root().Entries() {
		fmt.Println(k.Value(), v.Kind())
	}
}
Output:
scalar
sequence
mapping
x scalar
y sequence

func (Node) Kind

func (n Node) Kind() NodeKind

Kind returns the kind of the node.

func (Node) Len

func (n Node) Len() int

Len returns the number of items of a sequence or entries of a mapping.

func (Node) LineComment

func (n Node) LineComment() string

LineComment returns the comment that follows the node on its line, trimmed: it starts with the "#" and has no trailing blanks. It is "" when there is none. The comment of a block collection or an empty value is the one after the colon or dash that introduces it.

func (Node) Lookup

func (n Node) Lookup(key string) (Node, bool)

Lookup returns the value of the entry of a mapping whose key has the scalar text key. A key written more than once in the mapping has no single value, so Lookup reports false for it, as Document.Get refuses a pointer through it. Aliases are not followed. When the Document was parsed with MergeKeys and the mapping has no such key itself, Lookup looks in its "<<" merge sources, with the precedence Decode and Get use, because it answers the question "what value has this key?". Entries, Items and Len describe the mapping as written and do not include merged keys. Merge sources are searched depth-first, each mapping once; the keys read count against MaxAliasNodes.

Lookup is the convenience form: a false result means "no value", whatever the reason. That includes a Node that is not valid (see Err) and a merge search that went past MaxAliasNodes, which reads as absent here, and a key written more than once. Use Node.Find when the difference matters, for example before falling back to a default.

func (Node) Pointer

func (n Node) Pointer() (string, error)

Pointer returns the RFC 6901 pointer of the node within its own document, ready for Document.Set, Get or Remove. It is how to edit while walking: collect the pointers of the nodes you want, finish the loop, then edit (an edit makes every Node read earlier stale). A pointer is an address: removing or inserting a sequence item changes what the later indices address, and a rename or a removal changes what the pointers below it address. To remove several items by collected pointers, remove them from the end of the sequence first.

Pointer returns an error matching ErrStaleNode when the Document has been edited since the node was read, and one matching ErrNotFound for the zero Node. It returns an error matching ErrNoPointer, and never a guess, when no pointer addresses the node: a mapping key (simple or complex), anything inside a complex key, or a node whose pointer would lead to a different node (the key occurs twice in its mapping). An alias node has the pointer of the alias itself; Target gives the node it refers to, whose pointer is the place of the anchor, not a path through the alias.

Example

Collect the pointers of the images, then edit them.

package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	doc, err := oneyaml.Parse([]byte("images:\n  web: nginx:1.25\n  db: postgres:15\n"))
	if err != nil {
		log.Fatal(err)
	}
	imgs, ok := doc.Root().Lookup("images")
	if !ok {
		log.Fatal("no images")
	}

	// First collect where each value is, then edit. Replacing values moves
	// nothing, so every pointer still names the same value.
	var ptrs, vals []string
	for _, v := range imgs.Entries() {
		p, err := v.Pointer()
		if err != nil {
			log.Fatal(err)
		}
		ptrs = append(ptrs, p)
		vals = append(vals, v.Value())
	}
	// Then edit by pointer.
	for i, p := range ptrs {
		if err := doc.Set(p, vals[i]+"-patched"); err != nil {
			log.Fatal(err)
		}
	}
	fmt.Print(string(doc.Bytes()))

	// Nodes read before the edits are stale now.
	_, err = imgs.Pointer()
	fmt.Println(imgs.Stale(), errors.Is(err, oneyaml.ErrStaleNode))
}
Output:
images:
  web: nginx:1.25-patched
  db: postgres:15-patched
true true

func (Node) Position

func (n Node) Position() (line, column int)

Position returns the 1-based line and column of the start of the node, counted in characters.

func (Node) Source

func (n Node) Source() string

Source returns the node's text exactly as written.

func (Node) Span

func (n Node) Span() (start, end int)

Span returns the byte offsets of the node's text in Document.Bytes, without its anchor and tag. The offsets refer to the UTF-8 text of the stream.

func (Node) Stale

func (n Node) Stale() bool

Stale reports whether n came from a Document that has been edited since n was read. A stale Node is not valid: its methods return zero values (or ErrStaleNode where they return an error). The zero Node is not stale, only invalid.

func (Node) String added in v0.2.0

func (n Node) String() string

String returns the source text of the node (see Source), so that printing a Node prints its YAML. A stale Node prints "<stale node>" and the zero Node "<invalid node>".

func (Node) Style

func (n Node) Style() NodeStyle

Style returns how the node is written.

func (Node) Tag

func (n Node) Tag() string

Tag returns the resolved tag of the node: the explicit tag with its handle expanded by the %TAG directives of the node's document, or the tag the Core schema gives the node ("tag:yaml.org,2002:str", ...). The non-specific tag "!" gives a scalar the string tag and a collection its default tag, as the specification says (section 6.9.1); the decoder reads the node the same way. An alias has no tag of its own and returns "".

func (Node) TagAsWritten

func (n Node) TagAsWritten() string

TagAsWritten returns the tag exactly as it appears in the text, such as "!!str", "!e!foo" or "!<tag:x,2000:y>", or "" when the node has no tag.

func (Node) Target

func (n Node) Target() Node

Target returns the node an alias refers to; for any other node it returns n.

func (Node) Value

func (n Node) Value() string

Value returns the text of a scalar node: quotes removed, escapes expanded, line folding and block chomping applied. For other nodes it is "".

type NodeKind

type NodeKind uint8

NodeKind is the kind of a Node.

const (
	InvalidNode NodeKind = iota
	ScalarNode
	MappingNode
	SequenceNode
	AliasNode
)

Node kinds. The zero value marks an invalid Node.

func (NodeKind) String

func (k NodeKind) String() string

String returns the kind name: "scalar", "mapping", "sequence", "alias" or "invalid".

type NodeStyle

type NodeStyle uint8

NodeStyle is how a node is written in the source.

const (
	PlainStyle NodeStyle = iota
	SingleQuotedStyle
	DoubleQuotedStyle
	LiteralStyle
	FoldedStyle
	BlockStyle
	FlowStyle
)

Node styles. Plain, SingleQuoted, DoubleQuoted, Literal and Folded describe scalars; BlockStyle and FlowStyle describe collections.

type Option

type Option func(*config)

Option configures Marshal, Unmarshal, an Encoder, a Decoder, Parse, and the reads of a Document: Document.Get, Document.Decode and Node.Decode. One option type is shared by every entry point; an option that does not apply to the call it is passed to is ignored, never an error.

Options are applied in order, so the last one that sets a thing wins. The boolean options (KnownFields, MergeKeys, YAML11Strings) take an optional bool so a later KnownFields(false) can undo an earlier KnownFields().

A read of a Document starts from the options the Document was parsed with and applies the options given to the read on top, for that call only. This works in both directions: Limits, Untrusted or NoLimits given to a read replace the bounds the Document was parsed with for that read, so NoLimits on a read of a Document parsed with Untrusted removes its alias bounds. Pass such options only for input you trust. Inside an UnmarshalYAML hook it is the other way: options given to Node.Decode may only tighten the limits of the read that called the hook, never loosen them (see Node.Decode). The ones that matter to a read are KnownFields, MergeKeys, Limits, Untrusted and NoLimits. Indent and YAML11Strings concern writing and are ignored by a read, and the size limits that concern parsing (MaxInputBytes, MaxDocuments) have no effect on a Document that is already parsed.

func Indent

func Indent(n int) Option

Indent sets how many spaces each nesting level of block output is indented by. The default is 2. Values below 1 select the default and values above 9 are treated as 9.

Example
package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	out, err := oneyaml.Marshal(map[string]any{"a": map[string]any{"b": []any{1, 2}}}, oneyaml.Indent(4))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(out))
}
Output:
a:
    b:
        - 1
        - 2

func KnownFields

func KnownFields(on ...bool) Option

KnownFields makes reading into a struct fail on a mapping key that matches no field. The key is reported as a *TypeError that errors.Is matches with ErrUnknownField, and the rest of the document is still read. It applies to reads and is ignored when writing. KnownFields(false) turns it off again, so it can override an earlier option; the last one wins.

Example
var cfg struct {
	Name string `yaml:"name"`
}
err := oneyaml.Unmarshal([]byte("name: api\nnmae: typo\n"), &cfg, oneyaml.KnownFields())
var te *oneyaml.TypeError
if errors.As(err, &te) {
	fmt.Println(te.Path, te.Line, te.Column)
}
Output:
/nmae 2 1

func Limits

func Limits(l LimitSet) Option

Limits sets the bounds that l names and leaves the others as the options before it set them: the defaults if there are none, the bounds of Untrusted after Untrusted, no bounds after NoLimits. So Untrusted(), Limits(LimitSet{MaxDepth: 50}) keeps every untrusted bound and tightens the depth, and NoLimits(), Limits(LimitSet{MaxInputBytes: 1 << 20}) bounds the input and nothing else.

A positive field sets that bound; NoBound removes it. A bound you set applies exactly as given, while an alias bound you leave alone keeps the behaviour it had: the default and Untrusted ones still grow with the size of the document. Limits(LimitSet{}) changes nothing. When writing, MaxDepth and MaxNumberDigits are used.

Example
bomb := "a: &a [x, x, x, x]\nb: &b [*a, *a, *a, *a]\nc: [*b, *b, *b, *b]\n"
var v any
err := oneyaml.Unmarshal([]byte(bomb), &v, oneyaml.Limits(oneyaml.LimitSet{MaxAliasNodes: 20}))
var le *oneyaml.LimitError
if errors.As(err, &le) {
	fmt.Println(le.Limit, le.Max)
}
Output:
MaxAliasNodes 20
Example (AddBound)

A LimitSet passed to Limits changes only the fields that are set: here the input size gains a bound, and the default depth bound stays.

package main

import (
	"errors"
	"fmt"
	"strings"

	oneyaml "oneyaml.dev/go"
)

func main() {
	opt := oneyaml.Limits(oneyaml.LimitSet{MaxInputBytes: 1 << 20})
	large := []byte("a: " + strings.Repeat("x", 2<<20) + "\n")
	deep := []byte(strings.Repeat("[", 10001) + strings.Repeat("]", 10001))
	var v any
	var le *oneyaml.LimitError

	err := oneyaml.Unmarshal(large, &v, opt)
	if errors.As(err, &le) {
		fmt.Println(le.Limit, le.Max)
	}
	err = oneyaml.Unmarshal(deep, &v, opt)
	if errors.As(err, &le) {
		fmt.Println(le.Limit, le.Max)
	}
}
Output:
MaxInputBytes 1048576
MaxDepth 10000

func MergeKeys

func MergeKeys(on ...bool) Option

MergeKeys makes reading treat the "<<" key as a YAML merge key instead of an ordinary key. It applies to reads and is ignored by Marshal and Encoder. MergeKeys(false) turns it off again; the last one wins.

Given to Parse, it also makes the Document's pointers find merged keys: Document.Get, a patch "test" and Node.Lookup read them, Document.Set on such a key adds a key of the mapping's own, and an edit that would change a key only a merge source has is refused with ErrAliasWrite.

Example
src := []byte("base: &base {x: 1, y: 2}\nchild:\n  <<: *base\n  y: 3\n")
var v map[string]map[string]int
err := oneyaml.Unmarshal(src, &v, oneyaml.MergeKeys())
fmt.Println(v["child"], err)
Output:
map[x:1 y:3] <nil>
Example (Pointer)
package main

import (
	"errors"
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	src := "base: &base {image: nginx, port: 80}\nweb:\n  <<: *base\n  port: 8080\n"
	doc, err := oneyaml.Parse([]byte(src), oneyaml.MergeKeys())
	if err != nil {
		log.Fatal(err)
	}
	var image string
	var port int
	if err := doc.Get("/web/image", &image); err != nil { // from the merge
		log.Fatal(err)
	}
	if err := doc.Get("/web/port", &port); err != nil { // the mapping's own key wins
		log.Fatal(err)
	}
	fmt.Println(image, port)

	// Remove would have to write into the merge source, so it is refused.
	err = doc.Remove("/web/image")
	fmt.Println(errors.Is(err, oneyaml.ErrAliasWrite))

	// Set adds a key of its own to web, which overrides the merged one.
	if err := doc.Set("/web/image", "caddy"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(doc.Bytes()))
}
Output:
nginx 8080
true
base: &base {image: nginx, port: 80}
web:
  <<: *base
  port: 8080
  image: caddy

func NoLimits

func NoLimits() Option

NoLimits removes every limit, including the depth limit when writing. Only use it for input you trust. A Limits option after it adds back just the bounds it names.

Example

NoLimits removes every bound, including the depth limit. Use it only for input you trust.

package main

import (
	"errors"
	"fmt"
	"strings"

	oneyaml "oneyaml.dev/go"
)

func main() {
	deep := strings.Repeat("[", 10001) + strings.Repeat("]", 10001)
	var v any

	err := oneyaml.Unmarshal([]byte(deep), &v)
	var le *oneyaml.LimitError
	fmt.Println(errors.As(err, &le))
	fmt.Println(oneyaml.Unmarshal([]byte(deep), &v, oneyaml.NoLimits()))
}
Output:
true
<nil>

func Untrusted

func Untrusted() Option

Untrusted selects UntrustedLimits, for input from a source you do not control. It replaces any limits set before it. Options after it can tighten or lift single bounds: Untrusted(), Limits(LimitSet{MaxDepth: 50}).

Example

Untrusted tightens every limit, for input from a source you do not control. A limit that is hit is a *LimitError that names the option which raises it.

package main

import (
	"errors"
	"fmt"
	"strings"

	oneyaml "oneyaml.dev/go"
)

func main() {
	deep := strings.Repeat("[", 2000) + strings.Repeat("]", 2000)
	var v any

	// The default depth limit lets this through; Untrusted does not.
	fmt.Println(oneyaml.Unmarshal([]byte(deep), &v))
	err := oneyaml.Unmarshal([]byte(deep), &v, oneyaml.Untrusted())
	var le *oneyaml.LimitError
	if errors.As(err, &le) {
		fmt.Println(le.Limit, le.Max, le.Option)
	}
}
Output:
<nil>
MaxDepth 1000 Limits (MaxDepth)
Example (Tighten)

A Limits option after Untrusted changes only the bounds it names: here depth is tightened, and the untrusted bound on the digits of a number still holds.

package main

import (
	"errors"
	"fmt"
	"strings"

	oneyaml "oneyaml.dev/go"
)

func main() {
	opts := []oneyaml.Option{oneyaml.Untrusted(), oneyaml.Limits(oneyaml.LimitSet{MaxDepth: 50})}
	deep := strings.Repeat("[", 100) + strings.Repeat("]", 100)
	long := strings.Repeat("9", 5000)
	var v any
	var le *oneyaml.LimitError

	for _, in := range []string{deep, long} {
		err := oneyaml.Unmarshal([]byte(in), &v, opts...)
		if errors.As(err, &le) {
			fmt.Println(le.Limit, le.Max)
		}
	}
}
Output:
MaxDepth 50
MaxNumberDigits 4300

func YAML11Strings added in v0.2.0

func YAML11Strings(on ...bool) Option

YAML11Strings makes a Document quote the new strings it writes that a YAML 1.1 reader would take for another type. By default an edit judges plainness by the YAML 1.2 Core schema, so Set("/a", "yes") writes a: yes, a string to oneyaml but a boolean to a program that reads the file with YAML 1.1 rules, such as Kubernetes tooling built on sigs.k8s.io/yaml or yaml.v2. With YAML11Strings the strings written by Set, by the add, replace and copy operations of a patch, by a merge patch, and the new keys written by Rename and add use the rule Marshal uses: yes, on, y, 0755, 1_000, 12:30 and 2001-01-01 are quoted, hello is not.

An existing quoted or block style is kept as it is, and values that are not strings are written as before. The option applies to Parse and is remembered by the Document and by the views Doc returns; other functions ignore it.

Like KnownFields and MergeKeys, it takes an optional bool: YAML11Strings() and YAML11Strings(true) turn it on, YAML11Strings(false) turns it off again, so a later option can override a list of defaults (the last one wins).

Example

A file that Kubernetes tooling also reads is written so that programs using the YAML 1.1 rules read the same strings.

package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

func main() {
	src := []byte("debug: false\nname: x\n")

	// By default the Core schema decides: "yes" is a string there, so it stays plain.
	doc, err := oneyaml.Parse(src)
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/debug", "yes"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(doc)

	// With YAML11Strings, a string that YAML 1.1 reads as a boolean is quoted.
	doc, err = oneyaml.Parse(src, oneyaml.YAML11Strings())
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/debug", "yes"); err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/name", "hello"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(doc)
}
Output:
debug: yes
name: x
debug: 'yes'
name: hello

type PatchOp

type PatchOp struct {
	// Op is "add", "remove", "replace", "move", "copy" or "test".
	Op string
	// Path is the RFC 6901 pointer the operation works on.
	Path string
	// From is the source pointer of "move" and "copy".
	From string
	// Value is the operand of "add", "replace" and "test": anything Marshal
	// accepts, with nil meaning null.
	Value any
}

A PatchOp is one RFC 6902 operation for Apply.

func PatchAdd

func PatchAdd(path string, v any) PatchOp

PatchAdd returns an "add" operation.

func PatchCopy

func PatchCopy(from, path string) PatchOp

PatchCopy returns a "copy" operation.

func PatchMove

func PatchMove(from, path string) PatchOp

PatchMove returns a "move" operation.

func PatchRemove

func PatchRemove(path string) PatchOp

PatchRemove returns a "remove" operation.

func PatchReplace

func PatchReplace(path string, v any) PatchOp

PatchReplace returns a "replace" operation.

func PatchTest

func PatchTest(path string, v any) PatchOp

PatchTest returns a "test" operation.

type SyntaxError

type SyntaxError struct {
	// Line and Column are 1-based, counted in characters; Offset is the byte offset.
	Line, Column, Offset int
	// Category is the kind of error: one of the Category constants. Switch on it
	// or match it with errors.Is, rather than reading Msg.
	Category Category
	// Msg describes the problem in terms of what the input got wrong.
	Msg string
	// Key, FirstLine and FirstColumn describe a CategoryDuplicateKey error: the
	// text of the repeated key and the 1-based position of its first occurrence.
	// They are zero when the first occurrence is not known, which happens when
	// two keys are equal only after conversion to the target type.
	Key                    string
	FirstLine, FirstColumn int
}

SyntaxError reports YAML that is not well formed.

Example

A *SyntaxError carries the position and a stable category.

package main

import (
	"errors"
	"fmt"

	oneyaml "oneyaml.dev/go"
)

func main() {
	err := oneyaml.Unmarshal([]byte("a: 1\na: 2\n"), new(any))
	var se *oneyaml.SyntaxError
	if errors.As(err, &se) {
		fmt.Println(se.Category, se.Line, se.Column)
	}
}
Output:
duplicate-key 2 1
Example (DuplicateKey)

A duplicate key is a SyntaxError that names the key and where it first appeared.

var v any
err := oneyaml.Unmarshal([]byte("name: a\nport: 1\nname: b\n"), &v)

var se *oneyaml.SyntaxError
if errors.As(err, &se) && se.Category == oneyaml.CategoryDuplicateKey {
	fmt.Printf("%q at line %d, first at line %d\n", se.Key, se.Line, se.FirstLine)
}
Output:
"name" at line 3, first at line 1

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

Error returns the message with its line and column.

func (*SyntaxError) Is added in v1.0.0

func (e *SyntaxError) Is(target error) bool

Is reports whether target is ErrSyntax or the sentinel of the Category of e.

type TagDirective

type TagDirective struct {
	// Handle is the tag handle as written, such as "!" or "!e!".
	Handle string
	// Prefix is the tag prefix as written.
	Prefix string
}

A TagDirective is a %TAG directive: tags written with Handle in the document stand for Prefix followed by the rest of the tag.

type TypeError

type TypeError struct {
	// Path is the RFC 6901 pointer to the value, empty for the root.
	Path string
	// Line and Column locate the value in the input; zero when writing.
	Line, Column int
	// GoType is the Go type involved: the target of a value that does not fit,
	// or the type that has no YAML form. It is nil when no type was involved.
	GoType reflect.Type
	// Msg describes the problem.
	Msg string
	// Err is the error one of your hooks returned, such as an UnmarshalYAML,
	// MarshalYAML or MarshalText method, if that was the cause. errors.Is and
	// errors.As look inside it.
	Err error
	// Kind is the sentinel error for the cause when there is one, for now only
	// [ErrUnknownField]. errors.Is matches it.
	Kind error
	// More holds the mismatches after this one when a read found several. Each
	// has an empty More.
	More []*TypeError
	// contains filtered or unexported fields
}

TypeError reports a value that cannot be converted: a YAML value that does not fit its Go target when reading, or a Go value with no YAML form when writing.

When reading, a mismatch does not stop the decode. The value that does not fit is skipped and its target is left as it was (an element of a slice or array is left zero, and a map entry is not added); the rest of the document is still read, and every mismatch is reported, up to the bound below. The error returned describes the first one and More holds the others, in input order, so errors.As finds a *TypeError whether there was one problem or many. At most 100 mismatches are collected; the read stops at the 100th. The first mismatch is always reported whole, and the paths of the ones after it together take no more than the larger of the input size and 1 MiB: when the next would pass that, collecting stops and the later mismatches are left out, so a document with very long keys (keys reached through aliases, say) may report fewer than 100. A value that no hook can take (a type whose UnmarshalYAML has the wrong form, or a Node used as a target) is a mismatch like any other. Syntax errors, limit errors and errors from your own hooks stop the read at once and are returned alone, without the mismatches collected before them. When a hook returns the error of a Node.Decode it made (or of the function a func-style hook is given), the mismatches in it are reported as if the hook were not there, every one of them, at any depth of nested hooks: each mismatch is reported at the line and column of its own value. Its path is the path of the hook's value followed by the path inside the node the hook decoded, so a hook that decodes the node it was given reports the same paths as a read without the hook. Inside the hook, the error of its own Node.Decode holds the paths inside that node; a path of more than 4 KiB that runs through hooks nested in one another is completed when the outermost read ends, so it stays incomplete in an error the hook keeps or wraps (with %w, say) instead of returning it. An error a hook returns from reading other text, such as an Unmarshal of its own input, is reported at the hook's value with the hook's type as GoType; the position and path inside the other text are part of Msg.

Error shortens a Path longer than 512 bytes to its first and last 256 bytes joined by "..."; the Path field always holds the whole pointer.

Example

A value that does not fit is skipped and the rest of the document is still read. Every mismatch is reported, with its position.

var cfg struct {
	Port    int
	Workers int
	Name    string
}
err := oneyaml.Unmarshal([]byte("port: x\nworkers: y\nname: api\n"), &cfg)

var te *oneyaml.TypeError
if errors.As(err, &te) {
	for _, e := range te.All() {
		fmt.Printf("%d:%d %s\n", e.Line, e.Column, e.Path)
	}
}
fmt.Println(cfg.Name)
Output:
1:7 /port
2:10 /workers
api
Example (GoType)

A TypeError carries the Go type involved as a reflect.Type.

package main

import (
	"errors"
	"fmt"
	"reflect"
	"time"

	oneyaml "oneyaml.dev/go"
)

func main() {
	var v struct{ Wait time.Duration }
	err := oneyaml.Unmarshal([]byte("wait: [1]\n"), &v)
	var te *oneyaml.TypeError
	if errors.As(err, &te) {
		fmt.Println(te.GoType == reflect.TypeFor[time.Duration](), te.GoType)
	}
}
Output:
true time.Duration

func (*TypeError) All added in v0.2.0

func (e *TypeError) All() []*TypeError

All returns this error and the ones in More, in input order.

func (*TypeError) Error

func (e *TypeError) Error() string

Error returns the message with the position and path of the value. With several mismatches it lists them one to a line.

func (*TypeError) Is added in v0.2.0

func (e *TypeError) Is(target error) bool

Is reports whether target is the Kind of this error or of one in More.

func (*TypeError) Unwrap

func (e *TypeError) Unwrap() error

Unwrap returns the error from a user hook, if there was one.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalYAML(node Node) error
}

Unmarshaler is implemented by types that read their own YAML. Unmarshal, Decoder.Decode, Document.Decode and Node.Decode call UnmarshalYAML with a view of the node that holds the value, instead of decoding into the type's fields.

The Node is read-only. Node.Decode on it decodes the subtree into any target, with the same rules as Unmarshal. When the value comes from Unmarshal or a Decoder there is no Document, so the Node covers only that subtree; aliases inside it still resolve. A null value leaves a pointer target nil without calling the method. An error the method returns comes back from the decode as a *TypeError whose Unwrap gives it.

A Node the method keeps past its return may be used later, one goroutine at a time, and then decodes under the settings it was given. From Unmarshal or a Decoder it goes on reading its own subtree whatever is read next; one from Unmarshal reads the slice that was passed in, so leave that slice unchanged while the Node is kept. One from Document.Decode or Node.Decode reads the Document and becomes stale when the Document is edited. A kept Node must not be used at the moment the method returns, from another goroutine: the read that made the call reuses its state for the next one, and a use at that instant may see it.

Example
package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
)

// Port reads itself from a node: a bare number, or a mapping with a number.
type Port int

func (p *Port) UnmarshalYAML(n oneyaml.Node) error {
	if n.Kind() == oneyaml.MappingNode {
		var m struct {
			Number int `yaml:"number"`
		}
		if err := n.Decode(&m); err != nil {
			return err
		}
		*p = Port(m.Number)
		return nil
	}
	var v int
	if err := n.Decode(&v); err != nil {
		return err
	}
	*p = Port(v)
	return nil
}

func main() {
	var cfg struct {
		Ports []Port `yaml:"ports"`
	}
	if err := oneyaml.Unmarshal([]byte("ports: [80, {number: 443}]\n"), &cfg); err != nil {
		log.Fatal(err)
	}
	fmt.Println(cfg.Ports)
}
Output:
[80 443]
Example (FromYAMLv3)
package main

import (
	"fmt"
	"log"

	oneyaml "oneyaml.dev/go"
	yaml "oneyaml.dev/go/compat/yamlv3"
)

// legacySize was written for yaml.v3: its UnmarshalYAML takes a *yaml.Node.
// The oneyaml package does not call that form; it reports it, so the type is
// never filled by its fields without a word.
type legacySize struct{ Bytes int }

func (s *legacySize) UnmarshalYAML(n *yaml.Node) error { return n.Decode(&s.Bytes) }

// Size is the same type moved to the oneyaml package: the method takes a
// oneyaml.Node, which has the same Decode method.
type Size struct{ Bytes int }

func (s *Size) UnmarshalYAML(n oneyaml.Node) error { return n.Decode(&s.Bytes) }

func main() {
	src := []byte("limit: 512\n")

	var before struct {
		Limit legacySize `yaml:"limit"`
	}
	err := oneyaml.Unmarshal(src, &before)
	fmt.Println(err)

	var after struct {
		Limit Size `yaml:"limit"`
	}
	if err := oneyaml.Unmarshal(src, &after); err != nil {
		log.Fatal(err)
	}
	fmt.Println(after.Limit.Bytes)
}
Output:
oneyaml: line 1, column 8: /limit: oneyaml_test.legacySize has an UnmarshalYAML method that is neither UnmarshalYAML(oneyaml.Node) error nor UnmarshalYAML(func(any) error) error; change its signature, or remove it to fill the value by its fields
512

Directories

Path Synopsis
compat
k8syaml
Package yaml is a compatibility adapter for sigs.k8s.io/yaml (and its parent ghodss/yaml) built on oneyaml.
Package yaml is a compatibility adapter for sigs.k8s.io/yaml (and its parent ghodss/yaml) built on oneyaml.
yamlv3
Package yaml is a compatibility adapter for gopkg.in/yaml.v3 (go.yaml.in/yaml/v3) built on the oneyaml parser and binder.
Package yaml is a compatibility adapter for gopkg.in/yaml.v3 (go.yaml.in/yaml/v3) built on the oneyaml parser and binder.
internal
bind
Package bind decodes a stream of parse events into Go values.
Package bind decodes a stream of parse events into Go values.
compatopt
Package compatopt carries the hidden settings between the root package and the compat packages.
Package compatopt carries the hidden settings between the root package and the compat packages.
cst
Package cst builds a flat, lossless tree from parse events.
Package cst builds a flat, lossless tree from parse events.
edit
Package edit plans minimal-diff edits of a lossless tree.
Package edit plans minimal-diff edits of a lossless tree.
emit
Package emit writes Go values as YAML text.
Package emit writes Go values as YAML text.
encoding
Package encoding detects and converts the character encodings a YAML stream may use: UTF-8, UTF-16 and UTF-32, each in both byte orders.
Package encoding detects and converts the character encodings a YAML stream may use: UTF-8, UTF-16 and UTF-32, each in both byte orders.
kitcase
Package kitcase reads the parts of a shared conformance case directory that every test runner needs in the same way: the input bytes, which may be stored in a file or described by a recipe, and the "slow" tag.
Package kitcase reads the parts of a shared conformance case directory that every test runner needs in the same way: the input bytes, which may be stored in a file or described by a recipe, and the "slow" tag.
parse
Package parse turns scan tokens into YAML events with a pull API.
Package parse turns scan tokens into YAML events with a pull API.
patch
Package patch holds the parts of RFC 6902 and RFC 7396 patches that do not depend on the document: reading a JSON patch, comparing a node with a patch value under the data model, and copying a value so that it can be written elsewhere in the same document.
Package patch holds the parts of RFC 6902 and RFC 7396 patches that do not depend on the document: reading a JSON patch, comparing a node with a patch value under the data model, and copying a value so that it can be written elsewhere in the same document.
pointer
Package pointer parses RFC 6901 JSON Pointers.
Package pointer parses RFC 6901 JSON Pointers.
resolve
Package resolve decides what a plain scalar means.
Package resolve decides what a plain scalar means.
scan
Package scan is a lossless YAML 1.2 scanner.
Package scan is a lossless YAML 1.2 scanner.
typeinfo
Package typeinfo builds and caches, per Go type, what the encoder and the decoder need to know about it: struct fields with their YAML names and options, embedded and inline fields, and which hooks the type implements.
Package typeinfo builds and caches, per Go type, what the encoder and the decoder need to know about it: struct fields with their YAML names and options, embedded and inline fields, and which hooks the type implements.

Jump to

Keyboard shortcuts

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