-
Notifications
You must be signed in to change notification settings - Fork 64
/
activity.go
executable file
·76 lines (57 loc) · 1.56 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
package activity_mapper
import (
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data/mapper"
"github.com/project-flogo/core/data/metadata"
)
func init() {
_ = activity.Register(&Activity{}, New)
}
type Settings struct {
Mappings map[string]interface{} `md:"mappings,required"` // Set of mappings to execute
}
var activityMd = activity.ToMetadata(&Settings{})
func New(ctx activity.InitContext) (activity.Activity, error) {
s := &Settings{}
err := metadata.MapToStruct(ctx.Settings(), s, true)
if err != nil {
return nil, err
}
act := &Activity{}
ctx.Logger().Debugf("Mappings: %+v", s.Mappings)
act.mapper, err = ctx.MapperFactory().NewMapper(s.Mappings)
if err != nil {
return nil, err
}
return act, nil
}
// Activity is an Activity that is used to reply/return via the trigger
// inputs : {method,uri,params}
// outputs: {result}
type Activity struct {
mapper mapper.Mapper
}
// Metadata returns the activity's metadata
func (a *Activity) Metadata() *activity.Metadata {
return activityMd
}
// Eval implements api.Activity.Eval - Invokes a REST Operation
func (a *Activity) Eval(ctx activity.Context) (done bool, err error) {
actionCtx := ctx.ActivityHost()
if a.mapper == nil {
//No mapping
return true, nil
}
inputScope := actionCtx.Scope() //host data
results, err := a.mapper.Apply(inputScope)
if err != nil {
return false, activity.NewError(err.Error(), "", nil)
}
for name, value := range results {
err = actionCtx.Scope().SetValue(name, value)
if err != nil {
return false, err
}
}
return true, nil
}