/golpal

Golpal - Easy to use Golang Eval Library

Primary LanguageGoMIT LicenseMIT

This project is archived. Please use https://github.com/novalagung/go-eek for faster and safer evaluation library.

Golpal logo

Golpal

Easy to use Golang Eval Library

Introduction

Golpal is simple library to allow developer to do eval operation on golang source codes. Technically Golang doesn't provide API to do some eval, so we use temporary file to achieve that.

Build Status

Installation

Stable version: v1.0.0

Just go get the lib is enough

go get -u github.com/novalagung/golpal

Run test

cd $GOPATH/src/github.com/novalagung/golpal
go test *.go -v

CLI Installation

By using cli, golpal will run faster. Follow these instructions to install golpal CLI.

Linux

sh install_cli.sh
golpal -content "3 + 4"

Windows

install_cli.bat
golpal.exe -content "3 + 4"

Example

Simple Example

package main

import "github.com/novalagung/golpal"
import "fmt"

func main() {
	cmdString := `3 + 2`
	output, err := golpal.New().ExecuteSimple(cmdString)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("result", "=>", output)
}

For one line statement using ExecuteSimple(), return keyword is optional

Another Example

cmdString := `
	number := 3
	if number == 2 {
		return "wrong"
	} else {
		return "right"
	}
`

output, err := golpal.New().ExecuteSimple(cmdString)
if err != nil {
	fmt.Println(err)
}
fmt.Println("result", "=>", output)

For multiline statement using ExecuteSimple(), return must be defined

Example which use strings and runtime

cmdString := `
	osName := runtime.GOOS
	arr := []string{"my", "operation system", "is", osName}
	return strings.Join(arr, ", ")
`

output, err := golpal.New().AddLibs("strings", "runtime").ExecuteSimple(cmdString)
if err != nil {
	fmt.Println(err)
}
fmt.Println("result", "=>", output)

Example Not Simple (again, NOT SIMPLE, by using Execute() func)

cmdString := `
	func calculate(values ...int) int {
		total := 0
		for _, each := range values {
			total = total + each
		}
		return total
	}

	func main() {
		res := calculate(1, 2, 3, 4, 2, 3, 1)
		fmt.Printf("total : %d", res)
	}
`

output, err := golpal.New().Execute(cmdString)
if err != nil {
	fmt.Println(err)
}
fmt.Println("result", "=>", output)

Example Execute Raw (use ExecuteRaw() func)

cmdString := `
	package main

	import "fmt"

	func main() {
		fmt.Print("hello")
	}
`

output, err := golpal.New().ExecuteRaw(cmdString)
if err != nil {
	fmt.Println(err)
}
fmt.Println("result", "=>", output)

CLI Example

func prepareCmd(cmdString string, args ...string) (*exec.Cmd, *bytes.Buffer, *bytes.Buffer) {
	var stdout bytes.Buffer
	var stderr bytes.Buffer

	if runtime.GOOS == "windows" {
		cmdString = fmt.Sprintf(".exe", cmdString)
	}

	cmd := exec.Command(cmdString, args...)
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr

	return cmd, &stdout, &stderr
}

func main() {
	cmd, stdout, stderr := prepareCmd("golpal", "-content", "3 + 1")
	if err := cmd.Run(); err != nil {
		fmt.Println(err.Error())
		return
	}

	if stderr.Len() > 0 {
		fmt.Println(stderr.String())
		return
	}

	fmt.Println("result", "=>", strings.TrimSpace(stdout.String()))
}

More Example

For more examples please take a look at the golpal_test.go and cli_test.go.

API Reference

Func of golpal

There are only one func available, golpal.New() which return object that type is *golpal.Golpal

Func Usage
golpal.New() instantiate new *golpal.Golpal object

Properties of *golpal.Golpal

Property Type Usage
.AutoDeleteTemporaryPath bool Determine if temporary path will be deleted or not after executing the source codes (default is true)
.TemporaryPath string Path of temporary folder used to store all *.go temp files (default is .temp for *nix / *drwin, and temp for w*ndows)

Methods of *golpal.Golpal

Method Usage
.AddLibs(libs ...string) Add other libraries, by default only fmt is included
.ExecuteSimple(cmdString string) Run golang source codes. The code will be placed inside virtual main() func. This function doesn't allow fmt.Print*(). Also for multiline statement, return must be defined
.Execute(cmdString string) Run golang source codes which contains main() func
.ExecuteRaw(cmdString string) Run complete golang source code
.DeleteTemporaryPath() Force delete temporary path which used to do the exec process

CLI Commands

Golpal CLI (golpal / golpal.exe) has several arguments

Args Usage Example
-content Eval from string -content="3 + 4"
-file Eval from file -file="/path/to/file"
-mode Golpal mode (simple, normal, raw). Default is simple -mode="raw"
-libs Include libs -libs="io/ioutil, bytes, strings"

Contributing

Feel free to contribute

fork -> commit -> push -> pull request

License

MIT License

Author

Noval Agung Prayogo - http://novalagung.com/