-
Notifications
You must be signed in to change notification settings - Fork 69
/
add.go
157 lines (130 loc) · 4.51 KB
/
add.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
/*
Copyright 2022 TriggerMesh Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package add
import (
"encoding/json"
"fmt"
"strings"
"github.com/triggermesh/triggermesh/pkg/flow/adapter/transformation/common/convert"
"github.com/triggermesh/triggermesh/pkg/flow/adapter/transformation/common/storage"
"github.com/triggermesh/triggermesh/pkg/flow/adapter/transformation/transformer"
)
var _ transformer.Transformer = (*Add)(nil)
// Add object implements Transformer interface.
type Add struct {
Path string
Value string
Separator string
variables *storage.Storage
}
// 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 = "add"
// Register adds this transformation to the map which will
// be used to create Transformation pipeline.
func Register(m map[string]transformer.Transformer) {
m[operationName] = &Add{}
}
// SetStorage sets a shared Storage with Pipeline variables.
func (a *Add) SetStorage(storage *storage.Storage) {
a.variables = storage
}
// InitStep returns "true" if this Transformation should run
// as init step.
func (a *Add) InitStep() bool {
return InitStep
}
// New returns a new instance of Add object.
func (a *Add) New(key, value, separator string) transformer.Transformer {
return &Add{
Path: key,
Value: value,
Separator: separator,
variables: a.variables,
}
}
// Apply is a main method of Transformation that adds any type of
// variables into existing JSON.
func (a *Add) Apply(eventID string, data []byte) ([]byte, error) {
input := convert.SliceToMap(strings.Split(a.Path, a.Separator), a.composeValue(eventID))
var event interface{}
if err := json.Unmarshal(data, &event); err != nil {
return data, err
}
result := convert.MergeJSONWithMap(event, input)
output, err := json.Marshal(result)
if err != nil {
return data, err
}
return output, nil
}
func (a *Add) retrieveVariable(eventID, key string) interface{} {
if value := a.variables.Get(eventID, key); value != nil {
return value
}
return key
}
func (a *Add) composeValue(eventID string) interface{} {
result := a.Value
for _, key := range a.variables.ListEventVariables(eventID) {
// limit the number of iterations to prevent the loop if
// "add" variable is not updating the result (variable is not defined).
variableKeysInResult := strings.Count(result, key)
for i := 0; i <= variableKeysInResult; i++ {
keyIndex := strings.Index(result, key)
if keyIndex == -1 {
continue
}
storedValue := a.retrieveVariable(eventID, key)
if result == key {
return storedValue
}
openingBracketIndex := -1
closingBracketIndex := -1
for i := keyIndex; i >= 0; i-- {
if string(result[i]) == "(" {
openingBracketIndex = i
break
}
}
for i := keyIndex; i < len(result); i++ {
if string(result[i]) == ")" {
closingBracketIndex = i
break
}
}
// there is no brackets in the value
if (openingBracketIndex == -1 || closingBracketIndex == -1) ||
// brackets are screened with "\" symbol
((openingBracketIndex > 0 && string(result[openingBracketIndex-1]) == "\\") ||
string(result[closingBracketIndex-1]) == "\\") ||
// brackets are not surrounding the key
!(openingBracketIndex < keyIndex && closingBracketIndex >= keyIndex+len(key)) {
result = fmt.Sprintf("%s%v%s", result[:keyIndex], storedValue, result[keyIndex+len(key):])
continue
}
if storedValue == key {
// stored value that equals the variable key means no stored value is available
result = fmt.Sprintf("%s%s", result[:openingBracketIndex], result[closingBracketIndex+1:])
continue
}
result = result[:openingBracketIndex] + result[openingBracketIndex+1:]
result = result[:closingBracketIndex-1] + result[closingBracketIndex:]
result = fmt.Sprintf("%s%v%s", result[:keyIndex-1], storedValue, result[keyIndex+len(key)-1:])
}
}
return result
}