Documentation
ΒΆ
Overview ΒΆ
Package starbox provides a comprehensive set of utilities for building and managing Starlark virtual machines with ease.
Module Sources ΒΆ
Starbox supports loading modules from various sources, including built-in modules from Starlet, custom modules added by the user, and dynamic modules resolved by name on demand.
Built-in Modules:
Use SetModuleSet(modSet ModuleSetName) to select a predefined set of modules from Starlet to preload before execution. Available sets include:
- EmptyModuleSet: No modules.
- SafeModuleSet: Safe modules without access to the file system or network.
- NetworkModuleSet: Safe modules plus network modules.
- FullModuleSet: All available modules.
Custom Modules:
- AddModuleLoader(moduleName string, moduleLoader starlet.ModuleLoader): Adds a custom module loader. Members can be accessed in the script via load("module_name", "member_name") or member_name.
- AddModuleFunctions(name string, funcs FuncMap): Adds a module of custom functions. Functions can be accessed in the script via load("module_name", "func_name") or module_name.func_name.
- AddModuleData(moduleName string, moduleData starlark.StringDict): Adds a module of custom data. Data can be accessed in the script via load("module_name", "key") or module_name.key.
- AddStructFunctions(name string, funcs FuncMap): Adds a struct of custom functions. Functions can be accessed in the script via load("struct_name", "func_name") or struct_name.func_name.
- AddStructData(structName string, structData starlark.StringDict): Adds a struct of custom data. Data can be accessed in the script via load("struct_name", "key") or struct_name.key.
Dynamic Modules:
- SetDynamicModuleLoader(loader DynamicModuleLoader): Sets a dynamic module loader function, which returns module loaders based on their names before execution. These module names should be defined using AddNamedModules or AddModulesByName.
Module Loading Priority ΒΆ
Modules are loaded in the following order of priority before execution:
- Preloaded Starlet modules from predefined sets and additional Starlet modules by name.
- Custom modules added by users, preloaded Starlet modules with the same names would not be overwritten.
- Dynamically loaded modules based on their names just before execution.
- If a module name is not found in any of the built-in, custom, or dynamic modules, an error is returned.
Index ΒΆ
- Variables
- func NewMemory() *dataconv.SharedDict
- func SetLog(l *zap.SugaredLogger)
- type DoNotCompare
- type DynamicModuleLoader
- type FuncMap
- type InspectCondFunc
- type ModuleSetName
- type RunnerConfig
- func (c *RunnerConfig) Clone() *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) AddModulesByName(moduleNames ...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) CallStarlarkFunc(name string, args ...interface{}) (interface{}, error)
- func (s *Starbox) CreateMemory(name string) *dataconv.SharedDict
- func (s *Starbox) CreateRunConfig() *RunnerConfig
- func (s *Starbox) GetMachine() *starlet.Machine
- func (s *Starbox) GetModuleNames() []string
- 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) RunFile(file 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) SetDynamicModuleLoader(loader DynamicModuleLoader)
- func (s *Starbox) SetFS(hfs fs.FS)
- func (s *Starbox) SetLogger(sl *zap.SugaredLogger)
- func (s *Starbox) SetModuleSet(modSet ModuleSetName)
- func (s *Starbox) SetPrintFunc(printFunc starlet.PrintFunc)
- func (s *Starbox) SetScriptCache(cache starlet.ByteCache)
- func (s *Starbox) SetStructTag(tag string)
- func (s *Starbox) String() string
- type StarlarkFunc
Constants ΒΆ
This section is empty.
Variables ΒΆ
var ( // HereDoc returns unindented string as here-document. HereDoc = here.Doc // HereDocf returns formatted unindented string as here-document. HereDocf = here.Docf )
var ( // ErrModuleNotFound is the error for module cannot be found by name. ErrModuleNotFound = errors.New("module not found") )
var ( // ErrNoStarbox is the error for RunnerConfig.Execute() when no Starbox instance is set ErrNoStarbox = errors.New("no starbox instance") )
Functions ΒΆ
func NewMemory ΒΆ
func NewMemory() *dataconv.SharedDict
NewMemory creates a new shared dictionary for la mΓ©moire collective.
Types ΒΆ
type DoNotCompare ΒΆ added in v0.1.2
type DoNotCompare [0]func()
DoNotCompare prevents == and != comparisons on the containing struct.
type DynamicModuleLoader ΒΆ added in v0.1.2
type DynamicModuleLoader func(string) (starlet.ModuleLoader, error)
DynamicModuleLoader is a function type that takes a module name as input and returns a corresponding module loader. It is invoked before execution to dynamically load modules as needed, and serves as a complement to Starlet's built-in modules and custom-added modules. For given module names, if the module is not a built-in module or a custom-added module, this function is called to look it up. If the module is not found or fails to initialize, an error is returned. For non-existent modules, it should return (nil, nil) or (nil, error).
type InspectCondFunc ΒΆ
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 ΒΆ
type RunnerConfig struct {
// contains filtered or unexported fields
}
RunnerConfig defines the execution configuration for a Starbox instance.
func NewRunConfig ΒΆ
func NewRunConfig() *RunnerConfig
NewRunConfig creates a new RunnerConfig instance.
func (*RunnerConfig) Clone ΒΆ added in v0.1.1
func (c *RunnerConfig) Clone() *RunnerConfig
Clone creates a new RunnerConfig instance from the current one.
func (*RunnerConfig) Context ΒΆ
func (c *RunnerConfig) Context(ctx context.Context) *RunnerConfig
Context sets the context for the execution.
func (*RunnerConfig) Execute ΒΆ
func (c *RunnerConfig) Execute() (starlet.StringAnyMap, error)
Execute executes the box with the given configuration.
func (*RunnerConfig) FileName ΒΆ
func (c *RunnerConfig) FileName(name string) *RunnerConfig
FileName sets the script file name for the execution.
func (*RunnerConfig) Inspect ΒΆ
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 ΒΆ
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 ΒΆ
func (c *RunnerConfig) KeyValue(key string, value interface{}) *RunnerConfig
KeyValue sets the key-value pair for the execution.
func (*RunnerConfig) KeyValueMap ΒΆ
func (c *RunnerConfig) KeyValueMap(extras starlet.StringAnyMap) *RunnerConfig
KeyValueMap merges the key-value pairs for the execution.
func (*RunnerConfig) Script ΒΆ
func (c *RunnerConfig) Script(content string) *RunnerConfig
Script sets the script content for the execution.
func (*RunnerConfig) Starbox ΒΆ
func (c *RunnerConfig) Starbox(b *Starbox) *RunnerConfig
Starbox sets the Starbox instance for the execution.
func (*RunnerConfig) String ΒΆ
func (c *RunnerConfig) String() string
String returns a string representation of the RunnerConfig.
func (*RunnerConfig) Timeout ΒΆ
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 ΒΆ
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 ΒΆ
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 ΒΆ
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. All the module scripts added by this method would be overridden by SetFS() if it's not nil. It panics if called after execution.
func (*Starbox) AddModulesByName ΒΆ added in v0.1.2
AddModulesByName is an alias of AddNamedModules().
func (*Starbox) AddNamedModules ΒΆ
AddNamedModules adds builtin and custom 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 ΒΆ
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 ΒΆ
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 ΒΆ
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 ΒΆ
func (s *Starbox) AttachMemory(name string, memory *dataconv.SharedDict)
AttachMemory adds a shared dictionary to the global environment before execution.
func (*Starbox) CallStarlarkFunc ΒΆ added in v0.1.1
CallStarlarkFunc executes a function defined in Starlark with arguments and returns the converted output.
func (*Starbox) CreateMemory ΒΆ
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 ΒΆ
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) GetModuleNames ΒΆ
GetModuleNames returns the names of the modules loaded after execution.
func (*Starbox) GetSteps ΒΆ
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) RunFile ΒΆ added in v0.1.2
func (s *Starbox) RunFile(file string) (starlet.StringAnyMap, error)
RunFile executes a script file 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 ΒΆ
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) SetDynamicModuleLoader ΒΆ added in v0.1.2
func (s *Starbox) SetDynamicModuleLoader(loader DynamicModuleLoader)
SetDynamicModuleLoader sets the dynamic module loader for preload and lazyload modules. It panics if called after execution.
func (*Starbox) SetFS ΒΆ
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) SetLogger ΒΆ added in v0.1.2
func (s *Starbox) SetLogger(sl *zap.SugaredLogger)
SetLogger sets the logger for user-defined log output.
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) SetScriptCache ΒΆ added in v0.1.2
SetScriptCache sets custom cache provider for script content. nil cache provider will disable script cache. 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.