Recursive AI

Test a YAML value before replacing it in Go

oneyaml-go is a free, open-source YAML library for Go from Recursive AI. MIT or Apache-2.0, no dependencies.

Use PatchTest before PatchReplace in the same call.

Terminal
go mod init demo
go get oneyaml.dev/go

Requires Go 1.24 or later.

GO
package main
import (
  "fmt"
  yaml "oneyaml.dev/go"
)
func main() {
  src := []byte("replicas: 2 # scale\n")
  doc, err := yaml.Parse(src)
  if err != nil { panic(err) }
  err = doc.Apply(
    yaml.PatchTest("/replicas", 2),
    yaml.PatchReplace("/replicas", 3),
  )
  if err != nil { panic(err) }
  fmt.Print(doc)
}

go run . prints:

Text
replicas: 3 # scale

Unzip the example, then go run ..

The test matches the current count of 2, so the replacement runs.

If the scalar has changed, the call returns an error matching ErrTestFailed and leaves the document unchanged.

Variations

Apply a JSON patch supplied as text. Replace the doc.Apply call with:

GO
err = doc.ApplyPatch([]byte(`[
  {"op":"test","path":"/replicas","value":2},
  {"op":"replace","path":"/replicas","value":3}
]`))

It prints the same updated YAML.

Handle a failed test. In the program above, add "errors" to the imports and change replicas: 2 to replicas: 4 in src. Leave the expected value at 2 and replace the error check after doc.Apply with:

GO
if errors.Is(err, yaml.ErrTestFailed) {
  fmt.Print("Test failed; unchanged: ", doc)
  return
}
if err != nil { panic(err) }

It prints:

Text
Test failed; unchanged: replicas: 4 # scale

The short answer

Pass PatchTest and PatchReplace to the same Apply call, in that order. The test names the value you expect; the replacement names the value you want. Each call is all-or-nothing. A failing call doesn’t undo edits from an earlier call.

What does the JSON Patch test operation compare?

A test reads the value at a path without changing it. The path is a JSON Pointer, so /replicas points to the replicas key, and /deployment/replicas points to a key inside deployment. Comments and whitespace aren’t part of the compared value.

When the document is YAML, the comparison uses YAML’s data types, not the printed text or JSON’s rules for comparing numbers. For example, the YAML integer 0x1F matches decimal 31, but a YAML integer and a float with the same magnitude are different, as are the number 2 and the string "2". Match the type in the existing YAML, not just the number or text you see on the screen.

When should I use a JSON Patch test before replace?

Use it when your update is valid only from a particular starting value, such as a release script that should scale from two replicas to three but must leave a count of four alone. A missing path is an error too; a test isn’t a way to supply a default for that path. Keep all edits that depend on the test in the same patch so they either all apply or none do.

The test checks the document you parsed in memory. It doesn’t reread a file from disk before you save it. If another process can edit the same file between your read and write, handle that coordination separately.

All oneyaml Go recipes