📖 4 min read (~ 700 words).

YAML versions and tags

The rule

A written tag is honoured at any version. Only the reading of a plain scalar by its shape depends on the version.

YAML 1.2 kept the tags that YAML 1.1 named, such as !!timestamp and !!merge. It removed the rule that reads a plain scalar as one of those types because of how its text looks. So !!bool yes is true under both versions, and a plain yes is true only under 1.1.

Measured

Each value below is decoded into an any, once under YAML 1.2, the default, and once under YAML 1.1:

values := []string{
	"yes",
	"!!bool yes",
	"0777",
	"0o777",
	"1_000",
	"22:22",
	"1e3",
	"2001-12-14",
	"!!timestamp 2001-12-14",
	"!!str 0777",
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "written\tYAML 1.2\tYAML 1.1")
for _, value := range values {
	doc := "a: " + value + "\n"
	fmt.Fprintf(w, "%s\t%s\t%s\n", value, decode(doc), decode(doc, parser.WithYAMLVersion(parser.YAML11)))
}
_ = w.Flush()

Full source: docs/examples/decoding/versions/example_test.go

written                 YAML 1.2                        YAML 1.1
yes                     string "yes"                    bool true
!!bool yes              bool true                       bool true
0777                    uint64 777                      uint64 511
0o777                   uint64 511                      string "0o777"
1_000                   string "1_000"                  uint64 1000
22:22                   string "22:22"                  uint64 1342
1e3                     float64 1000                    string "1e3"
2001-12-14              string "2001-12-14"             time.Time 2001-12-14T00:00:00Z
!!timestamp 2001-12-14  time.Time 2001-12-14T00:00:00Z  time.Time 2001-12-14T00:00:00Z
!!str 0777              string "0777"                   string "0777"

Full source: docs/examples/decoding/versions/example_test.go

  • YAML 1.1 reads a leading 0 as octal, digits split by : as base 60, and skips _ in a number. It has no 0o prefix, and it wants a dot in a float, so 1e3 is a string.
  • YAML 1.1 reads a plain date as a timestamp. YAML 1.2 has no timestamp type in its core schema, so the date is a string, unless a !!timestamp tag asks for one.
  • A tag decides the type under both versions: !!str 0777 is a string.

Merge keys

The merge key << is a YAML 1.1 type. Under YAML 1.2 it is an ordinary key, unless the key carries the !!merge tag:

for _, key := range []string{"<<", "!!merge <<"} {
	doc := "d: &d {x: 1}\nm:\n  " + key + ": *d\n"
	fmt.Printf("%-11s 1.2: %s\n", key, decode(doc, "m"))
	fmt.Printf("%-11s 1.1: %s\n", key, decode(doc, "m", parser.WithYAMLVersion(parser.YAML11)))
}

Full source: docs/examples/decoding/versions/example_test.go

<<          1.2: map[<<:map[x:1]]
<<          1.1: map[x:1]
!!merge <<  1.2: map[x:1]
!!merge <<  1.1: map[x:1]

Full source: docs/examples/decoding/versions/example_test.go

parser.WithMergeKeys merges a plain << under every version. See Reading a merge key.

Choosing the version

A document chooses its own version with a %YAML directive:

%YAML 1.1
---
a: yes    # true

parser.WithYAMLVersion sets the version for a document that declares none. parser.YAML10, parser.YAML11 and parser.YAML12 are the three values. A codec caller passes the option through codec.WithParserOptions:

codec.Unmarshal(src, &v,
	codec.WithParserOptions(parser.WithYAMLVersion(parser.YAML11)))

The directive wins over the option. Here the option asks for YAML 1.2, and the document reads as 1.1:

doc := "%YAML 1.1\n---\na: yes\n"
fmt.Println(decode(doc, parser.WithYAMLVersion(parser.YAML12)))

Full source: docs/examples/decoding/versions/example_test.go

bool true

Full source: docs/examples/decoding/versions/example_test.go

A version go-yaml does not know, such as %YAML 1.9, is an error: unknown YAML version "1.9".

When a tag and the value disagree

!!int abc asserts something that does not hold. By default it is an error that names the text and the tag. parser.WithLaxTags reads the scalar as the text it was written with:

doc := "a: !!int abc\n"
fmt.Println(decode(doc))
fmt.Println(decode(doc, parser.WithLaxTags()))

Full source: docs/examples/decoding/versions/example_test.go

error: [1:10] cannot read "abc" as !!int
string "abc"

Full source: docs/examples/decoding/versions/example_test.go

!!bool, !!float, !!null, !!timestamp and !!binary are checked the same way. A collection tag must match its node: !!seq and !!omap want a sequence, !!map and !!set a mapping.

Tags go-yaml does not act on

!!pairs, !!value and !!yaml are read and ignored: the node decodes as if it had no tag. go-yaml does not check that a !!pairs node is a sequence of one-pair mappings.

A tag go-yaml has no type for, such as a local tag !point or a tag named through %TAG, also leaves the node as written. See Tags beyond the standard ones.