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
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
func assignStyleOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) {
|
2021-01-06 09:37:53 +00:00
|
|
|
|
|
|
|
log.Debugf("AssignStyleOperator: %v")
|
|
|
|
var style yaml.Style
|
2021-01-12 23:18:53 +00:00
|
|
|
if !expressionNode.Operation.UpdateAssign {
|
|
|
|
rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs)
|
2021-01-06 09:37:53 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs)
|
2020-11-02 00:20:38 +00:00
|
|
|
|
|
|
|
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-12 23:18:53 +00:00
|
|
|
if expressionNode.Operation.UpdateAssign {
|
|
|
|
rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs)
|
2021-01-06 09:37:53 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
func getStyleOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*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"}
|
2021-01-12 08:36:28 +00:00
|
|
|
result := candidate.CreateChild(nil, node)
|
|
|
|
results.PushBack(result)
|
2020-11-02 00:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|