/
activity.go
131 lines (106 loc) · 3.03 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
// Package tomlreader implements activities to read and query TOML files
package tomlreader
// Imports
import (
"os"
"strings"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/logger"
toml "github.com/pelletier/go-toml"
)
// Constants
const (
ivFilename = "filename"
ivKey = "key"
ivFilters = "filters"
ovResult = "result"
)
// log is the default package logger
var log = logger.GetLogger("activity-tomlreader")
// 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 action
filename := context.GetInput(ivFilename).(string)
key := context.GetInput(ivKey).(string)
filters := context.GetInput(ivFilters).(string)
// Check if the file exists
_, err = os.Stat(filename)
if err != nil {
log.Error(err.Error())
return true, err
}
// Read the file
config, err := toml.LoadFile(filename)
if err != nil {
log.Error(err.Error())
return true, err
}
// Find the keys
queryResult := config.Get(key)
if queryResult == nil {
// Set the output value in the context
context.SetOutput(ovResult, nil)
return true, nil
}
resultArray := queryResult.([]*toml.Tree)
// Prepare a result structure
datamap := make([]interface{}, len(resultArray))
// Loop over the queryResult and make a proper interface from it
for idx, val := range resultArray {
datamap[idx] = val.ToMap()
}
// Filter the results
if len(filters) > 0 {
filterArray := strings.Split(filters, "/")
for _, filter := range filterArray {
if strings.HasPrefix(filter, "ValueContains") {
value := strings.Replace(strings.Split(filter, "(")[1], ")", "", -1)
datamap = mapValueContains(datamap, value)
} else if strings.HasPrefix(filter, "KeyEquals") {
params := strings.Replace(strings.Split(filter, "(")[1], ")", "", -1)
vals := strings.Split(params, ",")
datamap = mapKeyEquals(datamap, vals[0], vals[1])
}
}
}
// Set the output value in the context
context.SetOutput(ovResult, datamap)
return true, nil
}
func mapKeyEquals(datamap []interface{}, key string, value interface{}) []interface{} {
tempmap := make([]interface{}, 0)
for _, val := range datamap {
item := val.(map[string]interface{})
if _, ok := item[key]; ok {
if item[key] == value {
tempmap = append(tempmap, item)
}
}
}
return tempmap
}
func mapValueContains(datamap []interface{}, value string) []interface{} {
tempmap := make([]interface{}, 0)
for _, val := range datamap {
item := val.(map[string]interface{})
for k := range item {
if strings.Contains(item[k].(string), value) {
tempmap = append(tempmap, item)
break
}
}
}
return tempmap
}