-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathactivity.go
executable file
·162 lines (140 loc) · 3.5 KB
/
activity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package jsexec
import (
"encoding/json"
"errors"
"github.com/dop251/goja"
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data/metadata"
)
var activityMetadata = activity.ToMetadata(&Settings{}, &Input{}, &Output{})
func init() {
_ = activity.Register(&Activity{}, New)
}
// Activity is a javascript activity
type Activity struct {
script string
}
// New creates a new javascript activity
func New(ctx activity.InitContext) (activity.Activity, error) {
settings := Settings{}
err := metadata.MapToStruct(ctx.Settings(), &settings, true)
if err != nil {
return nil, err
}
logger := ctx.Logger()
logger.Debugf("Setting: %b", settings)
act := Activity{
script: settings.Script,
}
return &act, nil
}
// Metadata return the metadata for the activity
func (a *Activity) Metadata() *activity.Metadata {
return activityMetadata
}
// Eval executes the activity
func (a *Activity) Eval(ctx activity.Context) (done bool, err error) {
input := Input{}
err = ctx.GetInputObject(&input)
if err != nil {
return false, err
}
output := Output{}
result := make(map[string]interface{})
vm, err := NewVM(nil)
if err != nil {
output.Error = true
output.ErrorMessage = err.Error()
}
if vm != nil {
//todo is ok to ignore the errors for the SetInVM calls?
_ = vm.SetInVM("parameters", input.Parameters)
_ = vm.SetInVM("result", result)
_, err = vm.vm.RunScript("JSServiceScript", a.script)
if err != nil {
output.Error = true
output.ErrorMessage = err.Error()
} else {
err = vm.GetFromVM("result", &result)
if err != nil {
output.Error = true
output.ErrorMessage = err.Error()
} else {
output.Error = false
output.ErrorMessage = ""
output.Result = result
}
}
}
err = ctx.SetOutputObject(&output)
if err != nil {
return false, err
}
return true, nil
}
// VM represents a VM object.
type VM struct {
vm *goja.Runtime
}
// NewVM initializes a new VM with defaults.
func NewVM(defaults map[string]interface{}) (vm *VM, err error) {
vm = &VM{}
vm.vm = goja.New()
for k, v := range defaults {
if v != nil {
vm.vm.Set(k, v)
}
}
return vm, err
}
// EvaluateToBool evaluates a string condition within the context of the VM.
func (vm *VM) EvaluateToBool(condition string) (truthy bool, err error) {
if condition == "" {
return true, nil
}
var res goja.Value
res, err = vm.vm.RunString(condition)
if err != nil {
return false, err
}
truthy, ok := res.Export().(bool)
if !ok {
err = errors.New("condition does not evaluate to bool")
return false, err
}
return truthy, err
}
// SetInVM sets the object name and value in the VM.
func (vm *VM) SetInVM(name string, object interface{}) (err error) {
var valueJSON json.RawMessage
var vmObject map[string]interface{}
valueJSON, err = json.Marshal(object)
if err != nil {
return err
}
err = json.Unmarshal(valueJSON, &vmObject)
if err != nil {
return err
}
vm.vm.Set(name, vmObject)
return err
}
// GetFromVM extracts the current object value from the VM.
func (vm *VM) GetFromVM(name string, object interface{}) (err error) {
var valueJSON json.RawMessage
var vmObject map[string]interface{}
_ = vm.vm.ExportTo(vm.vm.Get(name), &vmObject) //todo is ok to ignore the error?
valueJSON, err = json.Marshal(vmObject)
if err != nil {
return err
}
err = json.Unmarshal(valueJSON, object)
if err != nil {
return err
}
return err
}
// SetPrimitiveInVM sets primitive value in VM.
func (vm *VM) SetPrimitiveInVM(name string, primitive interface{}) {
vm.vm.Set(name, primitive)
}