-
Notifications
You must be signed in to change notification settings - Fork 153
/
scope.go
64 lines (59 loc) · 1.54 KB
/
scope.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
package valuestest
import (
"context"
"errors"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/flux"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
// ScopeComparer checks that two scopes are equal in both nesting and contents.
// Functions cannot be compared for equality so only function types are checked.
var ScopeComparer = cmp.Comparer(func(l, r values.Scope) bool {
for {
if l == nil && r == nil {
return true
}
if l == nil && r != nil || l != nil && r == nil {
return false
}
equal := true
l.LocalRange(func(k string, lv values.Value) {
rv, ok := r.LocalLookup(k)
if lv.PolyType().Nature() == semantic.Function {
// only compare functions by type
equal = equal && ok && lv.PolyType().Equal(rv.PolyType())
} else {
equal = equal && ok && lv.Equal(rv)
}
})
if !equal {
return false
}
r.LocalRange(func(k string, rv values.Value) {
_, ok := l.LocalLookup(k)
equal = equal && ok
})
if !equal {
return false
}
l = l.Pop()
r = r.Pop()
}
})
// NowScope generates scope with the prelude + the now option.
func NowScope() values.Scope {
scope := flux.Prelude()
scope.SetOption("universe", "now", values.NewFunction(
"now",
semantic.NewFunctionPolyType(semantic.FunctionPolySignature{
Return: semantic.Time,
}),
func(ctx context.Context, args values.Object) (values.Value, error) {
//Functions are only compared by type so the function body here is not important
return nil, errors.New("NowScope was called")
},
false,
))
return scope
}