risor

package module
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2023 License: Apache-2.0 Imports: 7 Imported by: 8

README

Risor

CircleCI MIT license Go.Dev reference Go Report Card Releases

Risor is a fast and flexible scripting language for Go developers and DevOps.

Its modules integrate the Go standard library, making it easy to use functions that you're already familiar with as a Go developer.

Scripts are compiled to bytecode and then run on a lightweight virtual machine. Risor is written in pure Go.

Documentation

Documentation is available at risor.io.

You might also want to try evaluating Risor scripts from your browser.

Getting Started

Head over to Getting Started in the documentation.

That said, if you use Homebrew, you can install the Risor CLI as follows:

brew tap risor-io/risor
brew install risor

Having done that, just run risor to start the CLI or risor -h to see usage information.

Execute a code snippet directly using the -c option:

risor -c "time.now()"
"2023-08-19T16:15:46-04:00"

Quick Example

Here's a short example of how Risor feels like a hybrid of Go and Python, with new features like pipe expressions for transformations, and with access to portions of the Go standard library (like the strings package):

array := ["gophers", "are", "burrowing", "rodents"]

sentence := array | strings.join(" ") | strings.to_upper

print(sentence)

Output:

GOPHERS ARE BURROWING RODENTS

Built-in Functions and Modules

30+ built-in functions are included and are documented here.

Modules are included that generally wrap the equivalent Go package. For example, there is direct correspondence between base64, bytes, json, math, os, rand, strconv, strings, and time Risor modules and the Go standard library.

Risor modules that are beyond the Go standard library include aws, pgx, and uuid. Additional modules are being added regularly.

Using Risor

Risor is designed to be versatile and accommodate a variety of usage patterns. You can leverage Risor in the following ways:

  • REPL: Risor offers a Read-Evaluate-Print-Loop (REPL) that you can use to interactively write and test scripts. This is perfect for experimentation and debugging.

  • Library: Risor can be imported as a library into existing Go projects. It provides a simple API for running scripts and interacting with the results, in isolated environments for sandboxing.

  • Executable script runner: Risor scripts can also be marked as executable, providing a simple way to leverage Risor in your build scripts, automation, and other tasks.

  • API: (Coming soon) A service and API will be provided for remotely executing and managing Risor scripts. This will allow integration into various web applications, potentially with self-hosted and a managed cloud version.

Go Interface

It is trivial to embed Risor in your Go program in order to evaluate scripts that have access to arbitrary Go structs and other types.

The simplest way to use Risor is to call the Eval function and provide the Risor script source code. The result of the script is returned as a Risor object:

result, err := risor.Eval(ctx, "math.min([5, 3, 7])")
min := result.(*object.Int).Value()

Provide input to the script using Risor options:

result, err := risor.Eval(ctx, "input | strings.to_upper", risor.WithGlobal("input", "hello"))
fmt.Println(result) // HELLO

Use the same mechanism to inject a struct. You can then access fields or call methods on the struct from the Risor script:

type Example struct {
    Message string
}

ex := &Example{"abc"}

result, err := risor.Eval(ctx, "len(ex.Message)", risor.WithGlobal("ex", ex))
fmt.Println(result) // 3

Syntax Highlighting

A Risor VSCode extension is already available which currently only offers syntax highlighting.

You can also make use of the Risor TextMate grammar.

Contributing

Risor is intended to be a community project. You can lend a hand in various ways:

  • Please ask questions and share ideas in GitHub discussions
  • Share Risor on any social channels that may appreciate it
  • Open GitHub issue or a pull request for any bugs you find
  • Star the project on GitHub

Discuss the Project

Please visit the GitHub discussions page to share thoughts and questions.

Credits

Check CREDITS.md.

License

Released under the Apache License, Version 2.0.

Copyright Curtis Myzie / github.com/myzie.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call added in v0.14.0

func Call(
	ctx context.Context,
	main *compiler.Code,
	functionName string,
	args []object.Object,
	options ...Option,
) (object.Object, error)

Call evaluates the precompiled code and then calls the named function. The supplied arguments are passed in the function call. The result of the function call is returned.

func Eval

func Eval(ctx context.Context, source string, options ...Option) (object.Object, error)

Eval evaluates the given source code and returns the result.

func EvalCode added in v0.14.0

func EvalCode(ctx context.Context, main *compiler.Code, options ...Option) (object.Object, error)

EvalCode evaluates the precompiled code and returns the result.

Types

type Option

type Option func(*cfg.RisorConfig)

Option describes a function used to configure a Risor evaluation.

func WithGlobal added in v0.14.0

func WithGlobal(name string, value any) Option

WithGlobal supplies a single named global variable to the Risor evaluation.

func WithGlobals added in v0.14.0

func WithGlobals(globals map[string]any) Option

WithGlobals provides global variables that are made available to Risor evaluations. This option is additive, so multiple WithGlobals options may be supplied. If the same key is supplied multiple times, the last supplied value is used.

func WithImporter

func WithImporter(i importer.Importer) Option

WithImporter supplies an Importer that will be used to execute import statements.

func WithLocalImporter added in v0.8.0

func WithLocalImporter(path string) Option

WithLocalImporter enables importing Risor modules from the given directory.

func WithoutDefaultGlobals added in v0.14.0

func WithoutDefaultGlobals() Option

WithoutDefaultGlobals opts out of all default global builtins and modules.

Directories

Path Synopsis
Package ast defines abstract syntax tree types that represent Risor code.
Package ast defines abstract syntax tree types that represent Risor code.
Package builtins defines the default set of builtin functions for Risor.
Package builtins defines the default set of builtin functions for Risor.
cmd
risor command
risor-api command
risor-lambda command
risor-lsp command
This package implements a Risor language server.
This package implements a Risor language server.
risor-docs module
risor-modgen module
Package compiler is used to compile an Abstract Syntax Tree (AST) into its corresponding bytecode.
Package compiler is used to compile an Abstract Syntax Tree (AST) into its corresponding bytecode.
examples
go/struct command
internal
arg
cfg
tmpl
Package tmpl is used to parse Risor string templates.
Package tmpl is used to parse Risor string templates.
Package lexer contains the code to lex input programs into a stream of tokens.
Package lexer contains the code to lex input programs into a stream of tokens.
modules
all
aws
fmt
os
pgx
bcrypt module
carbon module
cli module
color module
echarts module
gha module
github module
goquery module
isatty module
jmespath module
kubernetes module
playwright module
qrcode module
redis module
sched module
semver module
shlex module
slack module
sql module
ssh module
tablewriter module
template module
vault module
yaml module
Package object defines the standard set of object types in Risor.
Package object defines the standard set of object types in Risor.
Package op defines the opcodes that are used in the Risor virtual machine.
Package op defines the opcodes that are used in the Risor virtual machine.
os
Package parser is used to parse an input program from its tokens and produce an abstract syntax tree (AST) as output.
Package parser is used to parse an input program from its tokens and produce an abstract syntax tree (AST) as output.
Package repl implements a read-eval-print-loop for Risor.
Package repl implements a read-eval-print-loop for Risor.
tests
benchmark command
Package token defines the tokens that are produced when lexing Risor code.
Package token defines the tokens that are produced when lexing Risor code.

Jump to

Keyboard shortcuts

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