Working around goccy

This commit is contained in:
Mike Farah 2023-11-20 09:58:50 +11:00
parent ed5685319d
commit 08e95ebebf
3 changed files with 19 additions and 3 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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,