jsexec

package module
v0.0.0-...-ffcd528 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2023 License: BSD-3-Clause Imports: 6 Imported by: 0

README

Javascript Execution Activity

The jsexec activity evaluates a javascript script along with provided parameters and returns the result in the outputs.

Flogo CLI
flogo install github.com/iosif02/contrib/activity/jsexec

Configuration

Settings:
Name Type Description
script string The javascript code to evaluate
Input:
Name Type Description
parameters object Key/value pairs representing parameters to evaluate within the context of the script
Output:
Name Type Description
error bool Flag indicating if there was an error executing the script
errorMessage string The error message
result object The result object from the javascript code

Microgateway Usage

A sample service definition is:

{
  "name": "JSCalc",
  "description": "Make calls to a JS calculator",
  "ref": "github.com/iosif02/jsexec",
  "settings": {
    "script": "result.total = parameters.num * 2;"
  }
}

An example step that invokes the above JSCalc service using parameters is:

{
  "if": "$.PetStorePets.outputs.result.status == 'available'",
  "service": "JSCalc",
  "input": {
    "parameters.num": "=$.PetStorePets.outputs.result.available"
  }
}

Utilizing the response values can be seen in a response handler:

{
  "if": "$.PetStorePets.outputs.result.status == 'available'",
  "error": false,
  "output": {
    "code": 200,
    "data": {
      "body.pet": "=$.PetStorePets.outputs.result",
      "body.inventory": "=$.PetStoreInventory.outputs.result",
      "body.availableTimesTwo": "=$.JSCalc.outputs.result.total"
    }
  }
}

Additional microgateway examples that utilize the jsexec activity

  • api - A simple API example
  • json - An example that using a flogo.json

Development

Testing

To run tests issue the following command in the root of the project:

go test -p 1 ./...

The -p 1 is needed to prevent tests from being run in parallel. To re-run the tests first run the following:

go clean -testcache

To skip the integration tests use the -short flag:

go test -p 1 -short ./...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

New creates a new javascript activity

Types

type Activity

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

Activity is a javascript activity

func (*Activity) Eval

func (a *Activity) Eval(ctx activity.Context) (done bool, err error)

Eval executes the activity

func (*Activity) Metadata

func (a *Activity) Metadata() *activity.Metadata

Metadata return the metadata for the activity

type Input

type Input struct {
	Parameters map[string]interface{} `md:"parameters"`
}

Input is the input into the javascript engine

func (*Input) FromMap

func (i *Input) FromMap(values map[string]interface{}) error

FromMap converts the values from a map into the struct Input

func (*Input) ToMap

func (i *Input) ToMap() map[string]interface{}

ToMap converts the struct Input into a map

type Output

type Output struct {
	Error        bool                   `md:"error"`
	ErrorMessage string                 `md:"errorMessage"`
	Result       map[string]interface{} `md:"result"`
}

Output is the output from the javascript engine

func (*Output) FromMap

func (o *Output) FromMap(values map[string]interface{}) error

FromMap converts the values from a map into the struct Output

func (*Output) ToMap

func (o *Output) ToMap() map[string]interface{}

ToMap converts the struct Output into a map

type Settings

type Settings struct {
	Script string `md:"script"`
}

Settings are the jsexec settings

type VM

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

VM represents a VM object.

func NewVM

func NewVM(defaults map[string]interface{}) (vm *VM, err error)

NewVM initializes a new VM with defaults.

func (*VM) EvaluateToBool

func (vm *VM) EvaluateToBool(condition string) (truthy bool, err error)

EvaluateToBool evaluates a string condition within the context of the VM.

func (*VM) GetFromVM

func (vm *VM) GetFromVM(name string, object interface{}) (err error)

GetFromVM extracts the current object value from the VM.

func (*VM) SetInVM

func (vm *VM) SetInVM(name string, object interface{}) (err error)

SetInVM sets the object name and value in the VM.

func (*VM) SetPrimitiveInVM

func (vm *VM) SetPrimitiveInVM(name string, primitive interface{})

SetPrimitiveInVM sets primitive value in VM.

Jump to

Keyboard shortcuts

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