-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr.go
167 lines (129 loc) · 3.1 KB
/
expr.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package conflint
import (
"fmt"
"strings"
yaml "gopkg.in/yaml.v3"
)
type Context struct {
Current *yaml.Node
Root *yaml.Node
}
type CondOpOr struct {
Exprs []BoolExpr
}
func (e *CondOpOr) Eval(ctx Context) (bool, error) {
any := false
for _, expr := range e.Exprs {
r, err := expr.Eval(ctx)
if err != nil {
return false, fmt.Errorf("evaluating `%s` within or expr: %w", expr, err)
}
any = any || r
}
return any, nil
}
func (e *CondOpOr) String() string {
all := []string{}
for _, expr := range e.Exprs {
all = append(all, expr.String())
}
return strings.Join(all, " || ")
}
type CondOpAnd struct {
Exprs []BoolExpr
}
func (e *CondOpAnd) Eval(ctx Context) (bool, error) {
all := true
for _, expr := range e.Exprs {
r, err := expr.Eval(ctx)
if err != nil {
return false, fmt.Errorf("evaluating `%s` within and expr: %w", expr, err)
}
all = all && r
}
return all, nil
}
func (e *CondOpAnd) String() string {
all := []string{}
for _, expr := range e.Exprs {
all = append(all, expr.String())
}
return strings.Join(all, " && ")
}
type ExprEq struct {
Path *Path
Value string
Code string
}
func (e *ExprEq) String() string {
return e.Code
}
func (e *ExprEq) Eval(ctx Context) (bool, error) {
got, err := e.Path.Get(ctx.Current)
if err != nil {
return false, fmt.Errorf("evaluating `%s`: %w", e.Code, err)
}
return got.Value == e.Value, nil
}
type BoolExpr interface {
Eval(Context) (bool, error)
String() string
}
var _ BoolExpr = &ExprEq{}
func parseBoolExp(condStr string) (BoolExpr, error) {
var any []BoolExpr
anyComps := strings.Split(condStr, "||")
for i, anyComp := range anyComps {
var all []BoolExpr
andComps := strings.Split(anyComp, "&&")
for _, andComp := range andComps {
comp := strings.Split(andComp, "==")
if len(comp) != 2 {
return nil, fmt.Errorf("unsupported expression found: wanted an equality expression like `A == B`, but got %v", condStr)
}
left := strings.TrimSpace(comp[0])
right := strings.TrimSpace(comp[1])
var rightValue string
switch right[0] {
case '"':
rightValue = strings.TrimRight(right[1:], "\"")
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
rightValue = right
case '\'':
rightValue = strings.TrimRight(right[1:], "'")
default:
switch right {
case "true", "false":
rightValue = right
default:
return nil, fmt.Errorf("parsing right side of equality: unexpected expression: %v", right)
}
}
nestedPath, err := parseJsonpath(left)
if err != nil {
return nil, fmt.Errorf("parsing %s: %w", left, err)
}
boolExp := &ExprEq{
Path: nestedPath,
Value: rightValue,
Code: condStr,
}
all = append(all, boolExp)
}
if len(all) == 0 {
return nil, fmt.Errorf("parsing and components from `%s` at %d", anyComp, i)
} else if len(all) == 1 {
any = append(any, all[0])
} else {
any = append(any, &CondOpAnd{
Exprs: all,
})
}
}
if len(any) == 0 {
return nil, fmt.Errorf("parsing or components from `%s`", condStr)
} else if len(any) == 1 {
return any[0], nil
}
return &CondOpOr{Exprs: any}, nil
}