Repository navigation
Expand file tree
/
Copy pathtask_editor.go
More file actions
128 lines (118 loc) · 3.36 KB
/
Copy pathtask_editor.go
File metadata and controls
128 lines (118 loc) · 3.36 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package edit
import (
"github.com/influxdata/flux"
"github.com/influxdata/flux/ast"
"github.com/influxdata/flux/codes"
)
// GetOption finds and returns the init for the option's variable assignment
func GetOption(file *ast.File, name string) (ast.Expression, error) {
for _, st := range file.Body {
if val, ok := st.(*ast.OptionStatement); ok {
assign := val.Assignment
if va, ok := assign.(*ast.VariableAssignment); ok {
if va.ID.Name == name {
if ok {
return va.Init, nil
}
}
}
}
}
return nil, &flux.Error{
Code: codes.Internal,
Msg: "Option not found",
}
}
// SetOption replaces an existing option's init with the provided init or adds
// the option if it doesn't exist. The file AST is mutated in place.
func SetOption(file *ast.File, name string, expr ast.Expression) {
// check for the correct file
for _, st := range file.Body {
if val, ok := st.(*ast.OptionStatement); ok {
assign := val.Assignment
if va, ok := assign.(*ast.VariableAssignment); ok {
if va.ID.Name == name {
// replace the variable assignment's init
va.Init = expr
return
}
}
}
}
// option was not found. prepend new option to body
file.Body = append([]ast.Statement{&ast.OptionStatement{
Assignment: &ast.VariableAssignment{
ID: &ast.Identifier{Name: name},
Init: expr,
},
}}, file.Body...)
}
// DeleteOption removes an option if it exists. The file AST is mutated in place.
func DeleteOption(file *ast.File, name string) {
for i, st := range file.Body {
if val, ok := st.(*ast.OptionStatement); ok {
assign := val.Assignment
if va, ok := assign.(*ast.VariableAssignment); ok {
if va.ID.Name == name {
file.Body = append(file.Body[:i], file.Body[i+1:]...)
return
}
}
}
}
}
// GetProperty finds and returns the AST node for the property value.
func GetProperty(obj *ast.ObjectExpression, key string) (ast.Expression, error) {
for _, prop := range obj.Properties {
if key == prop.Key.Key() {
return prop.Value, nil
}
}
return nil, &flux.Error{
Code: codes.Internal,
Msg: "Property not found",
}
}
// SetProperty replaces an existing property definition with the provided object expression or adds
// the property if it doesn't exist. The object expression AST is mutated in place.
func SetProperty(obj *ast.ObjectExpression, key string, value ast.Expression) {
for _, prop := range obj.Properties {
if key == prop.Key.Key() {
prop.Value = value
return
}
}
obj.Properties = append(obj.Properties, &ast.Property{
BaseNode: obj.BaseNode,
Key: &ast.Identifier{Name: key},
Value: value,
})
}
// DeleteProperty removes a property from the object expression if it exists.
// The object expression AST is mutated in place.
func DeleteProperty(obj *ast.ObjectExpression, key string) {
for i, prop := range obj.Properties {
if key == prop.Key.Key() {
obj.Properties = append(obj.Properties[:i], obj.Properties[i+1:]...)
return
}
}
}
// HasDuplicateOptions determines whether or not there are multiple assignments
// to the same option variable.
func HasDuplicateOptions(file *ast.File, name string) bool {
var n int
for _, st := range file.Body {
if val, ok := st.(*ast.OptionStatement); ok {
assign := val.Assignment
if va, ok := assign.(*ast.VariableAssignment); ok {
if va.ID.Name == name {
if ok {
n++
}
}
}
}
}
return n > 1
}