forked from mikefarah/yq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperator_style.go
105 lines (90 loc) · 2.54 KB
/
operator_style.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package yqlib
import (
"container/list"
"fmt"
)
func parseStyle(customStyle string) (Style, error) {
if customStyle == "tagged" {
return TaggedStyle, nil
} else if customStyle == "double" {
return DoubleQuotedStyle, nil
} else if customStyle == "single" {
return SingleQuotedStyle, nil
} else if customStyle == "literal" {
return LiteralStyle, nil
} else if customStyle == "folded" {
return FoldedStyle, nil
} else if customStyle == "flow" {
return FlowStyle, nil
} else if customStyle != "" {
return 0, fmt.Errorf("Unknown style %v", customStyle)
}
return 0, nil
}
func assignStyleOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("AssignStyleOperator: %v")
var style Style
if !expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
if err != nil {
return Context{}, err
}
if rhs.MatchingNodes.Front() != nil {
style, err = parseStyle(rhs.MatchingNodes.Front().Value.(*CandidateNode).Value)
if err != nil {
return Context{}, err
}
}
}
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
if err != nil {
return Context{}, err
}
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
log.Debugf("Setting style of : %v", NodeToString(candidate))
if expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
if err != nil {
return Context{}, err
}
if rhs.MatchingNodes.Front() != nil {
style, err = parseStyle(rhs.MatchingNodes.Front().Value.(*CandidateNode).Value)
if err != nil {
return Context{}, err
}
}
}
candidate.Style = style
}
return context, nil
}
func getStyleOperator(_ *dataTreeNavigator, context Context, _ *ExpressionNode) (Context, error) {
log.Debugf("GetStyleOperator")
var results = list.New()
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
var style string
switch candidate.Style {
case TaggedStyle:
style = "tagged"
case DoubleQuotedStyle:
style = "double"
case SingleQuotedStyle:
style = "single"
case LiteralStyle:
style = "literal"
case FoldedStyle:
style = "folded"
case FlowStyle:
style = "flow"
case 0:
style = ""
default:
style = "<unknown>"
}
result := candidate.CreateReplacement(ScalarNode, "!!str", style)
results.PushBack(result)
}
return context.ChildContext(results), nil
}