/
activity.go
55 lines (42 loc) · 1.42 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
// Package randomnumber generates a random number between the minimum and maximum values supplied.
package randomnumber
import (
"math/rand"
"time"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/logger"
)
// Constants used by the code to represent the input and outputs of the JSON structure
const (
min = "min"
max = "max"
result = "result"
)
// log is the default package logger
var log = logger.GetLogger("activity-randomnumber")
// MyActivity is a stub for your Activity implementation
type MyActivity struct {
metadata *activity.Metadata
}
// NewActivity creates a new activity
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &MyActivity{metadata: metadata}
}
// Metadata implements activity.Activity.Metadata
func (a *MyActivity) Metadata() *activity.Metadata {
return a.metadata
}
// Eval implements activity.Activity.Eval
func (a *MyActivity) Eval(context activity.Context) (done bool, err error) {
// Get the inputs
minvalue := context.GetInput(min).(int)
maxvalue := context.GetInput(max).(int)
// Set a random seed
rand.Seed(time.Now().UTC().UnixNano())
// Create a random number
randvalue := minvalue + rand.Intn(maxvalue-minvalue)
log.Debugf("Created a random number [%s]", randvalue)
// Set the output value in the context
context.SetOutput(result, randvalue)
return true, nil
}