-
Notifications
You must be signed in to change notification settings - Fork 154
/
planner.go
59 lines (51 loc) · 1.55 KB
/
planner.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
package expect
import (
"context"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/dependencies/testing"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
const pkgpath = "testing/expect"
func init() {
signature := runtime.MustLookupBuiltinType(pkgpath, "planner")
runtime.RegisterPackageValue(pkgpath, "planner",
values.NewFunction("planner",
signature,
func(ctx context.Context, args values.Object) (values.Value, error) {
return interpreter.DoFunctionCallContext(Planner, ctx, args)
},
true,
),
)
}
func Planner(ctx context.Context, args interpreter.Arguments) (values.Value, error) {
rules, err := args.GetRequiredDictionary("rules")
if err != nil {
return nil, err
}
rulesType := rules.Type()
if keyType, err := rulesType.KeyType(); err != nil {
return nil, err
} else if got := keyType.Nature(); got != semantic.String {
return nil, errors.Newf(codes.FailedPrecondition, "key type must be a string, got %s", got)
}
if valueType, err := rulesType.ValueType(); err != nil {
return nil, err
} else if got := valueType.Nature(); got != semantic.Int {
return nil, errors.Newf(codes.FailedPrecondition, "value type must be an int, got %s", got)
}
rules.Dict().Range(func(key, value values.Value) {
if err != nil {
return
}
err = testing.ExpectPlannerRule(ctx, key.Str(), int(value.Int()))
})
if err != nil {
return nil, err
}
return values.Void, nil
}