Fixed multi doc anchor bug #1861

This commit is contained in:
Mike Farah 2023-11-14 13:00:30 +11:00
parent 79a50b9c20
commit c7ef946031
2 changed files with 12 additions and 1 deletions

View File

@ -20,6 +20,9 @@ type yamlDecoder struct {
leadingContent string leadingContent string
bufferRead bytes.Buffer bufferRead bytes.Buffer
// anchor map persists over multiple documents for convenience.
anchorMap map[string]*CandidateNode
readAnything bool readAnything bool
firstFile bool firstFile bool
documentIndex uint documentIndex uint
@ -95,6 +98,7 @@ func (dec *yamlDecoder) Init(reader io.Reader) error {
dec.decoder = *yaml.NewDecoder(readerToUse) dec.decoder = *yaml.NewDecoder(readerToUse)
dec.firstFile = false dec.firstFile = false
dec.documentIndex = 0 dec.documentIndex = 0
dec.anchorMap = make(map[string]*CandidateNode)
return nil return nil
} }
@ -121,7 +125,7 @@ func (dec *yamlDecoder) Decode() (*CandidateNode, error) {
candidateNode := CandidateNode{document: dec.documentIndex} candidateNode := CandidateNode{document: dec.documentIndex}
// don't bother with the DocumentNode // 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -32,6 +32,13 @@ var yamlFormatScenarios = []formatScenario{
input: "[null]", input: "[null]",
expected: "[null]\n", 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 - [~]", description: "basic - [~]",
skipDoc: true, skipDoc: true,