oneyaml Go cookbook
Short recipes for common YAML tasks in Go with oneyaml, each a complete program that was compiled and run against oneyaml.dev/go v1.0.0.
Recipes
Parse YAML into Go structsRead a YAML file into Go structs with oneyaml, with yaml tags, time.Duration, maps, an optional section, defaults and KnownFields to catch typos.Read YAML without a structRead values from YAML by JSON Pointer in Go, walk mappings and sequences with oneyaml’s Node, and get each value’s line, column and comment.Edit YAML and keep its commentsChange a value, remove a list item, add a key with a comment and rename a key in a YAML file from Go, with every other byte and comment kept.Merge or patch YAMLApply a merge patch (RFC 7396) as an environment overlay and a guarded JSON Patch (RFC 6902) built in Go to a YAML file, keeping its comments.Convert YAML to JSON and backConvert YAML to JSON in Go by reading it with oneyaml and writing it with encoding/json, and convert JSON back to YAML.Read Kubernetes manifests and multi-document YAMLRead every document of a multi-document YAML file such as a Kubernetes manifest into Go structs with a Decoder, then edit one document and keep the rest byte for byte.Report errors with line and columnGet the line, column and category of a YAML syntax error in Go, where a duplicate key was first defined, and every type mismatch in a document with its path.Anchors, aliases and merge keysRead YAML anchors and aliases in Go, turn on << merge keys for docker-compose-style files with MergeKeys(), and follow an alias to its anchor.Read untrusted YAML safelyRead YAML from the network in Go with oneyaml’s Untrusted() mode, refuse billion-laughs alias bombs and deep nesting, and size the limits yourself.Switch from yaml.v3 with one importMove Go code from gopkg.in/yaml.v3 to oneyaml by changing one import to oneyaml.dev/go/compat/yamlv3, keeping Unmarshal, Marshal and yaml.Node comments.