star

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2020 License: MIT Imports: 6 Imported by: 0

README

Star

Star uses starlark to provide a python-like environment to run Go code. This is a quick and dirty experiment and should not be taken seriously.

The intro blog post is likely the best place to get started. Or you can play around in the repl: https://embly.github.com/star

How to use

Head to the releases section to download a binary or if you have Go installed just run:

go get github.com/embly/star/cmd/star

Star provides a python-like environment to run Go packages. A small subset of the standard library is currently supported. You can see the supported packages here: https://github.com/embly/star/blob/master/src/packages.go

Some example scripts:

Use Go's concurrency model to fetch some urls in parallel:

http = require("net/http")
ioutil = require("io/ioutil")
sync = require("sync")
star = require("star")
time = require("time")

def get_url(url, wg):
    resp, err = http.Get(url)
    if err:
        return print(err)
    b, err = ioutil.ReadAll(resp.Body)
    if err:
        return print(err)
    body, err = star.bytes_to_string(b)
    if err:
        return print(err)
    time.Sleep(time.Second * 2)
    wg.Done()


def main():
    wg = sync.WaitGroup()
    wg.Add(3)
    urls = [
        "https://www.embly.run/hello/",
        "https://www.embly.run/hello/",
        "https://www.embly.run/hello/",
    ]
    for url in urls:
        star.go(get_url, url, wg)
    wg.Wait()

Run a web server:

http = require("net/http")


def hello(w, req):
    w.WriteHeader(201)
    w.Write("hello world\n")


http.HandleFunc("/hello", hello)

http.ListenAndServe(":8080", http.Handler)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Star = map[string]starlark.Value{
	"bytes_to_string": starlark.NewBuiltin("bytes_to_string", func(thread *starlark.Thread,
		fn *starlark.Builtin, args starlark.Tuple,
		kwargs []starlark.Tuple) (v starlark.Value, err error) {
		out := []starlark.Value{starlark.None, Error{}}
		if args.Len() != 1 || args.Index(0).Type() != "[]byte" {
			out[1] = Error{err: errors.New("bytes_to_string takes one argument of type []byte")}
		} else {
			out[0] = starlark.String(string(args.Index(0).(ByteArray).b))
		}
		v = starlark.Tuple(out)
		return
	}),
	"chan": starlark.NewBuiltin("chan", func(thread *starlark.Thread,
		fn *starlark.Builtin, args starlark.Tuple,
		kwargs []starlark.Tuple) (v starlark.Value, err error) {
		out := []starlark.Value{starlark.None, Error{}}
		if args.Len() != 1 || args.Index(0).Type() != "[]byte" {
			out[1] = Error{err: errors.New("bytes_to_string takes one argument of type []byte")}
		} else {
			out[0] = starlark.String(string(args.Index(0).(ByteArray).b))
		}
		v = starlark.Tuple(out)
		return
	}),
	"go": starlark.NewBuiltin("go", func(thread *starlark.Thread,
		fn *starlark.Builtin, args starlark.Tuple,
		kwargs []starlark.Tuple) (v starlark.Value, err error) {

		go func() {
			argsToPass := []starlark.Value(args)[1:]
			thread := &starlark.Thread{Name: ""}
			_, err := starlark.Call(thread, args.Index(0), starlark.Tuple(argsToPass), nil)
			if err != nil {
				panic(err)
			}
		}()
		v = starlark.None
		return
	}),
}

Functions

func AddPackages

func AddPackages(packages map[string]map[string]starlark.Value)

func Require

func Require(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (v starlark.Value, err error)

func RunScript

func RunScript(file string) (err error)

func ValidateArgs

func ValidateArgs(types []starlark.Value, args starlark.Tuple) (values []interface{}, err error)

ValidateArgs

Types

type ByteArray

type ByteArray struct {
	// contains filtered or unexported fields
}

func (ByteArray) Freeze

func (ba ByteArray) Freeze()

func (ByteArray) Hash

func (ba ByteArray) Hash() (uint32, error)

func (ByteArray) Iterate

func (ba ByteArray) Iterate() starlark.Iterator

func (ByteArray) Len

func (ba ByteArray) Len() int

func (ByteArray) String

func (ba ByteArray) String() string

func (ByteArray) Truth

func (ba ByteArray) Truth() starlark.Bool

func (ByteArray) Type

func (ba ByteArray) Type() string

type Channel

type Channel struct {
	// contains filtered or unexported fields
}

func (Channel) Attr

func (c Channel) Attr(name string) (v starlark.Value, err error)

func (Channel) AttrNames

func (c Channel) AttrNames() []string

func (Channel) Freeze

func (c Channel) Freeze()

func (Channel) Hash

func (c Channel) Hash() (uint32, error)

func (Channel) String

func (c Channel) String() string

func (Channel) Truth

func (c Channel) Truth() starlark.Bool

func (Channel) Type

func (c Channel) Type() string

type Error

type Error struct {
	// contains filtered or unexported fields
}

func (Error) Attr

func (e Error) Attr(name string) (v starlark.Value, err error)

func (Error) AttrNames

func (err Error) AttrNames() []string

func (Error) Freeze

func (err Error) Freeze()

func (Error) Hash

func (err Error) Hash() (uint32, error)

func (Error) String

func (err Error) String() string

func (Error) Truth

func (err Error) Truth() starlark.Bool

func (Error) Type

func (err Error) Type() string

type Function

type Function struct {
	FunctionName string
	Args         []starlark.Value
	Returns      []starlark.Value
	Call         func([]interface{}) []interface{}
}

func (Function) Builtin

func (f Function) Builtin() *starlark.Builtin

func (Function) CallInternal

func (f Function) CallInternal(thread *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (v starlark.Value, err error)

func (Function) Freeze

func (f Function) Freeze()

func (Function) Hash

func (f Function) Hash() (uint32, error)

func (Function) Name

func (f Function) Name() string

func (Function) String

func (f Function) String() string

func (Function) Truth

func (f Function) Truth() starlark.Bool

func (Function) Type

func (f Function) Type() string

type Interface

type Interface struct {
	Name    string
	Methods map[string]Method
	Value   interface{}
}

func (Interface) Attr

func (i Interface) Attr(name string) (v starlark.Value, err error)

func (Interface) AttrNames

func (i Interface) AttrNames() []string

func (Interface) Freeze

func (i Interface) Freeze()

func (Interface) Hash

func (i Interface) Hash() (uint32, error)

func (Interface) String

func (i Interface) String() string

func (Interface) Truth

func (i Interface) Truth() starlark.Bool

func (Interface) Type

func (i Interface) Type() string

type Method

type Method struct {
	Args    []starlark.Value
	Returns []starlark.Value
	Call    MethodCall
}

type MethodCall

type MethodCall func(interface{}, []interface{}) []interface{}

type Struct

type Struct struct {
	TypeName   string
	Value      interface{}
	Initialize func() interface{}
	Print      func(interface{}) string
	Methods    map[string]Method
	Attributes map[string]starlark.Value
}

func (Struct) Attr

func (s Struct) Attr(name string) (v starlark.Value, err error)

func (Struct) AttrNames

func (s Struct) AttrNames() []string

func (Struct) CallInternal

func (s Struct) CallInternal(thread *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

func (Struct) Freeze

func (s Struct) Freeze()

func (Struct) Hash

func (s Struct) Hash() (uint32, error)

func (Struct) Name

func (s Struct) Name() string

func (Struct) String

func (s Struct) String() string

func (Struct) Truth

func (s Struct) Truth() starlark.Bool

func (Struct) Type

func (s Struct) Type() string

type ValueSetter

type ValueSetter interface {
	SetValue(v interface{})
	GetValue() (v interface{})
}

Directories

Path Synopsis
cmd
pkg
src
io
web-repl
src

Jump to

Keyboard shortcuts

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