yq/pkg/yqlib/operator_style.go

109 lines
2.6 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-11-02 00:20:38 +00:00
import (
"container/list"
"fmt"
2021-01-06 09:37:53 +00:00
yaml "gopkg.in/yaml.v3"
2020-11-02 00:20:38 +00:00
)
2021-01-06 09:37:53 +00:00
func parseStyle(customStyle string) (yaml.Style, error) {
2020-11-02 00:20:38 +00:00
if customStyle == "tagged" {
2021-01-06 09:37:53 +00:00
return yaml.TaggedStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "double" {
2021-01-06 09:37:53 +00:00
return yaml.DoubleQuotedStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "single" {
2021-01-06 09:37:53 +00:00
return yaml.SingleQuotedStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "literal" {
2021-01-06 09:37:53 +00:00
return yaml.LiteralStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "folded" {
2021-01-06 09:37:53 +00:00
return yaml.FoldedStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "flow" {
2021-01-06 09:37:53 +00:00
return yaml.FlowStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle != "" {
2021-01-06 09:37:53 +00:00
return 0, fmt.Errorf("Unknown style %v", customStyle)
2020-11-02 00:20:38 +00:00
}
2021-01-06 09:37:53 +00:00
return 0, nil
}
func assignStyleOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
2021-01-06 09:37:53 +00:00
log.Debugf("AssignStyleOperator: %v")
var style yaml.Style
if !pathNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs)
if err != nil {
return nil, err
}
if rhs.Front() != nil {
style, err = parseStyle(rhs.Front().Value.(*CandidateNode).Node.Value)
if err != nil {
return nil, err
}
}
}
2020-11-02 00:20:38 +00:00
lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
if err != nil {
return nil, err
}
for el := lhs.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
log.Debugf("Setting style of : %v", candidate.GetKey())
2021-01-06 09:37:53 +00:00
if pathNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs)
if err != nil {
return nil, err
}
if rhs.Front() != nil {
style, err = parseStyle(rhs.Front().Value.(*CandidateNode).Node.Value)
if err != nil {
return nil, err
}
}
}
2020-11-02 00:20:38 +00:00
candidate.Node.Style = style
}
return matchingNodes, nil
}
func getStyleOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
2020-11-02 00:20:38 +00:00
log.Debugf("GetStyleOperator")
var results = list.New()
for el := matchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
2020-11-13 03:07:11 +00:00
var style string
2020-11-02 00:20:38 +00:00
switch candidate.Node.Style {
case yaml.TaggedStyle:
style = "tagged"
case yaml.DoubleQuotedStyle:
style = "double"
case yaml.SingleQuotedStyle:
style = "single"
case yaml.LiteralStyle:
style = "literal"
case yaml.FoldedStyle:
style = "folded"
case yaml.FlowStyle:
style = "flow"
case 0:
style = ""
default:
style = "<unknown>"
}
node := &yaml.Node{Kind: yaml.ScalarNode, Value: style, Tag: "!!str"}
lengthCand := &CandidateNode{Node: node, Document: candidate.Document, Path: candidate.Path}
results.PushBack(lengthCand)
}
return results, nil
}