forked from TIBCOSoftware/flogo-contrib
/
activity.go
executable file
·89 lines (66 loc) · 2.36 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
package actreturn
import (
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/core/data"
"github.com/TIBCOSoftware/flogo-lib/core/mapper"
"github.com/TIBCOSoftware/flogo-lib/logger"
"github.com/TIBCOSoftware/flogo-lib/core/action"
)
// log is the default package logger
var log = logger.GetLogger("activity-flogo-return")
const (
ivMappings = "mappings"
)
// ReturnActivity is an Activity that is used to return/return via the trigger
// inputs : {method,uri,params}
// outputs: {result}
type ReturnActivity struct {
metadata *activity.Metadata
}
// NewActivity creates a new ReturnActivity
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &ReturnActivity{metadata: metadata}
}
// Metadata returns the activity's metadata
func (a *ReturnActivity) Metadata() *activity.Metadata {
return a.metadata
}
// Eval implements api.Activity.Eval - Invokes a REST Operation
func (a *ReturnActivity) Eval(ctx activity.Context) (done bool, err error) {
mappings := ctx.GetInput(ivMappings).([]interface{})
log.Debugf("Mappings: %+v", mappings)
mapperDef, err := mapper.NewMapperDefFromAnyArray(mappings)
//todo move this to a action instance level initialization, need the notion of static inputs or config
returnMapper := mapper.NewBasicMapper(mapperDef, ctx.ActionContext().GetResolver())
if err != nil {
return false, err
}
actionCtx := ctx.ActionContext()
outputScope := newOutputScope(actionCtx, mapperDef)
inputScope := actionCtx.WorkingData() //flow data
err = returnMapper.Apply(inputScope, outputScope)
if err != nil {
return false, err
}
actionCtx.Return(outputScope.GetAttrs(), nil)
return true, nil
}
func newOutputScope(actionCtx action.Context, mapperDef *data.MapperDef) *data.FixedScope {
if actionCtx.InstanceMetadata() == nil {
//todo temporary fix to support tester service
attrs := make([]*data.Attribute, 0, len(mapperDef.Mappings))
for _, mappingDef := range mapperDef.Mappings {
attr, _ := data.NewAttribute(mappingDef.MapTo, data.ANY, nil)
attrs = append(attrs, attr)
}
return data.NewFixedScope(attrs)
} else {
outAttrs := actionCtx.InstanceMetadata().Output
attrs := make([]*data.Attribute, 0, len(outAttrs))
for _, outAttr := range outAttrs {
attrs = append(attrs, outAttr)
}
//create a fixed scope using the output metadata
return data.NewFixedScope(attrs)
}
}