tgx

package module
v0.0.0-...-3c0e183 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2022 License: MIT Imports: 9 Imported by: 0

README

tengo-x

Tengo is a small, dynamic, fast, secure script language for Go.

This package is intended to extend Tengo to make golang functions and struct instances as Tengo built-in functions and objects very easily, so calling golang functions or objects from Tengo is very simple.

Usage

The package is fully go-getable, So, just type

go get github.com/rosbit/tengo-x

to install.

package main

import "fmt"
import "github.com/rosbit/tengo-x"

func main() {
  ctx := tgx.NewTengo()

  res, _ := ctx.Eval("1 + 2", nil)
  fmt.Println("result is:", res)
}
Tengo calls Go function and objects

Tengo calling Go function is also easy. In the Go code, make a golang function. Just put varibles, functions, struct instances in an enviroment map when loading Tengo script.

package main

import "github.com/rosbit/tengo-x"
import "fmt"

type M struct {
   Name string
   Age int
}
func (m *M) IncAge(a int) {
   m.Age += a
}

func adder(a1 float64, a2 float64) float64 {
    return a1 + a2
}

func main() {
  vars := map[string]interface{}{
     "m": &M{Name:"rosbit", Age:1}, // to Tengo object
     "adder": adder,                // to Tengo built-in function
     "a": []int{1,2,3}              // to Tengo array
  }

  ctx := js.NewTengo()
  if err := ctx.LoadFile("a.tengo", vars); err != nil {
     fmt.Printf("%v\n", err)
     return
  }

  res, err := ctx.GetGlobals("r") // get the value of var named "r"
  if err != nil {
     fmt.Printf("%v\n", err)
     return
  }
  fmt.Printf("res:", res)
}

In Tengo code, one can call the registered name directly. There's the example a.tengo.

fmt := import("fmt")
r := adder(1, 100)   // the function "adder" is implemented in Go
fmt.println(r)
fmt.println(m.name, " ", m.age) // m is an object
Status

The package is not fully tested, so be careful.

Contribution

Pull requests are welcome! Also, if you want to discuss something send a pull request with proposal and changes. Convention: fork the repository and make changes on your fork in a feature branch.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TengoX

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

func NewTengo

func NewTengo() *TengoX

func (*TengoX) Eval

func (tgw *TengoX) Eval(script string, params map[string]interface{}) (res interface{}, err error)

func (*TengoX) EvalFile

func (tgw *TengoX) EvalFile(path string, params map[string]interface{}) (res interface{}, err error)

func (*TengoX) GetGlobal

func (tgw *TengoX) GetGlobal(name string) (res interface{}, err error)

func (*TengoX) LoadFile

func (tgw *TengoX) LoadFile(path string, vars map[string]interface{}) (err error)

func (*TengoX) LoadScript

func (tgw *TengoX) LoadScript(script string, vars map[string]interface{}) (err error)

func (*TengoX) Run

func (tgw *TengoX) Run(vars map[string]interface{}) (err error)

Jump to

Keyboard shortcuts

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