eek

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2019 License: MIT Imports: 12 Imported by: 3

README

go-eek

Blazingly fast and safe go evaluation library, created on top of Go pkg/plugin package.

On go-eek, the eval expression is encapsulated into a single function, and stored in a go file. The go file later on will be build into a plugin file (*.so file). And then next, for every evaluation call, it will happen in the plugin file. This is why go-eek is insanely fast.

go-eek accept standar Go syntax expression.

Example

Simple Example
import . "github.com/novalagung/go-eek"

// create new eek object and name it
obj := NewEek()
obj.SetName("simple operation")

// define variables (and default value of particular variable if available)
obj.DefineVariable(Var{Name: "VarA", Type: "int"})
obj.DefineVariable(Var{Name: "VarB", Type: "float64", DefaultValue: 10.5})

// specify the evaluation expression in go standard syntax
obj.PrepareEvaluation(`
    VarACasted := float64(VarA)
    VarC := VarACasted + VarB
    return VarC
`)

// build only need to happen once
err := obj.Build()
if err != nil {
    log.Fatal(err)
}

// evaluate!
output1, _ := obj.Evaluate(ExecVar{ "A": 9 })
fmt.Println("with A = 9, the result will be", output1)
output2, _ := obj.Evaluate(ExecVar{ "A": 12, "B": 12.4 })
fmt.Println("with A = 12 and B = 12.4, the result will be", output2)
More Complex Example
obj := eek.New("evaluation with 3rd party library")

obj.ImportPackage("fmt")
obj.ImportPackage("github.com/novalagung/gubrak")

obj.DefineVariable(eek.Var{Name: "MessageWin", Type: "string", DefaultValue: "Congrats! You win the lottery!"})
obj.DefineVariable(eek.Var{Name: "MessageLose", Type: "string", DefaultValue: "You lose"})
obj.DefineVariable(eek.Var{Name: "YourLotteryCode", Type: "int"})
obj.DefineVariable(eek.Var{Name: "RepeatUntil", Type: "int", DefaultValue: 5})

obj.PrepareEvaluation(`
    generateRandomNumber := func() int {
        return gubrak.RandomInt(0, 10)
    }

    i := 0
    for i < RepeatUntil {
        if generateRandomNumber() == YourLotteryCode {
            return fmt.Sprintf("%s after %d tried", MessageWin, i + 1)
        }

        i++
    }
    
    return MessageLose
`)

err := obj.Build()
if err != nil {
    log.Fatal(err)
}

output, _ = obj.Evaluate(eek.ExecVar{
    "YourLotteryCode": 3,
    "RepeatUntil":     10,
})
fmt.Println("output:", output)
Arithmethic expression Example
obj := New("aritmethic expressions")
obj.DefineVariable(eek.Var{Name: "N", Type: "int"})
obj.DefineFunction(eek.Func{
    Name: "IF",
    BodyFunction: `
        func(cond bool, ok, nok string) string {
            if cond {
                return ok
            } else {
                return nok
            }
        }
    `,
})
obj.DefineFunction(eek.Func{
    Name: "OR",
    BodyFunction: `
        func(cond1, cond2 bool) bool {
            return cond1 || cond2
        }
    `,
})
obj.DefineFunction(eek.Func{
    Name:         "NOT",
    BodyFunction: `func(cond bool) bool { return !cond }`,
})
obj.PrepareEvaluation(`
    result := IF(N>20,IF(OR(N>40,N==40),IF(N>60,IF(NOT(N>80),"good",IF(N==90,"perfect","terrific")),"ok"),"ok, but still bad"),"bad")
    
    return result
`)

err := obj.Build()
if err != nil {
    log.Fatal(err)
}

output, _ := obj.Evaluate(eek.ExecVar{"N": 76})
fmt.Println(output)

More example available on the *_test.go file.

Documentation

Godoc documentation

Author

Noval Agung Prayog

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Eek

type Eek struct {
	UseCachedBuildForSameFormula bool
	// contains filtered or unexported fields
}

Eek is main type used on the evaluation

func New

func New(args ...string) *Eek

New used to create eek object. This function accept an optional variable that will be used as the evaluation name

func (*Eek) Build

func (e *Eek) Build() error

Build build the evaluation

func (*Eek) DefineFunction

func (e *Eek) DefineFunction(fun Func)

DefineFunction used to define reusable functions that will be used in the operation

func (*Eek) DefineVariable

func (e *Eek) DefineVariable(variable Var)

DefineVariable used to define variables that will be used in the operation

func (*Eek) Evaluate

func (e *Eek) Evaluate(data ExecVar) (interface{}, error)

Evaluate execute using particular data

func (*Eek) ImportPackage

func (e *Eek) ImportPackage(dependencies ...string)

ImportPackage specify which packages will be imported

func (*Eek) PrepareEvaluation

func (e *Eek) PrepareEvaluation(operation string)

PrepareEvaluation prepare the layout of evaluation string

func (*Eek) SetBaseBuildPath

func (e *Eek) SetBaseBuildPath(baseBuildPath string)

SetBaseBuildPath set the base build path. Every so file generated from build will be stored into <baseBuildPath>/<name>/<name>.so

func (*Eek) SetName

func (e *Eek) SetName(name string)

SetName set the evaluation name

type ExecVar

type ExecVar map[string]interface{}

ExecVar is used on defining value in the evaluation

type Func

type Func struct {
	Name         string
	BodyFunction string
}

Func is reflect to a single typed reusable function

type Var

type Var struct {
	Name         string
	Type         string
	DefaultValue interface{}
}

Var is reflect to a single typed variable with/without a default value

Jump to

Keyboard shortcuts

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