-
Notifications
You must be signed in to change notification settings - Fork 153
/
global.go
99 lines (87 loc) · 2.85 KB
/
global.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
package runtime
import (
"context"
"encoding/json"
"github.com/influxdata/flux"
"github.com/influxdata/flux/ast"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/parser"
"github.com/influxdata/flux/values"
)
// RegisterPackageValue adds a value for an identifier in a builtin package
func RegisterPackageValue(pkgpath, name string, value values.Value) {
if err := Default.RegisterPackageValue(pkgpath, name, value); err != nil {
panic(err)
}
}
// ReplacePackageValue replaces a value for an identifier in a builtin package
func ReplacePackageValue(pkgpath, name string, value values.Value) {
if err := Default.ReplacePackageValue(pkgpath, name, value); err != nil {
panic(err)
}
}
// StdLib returns an importer for the Flux standard library.
func StdLib() interpreter.Importer {
return Default.Stdlib()
}
// Prelude returns a scope object representing the Flux universe block
func Prelude() values.Scope {
return Default.Prelude()
}
// Eval accepts a Flux script and evaluates it to produce a set of side effects (as a slice of values) and a scope.
func Eval(ctx context.Context, flux string, opts ...flux.ScopeMutator) ([]interpreter.SideEffect, values.Scope, error) {
h, err := parser.ParseToHandle([]byte(flux))
if err != nil {
return nil, nil, err
}
return Default.Eval(ctx, h, nil, opts...)
}
// EvalAST accepts a Flux AST and evaluates it to produce a set of side effects (as a slice of values) and a scope.
func EvalAST(ctx context.Context, astPkg *ast.Package, opts ...flux.ScopeMutator) ([]interpreter.SideEffect, values.Scope, error) {
bs, err := json.Marshal(astPkg)
if err != nil {
return nil, nil, err
}
hdl, err := Default.JSONToHandle(bs)
if err != nil {
return nil, nil, err
}
return Default.Eval(ctx, hdl, nil, opts...)
}
// EvalOptions is like EvalAST, but only evaluates options.
func EvalOptions(ctx context.Context, astPkg *ast.Package, opts []flux.ScopeMutator) ([]interpreter.SideEffect, values.Scope, error) {
return EvalAST(ctx, options(astPkg), opts...)
}
// options returns a shallow copy of the AST, trimmed to include only option statements.
func options(astPkg *ast.Package) *ast.Package {
trimmed := &ast.Package{
BaseNode: astPkg.BaseNode,
Path: astPkg.Path,
Package: astPkg.Package,
}
for _, f := range astPkg.Files {
var body []ast.Statement
for _, s := range f.Body {
if opt, ok := s.(*ast.OptionStatement); ok {
body = append(body, opt)
}
}
if len(body) > 0 {
trimmed.Files = append(trimmed.Files, &ast.File{
Body: body,
BaseNode: f.BaseNode,
Name: f.Name,
Package: f.Package,
Imports: f.Imports,
})
}
}
return trimmed
}
// FinalizeBuiltIns must be called to complete registration.
// Future calls to RegisterFunction or RegisterPackageValue will panic.
func FinalizeBuiltIns() {
if err := Default.Finalize(); err != nil {
panic(err)
}
}