-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpipeline.go
More file actions
188 lines (167 loc) · 3.76 KB
/
Copy pathpipeline.go
File metadata and controls
188 lines (167 loc) · 3.76 KB
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
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"
pool string = "pool"
// config
config string = "config"
trueStr string = "true"
falseStr string = "false"
)
// resolve performs a two-phase lookup: first checks the top-level map,
// then falls back to the nested "config" sub-map.
func (p Pipeline) resolve(key string) (any, bool) {
if v, ok := p[key]; ok {
return v, true
}
if val, ok := p[config]; ok {
if rv, ok := val.(map[string]any); ok {
if v, ok := rv[key]; ok {
return v, true
}
}
}
return nil, false
}
// With pipeline value
func (p Pipeline) With(name string, value any) {
p[name] = value
}
func (p Pipeline) Pool() string {
return p.String(pool, "")
}
// 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 n := p.String(name, ""); n != "" {
return n
}
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(key string) bool {
_, ok := p.resolve(key)
return ok
}
// String must return option value as string or return default value.
func (p Pipeline) String(key string, d string) string {
v, ok := p.resolve(key)
if !ok || v == nil {
return d
}
if str, ok := v.(string); ok {
if str != "" {
return str
}
}
return d
}
// toInt64 converts common numeric/string types to int64; returns (0, false) when conversion fails.
func toInt64(v any) (int64, bool) {
switch val := v.(type) {
case string:
res, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return 0, false
}
return res, true
case float32:
return int64(val), true
case float64:
return int64(val), true
case int:
return int64(val), true
case int64:
return val, true
case int32:
return int64(val), true
case int16:
return int64(val), true
case int8:
return int64(val), true
default:
return 0, false
}
}
// Int must return option value as int or return default value.
func (p Pipeline) Int(key string, d int) int {
v, ok := p.resolve(key)
if !ok || v == nil {
return d
}
if res, ok := toInt64(v); ok && res >= math.MinInt && res <= math.MaxInt {
return int(res)
}
return d
}
// Bool must return option value as bool or return default value.
func (p Pipeline) Bool(key string, d bool) bool {
v, ok := p.resolve(key)
if !ok || v == nil {
return d
}
if i, ok := v.(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(key string, out map[string]string) error {
v, ok := p.resolve(key)
if !ok || v == nil {
return nil
}
if m, ok := v.(string); ok {
err := json.Unmarshal(strToBytes(m), &out)
if err != nil {
return err
}
}
return nil
}
// Priority returns default pipeline priority
func (p Pipeline) Priority() int64 {
v, ok := p.resolve(priority)
if !ok || v == nil {
return defaultPriority
}
if res, ok := toInt64(v); ok {
return res
}
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))
}