-
Notifications
You must be signed in to change notification settings - Fork 16
/
unresolved_check.go
372 lines (330 loc) · 7.25 KB
/
unresolved_check.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package dynaml
import (
"fmt"
"reflect"
"strings"
"github.com/mandelsoft/spiff/yaml"
)
type UnresolvedNodes struct {
Nodes []UnresolvedNode
}
type UnresolvedNode struct {
yaml.Node
Context []string
Path []string
}
func PrintableNodeValue(node yaml.Node) interface{} {
nv := node.Value()
switch nv.(type) {
case map[string]yaml.Node:
nv = "<map>"
case []yaml.Node:
nv = "<list>"
}
return nv
}
func (e UnresolvedNodes) Issue(msgfmt string, args ...interface{}) (result yaml.Issue, localError bool, failed bool) {
format := ""
result = yaml.NewIssue(msgfmt, args...)
localError = false
failed = false
for _, node := range e.Nodes {
issue := node.Issue()
msg := issue.Issue
if msg != "" {
msg = tag(node) + msg
}
if node.HasError() {
localError = true
}
if node.Failed() {
failed = true
}
switch node.Value().(type) {
case Expression:
format = "(( %s ))\tin %s\t%s\t(%s)%s"
default:
format = "%s\tin %s\t%s\t(%s)%s"
}
nv := PrintableNodeValue(node)
message := fmt.Sprintf(
format,
nv,
node.SourceName(),
strings.Join(node.Context, "."),
strings.Join(node.Path, "."),
msg,
)
issue.Issue = message
result.Nested = append(result.Nested, issue)
}
return
}
func (e UnresolvedNodes) HasError() bool {
for _, node := range e.Nodes {
issue := node.Issue()
msg := issue.Issue
if msg != "" {
return true
}
}
return false
}
func (e UnresolvedNodes) Error() string {
message := "unresolved nodes:"
format := ""
for _, node := range e.Nodes {
issue := node.Issue()
msg := issue.Issue
if msg != "" {
msg = "\t" + tag(node) + msg
}
nv := PrintableNodeValue(node)
switch nv.(type) {
case Expression:
format = "%s\n\t(( %s ))\tin %s\t%s\t(%s)%s"
default:
format = "%s\n\t%s\tin %s\t%s\t(%s)%s"
}
val := strings.Replace(fmt.Sprintf("%s", nv), "\n", "\n\t", -1)
message = fmt.Sprintf(
format,
message,
val,
node.SourceName(),
strings.Join(node.Context, "."),
strings.Join(node.Path, "."),
msg,
)
message += nestedIssues(issue)
}
return message
}
func tag(node yaml.Node) string {
tag := " "
if !node.Failed() {
tag = "@"
} else {
tag = "-"
}
if node.HasError() {
tag = "*"
}
return tag
}
func nestedIssues(issue yaml.Issue) string {
prefix := "\n"
gap := ""
if issue.Sequence {
prefix = "\n\t\t... "
} else {
gap = "\t\t\t"
}
message := ""
if issue.Nested != nil {
for _, sub := range issue.Nested {
message = message + prefix + sub.Issue
message += nestedIssues(sub)
}
}
return strings.Replace(message, "\n", "\n"+gap, -1)
}
func FindUnresolvedNodes(root yaml.Node, context ...string) (result []UnresolvedNode) {
if root == nil {
return result
}
var nodes []UnresolvedNode
dummy := []string{"dummy"}
found := false
switch val := root.Value().(type) {
case map[string]yaml.Node:
for key, val := range val {
nodes = append(
nodes,
FindUnresolvedNodes(val, addContext(context, key)...)...,
)
}
case []yaml.Node:
for i, val := range val {
context := addContext(context, fmt.Sprintf("[%d]", i))
nodes = append(
nodes,
FindUnresolvedNodes(val, context...)...,
)
}
case Expression:
nodes = append(nodes, UnresolvedNode{
Node: root,
Context: context,
Path: effectivePath(root, context),
})
found = true
case TemplateValue:
// context := addContext(context, fmt.Sprintf("&"))
// nodes = append(
// nodes,
// FindUnresolvedNodes(val.Orig, context...)...,
// )
case string:
if s := yaml.EmbeddedDynaml(root, false); s != nil {
_, err := Parse(*s, dummy, dummy)
if err != nil {
nodes = append(nodes, UnresolvedNode{
Node: yaml.IssueNode(root, true, false, yaml.Issue{Issue: err.Error()}),
Context: context,
Path: []string{},
})
found = true
}
}
}
if root.Failed() {
if !found {
nodes = append(nodes, UnresolvedNode{
Node: root,
Context: context,
Path: effectivePath(root, context),
})
}
}
for _, n := range nodes {
if n.GetAnnotation().HasError() {
result = append(result, n)
}
}
for _, n := range nodes {
if !n.GetAnnotation().HasError() && !n.GetAnnotation().Failed() {
result = append(result, n)
}
}
for _, n := range nodes {
if !n.GetAnnotation().HasError() && n.GetAnnotation().Failed() {
result = append(result, n)
}
}
return result
}
func ResetUnresolvedNodes(root yaml.Node) yaml.Node {
if root == nil {
return root
}
switch elem := root.Value().(type) {
case map[string]yaml.Node:
for key, val := range elem {
elem[key] = ResetUnresolvedNodes(val)
}
case []yaml.Node:
for i, val := range elem {
elem[i] = ResetUnresolvedNodes(val)
}
case Expression:
root = NewNode(fmt.Sprintf("(( %s ))", elem), nil)
}
return root
}
func effectivePath(node yaml.Node, context []string) []string {
var path []string
switch val := node.Value().(type) {
case AutoExpr:
path = val.Path
case MergeExpr:
path = val.Path
default:
orig := node.Issue().OrigPath
if orig != nil {
if !reflect.DeepEqual(context, orig) {
if len(orig) > len(context) && reflect.DeepEqual(context, orig[:len(context)]) {
path = append(append(orig[0:0:0], ".."), orig[len(context):]...)
} else {
path = orig
}
}
}
}
return path
}
func addContext(context []string, step string) []string {
dup := make([]string, len(context))
copy(dup, context)
return append(dup, step)
}
func IsExpression(val interface{}) bool {
if val == nil {
return false
}
_, ok := val.(Expression)
return ok
}
func isLocallyResolved(node yaml.Node, binding Binding) bool {
return isLocallyResolvedValue(node.Value(), binding)
}
func isLocallyResolvedValue(value interface{}, binding Binding) bool {
switch v := value.(type) {
case Expression:
return false
case map[string]yaml.Node:
if !yaml.IsMapResolved(v, binding.GetFeatures()) {
return false
}
case []yaml.Node:
if !yaml.IsListResolved(v, binding.GetFeatures()) {
return false
}
default:
}
return true
}
func IsResolvedNode(node yaml.Node, binding Binding) bool {
if node == nil {
return false
}
if node.Failed() || node.Undefined() {
return false
}
return isResolvedValue(node.Value(), binding)
}
func isResolved(node yaml.Node, binding Binding) bool {
return node == nil || isResolvedValue(node.Value(), binding)
}
func _isResolved(node yaml.Node, acceptFailed bool, binding Binding) bool {
if node == nil || (acceptFailed && (node.Failed() || node.HasError())) {
return true
}
return _isResolvedValue(node.Value(), acceptFailed, binding)
}
func isResolvedValue(val interface{}, binding Binding) bool {
return _isResolvedValue(val, false, binding)
}
func _isResolvedValue(val interface{}, acceptFailed bool, binding Binding) bool {
if val == nil {
return true
}
switch v := val.(type) {
case Expression:
return false
case []yaml.Node:
for _, n := range v {
if !_isResolved(n, acceptFailed, binding) {
return false
}
}
return true
case map[string]yaml.Node:
if !yaml.IsMapResolved(v, binding.GetFeatures()) {
return false
}
for _, n := range v {
if !_isResolved(n, acceptFailed, binding) {
return false
}
}
return true
case string:
if yaml.EmbeddedDynaml(NewNode(val, nil), false) != nil {
return false
}
return true
default:
return true
}
}