forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variables.go
282 lines (256 loc) · 7.94 KB
/
variables.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package util
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"encoding/json"
"unicode"
"github.com/hyperledger/burrow/deploy/def"
"github.com/hyperledger/burrow/deploy/def/rule"
"github.com/hyperledger/burrow/execution/evm/abi"
"github.com/hyperledger/burrow/logging"
)
func Variables(value interface{}) []*abi.Variable {
rv := reflect.ValueOf(value)
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
rt := rv.Type()
var variables []*abi.Variable
for i := 0; i < rv.NumField(); i++ {
field := rv.Field(i)
if field.Kind() == reflect.String {
variables = append(variables, &abi.Variable{Name: lowerFirstCharacter(rt.Field(i).Name), Value: field.String()})
}
}
return variables
}
func lowerFirstCharacter(name string) string {
if name == "" {
return name
}
bs := []byte(name)
bs[0] = byte(unicode.ToLower(rune(bs[0])))
return string(bs)
}
func PreProcessFields(value interface{}, do *def.DeployArgs, script *def.Playbook, client *def.Client, logger *logging.Logger) (err error) {
rv := reflect.ValueOf(value)
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
for i := 0; i < rv.NumField(); i++ {
field := rv.Field(i)
if field.Kind() == reflect.String {
str, err := PreProcess(field.String(), do, script, client, logger)
if err != nil {
return err
}
field.SetString(str)
}
}
return nil
}
func PreProcess(toProcess string, do *def.DeployArgs, script *def.Playbook, client *def.Client, logger *logging.Logger) (string, error) {
// Run through the replacement process for any placeholder matches
for _, pm := range rule.MatchPlaceholders(toProcess) {
logger.TraceMsg("Replacement Match Found",
"match", toProcess)
// first parse the reserved words.
if strings.Contains(pm.JobName, "block") {
block, err := replaceBlockVariable(pm.Match, client, logger)
if err != nil {
logger.InfoMsg("Errir replacing block variable",
"error", fmt.Sprintf("%v", err))
return "", err
}
/*log.WithFields(log.Fields{
"var": toProcess,
"res": block,
}).Debug("Fixing Variables =>")*/
toProcess = strings.Replace(toProcess, pm.Match, block, 1)
continue
}
// second we loop through the jobNames to do a result replace
var loopJobs func(script *def.Playbook) error
loopJobs = func(script *def.Playbook) error {
if script.Parent != nil {
err := loopJobs(script.Parent)
if err != nil {
return err
}
}
for _, job := range script.Jobs {
if pm.JobName == job.Name {
if pm.VariableName != "" {
for _, variable := range job.Variables {
if variable.Name == pm.VariableName { //find the value we want from the bunch
toProcess = strings.Replace(toProcess, pm.Match, variable.Value, 1)
logger.TraceMsg("Fixing Inner Vars",
"job", pm.JobName,
"varName", pm.VariableName,
"result", variable.Value)
}
}
} else {
// If result is returned as string assume that rendering otherwise marshal to JSON
result, ok := job.Result.(string)
if !ok {
bs, err := json.Marshal(job.Result)
if err != nil {
return fmt.Errorf("error marhsalling tx result in post processing: %v", err)
}
result = string(bs)
}
logger.TraceMsg("Fixing Variables",
"var", string(pm.JobName),
"res", result)
toProcess = strings.Replace(toProcess, pm.Match, result, 1)
}
}
}
return nil
}
err := loopJobs(script)
if err != nil {
return "", err
}
}
return toProcess, nil
}
func replaceBlockVariable(toReplace string, client *def.Client, logger *logging.Logger) (string, error) {
logger.TraceMsg("Correcting $block variable",
"var", toReplace)
blockHeight, err := GetBlockHeight(client, logger)
block := itoaU64(blockHeight)
logger.TraceMsg("Current height", "height", block)
if err != nil {
return "", err
}
if toReplace == "$block" {
logger.TraceMsg("Replacement (=)",
"block", block)
return block, nil
}
catchEr := regexp.MustCompile(`\$block\+(\d*)`)
if catchEr.MatchString(toReplace) {
height := catchEr.FindStringSubmatch(toReplace)[1]
h1, err := strconv.Atoi(height)
if err != nil {
return "", err
}
h2, err := strconv.Atoi(block)
if err != nil {
return "", err
}
height = strconv.Itoa(h1 + h2)
logger.TraceMsg("Replacement (+)",
"replacement", height)
return height, nil
}
catchEr = regexp.MustCompile(`\$block\-(\d*)`)
if catchEr.MatchString(toReplace) {
height := catchEr.FindStringSubmatch(toReplace)[1]
h1, err := strconv.Atoi(height)
if err != nil {
return "", err
}
h2, err := strconv.Atoi(block)
if err != nil {
return "", err
}
height = strconv.Itoa(h1 - h2)
logger.TraceMsg("Replacement (-)",
"replacement", height)
return height, nil
}
logger.TraceMsg("Replacement (unknown)",
"replacement", toReplace)
return toReplace, nil
}
func PreProcessInputData(function string, data interface{}, do *def.DeployArgs, script *def.Playbook, client *def.Client, constructor bool, logger *logging.Logger) (string, []interface{}, error) {
var callDataArray []interface{}
var callArray []string
if _, ok := data.(string); function == "" && !constructor && ok {
callArray = strings.Split(data.(string), " ")
function = callArray[0]
callArray = callArray[1:]
for _, val := range callArray {
output, _ := PreProcess(val, do, script, client, logger)
callDataArray = append(callDataArray, output)
}
} else if data != nil {
if reflect.TypeOf(data).Kind() != reflect.Slice {
if constructor {
logger.InfoMsg("Deprecation Warning: Your deploy job is currently using a soon to be deprecated way of declaring constructor values. Please remember to update your run file to store them as a array rather than a string. See documentation for further details.")
callArray = strings.Split(data.(string), " ")
for _, val := range callArray {
output, _ := PreProcess(val, do, script, client, logger)
callDataArray = append(callDataArray, output)
}
return function, callDataArray, nil
} else {
return "", []interface{}{}, fmt.Errorf("Incorrect formatting of deploy.yaml file. Please update it to include a function field.")
}
}
val := reflect.ValueOf(data)
for i := 0; i < val.Len(); i++ {
s := val.Index(i)
var newString string
switch s.Interface().(type) {
case bool:
newString = strconv.FormatBool(s.Interface().(bool))
case int, int32, int64:
newString = strconv.FormatInt(int64(s.Interface().(int)), 10)
case []interface{}:
var args []string
for _, index := range s.Interface().([]interface{}) {
value := reflect.ValueOf(index)
var stringified string
switch value.Kind() {
case reflect.Int:
stringified = strconv.FormatInt(value.Int(), 10)
case reflect.String:
stringified = value.String()
}
index, _ = PreProcess(stringified, do, script, client, logger)
args = append(args, stringified)
}
newString = "[" + strings.Join(args, ",") + "]"
logger.TraceMsg(newString)
default:
newString = s.Interface().(string)
}
newString, _ = PreProcess(newString, do, script, client, logger)
callDataArray = append(callDataArray, newString)
}
}
return function, callDataArray, nil
}
func PreProcessLibs(libs string, do *def.DeployArgs, script *def.Playbook, client *def.Client, logger *logging.Logger) (string, error) {
libraries, _ := PreProcess(libs, do, script, client, logger)
if libraries != "" {
pairs := strings.Split(libraries, ",")
libraries = strings.Join(pairs, " ")
}
logger.TraceMsg("Library String", "libs", libraries)
return libraries, nil
}
func GetReturnValue(vars []*abi.Variable, logger *logging.Logger) string {
var result []string
if len(vars) > 1 {
for _, value := range vars {
logger.TraceMsg("Value",
value.Name, value.Value)
result = append(result, value.Value)
}
return "(" + strings.Join(result, ", ") + ")"
} else if len(vars) == 1 {
logger.TraceMsg("Debugging",
"value", vars[0].Value)
return vars[0].Value
} else {
return ""
}
}