starbox

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2024 License: MIT Imports: 19 Imported by: 4

README ΒΆ

πŸ₯‘ Starbox - Unboxing the Potential of Starlark

godoc codecov codacy codeclimate go report

Starbox is a pragmatic Go wrapper around the Starlark in Go project, making it easier to execute Starlark scripts, exchange data between Go and Starlark, and call functions across the Go-Starlark boundary. With a focus on simplicity and usability, Starbox aims to provide an enhanced experience for developers integrating Starlark scripting into their Go applications.

πŸš€ Key Features

A host of powerful features are provided to supercharge your Starlark scripting experience:

  • Streamlined Script Execution: Simplifies setting up and running Starlark scripts, offering a seamless interface for both script execution and interactive REPL sessions.
  • Efficient Data Interchange: Enables robust and smooth data exchange between Go and Starlark, enhancing the interoperability and simplifying the integration process.
  • Versatile Module Management: Extends Starlark's capabilities with a suite of built-in functions and the ability to load custom and override existing modules, covering functionalities from data processing to HTTP handling and file manipulation.
  • Cross-Language Function Calls: Leverage the power of both languages by calling Go functions from Starlark and vice versa, creating powerful integrations.
  • Integrated HTTP Context: Facilitates handling HTTP requests and responses within Starlark scripts, catering to web application development and server-side scripting.
  • Collective Memory Sharing: Introduces a shared memory concept, enabling data sharing across different script executions and instances, fostering a more connected and dynamic scripting environment.
  • Advanced Scripting Tools: Utilize features like REPL for interactive exploration and debugging, along with script caching for improved performance.

πŸ“¦ Installation

To include starbox in your Go project, use the following command:

go get github.com/1set/starbox

βš™οΈ Usage

Here's a quick example of how you can use Starbox:

import "github.com/1set/starbox"

// Define your box with global variables and modules
box := starbox.New("quick")
box.AddKeyValue("greet", func(name string) string {
    return fmt.Sprintf("Hello, %s!", name)
})
box.AddNamedModules("random")

// Run a Starlark script
script := starbox.HereDoc(`
    target = random.choice(["World", "Starlark", "Starbox"])
    text = greet(target)
    print("Starlark:", text)
    print(__modules__)
`)
res, err := box.Run(script)

// Check for errors and results
if err != nil {
    fmt.Println("Error executing script:", err)
    return
}
fmt.Println("Go:", res["text"].(string))

This may output:

[⭐|quick](15:50:27.677) Starlark: Hello, Starbox!
[⭐|quick](15:50:27.677) ["random"]
Go: Hello, Starbox!

πŸ‘₯ Contributing

We welcome contributions to the Starbox project. If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request. Before undertaking any significant changes, please let us know by filing an issue or claiming an existing one to ensure there is no duplication of effort.

πŸ“œ License

Starbox is licensed under the MIT License.

πŸ™Œ Credits

This project is inspired by and builds upon several open-source projects:

  • Starlark in Go: The official Starlark interpreter in Go, created by Google.
  • Starlight: A well-known Go wrapper and data conversion tool between Go and Starlark.
  • Starlight Enhanced: A sophisticated fork of the original Starlight, with bug fixes and enhancement features.
  • Starlib: A collection of third-party libraries for Starlark.
  • Starlet: A Go wrapper that simplifies usage, offers data conversion, libraries and extensions for Starlark.

We thank the authors and contributors of these projects for their excellent works πŸŽ‰

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:

  1. Preloaded Starlet modules from predefined sets and additional Starlet modules by name.
  2. Custom modules added by users, preloaded Starlet modules with the same names would not be overwritten.
  3. Dynamically loaded modules based on their names just before execution.
  4. If a module name is not found in any of the built-in, custom, or dynamic modules, an error is returned.

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

View Source
var (
	// HereDoc returns unindented string as here-document.
	HereDoc = here.Doc
	// HereDocf returns formatted unindented string as here-document.
	HereDocf = here.Docf
)
View Source
var (
	// ErrModuleNotFound is the error for module cannot be found by name.
	ErrModuleNotFound = errors.New("module not found")
)
View Source
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.

func SetLog ΒΆ

func SetLog(l *zap.SugaredLogger)

SetLog sets the logger from outside the package.

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 FuncMap ΒΆ

type FuncMap map[string]StarlarkFunc

FuncMap is a map of Starlark functions.

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 New ΒΆ

func New(name string) *Starbox

New creates a new Starbox instance with default settings.

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 ΒΆ

func (s *Starbox) AddKeyStarlarkValue(key string, value starlark.Value)

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 ΒΆ

func (s *Starbox) AddKeyValue(key string, value interface{})

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 ΒΆ

func (s *Starbox) AddModuleFunctions(name string, funcs FuncMap)

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 ΒΆ

func (s *Starbox) AddModuleScript(moduleName, moduleScript string)

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

func (s *Starbox) AddModulesByName(moduleNames ...string)

AddModulesByName is an alias of AddNamedModules().

func (*Starbox) AddNamedModules ΒΆ

func (s *Starbox) AddNamedModules(moduleNames ...string)

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 ΒΆ

func (s *Starbox) AddStructFunctions(name string, funcs FuncMap)

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

func (s *Starbox) CallStarlarkFunc(name string, args ...interface{}) (interface{}, error)

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 ΒΆ

func (s *Starbox) GetMachine() *starlet.Machine

GetMachine returns the underlying starlet.Machine instance.

func (*Starbox) GetModuleNames ΒΆ

func (s *Starbox) GetModuleNames() []string

GetModuleNames returns the names of the modules loaded after execution.

func (*Starbox) GetSteps ΒΆ

func (s *Starbox) GetSteps() uint64

GetSteps returns the computation steps executed by the underlying Starlark thread.

func (*Starbox) REPL ΒΆ

func (s *Starbox) REPL() error

REPL starts a REPL session.

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 ΒΆ

func (s *Starbox) RunTimeout(script string, timeout time.Duration) (starlet.StringAnyMap, error)

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 ΒΆ

func (s *Starbox) SetFS(hfs fs.FS)

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 ΒΆ

func (s *Starbox) SetPrintFunc(printFunc starlet.PrintFunc)

SetPrintFunc sets the print function for Starlark. It panics if called after execution.

func (*Starbox) SetScriptCache ΒΆ added in v0.1.2

func (s *Starbox) SetScriptCache(cache starlet.ByteCache)

SetScriptCache sets custom cache provider for script content. nil cache provider will disable script cache. It panics if called after execution.

func (*Starbox) SetStructTag ΒΆ

func (s *Starbox) SetStructTag(tag string)

SetStructTag sets the custom tag of Go struct fields for Starlark. It panics if called after execution.

func (*Starbox) String ΒΆ

func (s *Starbox) String() string

String returns the name of the Starbox instance.

type StarlarkFunc ΒΆ

type StarlarkFunc func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

StarlarkFunc is a function that can be called from Starlark.

Jump to

Keyboard shortcuts

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