yq/pkg/yqlib/operator_style.go

109 lines
2.7 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, context Context, expressionNode *ExpressionNode) (Context, 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(context.ReadOnlyClone(), expressionNode.RHS)
2021-01-06 09:37:53 +00:00
if err != nil {
return Context{}, err
2021-01-06 09:37:53 +00:00
}
if rhs.MatchingNodes.Front() != nil {
style, err = parseStyle(rhs.MatchingNodes.Front().Value.(*CandidateNode).Node.Value)
2021-01-06 09:37:53 +00:00
if err != nil {
return Context{}, err
2021-01-06 09:37:53 +00:00
}
}
}
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
2020-11-02 00:20:38 +00:00
if err != nil {
return Context{}, err
2020-11-02 00:20:38 +00:00
}
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
2020-11-02 00:20:38 +00:00
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(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
2021-01-06 09:37:53 +00:00
if err != nil {
return Context{}, err
2021-01-06 09:37:53 +00:00
}
if rhs.MatchingNodes.Front() != nil {
style, err = parseStyle(rhs.MatchingNodes.Front().Value.(*CandidateNode).Node.Value)
2021-01-06 09:37:53 +00:00
if err != nil {
return Context{}, err
2021-01-06 09:37:53 +00:00
}
}
}
2020-11-02 00:20:38 +00:00
candidate.Node.Style = style
}
return context, nil
2020-11-02 00:20:38 +00:00
}
func getStyleOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
2020-11-02 00:20:38 +00:00
log.Debugf("GetStyleOperator")
var results = list.New()
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
2020-11-02 00:20:38 +00:00
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-11-23 22:57:35 +00:00
result := candidate.CreateReplacement(node)
results.PushBack(result)
2020-11-02 00:20:38 +00:00
}
return context.ChildContext(results), nil
2020-11-02 00:20:38 +00:00
}