Skip to content

Files

Latest commit

Jun 11, 2019
4bb6ee1 · Jun 11, 2019

History

History
49 lines (36 loc) · 1.06 KB

README.md

File metadata and controls

49 lines (36 loc) · 1.06 KB

gopcp

A simple Lisp-Style protocol for communication (golang version)

travis-ci build status

Features

  • lisp style composation

  • function sandbox. User can control every function and detail by providing the sandbox.

  • based on json grammer

Quick Example

import (
  "github.com/lock-free/gopcp"
  "errors"
)

// create sandbox
sandBox := gopcp.GetSandbox(map[string]*gopcp.BoxFunc{
	"add": gopcp.ToSandboxFun(func(args []interface{}, pcpServer *gopcpc.PcpServer) (interface{}, error) {
		var res float64
		for _, arg := range args {
			if val, ok := arg.(float64); !ok {
				return nil, errors.New("args should be int")
			} else {
				res += val
			}
		}
		return res, nil
	}),
})

pcpServer := NewPcpServer(sandbox)

// we can just use json array string
// output: 3
r1, e1 := pcpServer.Execute("[\"add\", 1, 2]")

// we can also use PcpClient, instead of raw string
var pcpClient = PcpClient{}
callText, jerr := pcpClient.ToJSON(pcpClient.Call("add", 1, 2))

r2, e2 := pcpServer.Execute(callText)