yq/pkg/yqlib/candidate_node_goccy_yaml.go

92 lines
2.3 KiB
Go
Raw Normal View History

2023-10-05 06:53:42 +00:00
package yqlib
2023-10-05 07:04:11 +00:00
import (
"fmt"
"github.com/goccy/go-yaml/ast"
)
2023-10-05 06:53:42 +00:00
func (o *CandidateNode) goccyDecodeIntoChild(childNode ast.Node, anchorMap map[string]*CandidateNode) (*CandidateNode, error) {
newChild := o.CreateChild()
err := newChild.UnmarshalGoccyYAML(childNode, anchorMap)
return newChild, err
}
func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, anchorMap map[string]*CandidateNode) error {
log.Debugf("UnmarshalYAML %v", node)
log.Debugf("UnmarshalYAML %v", node.Type().String())
o.Value = node.String()
switch node.Type() {
case ast.IntegerType:
o.Kind = ScalarNode
o.Tag = "!!int"
case ast.FloatType:
o.Kind = ScalarNode
o.Tag = "!!float"
case ast.StringType:
o.Kind = ScalarNode
o.Tag = "!!str"
case ast.TagType:
o.UnmarshalGoccyYAML(node.(*ast.TagNode).Value, anchorMap)
o.Tag = node.(*ast.TagNode).Start.Value
2023-10-05 07:04:11 +00:00
case ast.MappingValueType, ast.MappingType:
2023-10-05 06:53:42 +00:00
log.Debugf("UnmarshalYAML - a mapping node")
o.Kind = MappingNode
2023-10-05 07:04:11 +00:00
o.Tag = "!!map"
if node.Type() == ast.MappingType {
o.Style = FlowStyle
}
2023-10-05 06:53:42 +00:00
astMapIter := node.(ast.MapNode).MapRange()
for astMapIter.Next() {
log.Debug("UnmarshalYAML map entry %v", astMapIter.Key().String())
keyNode, err := o.goccyDecodeIntoChild(astMapIter.Key(), anchorMap)
if err != nil {
return err
}
keyNode.IsMapKey = true
log.Debug("UnmarshalYAML map value %v", astMapIter.Value().String())
valueNode, err := o.goccyDecodeIntoChild(astMapIter.Value(), anchorMap)
if err != nil {
return err
}
o.Content = append(o.Content, keyNode, valueNode)
}
2023-10-05 07:04:11 +00:00
case ast.SequenceType:
log.Debugf("UnmarshalYAML - a sequence node")
o.Kind = SequenceNode
o.Tag = "!!seq"
sequenceNode := node.(*ast.SequenceNode)
if sequenceNode.IsFlowStyle {
o.Style = FlowStyle
}
astSeq := sequenceNode.Values
o.Content = make([]*CandidateNode, len(astSeq))
for i := 0; i < len(astSeq); i++ {
keyNode := o.CreateChild()
keyNode.IsMapKey = true
keyNode.Tag = "!!int"
keyNode.Kind = ScalarNode
keyNode.Value = fmt.Sprintf("%v", i)
valueNode, err := o.goccyDecodeIntoChild(astSeq[i], anchorMap)
if err != nil {
return err
}
valueNode.Key = keyNode
o.Content[i] = valueNode
}
2023-10-05 06:53:42 +00:00
default:
log.Debugf("UnmarshalYAML - node idea of the type!!")
}
log.Debugf("KIND: %v", o.Kind)
return nil
}