gojajs

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2017 License: BSD-3-Clause Imports: 10 Imported by: 0

README

goja function

goja() creates a JavaScript VM that receives and sends data through the defined javascript function for processing. The parameter passed to the function has been converted from a go map[string]interface{} to a JS object of the following form:

{
    "ns":"message.namespace",
    "ts":12345, // time represented in milliseconds since epoch
    "op":"insert",
    "data": {
        "id": "abcdef",
        "name": "hello world"
    }
}

NOTE when working with data from MongoDB, the _id field will be represented in the following fashion:

{
    "ns":"message.namespace",
    "ts":12345, // time represented in milliseconds since epoch
    "op":"insert",
    "data": {
        "_id": {
            "$oid": "54a4420502a14b9641000001"
        },
        "name": "hello world"
    }
}
configuration
goja({"filename": "/path/to/transform.js"})
// js() is aliased to goja
// js({"filename": "/path/to/transform.js"})
example

message in

{
    "_id": 0,
    "name": "transporter",
    "type": "function"
}

config

goja({"filename":"transform.js"})

transform function (i.e. transform.js)

function transform(doc) {
    doc["data"]["name_type"] = doc["data"]["name"] + " " + doc["data"]["type"];
    return doc
}

message out

{
    "_id": 0,
    "name": "transporter",
    "type": "function",
    "name_type": "transporter function"
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrInvalidMessageType is a generic error returned when the `data` property returned in the document from
	// the JS function was not of type map[string]interface{}
	ErrInvalidMessageType = errors.New("returned document was not a map")

	// ErrEmptyFilename will be returned when the profided filename is empty.
	ErrEmptyFilename = errors.New("no filename specified")
)

Functions

This section is empty.

Types

type Goja

type Goja struct {
	Filename string `json:"filename"`
	// contains filtered or unexported fields
}

func (*Goja) Apply

func (g *Goja) Apply(msg message.Msg) (message.Msg, error)

Apply fulfills the function.Function interface by transforming the incoming message with the configured JavaScript function.

type JSFunc

type JSFunc func(map[string]interface{}) *goja.Object

JSFunc defines the structure a transformer function.

Jump to

Keyboard shortcuts

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