yq/pkg/yqlib/operator_style.go

106 lines
2.5 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"
)
func parseStyle(customStyle string) (Style, error) {
2020-11-02 00:20:38 +00:00
if customStyle == "tagged" {
return TaggedStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "double" {
return DoubleQuotedStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "single" {
return SingleQuotedStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "literal" {
return LiteralStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "folded" {
return FoldedStyle, nil
2020-11-02 00:20:38 +00:00
} else if customStyle == "flow" {
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")
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 {
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)
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 {
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
}
}
}
candidate.Style = style
2020-11-02 00:20:38 +00:00
}
return context, nil
2020-11-02 00:20:38 +00:00
}
2024-01-11 02:17:34 +00:00
func getStyleOperator(_ *dataTreeNavigator, context Context, _ *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
switch candidate.Style {
case TaggedStyle:
2020-11-02 00:20:38 +00:00
style = "tagged"
case DoubleQuotedStyle:
2020-11-02 00:20:38 +00:00
style = "double"
case SingleQuotedStyle:
2020-11-02 00:20:38 +00:00
style = "single"
case LiteralStyle:
2020-11-02 00:20:38 +00:00
style = "literal"
case FoldedStyle:
2020-11-02 00:20:38 +00:00
style = "folded"
case FlowStyle:
2020-11-02 00:20:38 +00:00
style = "flow"
case 0:
style = ""
default:
style = "<unknown>"
}
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
}