runtime

package
v0.95.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2020 License: MIT Imports: 14 Imported by: 11

Documentation

Overview

Example (Option)

Example_option demonstrates retrieving an option value from a scope object

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/influxdata/flux/dependencies/dependenciestest"
	_ "github.com/influxdata/flux/fluxinit/static"
	"github.com/influxdata/flux/runtime"
)

func main() {

	// Import the universe package.
	importer := runtime.StdLib()
	universe, _ := importer.ImportPackageObject("universe")

	// Retrieve the default value for the now option
	nowFunc, _ := universe.Get("now")

	// The now option is a function value whose default behavior is to return
	// the current system time when called. The function now() doesn't take
	// any arguments so can be called with nil.
	nowTime, _ := nowFunc.Function().Call(dependenciestest.Default().Inject(context.TODO()), nil)
	fmt.Fprintf(os.Stderr, "The current system time (UTC) is: %v\n", nowTime)
}
Output:

Example (OverrideDefaultOptionExternally)

Example_overrideDefaultOptionExternally demonstrates how declaring an option in a Flux script will change that option's binding globally.

package main

import (
	"context"
	"fmt"

	"github.com/influxdata/flux/dependencies/dependenciestest"
	_ "github.com/influxdata/flux/fluxinit/static"
	"github.com/influxdata/flux/runtime"
)

func main() {
	queryString := `
		option now = () => 2018-07-13T00:00:00Z
		what_time_is_it = now()`

	ctx := dependenciestest.Default().Inject(context.Background())
	_, scope, err := runtime.Eval(ctx, queryString)
	if err != nil {
		fmt.Println(err)
	}

	// After evaluating the package, lookup the value of what_time_is_it
	now, _ := scope.Lookup("what_time_is_it")

	// what_time_is_it? Why it's ....
	fmt.Printf("The new current time (UTC) is: %v", now)
}
Output:

The new current time (UTC) is: 2018-07-13T00:00:00.000000000Z

Index

Examples

Constants

This section is empty.

Variables

View Source
var Default = &runtime{}

Default contains the preregistered packages and builtin values required to execute a flux script.

Functions

func AnalyzePackage

func AnalyzePackage(astPkg flux.ASTHandle) (*semantic.Package, error)

func AnalyzeSource

func AnalyzeSource(fluxSrc string) (*semantic.Package, error)

AnalyzeSource parses and analyzes the given Flux source, using libflux.

func Eval

Eval accepts a Flux script and evaluates it to produce a set of side effects (as a slice of values) and a scope.

func EvalAST

func EvalAST(ctx context.Context, astPkg *ast.Package, opts ...flux.ScopeMutator) ([]interpreter.SideEffect, values.Scope, error)

EvalAST accepts a Flux AST and evaluates it to produce a set of side effects (as a slice of values) and a scope.

func EvalOptions

func EvalOptions(ctx context.Context, astPkg *ast.Package, opts []flux.ScopeMutator) ([]interpreter.SideEffect, values.Scope, error)

EvalOptions is like EvalAST, but only evaluates options.

func FinalizeBuiltIns

func FinalizeBuiltIns()

FinalizeBuiltIns must be called to complete registration. Future calls to RegisterFunction or RegisterPackageValue will panic.

func LookupBuiltinType

func LookupBuiltinType(pkg, name string) (semantic.MonoType, error)

LookupBuiltinType returns the type of the builtin value for a given Flux stdlib package. Returns an error if lookup fails.

func MergePackages

func MergePackages(dst, src flux.ASTHandle) error

func MustLookupBuiltinType

func MustLookupBuiltinType(pkg, name string) semantic.MonoType

MustLookupBuiltinType validates that call to LookupBuiltInType was successful. If there is an error with lookup, then panic.

func Parse

func Parse(flux string) (flux.ASTHandle, error)

Parse parses a Flux script and produces an ast.Package.

func ParseToJSON

func ParseToJSON(flux string) ([]byte, error)

func Prelude

func Prelude() values.Scope

Prelude returns a scope object representing the Flux universe block

func RegisterPackage

func RegisterPackage(pkg *ast.Package)

RegisterPackage adds a builtin package

func RegisterPackageValue

func RegisterPackageValue(pkgpath, name string, value values.Value)

RegisterPackageValue adds a value for an identifier in a builtin package

func ReplacePackageValue

func ReplacePackageValue(pkgpath, name string, value values.Value)

ReplacePackageValue replaces a value for an identifier in a builtin package

func StdLib

func StdLib() interpreter.Importer

StdLib returns an importer for the Flux standard library.

func TypeEnvMap

func TypeEnvMap(env *fbsemantic.TypeEnvironment) map[envKey]*fbsemantic.Prop

TypeEnvMap creates a global map of the TypeEnvironment

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL