yq/pkg/yqlib/candidate_node_yaml.go

231 lines
5.4 KiB
Go
Raw Normal View History

2023-04-11 05:33:32 +00:00
package yqlib
import (
"fmt"
yaml "gopkg.in/yaml.v3"
)
func MapYamlStyle(original yaml.Style) Style {
switch original {
case yaml.TaggedStyle:
return TaggedStyle
case yaml.DoubleQuotedStyle:
return DoubleQuotedStyle
case yaml.SingleQuotedStyle:
return SingleQuotedStyle
case yaml.LiteralStyle:
return LiteralStyle
case yaml.FoldedStyle:
return FoldedStyle
case yaml.FlowStyle:
return FlowStyle
}
return 0
}
func MapToYamlStyle(original Style) yaml.Style {
switch original {
case TaggedStyle:
return yaml.TaggedStyle
case DoubleQuotedStyle:
return yaml.DoubleQuotedStyle
case SingleQuotedStyle:
return yaml.SingleQuotedStyle
case LiteralStyle:
return yaml.LiteralStyle
case FoldedStyle:
return yaml.FoldedStyle
case FlowStyle:
return yaml.FlowStyle
}
return 0
}
func (o *CandidateNode) copyFromYamlNode(node *yaml.Node) {
o.Style = MapYamlStyle(node.Style)
o.Tag = node.Tag
o.Value = node.Value
o.Anchor = node.Anchor
// o.Alias = TODO - find Alias in our own structure
// might need to be a post process thing
o.HeadComment = node.HeadComment
o.LineComment = node.LineComment
o.FootComment = node.FootComment
o.Line = node.Line
o.Column = node.Column
}
func (o *CandidateNode) copyToYamlNode(node *yaml.Node) {
node.Style = MapToYamlStyle(o.Style)
node.Tag = o.Tag
node.Value = o.Value
node.Anchor = o.Anchor
// node.Alias = TODO - find Alias in our own structure
// might need to be a post process thing
node.HeadComment = o.HeadComment
node.LineComment = o.LineComment
node.FootComment = o.FootComment
node.Line = o.Line
node.Column = o.Column
}
2023-04-13 04:34:34 +00:00
func (o *CandidateNode) decodeIntoChild(childNode *yaml.Node) (*CandidateNode, error) {
newChild := o.CreateChild()
// null yaml.Nodes to not end up calling UnmarshalYAML
// so we call it explicitly
if childNode.Tag == "!!null" {
newChild.Kind = ScalarNode
newChild.copyFromYamlNode(childNode)
return newChild, nil
}
err := childNode.Decode(newChild)
return newChild, err
}
2023-04-11 05:33:32 +00:00
func (o *CandidateNode) UnmarshalYAML(node *yaml.Node) error {
2023-04-13 04:34:34 +00:00
log.Debugf("UnmarshalYAML %v", node.Tag)
2023-04-11 05:33:32 +00:00
switch node.Kind {
case yaml.DocumentNode:
2023-04-13 04:34:34 +00:00
log.Debugf("UnmarshalYAML - a document")
2023-04-11 05:33:32 +00:00
o.Kind = DocumentNode
o.copyFromYamlNode(node)
if len(node.Content) == 0 {
return nil
}
2023-04-13 04:34:34 +00:00
singleChild, err := o.decodeIntoChild(node.Content[0])
2023-04-11 05:33:32 +00:00
if err != nil {
return err
}
o.Content = []*CandidateNode{singleChild}
return nil
case yaml.AliasNode:
2023-04-13 04:34:34 +00:00
log.Debug("UnmarshalYAML - alias from yaml: %v", o.Tag)
2023-04-11 05:33:32 +00:00
o.Kind = AliasNode
o.copyFromYamlNode(node)
return nil
case yaml.ScalarNode:
2023-04-13 04:34:34 +00:00
log.Debugf("UnmarshalYAML - a scalar")
2023-04-11 05:33:32 +00:00
o.Kind = ScalarNode
o.copyFromYamlNode(node)
return nil
case yaml.MappingNode:
2023-04-13 04:34:34 +00:00
log.Debugf("UnmarshalYAML - a mapping node")
2023-04-11 05:33:32 +00:00
o.Kind = MappingNode
o.copyFromYamlNode(node)
o.Content = make([]*CandidateNode, len(node.Content))
for i := 0; i < len(node.Content); i += 2 {
2023-04-13 04:34:34 +00:00
keyNode, err := o.decodeIntoChild(node.Content[i])
2023-04-11 05:33:32 +00:00
if err != nil {
return err
}
2023-04-13 04:34:34 +00:00
keyNode.IsMapKey = true
valueNode, err := o.decodeIntoChild(node.Content[i+1])
2023-04-11 05:33:32 +00:00
if err != nil {
return err
}
2023-04-13 04:34:34 +00:00
valueNode.Key = keyNode
2023-04-11 05:33:32 +00:00
o.Content[i] = keyNode
o.Content[i+1] = valueNode
}
return nil
case yaml.SequenceNode:
2023-04-13 04:34:34 +00:00
log.Debugf("UnmarshalYAML - a sequence: %v", len(node.Content))
2023-04-11 05:33:32 +00:00
o.Kind = SequenceNode
o.copyFromYamlNode(node)
o.Content = make([]*CandidateNode, len(node.Content))
for i := 0; i < len(node.Content); i += 1 {
keyNode := o.CreateChild()
keyNode.IsMapKey = true // can't remember if we need this for sequences
keyNode.Tag = "!!int"
keyNode.Kind = ScalarNode
keyNode.Value = fmt.Sprintf("%v", i)
2023-04-13 04:34:34 +00:00
valueNode, err := o.decodeIntoChild(node.Content[i])
2023-04-11 05:33:32 +00:00
if err != nil {
return err
}
2023-04-13 04:34:34 +00:00
valueNode.Key = keyNode
2023-04-11 05:33:32 +00:00
o.Content[i] = valueNode
}
return nil
case 0:
2023-04-13 04:34:34 +00:00
log.Debugf("UnmarshalYAML - errr.. %v", node.Tag)
2023-04-11 05:33:32 +00:00
// not sure when this happens
o.copyFromYamlNode(node)
return nil
default:
return fmt.Errorf("orderedMap: invalid yaml node")
}
}
func (o *CandidateNode) MarshalYAML() (interface{}, error) {
2023-04-13 05:40:41 +00:00
log.Debug("MarshalYAML to yaml: %v", o.Tag)
2023-04-11 05:33:32 +00:00
switch o.Kind {
case DocumentNode:
2023-04-13 05:40:41 +00:00
log.Debug("MarshalYAML its a document")
2023-04-11 05:33:32 +00:00
target := &yaml.Node{Kind: yaml.DocumentNode}
o.copyToYamlNode(target)
singleChild := &yaml.Node{}
err := singleChild.Encode(o.Content[0])
2023-04-13 05:40:41 +00:00
log.Debug("MarshalYAML its a document - singChild is %v", singleChild.Tag)
2023-04-11 05:33:32 +00:00
if err != nil {
return nil, err
}
target.Content = []*yaml.Node{singleChild}
return target, nil
case AliasNode:
2023-04-13 05:40:41 +00:00
log.Debug("MarshalYAML - alias to yaml: %v", o.Tag)
2023-04-11 05:33:32 +00:00
target := &yaml.Node{Kind: yaml.AliasNode}
o.copyToYamlNode(target)
return target, nil
case ScalarNode:
2023-04-13 05:40:41 +00:00
log.Debug("MarshalYAML - scalar: %v", o.Value)
2023-04-11 05:33:32 +00:00
target := &yaml.Node{Kind: yaml.ScalarNode}
o.copyToYamlNode(target)
return target, nil
case MappingNode, SequenceNode:
targetKind := yaml.MappingNode
if o.Kind == SequenceNode {
targetKind = yaml.SequenceNode
}
target := &yaml.Node{Kind: targetKind}
o.copyToYamlNode(target)
target.Content = make([]*yaml.Node, len(o.Content))
for i := 0; i < len(o.Content); i += 1 {
child := &yaml.Node{}
err := child.Encode(o.Content[i])
if err != nil {
return nil, err
}
2023-04-12 09:04:02 +00:00
log.Debug("child type %v", child.Tag)
log.Debug("child is doc %v", child.Kind == yaml.DocumentNode)
2023-04-11 05:33:32 +00:00
target.Content[i] = child
}
return target, nil
}
target := &yaml.Node{}
o.copyToYamlNode(target)
return target, nil
}