yq/pkg/yqlib/candidate_node_goccy_yaml.go

180 lines
5.2 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"
2023-10-05 23:36:28 +00:00
"strings"
2023-10-05 07:04:11 +00:00
2023-10-05 23:36:28 +00:00
yaml "github.com/goccy/go-yaml"
2023-10-05 07:04:11 +00:00
"github.com/goccy/go-yaml/ast"
2023-10-05 09:22:53 +00:00
goccyToken "github.com/goccy/go-yaml/token"
2023-10-05 07:04:11 +00:00
)
2023-10-05 06:53:42 +00:00
2023-10-05 23:36:28 +00:00
func (o *CandidateNode) goccyDecodeIntoChild(childNode ast.Node, cm yaml.CommentMap) (*CandidateNode, error) {
2023-10-05 06:53:42 +00:00
newChild := o.CreateChild()
2023-10-05 23:36:28 +00:00
err := newChild.UnmarshalGoccyYAML(childNode, cm)
2023-10-05 06:53:42 +00:00
return newChild, err
}
2023-10-05 23:36:28 +00:00
func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap) error {
2023-10-05 06:53:42 +00:00
log.Debugf("UnmarshalYAML %v", node)
log.Debugf("UnmarshalYAML %v", node.Type().String())
2023-10-05 23:36:28 +00:00
log.Debugf("UnmarshalYAML Node Value: %v", node.String())
log.Debugf("UnmarshalYAML Node GetComment: %v", node.GetComment())
if node.GetComment() != nil {
commentMapComments := cm[node.GetPath()]
for _, comment := range node.GetComment().Comments {
// need to use the comment map to find the position :/
log.Debugf("%v has a comment of [%v]", node.GetPath(), comment.Token.Value)
for _, commentMapComment := range commentMapComments {
commentMapValue := strings.Join(commentMapComment.Texts, "\n")
if commentMapValue == comment.Token.Value {
log.Debug("found a matching entry in comment map")
// we found the comment in the comment map,
// now we can process the position
switch commentMapComment.Position {
case yaml.CommentHeadPosition:
o.HeadComment = comment.String()
log.Debug("its a head comment %v", comment.String())
case yaml.CommentLinePosition:
o.LineComment = comment.String()
log.Debug("its a line comment %v", comment.String())
case yaml.CommentFootPosition:
o.FootComment = comment.String()
log.Debug("its a foot comment %v", comment.String())
}
}
}
}
}
2023-10-05 06:53:42 +00:00
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"
2023-10-10 03:28:53 +00:00
case ast.BoolType:
o.Kind = ScalarNode
o.Tag = "!!bool"
case ast.NullType:
o.Kind = ScalarNode
o.Tag = "!!null"
o.Value = node.GetToken().Value
2023-10-05 06:53:42 +00:00
case ast.StringType:
o.Kind = ScalarNode
o.Tag = "!!str"
2023-10-05 09:22:53 +00:00
switch node.GetToken().Type {
case goccyToken.SingleQuoteType:
o.Style = SingleQuotedStyle
case goccyToken.DoubleQuoteType:
o.Style = DoubleQuotedStyle
}
o.Value = node.(*ast.StringNode).Value
log.Debugf("string value %v", node.(*ast.StringNode).Value)
case ast.LiteralType:
o.Kind = ScalarNode
o.Tag = "!!str"
o.Style = LiteralStyle
astLiteral := node.(*ast.LiteralNode)
2023-10-05 23:36:28 +00:00
log.Debugf("astLiteral.Start.Type %v", astLiteral.Start.Type)
2023-10-05 09:22:53 +00:00
if astLiteral.Start.Type == goccyToken.FoldedType {
2023-10-05 23:36:28 +00:00
log.Debugf("folded Type %v", astLiteral.Start.Type)
2023-10-05 09:22:53 +00:00
o.Style = FoldedStyle
}
2023-10-05 23:36:28 +00:00
log.Debug("start value: %v ", node.(*ast.LiteralNode).Start.Value)
log.Debug("start value: %v ", node.(*ast.LiteralNode).Start.Type)
// TODO: here I could put the original value with line breaks
// to solve the multiline > problem
2023-10-05 09:22:53 +00:00
o.Value = astLiteral.Value.Value
2023-10-05 06:53:42 +00:00
case ast.TagType:
2023-10-05 23:36:28 +00:00
o.UnmarshalGoccyYAML(node.(*ast.TagNode).Value, cm)
2023-10-05 06:53:42 +00:00
o.Tag = node.(*ast.TagNode).Start.Value
2023-10-05 23:36:28 +00:00
case 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"
2023-10-05 23:36:28 +00:00
mappingNode := node.(*ast.MappingNode)
if mappingNode.IsFlowStyle {
2023-10-05 07:04:11 +00:00
o.Style = FlowStyle
}
2023-10-05 23:36:28 +00:00
for _, mappingValueNode := range mappingNode.Values {
err := o.goccyProcessMappingValueNode(mappingValueNode, cm)
2023-10-05 06:53:42 +00:00
if err != nil {
2023-10-05 23:36:28 +00:00
return ast.ErrInvalidAnchorName
2023-10-05 06:53:42 +00:00
}
2023-10-05 23:36:28 +00:00
}
if mappingNode.FootComment != nil {
log.Debugf("mapping node has a foot comment of: %v", mappingNode.FootComment)
o.FootComment = mappingNode.FootComment.String()
}
case ast.MappingValueType:
log.Debugf("UnmarshalYAML - a mapping node")
o.Kind = MappingNode
o.Tag = "!!map"
mappingValueNode := node.(*ast.MappingValueNode)
err := o.goccyProcessMappingValueNode(mappingValueNode, cm)
if err != nil {
return ast.ErrInvalidAnchorName
2023-10-05 06:53:42 +00:00
}
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)
2023-10-05 23:36:28 +00:00
valueNode, err := o.goccyDecodeIntoChild(astSeq[i], cm)
2023-10-05 07:04:11 +00:00
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
}
2023-10-05 23:36:28 +00:00
func (o *CandidateNode) goccyProcessMappingValueNode(mappingEntry *ast.MappingValueNode, cm yaml.CommentMap) error {
log.Debug("UnmarshalYAML MAP KEY entry %v", mappingEntry.Key)
keyNode, err := o.goccyDecodeIntoChild(mappingEntry.Key, cm)
if err != nil {
return err
}
keyNode.IsMapKey = true
log.Debug("UnmarshalYAML MAP VALUE entry %v", mappingEntry.Value)
valueNode, err := o.goccyDecodeIntoChild(mappingEntry.Value, cm)
if err != nil {
return err
}
if mappingEntry.FootComment != nil {
valueNode.FootComment = mappingEntry.FootComment.String()
}
o.Content = append(o.Content, keyNode, valueNode)
return nil
}