-
Notifications
You must be signed in to change notification settings - Fork 0
/
shift.go
194 lines (171 loc) · 4.36 KB
/
shift.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package shift
import (
"encoding/json"
"strings"
"github.com/zeiss/typhoon/pkg/flow/adapter/transformation/common/convert"
"github.com/zeiss/typhoon/pkg/flow/adapter/transformation/common/storage"
"github.com/zeiss/typhoon/pkg/flow/adapter/transformation/transformer"
)
var _ transformer.Transformer = (*Shift)(nil)
// Shift object implements Transformer interface.
type Shift struct {
Path string
NewPath string
Value string
Separator string
variables *storage.Storage
}
const delimeter string = ":"
// InitStep is used to figure out if this operation should
// run before main Transformations. For example, Store
// operation needs to run first to load all Pipeline variables.
var InitStep bool = false
// operationName is used to identify this transformation.
var operationName string = "shift"
// Register adds this transformation to the map which will
// be used to create Transformation pipeline.
func Register(m map[string]transformer.Transformer) {
m[operationName] = &Shift{}
}
// SetStorage sets a shared Storage with Pipeline variables.
func (s *Shift) SetStorage(storage *storage.Storage) {
s.variables = storage
}
// InitStep returns "true" if this Transformation should run
// as init step.
func (s *Shift) InitStep() bool {
return InitStep
}
// New returns a new instance of Shift object.
func (s *Shift) New(key, value, separator string) transformer.Transformer {
// doubtful scheme, review needed
keys := strings.Split(key, delimeter)
if len(keys) != 2 {
return nil
}
return &Shift{
Path: keys[0],
NewPath: keys[1],
Value: value,
Separator: separator,
variables: s.variables,
}
}
// Apply is a main method of Transformation that moves existing
// values to a new locations.
func (s *Shift) Apply(eventID string, data []byte) ([]byte, error) {
oldPath := convert.SliceToMap(strings.Split(s.Path, s.Separator), "")
var event interface{}
if err := json.Unmarshal(data, &event); err != nil {
return data, err
}
newEvent, value := extractValue(event, oldPath)
if s.Value != "" {
if !equal(s.retrieveInterface(eventID, s.Value), value) {
return data, nil
}
}
if value == nil {
return data, nil
}
newPath := convert.SliceToMap(strings.Split(s.NewPath, s.Separator), value)
result := convert.MergeJSONWithMap(newEvent, newPath)
output, err := json.Marshal(result)
if err != nil {
return data, err
}
return output, nil
}
func (s *Shift) retrieveInterface(eventID, key string) interface{} {
if value := s.variables.Get(eventID, key); value != nil {
return value
}
return key
}
// nolint:gocyclo
func extractValue(source interface{}, path map[string]interface{}) (map[string]interface{}, interface{}) {
var ok bool
var result interface{}
sourceMap := make(map[string]interface{})
for k, v := range path {
switch value := v.(type) {
case float64, bool, string:
sourceMap, ok = source.(map[string]interface{})
if !ok {
break
}
result = sourceMap[k]
delete(sourceMap, k)
case []interface{}:
if k != "" {
// array is inside the object
// {"foo":[{},{},{}]}
sourceMap, ok = source.(map[string]interface{})
if !ok {
break
}
source, ok = sourceMap[k]
if !ok {
break
}
}
// array is a root object
// [{},{},{}]
sourceArr, ok := source.([]interface{})
if !ok {
break
}
index := len(value) - 1
if index >= len(sourceArr) {
break
}
m, ok := value[index].(map[string]interface{})
if ok {
sourceArr[index], result = extractValue(sourceArr[index].(map[string]interface{}), m)
sourceMap[k] = sourceArr
break
}
result = sourceArr[index]
sourceMap[k] = sourceArr[:index]
if len(sourceArr) > index {
sourceMap[k] = append(sourceArr[:index], sourceArr[index+1:]...)
}
case map[string]interface{}:
if k == "" {
result = source
break
}
sourceMap, ok = source.(map[string]interface{})
if !ok {
break
}
if _, ok := sourceMap[k]; !ok {
break
}
sourceMap[k], result = extractValue(sourceMap[k], value)
case nil:
sourceMap[k] = nil
}
}
return sourceMap, result
}
func equal(a, b interface{}) bool {
switch value := b.(type) {
case string:
v, ok := a.(string)
if ok && v == value {
return true
}
case bool:
v, ok := a.(bool)
if ok && v == value {
return true
}
case float64:
v, ok := a.(float64)
if ok && v == value {
return true
}
}
return false
}