Skip to content

Commit

Permalink
Fixed multi doc anchor bug #1861
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Nov 14, 2023
1 parent 79a50b9 commit c7ef946
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/yqlib/decoder_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type yamlDecoder struct {
leadingContent string
bufferRead bytes.Buffer

// anchor map persists over multiple documents for convenience.
anchorMap map[string]*CandidateNode

readAnything bool
firstFile bool
documentIndex uint
Expand Down Expand Up @@ -95,6 +98,7 @@ func (dec *yamlDecoder) Init(reader io.Reader) error {
dec.decoder = *yaml.NewDecoder(readerToUse)
dec.firstFile = false
dec.documentIndex = 0
dec.anchorMap = make(map[string]*CandidateNode)
return nil
}

Expand All @@ -121,7 +125,7 @@ func (dec *yamlDecoder) Decode() (*CandidateNode, error) {

candidateNode := CandidateNode{document: dec.documentIndex}
// don't bother with the DocumentNode
err = candidateNode.UnmarshalYAML(yamlNode.Content[0], make(map[string]*CandidateNode))
err = candidateNode.UnmarshalYAML(yamlNode.Content[0], dec.anchorMap)
if err != nil {
return nil, err
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/yqlib/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ var yamlFormatScenarios = []formatScenario{
input: "[null]",
expected: "[null]\n",
},
{
description: "multi document anchor map",
skipDoc: true,
input: "a: &remember mike\n---\nb: *remember",
expression: "explode(.)",
expected: "a: mike\n---\nb: mike\n",
},
{
description: "basic - [~]",
skipDoc: true,
Expand Down

0 comments on commit c7ef946

Please sign in to comment.