-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.go
81 lines (64 loc) · 2 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
package goeval
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/logger"
)
const (
ivInput = "Input"
ivScriptURL = "ScriptURL"
ovOutput = "Output"
)
var activityLog = logger.GetLogger("Scripting-activity-GO")
type goevalActivity struct {
metadata *activity.Metadata
}
//NewActivity TCI Wi Activity
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &goevalActivity{metadata: metadata}
}
func (a *goevalActivity) Metadata() *activity.Metadata {
return a.metadata
}
func (a *goevalActivity) Eval(context activity.Context) (done bool, err error) {
activityLog.Info("Eval GO Scripting activity")
//Read Inputs
if context.GetInput(ivInput) == nil {
// ivInput string is not configured
// return error to the engine
return false, activity.NewError("Scripting ivInput string is not configured", "Scripting-GO-4001", nil)
}
input := context.GetInput(ivInput).(string)
if context.GetInput(ivScriptURL) == nil {
// APIsecret string is not configured
// return error to the engine
return false, activity.NewError("Scripting URL string is not configured", "Scripting-GO-4002", nil)
}
scriptURL := context.GetInput(ivScriptURL).(string)
// execute validation - Start
// load Script from Web
res, err := http.Get(scriptURL)
if err != nil {
return false, activity.NewError("Scripting URL not found", "Scripting-GO-4003", nil)
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var script = string(body)
// execute
output := evalgo(input, script)
// Output
context.SetOutput(ovOutput, output)
return true, nil
}
func evalgo(in string, script string) int {
activityLog.Info("GO Eval script: ", script)
s := NewScope()
s.Set("print", fmt.Println) //not used yet, but with this you are able to replace any function, e.g. to output something.
s.Eval("input :=" + in)
output, err := s.Eval(script)
activityLog.Info("activity output: ", output, err)
ret := output.(int)
return ret
}