yq/pkg/yqlib/operator_style.go

106 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"
)
2023-04-09 01:14:51 +00:00
func parseStyle(customStyle string) (Style, error) {
2020-11-02 00:20:38 +00:00
if customStyle == "tagged" {
2023-04-09 01:14:51 +00:00
return TaggedStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "double" {
2023-04-09 01:14:51 +00:00
return DoubleQuotedStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "single" {
2023-04-09 01:14:51 +00:00
return SingleQuotedStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "literal" {
2023-04-09 01:14:51 +00:00
return LiteralStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "folded" {
2023-04-09 01:14:51 +00:00
return FoldedStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "flow" {
2023-04-09 01:14:51 +00:00
return 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")
2023-04-09 01:14:51 +00:00
var style 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 {
2023-04-09 01:14:51 +00:00
style, err = parseStyle(rhs.MatchingNodes.Front().Value.(*CandidateNode).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)
2023-05-30 05:05:28 +00:00
log.Debugf("Setting style of : %v", NodeToString(candidate))
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 {
2023-04-09 01:14:51 +00:00
style, err = parseStyle(rhs.MatchingNodes.Front().Value.(*CandidateNode).Value)
2021-01-06 09:37:53 +00:00
if err != nil {
return Context{}, err
2021-01-06 09:37:53 +00:00
}
}
}
2023-04-09 01:14:51 +00:00
candidate.Style = style
2020-11-02 00:20:38 +00:00
}
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
2023-04-09 01:14:51 +00:00
switch candidate.Style {
case TaggedStyle:
2020-11-02 00:20:38 +00:00
style = "tagged"
2023-04-09 01:14:51 +00:00
case DoubleQuotedStyle:
2020-11-02 00:20:38 +00:00
style = "double"
2023-04-09 01:14:51 +00:00
case SingleQuotedStyle:
2020-11-02 00:20:38 +00:00
style = "single"
2023-04-09 01:14:51 +00:00
case LiteralStyle:
2020-11-02 00:20:38 +00:00
style = "literal"
2023-04-09 01:14:51 +00:00
case FoldedStyle:
2020-11-02 00:20:38 +00:00
style = "folded"
2023-04-09 01:14:51 +00:00
case FlowStyle:
2020-11-02 00:20:38 +00:00
style = "flow"
case 0:
style = ""
default:
style = "<unknown>"
}
2023-04-09 01:14:51 +00:00
result := candidate.CreateReplacement(ScalarNode, "!!str", style)
results.PushBack(result)
2020-11-02 00:20:38 +00:00
}
return context.ChildContext(results), nil
2020-11-02 00:20:38 +00:00
}