-
Notifications
You must be signed in to change notification settings - Fork 8
/
analyze.go
480 lines (420 loc) · 11.7 KB
/
analyze.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package cuetsy
import (
"bytes"
"fmt"
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/format"
)
func tpv(v cue.Value) {
fmt.Printf("%s:\n%s\n", v.Path(), exprTree(v))
}
func findInstance(raw cue.Value) *build.Instance {
// Search, recursively, for cue.Value.BuildInstance() on:
// - the provided raw node
// - the first direct conjunct of the raw node, if any
// - the ReferencePath() result, if different, of either of the above
bi := raw.BuildInstance()
if bi != nil {
return bi
}
if ref, path := raw.ReferencePath(); len(path.Selectors()) != 0 && ref != raw {
if bi = findInstance(ref); bi != nil {
return bi
}
}
// No instance on anything reachable from the value itself. Try again with any
// immediate conjuncts.
if op, dvals := raw.Expr(); op == cue.AndOp {
// Only try the first value, which will represent the package. Additional values
// will be constraints specified on the whole package instance.
bi = findInstance(dvals[0])
}
return bi
}
func isReference(v cue.Value) bool {
// return cue.Dereference(v) == v
// FIXME the below impl is just wrong, but current behavior somehow relies on it. Above is correct, it's a pointer comparison
_, path := v.ReferencePath()
return len(path.Selectors()) > 0
}
func getKindFor(v cue.Value) (TSType, error) {
// Direct lookup of attributes with Attribute() seems broken-ish, so do our
// own search as best we can, allowing ValueAttrs, which include both field
// and decl attributes.
// TODO write a unit test checking expected attribute output behavior to
// protect this brittleness against regressions due to language changes
var found bool
var attr cue.Attribute
for _, a := range v.Attributes(cue.ValueAttr) {
if a.Name() == attrname {
found = true
attr = a
}
}
if !found {
if t, ok := getKindForField(v); ok {
return t, nil
}
return "", valError(v, "value has no \"@%s\" attribute", attrname)
}
tt, found, err := attr.Lookup(0, attrKind)
if err != nil {
return "", err
}
if !found {
return "", valError(v, "no value for the %q key in @%s attribute", attrKind, attrname)
}
return TSType(tt), nil
}
func getKindForField(v cue.Value) (TSType, bool) {
if field, ok := v.Source().(*ast.Field); ok {
if s, ok := field.Value.(*ast.StructLit); ok {
return iterateField(s)
}
}
return "", false
}
func iterateField(s *ast.StructLit) (TSType, bool) {
for _, el := range s.Elts {
if field, ok := el.(*ast.Field); ok {
if len(field.Attrs) > 0 {
return parseStringAttribute(field.Attrs[0].Text), true
}
if s, ok := field.Value.(*ast.StructLit); ok {
return iterateField(s)
}
}
}
return "", false
}
func parseStringAttribute(attr string) TSType {
switch attr {
case "@cuetsy(kind=\"interface\")":
return TypeInterface
case "@cuetsy(kind=\"enum\")":
return TypeEnum
case "@cuetsy(kind=\"type\")":
return TypeAlias
default:
return ""
}
}
func targetsKind(v cue.Value, kinds ...TSType) bool {
vkind, err := getKindFor(v)
if err != nil {
return false
}
if len(kinds) == 0 {
kinds = allKinds[:]
}
for _, knd := range kinds {
if vkind == knd {
return true
}
}
return false
}
// containsCuetsyReference does the same as containsReference, but returns true
// iff at least one referenced node passes the targetsKind predicate check
func containsCuetsyReference(v cue.Value, kinds ...TSType) bool {
if isReference(v) && targetsKind(cue.Dereference(v), kinds...) {
return true
}
for _, dv := range flatten(v) {
if isReference(dv) && targetsKind(cue.Dereference(dv), kinds...) {
return true
}
}
return hasOverrideValues(v, kinds...)
}
func hasOverrideValues(v cue.Value, kinds ...TSType) bool {
// When a value overrides the parent, its marked as BottomKind with two elements.
// It also conflicts with enums with default value, so internally we need to check if
// the operator of the second element isn't an OrOp
op, values := v.Expr()
if op != cue.AndOp || len(values) != 2 {
return false
}
if values[1].Kind() != cue.BottomKind || values[1].IncompleteKind() == cue.TopKind {
return false
}
for _, dv := range flatten(values[0]) {
if isReference(dv) && targetsKind(cue.Dereference(dv), kinds...) {
return true
}
}
return false
}
type valuePredicate func(cue.Value) bool
type valuePredicates []valuePredicate
func (pl valuePredicates) And(v cue.Value) bool {
for _, p := range pl {
if !p(v) {
return false
}
}
return true
}
func (pl valuePredicates) Or(v cue.Value) bool {
for _, p := range pl {
if p(v) {
return true
}
}
return len(pl) == 0
}
func containsPred(v cue.Value, depth int, pl ...valuePredicate) bool {
vpl := valuePredicates(pl)
if vpl.And(v) {
return true
}
if depth != -1 {
op, args := v.Expr()
_, has := v.Default()
if op != cue.NoOp || has {
for _, dv := range args {
if containsPred(dv, depth-1, vpl...) {
return true
}
}
}
}
return false
}
func flatten(v cue.Value) []cue.Value {
all := []cue.Value{v}
op, dvals := v.Expr()
defv, has := v.Default()
// if (op != cue.NoOp || has) &&
// v.Subsume(defv, cue.Raw()) != nil &&
// defv.Subsume(v, cue.Raw()) != nil {
// FIXME clearly wrong for using Equals(), but referencesValueAs currently depends on its wrong-ness
if !v.Equals(defv) && (op != cue.NoOp || has) {
all = append(all, dvals...)
for _, dv := range dvals {
all = append(all, flatten(dv)...)
}
}
return all
}
func findRefWithKind(v cue.Value, kinds ...TSType) (ref, referrer cue.Value, has bool) {
xt := exprTree(v)
xt.Walk(func(n *exprNode) bool {
// don't explore defaults paths
if n.isdefault {
return false
}
if !has && targetsKind(n.self, kinds...) {
ref = n.self
if n.parent != nil {
referrer = n.parent.self
}
has = true
}
return !has
})
return ref, referrer, has
}
// appendSplit splits a cue.Value into the
func appendSplit(a []cue.Value, splitBy cue.Op, v cue.Value) []cue.Value {
op, args := v.Expr()
// dedup elements.
k := 1
outer:
for i := 1; i < len(args); i++ {
for j := 0; j < k; j++ {
if args[i].Subsume(args[j], cue.Raw()) == nil &&
args[j].Subsume(args[i], cue.Raw()) == nil {
continue outer
}
}
args[k] = args[i]
k++
}
args = args[:k]
if op == cue.NoOp && len(args) == 1 {
// TODO: this is to deal with default value removal. This may change
a = append(a, args...)
} else if op != splitBy {
a = append(a, v)
} else {
for _, v := range args {
a = appendSplit(a, splitBy, v)
}
}
if defaultValue, is := v.Default(); is {
if o, _ := v.Expr(); o == cue.NoOp {
a = append(a, defaultValue)
}
}
return a
}
func dumpsyn(v cue.Value) (string, error) {
syn := v.Syntax(
cue.Concrete(false), // allow incomplete values
cue.Definitions(false),
cue.Optional(true),
cue.Attributes(true),
cue.Docs(true),
cue.ResolveReferences(false),
)
byt, err := format.Node(syn, format.Simplify(), format.TabIndent(true))
return string(byt), err
}
func dumpsynP(v cue.Value) string {
str, err := dumpsyn(v)
if err != nil {
panic(err)
}
return str
}
type listProps struct {
isOpen bool
divergentTypes bool
noDefault bool
differentDefault bool
emptyDefault bool
bottomKinded bool
argBottomKinded bool
}
type listField struct {
v cue.Value
lenElems int
anyType cue.Value
defv cue.Value
props listProps
}
func (l *listField) eq(oli *listField) bool {
if l.props.isOpen == oli.props.isOpen && l.props.divergentTypes == oli.props.divergentTypes && l.lenElems == oli.lenElems {
if !l.props.isOpen {
if l.lenElems == 0 {
return true
}
p := cue.MakePath(cue.Index(0))
// Sloppy, but enough to cover all but really complicated cases that
// are likely unsupportable anyway
return l.v.LookupPath(p).Equals(oli.v.LookupPath(p))
}
return l.anyType.Subsume(oli.anyType, cue.Raw(), cue.Schema()) == nil && oli.anyType.Subsume(l.anyType, cue.Raw(), cue.Schema()) == nil
}
return false
}
// analyzeList extracts useful characteristics of pure lists (i.e. no disjuncts
// with other kinds) into shorthand. The analysis walks down logic structures,
// but does NOT traverse references.
//
// The return value is a slice of listFields. An empty slice indicates the
// input was not a list. If the kind is mixed (i.e. disjunct over soem list and
// non-list types), this peels out
func analyzeList(v cue.Value) []*listField {
// Start by checking incomplete kind and expr. We can bail early if there's no
// ListKind at all. The recursion in this function relies on that behavior.
ik := v.IncompleteKind()
if ik&cue.ListKind != cue.ListKind {
return nil
}
li := &listField{
v: v,
}
all := []*listField{li}
// There's at least _some_ non-empty lists in the value. Next, tease out
// expressions and defaults.
op, args := v.Expr()
switch op {
case cue.NoOp:
// This branch hits whether there's an explicit default or not. e.g.,
// - `[...string] | *[]`
// - `[...string]`
// Open lists are guaranteed to have a default: the empty list. And the default of
// the empty list is itself (the empty list), recursively. This is annoying,
// even if reasonable.
defv, has := v.Default()
li.props.noDefault = !has
if has {
li.props.differentDefault = !v.Equals(defv)
li.props.emptyDefault = v.Context().NewList().Equals(defv)
}
li.props.bottomKinded = v.Kind() == cue.BottomKind
li.props.isOpen = v.Allows(cue.AnyIndex)
// li.props.isOpen = !v.IsClosed()
v = args[0]
li.props.argBottomKinded = v.Kind() == cue.BottomKind
// if !li.props.differentDefault && li.props.emptyDefault {
// // Input v is `[] | *[]`. Bail out to avoid infinite recursion.
// break
// }
iter, _ := v.List()
var first cue.Value
var nonempty bool
var ct int
if nonempty = iter.Next(); nonempty {
ct++
first = iter.Value()
}
for iter.Next() {
ct++
iv := iter.Value()
lerr, rerr := first.Subsume(iv, cue.Schema()), iv.Subsume(first, cue.Schema())
if lerr != nil || rerr != nil {
li.props.divergentTypes = true
}
}
li.lenElems = ct
if li.props.isOpen && nonempty {
li.anyType = v.LookupPath(cue.MakePath(cue.AnyIndex))
lerr, rerr := first.Subsume(li.anyType, cue.Schema()), li.anyType.Subsume(first, cue.Schema())
if lerr != nil || rerr != nil {
li.props.divergentTypes = true
}
}
case cue.AndOp, cue.OrOp:
// not sure this is a good idea but whatever
for _, arg := range args {
all = append(all, analyzeList(arg)...)
}
default:
panic("wat")
}
return all
}
type listpred func(props listProps) bool
func groupBy(lfs []*listField, f listpred) (has, not []*listField) {
for _, lf := range lfs {
if f(lf.props) {
has = append(has, lf)
} else {
not = append(not, lf)
}
}
return
}
func (l *listField) String() string {
var buf bytes.Buffer
synstr, err := dumpsyn(l.v)
if err != nil {
synstr = l.v.Path().String()
}
fmt.Fprintf(&buf, "`%s`:\n", synstr)
fmt.Fprintf(&buf, "\tisOpen: %v\n", l.props.isOpen)
fmt.Fprintf(&buf, "\tdivergentTypes: %v\n", l.props.divergentTypes)
fmt.Fprintf(&buf, "\tnoDefault: %v\n", l.props.noDefault)
fmt.Fprintf(&buf, "\temptyDefault: %v\n", l.props.emptyDefault)
fmt.Fprintf(&buf, "\tdifferentDefault: %v\n", l.props.differentDefault)
fmt.Fprintf(&buf, "\tbottomKinded: %v\n", l.props.bottomKinded)
fmt.Fprintf(&buf, "\targBottomKinded: %v\n", l.props.argBottomKinded)
return buf.String()
}
func veq(a, b cue.Value) bool {
// Theoretically, lattice equality can be defined as mutual subsumption. In
// practice, Subsume() seems to ignore optional fields, and Equals() doesn't.
//
// But, Equals has false positives on cases involving incomplete values. It does
// not seem to have any false negatives. So, use both and cross fingers.
return a.Exists() &&
b.Exists() &&
a.Equals(b) &&
a.Subsume(b, cue.Raw()) == nil &&
b.Subsume(a, cue.Raw()) == nil
}