-
Notifications
You must be signed in to change notification settings - Fork 153
/
object_keys.go
35 lines (31 loc) · 1011 Bytes
/
object_keys.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
package experimental
import (
"context"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
func init() {
runtime.RegisterPackageValue("experimental", "objectKeys", values.NewFunction(
"objectKeys",
runtime.MustLookupBuiltinType("experimental", "objectKeys"),
func(ctx context.Context, args values.Object) (values.Value, error) {
o, ok := args.Get("o")
if !ok {
return nil, errors.New(codes.Invalid, "missing parameter \"o\"")
}
if o.Type().Nature() != semantic.Object {
return nil, errors.New(codes.Invalid, "parameter \"o\" is not an object")
}
obj := o.Object()
keys := make([]values.Value, 0, obj.Len())
obj.Range(func(name string, _ values.Value) {
keys = append(keys, values.NewString(name))
})
return values.NewArrayWithBacking(semantic.NewArrayType(semantic.BasicString), keys), nil
},
false,
))
}