-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpipeline.go
270 lines (242 loc) · 5.13 KB
/
pipeline.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
package jobs
import (
"encoding/json"
"math"
"strconv"
"unsafe"
)
// Pipeline defines pipeline options.
type Pipeline map[string]any
const (
priority string = "priority"
defaultPriority int64 = 10
driver string = "driver"
name string = "name"
queue string = "queue"
// config
config string = "config"
trueStr string = "true"
falseStr string = "false"
)
// With pipeline value
func (p Pipeline) With(name string, value any) {
p[name] = value
}
// Name returns pipeline name.
func (p Pipeline) Name() string {
// https://github.com/spiral/roadrunner-jobs/blob/master/src/Queue/CreateInfo.php#L81
// In the PHP client library used the wrong key name
// should be "name" instead of "queue"
if p.String(name, "") != "" {
return p.String(name, "")
}
return p.String(queue, "")
}
// Driver associated with the pipeline.
func (p Pipeline) Driver() string {
return p.String(driver, "")
}
// Has checks if value presented in pipeline.
func (p Pipeline) Has(name string) bool {
if _, ok := p[name]; ok {
return true
}
// check the config section if exists
if val, ok := p[config]; ok {
if rv, ok := val.(map[string]any); ok {
if _, ok := rv[name]; ok {
return true
}
return false
}
}
return false
}
// String must return option value as string or return default value.
func (p Pipeline) String(name string, d string) string {
if value, ok := p[name]; ok {
if str, ok := value.(string); ok {
return str
}
} else {
// check the config section if exists
if val, ok := p[config]; ok {
if rv, ok := val.(map[string]any); ok {
if rv[name] != "" {
return rv[name].(string)
}
}
}
}
return d
}
// Int must return option value as string or return default value.
func (p Pipeline) Int(name string, d int) int {
if value, ok := p[name]; ok {
switch v := value.(type) {
// the most probable case
case string:
res, err := strconv.ParseInt(v, 10, 32)
if err != nil {
// return default on failure
return d
}
if res > math.MaxInt32 || res < math.MinInt32 {
// return default if out of bounds
return d
}
return int(res)
case int:
return v
case int64:
return int(v)
case int32:
return int(v)
case int16:
return int(v)
case int8:
return int(v)
default:
return d
}
} else {
// check the config section if exists
if val, ok := p[config]; ok {
if rv, ok := val.(map[string]any); ok {
if rv[name] != nil {
switch v := rv[name].(type) {
case float32:
return int(v)
case float64:
return int(v)
case int:
return v
default:
return 0
}
}
}
}
}
return d
}
// Bool must return option value as bool or return default value.
func (p Pipeline) Bool(name string, d bool) bool {
if value, ok := p[name]; ok {
if i, ok := value.(string); ok {
switch i {
case trueStr:
return true
case falseStr:
return false
default:
return false
}
}
} else {
// check the config section if exists
if val, ok := p[config]; ok {
if rv, ok := val.(map[string]any); ok {
if rv[name] != nil {
if i, ok := value.(string); ok {
switch i {
case trueStr:
return true
case falseStr:
return false
default:
return false
}
}
}
}
}
}
return d
}
// Map must return nested map value or empty config.
// There might be sqs attributes or tags, for example
func (p Pipeline) Map(name string, out map[string]string) error {
if value, ok := p[name]; ok {
if m, ok := value.(string); ok {
err := json.Unmarshal(strToBytes(m), &out)
if err != nil {
return err
}
}
} else {
// check the config section if exists
if val, ok := p[config]; ok {
if rv, ok := val.(map[string]any); ok {
if val, ok := rv[name]; ok {
if m, ok := val.(string); ok {
err := json.Unmarshal(strToBytes(m), &out)
if err != nil {
return err
}
}
return nil
}
}
}
}
return nil
}
// Priority returns default pipeline priority
func (p Pipeline) Priority() int64 {
if value, ok := p[priority]; ok {
switch v := value.(type) {
// the most probable case
case string:
res, err := strconv.ParseInt(v, 10, 64)
if err != nil {
// return default on failure
return defaultPriority
}
return res
case int:
return int64(v)
case int64:
return v
case int32:
return int64(v)
case int16:
return int64(v)
case int8:
return int64(v)
default:
return defaultPriority
}
} else {
// check the config section if exists
if val, ok := p[config]; ok {
if rv, ok := val.(map[string]any); ok {
if rv[name] != nil {
switch v := rv[name].(type) {
case float32:
return int64(v)
case float64:
return int64(v)
case int:
return int64(v)
case int64:
return v
default:
return defaultPriority
}
}
}
}
}
return defaultPriority
}
// Get used to get the data associated with the key
func (p Pipeline) Get(key string) any {
return p[key]
}
func strToBytes(data string) []byte {
if data == "" {
return nil
}
return unsafe.Slice(unsafe.StringData(data), len(data))
}