Documentation ¶
Overview ¶
Package starbox provides a set of utilities for building Starlark virtual machine.
Index ¶
- Variables
- func NewMemory() *dataconv.SharedDict
- func SetLog(l *zap.SugaredLogger)
- type FuncMap
- type InspectCondFunc
- type ModuleSetName
- type RunnerConfig
- func (c *RunnerConfig) Context(ctx context.Context) *RunnerConfig
- func (c *RunnerConfig) Execute() (starlet.StringAnyMap, error)
- func (c *RunnerConfig) FileName(name string) *RunnerConfig
- func (c *RunnerConfig) Inspect(force bool) *RunnerConfig
- func (c *RunnerConfig) InspectCond(cond InspectCondFunc) *RunnerConfig
- func (c *RunnerConfig) KeyValue(key string, value interface{}) *RunnerConfig
- func (c *RunnerConfig) KeyValueMap(extras starlet.StringAnyMap) *RunnerConfig
- func (c *RunnerConfig) Script(content string) *RunnerConfig
- func (c *RunnerConfig) Starbox(b *Starbox) *RunnerConfig
- func (c *RunnerConfig) String() string
- func (c *RunnerConfig) Timeout(timeout time.Duration) *RunnerConfig
- type Starbox
- func (s *Starbox) AddBuiltin(name string, starFunc StarlarkFunc)
- func (s *Starbox) AddHTTPContext(req *http.Request) *libhttp.ServerResponse
- func (s *Starbox) AddKeyStarlarkValue(key string, value starlark.Value)
- func (s *Starbox) AddKeyValue(key string, value interface{})
- func (s *Starbox) AddKeyValues(keyValues starlet.StringAnyMap)
- func (s *Starbox) AddModuleData(moduleName string, moduleData starlark.StringDict)
- func (s *Starbox) AddModuleFunctions(name string, funcs FuncMap)
- func (s *Starbox) AddModuleLoader(moduleName string, moduleLoader starlet.ModuleLoader)
- func (s *Starbox) AddModuleScript(moduleName, moduleScript string)
- func (s *Starbox) AddNamedModules(moduleNames ...string)
- func (s *Starbox) AddStarlarkValues(keyValues starlark.StringDict)
- func (s *Starbox) AddStructData(structName string, structData starlark.StringDict)
- func (s *Starbox) AddStructFunctions(name string, funcs FuncMap)
- func (s *Starbox) AttachMemory(name string, memory *dataconv.SharedDict)
- func (s *Starbox) CreateMemory(name string) *dataconv.SharedDict
- func (s *Starbox) CreateRunConfig() *RunnerConfig
- func (s *Starbox) GetMachine() *starlet.Machine
- func (s *Starbox) GetSteps() uint64
- func (s *Starbox) REPL() error
- func (s *Starbox) Reset()
- func (s *Starbox) Run(script string) (starlet.StringAnyMap, error)
- func (s *Starbox) RunInspect(script string) (starlet.StringAnyMap, error)
- func (s *Starbox) RunInspectIf(script string, cond InspectCondFunc) (starlet.StringAnyMap, error)
- func (s *Starbox) RunTimeout(script string, timeout time.Duration) (starlet.StringAnyMap, error)
- func (s *Starbox) SetFS(hfs fs.FS)
- func (s *Starbox) SetModuleSet(modSet ModuleSetName)
- func (s *Starbox) SetPrintFunc(printFunc starlet.PrintFunc)
- func (s *Starbox) SetStructTag(tag string)
- func (s *Starbox) String() string
- type StarlarkFunc
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoStarbox is the error for RunnerConfig.Execute() when no Starbox instance is set ErrNoStarbox = errors.New("no starbox instance") )
Functions ¶
func NewMemory ¶ added in v0.0.3
func NewMemory() *dataconv.SharedDict
NewMemory creates a new shared dictionary for la mémoire collective.
Types ¶
type FuncMap ¶ added in v0.0.4
type FuncMap map[string]StarlarkFunc
FuncMap is a map of Starlark functions.
type InspectCondFunc ¶ added in v0.0.2
type InspectCondFunc func(starlet.StringAnyMap, error) bool
InspectCondFunc is a function type for inspecting the converted output of Run*() and decide whether to continue.
type ModuleSetName ¶
type ModuleSetName string
ModuleSetName defines the name of a module set.
const ( // EmptyModuleSet represents the predefined module set for empty scripts, it contains no modules. EmptyModuleSet ModuleSetName = "none" // SafeModuleSet represents the predefined module set for safe scripts, it contains only safe modules that do not have side effects with outside world. SafeModuleSet ModuleSetName = "safe" // NetworkModuleSet represents the predefined module set for network scripts, it's based on SafeModuleSet with additional network modules. NetworkModuleSet ModuleSetName = "network" // FullModuleSet represents the predefined module set for full scripts, it includes all available modules. FullModuleSet ModuleSetName = "full" )
type RunnerConfig ¶ added in v0.0.5
type RunnerConfig struct {
// contains filtered or unexported fields
}
RunnerConfig defines the execution configuration for a Starbox instance.
func NewRunConfig ¶ added in v0.0.5
func NewRunConfig() *RunnerConfig
NewRunConfig creates a new RunnerConfig instance.
func (*RunnerConfig) Context ¶ added in v0.0.5
func (c *RunnerConfig) Context(ctx context.Context) *RunnerConfig
Context sets the context for the execution.
func (*RunnerConfig) Execute ¶ added in v0.0.5
func (c *RunnerConfig) Execute() (starlet.StringAnyMap, error)
Execute executes the box with the given configuration.
func (*RunnerConfig) FileName ¶ added in v0.0.5
func (c *RunnerConfig) FileName(name string) *RunnerConfig
FileName sets the script file name for the execution.
func (*RunnerConfig) Inspect ¶ added in v0.0.5
func (c *RunnerConfig) Inspect(force bool) *RunnerConfig
Inspect sets the inspection mode for the execution. It works like InspectCond with a condition function that forces the REPL mode, by adding a condition function to force the REPL mode, regardless of the output or error. It can be overridden by InspectCond() or Inspect().
func (*RunnerConfig) InspectCond ¶ added in v0.0.5
func (c *RunnerConfig) InspectCond(cond InspectCondFunc) *RunnerConfig
InspectCond sets the inspection mode with a condition function for the execution. It can be overridden by InspectCond() or Inspect().
func (*RunnerConfig) KeyValue ¶ added in v0.0.5
func (c *RunnerConfig) KeyValue(key string, value interface{}) *RunnerConfig
KeyValue sets the key-value pair for the execution.
func (*RunnerConfig) KeyValueMap ¶ added in v0.0.5
func (c *RunnerConfig) KeyValueMap(extras starlet.StringAnyMap) *RunnerConfig
KeyValueMap merges the key-value pairs for the execution.
func (*RunnerConfig) Script ¶ added in v0.0.5
func (c *RunnerConfig) Script(content string) *RunnerConfig
Script sets the script content for the execution.
func (*RunnerConfig) Starbox ¶ added in v0.0.5
func (c *RunnerConfig) Starbox(b *Starbox) *RunnerConfig
Starbox sets the Starbox instance for the execution.
func (*RunnerConfig) String ¶ added in v0.0.5
func (c *RunnerConfig) String() string
String returns a string representation of the RunnerConfig.
func (*RunnerConfig) Timeout ¶ added in v0.0.5
func (c *RunnerConfig) Timeout(timeout time.Duration) *RunnerConfig
Timeout sets the timeout for the execution.
type Starbox ¶
type Starbox struct {
// contains filtered or unexported fields
}
Starbox is a wrapper of starlet.Machine with additional features.
func (*Starbox) AddBuiltin ¶
func (s *Starbox) AddBuiltin(name string, starFunc StarlarkFunc)
AddBuiltin adds a builtin function with name to the global environment before execution. If the name already exists, it will be overwritten. It panics if called after execution.
func (*Starbox) AddHTTPContext ¶ added in v0.0.4
func (s *Starbox) AddHTTPContext(req *http.Request) *libhttp.ServerResponse
AddHTTPContext adds HTTP request and response data wrapper to the global environment before execution. It takes an HTTP request and returns the response data wrapper for setting response headers and body. It panics if called after execution.
func (*Starbox) AddKeyStarlarkValue ¶ added in v0.0.4
AddKeyStarlarkValue adds a key-value pair to the global environment before execution, the value is a Starlark value. If the key already exists, it will be overwritten. It panics if called after execution.
func (*Starbox) AddKeyValue ¶
AddKeyValue adds a key-value pair to the global environment before execution. If the key already exists, it will be overwritten. It panics if called after execution.
func (*Starbox) AddKeyValues ¶
func (s *Starbox) AddKeyValues(keyValues starlet.StringAnyMap)
AddKeyValues adds key-value pairs to the global environment before execution. Usually for output of Run()*. For each key-value pair, if the key already exists, it will be overwritten. It panics if called after execution.
func (*Starbox) AddModuleData ¶
func (s *Starbox) AddModuleData(moduleName string, moduleData starlark.StringDict)
AddModuleData creates a module for the given module data along with a module loader, and adds it to the preload and lazyload registry. The given module data can be accessed in script via load("module_name", "key1") or module_name.key1. It panics if called after execution.
func (*Starbox) AddModuleFunctions ¶ added in v0.0.4
AddModuleFunctions adds a module with the given module functions along with a module loader, and adds it to the preload and lazyload registry. The given module function can be accessed in script via load("module_name", "func1") or module_name.func1. It works like AddModuleData() but allows only functions as values. It panics if called after execution.
func (*Starbox) AddModuleLoader ¶
func (s *Starbox) AddModuleLoader(moduleName string, moduleLoader starlet.ModuleLoader)
AddModuleLoader adds a custom module loader to the preload and lazyload registry. It will not load the module until the first run, and load result can be accessed in script via load("module_name", "key1") or key1 directly. It panics if called after execution.
func (*Starbox) AddModuleScript ¶
AddModuleScript creates a module with given module script in virtual filesystem, and adds it to the preload and lazyload registry. The given module script can be accessed in script via load("module_name", "key1") or load("module_name.star", "key1") if module name has no ".star" suffix. It panics if called after execution.
func (*Starbox) AddNamedModules ¶
AddNamedModules adds builtin modules by name to the preload and lazyload registry. It will not load the modules until the first run. It panics if called after execution.
func (*Starbox) AddStarlarkValues ¶ added in v0.0.2
func (s *Starbox) AddStarlarkValues(keyValues starlark.StringDict)
AddStarlarkValues adds key-value pairs to the global environment before execution, the values are already converted to Starlark values. For each key-value pair, if the key already exists, it will be overwritten. It panics if called after execution.
func (*Starbox) AddStructData ¶ added in v0.0.4
func (s *Starbox) AddStructData(structName string, structData starlark.StringDict)
AddStructData creates a module for the given struct data along with a module loader, and adds it to the preload and lazyload registry. The given struct data can be accessed in script via load("struct_name", "key1") or struct_name.key1. It panics if called after execution.
func (*Starbox) AddStructFunctions ¶ added in v0.0.4
AddStructFunctions adds a module with the given struct functions along with a module loader, and adds it to the preload and lazyload registry. The given struct function can be accessed in script via load("struct_name", "func1") or struct_name.func1. It works like AddStructData() but allows only functions as values. It panics if called after execution.
func (*Starbox) AttachMemory ¶ added in v0.0.3
func (s *Starbox) AttachMemory(name string, memory *dataconv.SharedDict)
AttachMemory adds a shared dictionary to the global environment before execution.
func (*Starbox) CreateMemory ¶ added in v0.0.3
func (s *Starbox) CreateMemory(name string) *dataconv.SharedDict
CreateMemory creates a new shared dictionary for la mémoire collective with the given name, and adds it to the global environment before execution.
func (*Starbox) CreateRunConfig ¶ added in v0.0.5
func (s *Starbox) CreateRunConfig() *RunnerConfig
CreateRunConfig creates a new RunnerConfig instance from a given Starbox instance.
func (*Starbox) GetMachine ¶
GetMachine returns the underlying starlet.Machine instance.
func (*Starbox) GetSteps ¶ added in v0.0.6
GetSteps returns the computation steps executed by the underlying Starlark thread.
func (*Starbox) Reset ¶
func (s *Starbox) Reset()
Reset creates an new Starlet machine and keeps the settings.
func (*Starbox) Run ¶
func (s *Starbox) Run(script string) (starlet.StringAnyMap, error)
Run executes a script and returns the converted output.
func (*Starbox) RunInspect ¶
func (s *Starbox) RunInspect(script string) (starlet.StringAnyMap, error)
RunInspect executes a script and then REPL with result and returns the converted output.
func (*Starbox) RunInspectIf ¶ added in v0.0.2
func (s *Starbox) RunInspectIf(script string, cond InspectCondFunc) (starlet.StringAnyMap, error)
RunInspectIf executes a script and then REPL with result and returns the converted output, if the condition is met. The condition function is called with the converted output and the error from Run*(), and returns true if REPL is needed.
func (*Starbox) RunTimeout ¶
RunTimeout executes a script and returns the converted output.
func (*Starbox) SetFS ¶ added in v0.0.3
SetFS sets the virtual filesystem for module scripts. If it's not nil, it'll override all the scripts added by AddModuleScript(). It panics if called after execution.
func (*Starbox) SetModuleSet ¶
func (s *Starbox) SetModuleSet(modSet ModuleSetName)
SetModuleSet sets the module set to be loaded before execution. It panics if called after execution.
func (*Starbox) SetPrintFunc ¶
SetPrintFunc sets the print function for Starlark. It panics if called after execution.
func (*Starbox) SetStructTag ¶
SetStructTag sets the custom tag of Go struct fields for Starlark. It panics if called after execution.