runtime

package
v0.195.2 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2024 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/dependency"
	"github.com/influxdata/flux/runtime"

	_ "github.com/influxdata/flux/fluxinit/static"
)

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.
	ctx, deps := dependency.Inject(context.Background(), dependenciestest.Default())
	defer deps.Finish()
	nowTime, _ := nowFunc.Function().Call(ctx, 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/dependency"
	"github.com/influxdata/flux/runtime"

	_ "github.com/influxdata/flux/fluxinit/static"
)

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

	ctx, deps := dependency.Inject(context.Background(), dependenciestest.Default())
	defer deps.Finish()

	_, 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.

View Source
var (
	// list of packages included in the prelude.
	// Packages must be listed in import order
	PreludeList = []string{
		"internal/boolean",
		"internal/location",
		"universe",
		"influxdata/influxdb",
	}
)

Functions

func AnalyzePackage

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

func AnalyzeSource

func AnalyzeSource(ctx context.Context, 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(ctx context.Context, flux string) (flux.ASTHandle, error)

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

func ParseToJSON

func ParseToJSON(ctx context.Context, flux string) ([]byte, error)

func Prelude

func Prelude() values.Scope

Prelude returns a scope object representing the Flux universe block

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