From 08e95ebebfdae27ede8ee7d7a45ec67966e4cc51 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 20 Nov 2023 09:58:50 +1100 Subject: [PATCH] Working around goccy --- pkg/yqlib/candidate_node_goccy_yaml.go | 11 +++++++++-- pkg/yqlib/decoder_goccy_yaml.go | 4 +++- pkg/yqlib/yaml_test.go | 7 +++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pkg/yqlib/candidate_node_goccy_yaml.go b/pkg/yqlib/candidate_node_goccy_yaml.go index 7974c723..99855dfc 100644 --- a/pkg/yqlib/candidate_node_goccy_yaml.go +++ b/pkg/yqlib/candidate_node_goccy_yaml.go @@ -16,7 +16,7 @@ func (o *CandidateNode) goccyDecodeIntoChild(childNode ast.Node, cm yaml.Comment return newChild, err } -func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap) error { +func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap, anchorMap map[string]*CandidateNode) error { log.Debugf("UnmarshalYAML %v", node) log.Debugf("UnmarshalYAML %v", node.Type().String()) log.Debugf("UnmarshalYAML Node Value: %v", node.String()) @@ -49,6 +49,13 @@ func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap) er } } + o.Anchor = node. + + if o.Anchor != "" { + anchorMap[o.Anchor] = o + } + + if o.Ali o.Value = node.String() switch node.Type() { @@ -92,7 +99,7 @@ func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap) er // to solve the multiline > problem o.Value = astLiteral.Value.Value case ast.TagType: - if err := o.UnmarshalGoccyYAML(node.(*ast.TagNode).Value, cm); err != nil { + if err := o.UnmarshalGoccyYAML(node.(*ast.TagNode).Value, cm, anchorMap); err != nil { return err } o.Tag = node.(*ast.TagNode).Start.Value diff --git a/pkg/yqlib/decoder_goccy_yaml.go b/pkg/yqlib/decoder_goccy_yaml.go index 7598b486..2499127b 100644 --- a/pkg/yqlib/decoder_goccy_yaml.go +++ b/pkg/yqlib/decoder_goccy_yaml.go @@ -17,6 +17,7 @@ type goccyYamlDecoder struct { cm yaml.CommentMap bufferRead bytes.Buffer readAnything bool + anchorMap map[string]*CandidateNode } func NewGoccyYAMLDecoder() Decoder { @@ -26,6 +27,7 @@ func NewGoccyYAMLDecoder() Decoder { func (dec *goccyYamlDecoder) Init(reader io.Reader) error { dec.cm = yaml.CommentMap{} dec.readAnything = false + dec.anchorMap = make(map[string]*CandidateNode) readerToUse := io.TeeReader(reader, &dec.bufferRead) dec.decoder = *yaml.NewDecoder(readerToUse, yaml.CommentToMap(dec.cm), yaml.UseOrderedMap()) return nil @@ -57,7 +59,7 @@ func (dec *goccyYamlDecoder) Decode() (*CandidateNode, error) { } candidateNode := &CandidateNode{} - if err := candidateNode.UnmarshalGoccyYAML(ast, dec.cm); err != nil { + if err := candidateNode.UnmarshalGoccyYAML(ast, dec.cm, dec.anchorMap); err != nil { return nil, err } diff --git a/pkg/yqlib/yaml_test.go b/pkg/yqlib/yaml_test.go index 13666bd7..641ce732 100644 --- a/pkg/yqlib/yaml_test.go +++ b/pkg/yqlib/yaml_test.go @@ -38,6 +38,13 @@ var yamlFormatScenarios = []formatScenario{ input: "[null]", expected: "[null]\n", }, + { + description: "simple anchor map", + skipDoc: true, + input: "a: &remember mike\nb: *remember", + expression: "explode(.)", + expected: "a: mike\nb: mike\n", + }, { description: "multi document anchor map", skipDoc: true,