Change one value, keep the file
The task: bump version in a file a person maintains, without touching its
comments, its indentation or its layout.
The file
const src = `# service
name: pet-store
version: 1.2.3 # bump me
ports:
- 80
- 443
`Full source: docs/examples/documents/editvalue/example_test.go
Parse with comments
// Parse with comments: without parser.WithComments, the tree holds none to keep.
f, err := parser.ParseBytes([]byte(src), parser.WithComments())
if err != nil {
fmt.Println(err)
return
}Full source: docs/examples/documents/editvalue/example_test.go
Comments are dropped by default, before the tree is built. A tree parsed
without parser.WithComments has no comment to keep.
Find the node
// Address the node by path. FilterNode returns the first node the path matches.
path, err := expressions.PathString("$.version")
if err != nil {
fmt.Println(err)
return
}
old, err := path.FilterNode(f.Docs[0].Body)
if err != nil {
fmt.Println(err)
return
}Full source: docs/examples/documents/editvalue/example_test.go
expressions.PathString compiles a YAMLPath
expression. FilterNode returns the first node it matches, here the scalar
1.2.3 at line 3, column 10.
Replace it
// Build the new scalar at the old one's position, and hand it the old comment:
// the comment belongs to the node it is replacing.
next := ast.String(token.New("2.0.0", "2.0.0", old.GetToken().Position))
if err := next.SetComment(old.GetComment()); err != nil {
fmt.Println(err)
return
}
if err := path.ReplaceWithNode(f, next); err != nil {
fmt.Println(err)
return
}Full source: docs/examples/documents/editvalue/example_test.go
# bump me is attached to the node being replaced, so it goes with that node
unless the new one takes it over. ReplaceWithNode replaces every match in
every document of the file.
Write it back
// VerbatimFile copies every byte of the source the edit did not touch.
r := ast.NewRenderer(ast.WithSource([]byte(src)))
if err := r.VerbatimFile(os.Stdout, f); err != nil {
fmt.Println(err)
}Full source: docs/examples/documents/editvalue/example_test.go
# service
name: pet-store
version: 2.0.0 # bump me
ports:
- 80
- 443Full source: docs/examples/documents/editvalue/example_test.go
ast.WithSource gives the renderer the original bytes, and VerbatimFile
copies every byte the edit did not touch: the # service header and the
indented ports: entries stay as written.
The new node’s comment is laid out by the renderer, one space after the value.
The three spaces the file had before # bump me belonged to the old node and
went with it.
Variations
- Render the tree instead.
fmt.Print(f)lays the whole document out by depth:ports:entries move to column 0 and the comment spacing is reset. See The syntax tree. - Keep the three spaces too.
transform.Walkrewrites the value token alone, so every other byte, the spacing included, is copied. - Several edits.
VerbatimFilecopies the source forward once. A tree whose nodes now stand in another order returnsast.ErrMove. Render into abytes.Bufferwhen you need all or nothing.