script

package
v0.0.0-...-d33de98 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2022 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Unlimited        = -1              // Unlimited number fo concurrency
	MaxExecutionTime = 5 * time.Minute // Default maximum execution time to 5 minutes
)

Variables

View Source
var (
	ErrEntryAlreadyRegistered  = errors.New("entry already registered")
	ErrEntryDoesNotExists      = errors.New("entry does not exists")
	ErrUnsupportedConfigFormat = errors.New("unsupported configuration file format")
	ErrBytecodeNotReady        = errors.New("bytecode not ready")
	ErrInvalidBytecode         = errors.New("invalid bytecode")
	ErrBytecodeNotRecompilable = errors.New("can not recompile bytecode")
)

List of known errors

Functions

func GetImportableModuleMap

func GetImportableModuleMap(modules []string) *tengo.ModuleMap

GetImportableModuleMap from given modules name. Search module from stdlib and tnglib

func RunFile

func RunFile(filename string, modules ...string) error

RunFile execute tengo script file using default context

func RunFileContext

func RunFileContext(ctx context.Context, filename string, modules ...string) error

RunFileContext execute tengo script file with given context

Types

type Config

type Config struct {
	MaxExecutionTime tnglib.Span            `json:"maxExecutionTime"`
	EnableFileImport bool                   `json:"enableFileImport"`
	ImportDir        string                 `json:"importDir"`
	MaxAllocs        int64                  `json:"maxAllocs"`
	MaxConstObjects  int                    `json:"maxConstObjects"`
	ImportFileExt    []string               `json:"importFileExt"`
	Modules          []string               `json:"modules"` // empty means all modules
	InitVars         map[string]interface{} `json:"initVars"`
}

Config stores configuration for each script to be executed.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig create default configuration for script

func NewFileConfig

func NewFileConfig(filename string) (*Config, error)

NewFileConfig load configuration from given file. Supported formats: json

func (Config) ImportDirectory

func (c Config) ImportDirectory(srcFilename string) string

ImportDirectory for given srcFilename

func (Config) ImportFileExtensions

func (c Config) ImportFileExtensions() []string

func (Config) IsSourceFile

func (c Config) IsSourceFile(filename string) bool

IsSourceFile return true if extension is registered as compiled extension

type Entry

type Entry interface {
	Configuration() Config
	CompiledAt() time.Time
	Age() time.Duration
	Runnable() Runnable
	Recompile() error
	SaveTo(filename string) error
	String() string
}

Entry properties

func BytecodeFromBytes

func BytecodeFromBytes(input []byte) (Entry, error)

BytecodeFromBytes convert byte array to byte code

func BytecodeFromCompiled

func BytecodeFromCompiled(filename string) (Entry, error)

BytecodeFromCompiled loads compiled code from file

func BytecodeFromFile

func BytecodeFromFile(filename string, conf *Config) (Entry, error)

BytecodeFromFile create bytecode either from source or compiled

func BytecodeFromSource

func BytecodeFromSource(filename string, conf *Config) (Entry, error)

BytecodeFromSource compiles source file and then store it to bytecode

func BytecodeFromString

func BytecodeFromString(b64 string) (Entry, error)

BytecodeFromString convert base-64 encoded string to bytecode

type Executor

type Executor interface {
	Exec(id string, inpVars map[string]interface{}, outVars ...string) ([]*tengo.Variable, error)
	ExecContext(ctx context.Context, id string, inpVars map[string]interface{}, outVars ...string) ([]*tengo.Variable, error)
	NumInProgress() int
	WaitAll(ctx context.Context) error
}

Executor executes manages script

func NewExecutor

func NewExecutor(man Manager, maxConcurrent int) Executor

NewExecutor create script executor with limited concurrency. If set to <= 0, then there will be no limit.

type Manager

type Manager interface {
	Exists(id string) bool
	AddEntry(id string, e Entry) error
	Add(id string, data []byte) error
	AddFile(id, filename string, conf *Config) error
	Recompile(id string) error
	Entry(id string) Entry
	Remove(id string) bool
	Clear()
	Age(id string) (time.Duration, bool)
}

Manager manages compiled script

func NewManager

func NewManager() Manager

NewManager creates compiled script manager

type Runnable

type Runnable interface {
	Get(name string) *tengo.Variable
	GetAll() []*tengo.Variable
	IsDefined(name string) bool
	Run() error
	RunContext(ctx context.Context) (err error)
	Set(name string, value interface{}) error
}

Runnable code

Jump to

Keyboard shortcuts

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