Put PatchTest before PatchReplace in the same call: they’re the test and replace operations from JSON Patch (RFC 6902). A YAML integer written as 0x1F matches decimal 31; the test checks the value and type, not the spelling.
go get oneyaml.dev/go@v1.0.0Go 1.24+.
package main import ( "fmt" yaml "oneyaml.dev/go" ) func main() { input := "level: 0x1F # current\n" doc, err := yaml.Parse([]byte(input)) if err != nil { panic(err) } err = doc.Apply( yaml.PatchTest("/level", 31), yaml.PatchReplace("/level", "high"), ) if err != nil { panic(err) } fmt.Print(doc) }
Paste the code above into main.go, run go mod init example and go get oneyaml.dev/go@v1.0.0, then go run .; or unzip the example and go run ..
go run . prints:
level: high # currentIf the value doesn’t match, Apply returns an error matching ErrTestFailed and leaves the document unchanged.
Variations
Check a float against the integer. Add "errors" to the imports and replace the code from doc.Apply onward:
err = doc.Apply( yaml.PatchTest("/level", 31.0), yaml.PatchReplace("/level", "high"), ) if errors.Is(err, yaml.ErrTestFailed) { fmt.Print("No match: ", doc) return } if err != nil { panic(err) } fmt.Print(doc)
It prints:
No match: level: 0x1F # currentRelated recipes
The short answer
Call PatchTest with the expected current value and PatchReplace with the new value in the same Apply call. The test can distinguish an integer, a float, and a string even when all three look like the same number. A failed test rejects the entire patch.
How do I check if a YAML value is what I expect?
Give the test a JSON Pointer to the value. Here /level selects the top-level level key. The test checks that one value; it doesn’t validate a schema or check the rest of the file. If you have several conditions, put their tests in the same call before the edits that depend on them. You can then tell whether the group applied by checking the error from that call.
Comparisons use YAML’s Core schema. Comparing the source line as text would reject 0x1F when you expect 31, even though the document holds the same integer. The patch test accepts that pair and ignores the # current comment. Quoting a plain number is different: "31" is a string, not another spelling of the integer.
Why does a JSON Patch test fail for the same number?
In a patch applied to YAML, an integer and a float are different types. 31.0 is a Go floating-point constant when passed to PatchTest in the example. It doesn’t match the YAML integer. The rule also applies to numeric literals in patch text: YAML comparison doesn’t adopt RFC 6902’s JSON number equality just because you wrote the operations as JSON. Match the number’s type as well as its magnitude.
How do I set a YAML value only if the old value matches?
Place the test before the edit so it reads the old value, not the value you just wrote. If an operation fails, even operations earlier in that same call are rolled back. Separate Apply calls aren’t part of that rollback. The patch checks the parsed document in memory; it doesn’t check for a newer version on disk. If multiple writers can change the same file, coordinate the read and write outside the patch.