-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutil.go
More file actions
242 lines (219 loc) · 4.87 KB
/
util.go
File metadata and controls
242 lines (219 loc) · 4.87 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
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
package server
import (
"context"
"fmt"
"regexp"
"strconv"
"time"
// "runtime/debug"
"bytes"
"encoding/json"
"strings"
// "reflect"
// "runtime"
"slices"
"sync"
"github.com/urnetwork/sdk"
)
// func Ptr[T any](value T) *T {
// return &value
// }
// this is just max in go 1.21
// func MaxInt(values... int) int {
// if len(values) == 0 {
// return 0
// }
// var max int = values[0]
// for i := 1; i < len(values); i += i {
// if max < values[i] {
// max = values[i]
// }
// }
// return max
// }
// this is just min in go 1.21
// func MinInt(values... int) int {
// if len(values) == 0 {
// return 0
// }
// var min int = values[0]
// for i := 1; i < len(values); i += i {
// if values[i] < min {
// min = values[i]
// }
// }
// return min
// }
func NowUtc() time.Time {
// data stores use utc time without time zone
// use the same time format locally to keep the local time in sync with the data store time
return time.Now().UTC()
}
func CodecTime(t time.Time) time.Time {
// nanosecond resolution can be serialized and unserialized in most codecs:
// - json
// - postgres
return t.Round(time.Nanosecond)
}
func MinTime(a time.Time, b time.Time) time.Time {
if a.Before(b) {
return a
} else {
return b
}
}
func MaxTime(a time.Time, b time.Time) time.Time {
if a.Before(b) {
return b
} else {
return a
}
}
func Raise(err error) {
if err != nil {
panic(err)
}
}
// returns source if cannot compact
func AttemptCompactJson(jsonBytes []byte) []byte {
b := &bytes.Buffer{}
if err := json.Compact(b, jsonBytes); err == nil {
return b.Bytes()
} else {
// there was an error compacting the json
// return the original
return jsonBytes
}
}
func MaskValue(v string) string {
if len(v) < 6 {
return "***"
} else {
return fmt.Sprintf("%s***%s", v[:2], v[len(v)-2:])
}
}
var portRangeRegex = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile("^\\s*(\\d+)\\s*-\\s*(\\d+)\\s*$")
})
var portRange2Regex = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile("^\\s*(\\d+)\\s*\\+\\s*(\\d+)\\s*$")
})
var portRegex = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile("^\\s*(\\d+)\\s*$")
})
func ExpandPorts(portsListStr string) ([]int, error) {
if portsListStr == "" {
return []int{}, nil
}
ports := []int{}
for _, portsStr := range strings.Split(portsListStr, ",") {
if portStrs := portRangeRegex().FindStringSubmatch(portsStr); portStrs != nil {
minPort, err := strconv.Atoi(portStrs[1])
if err != nil {
panic(err)
}
maxPort, err := strconv.Atoi(portStrs[2])
if err != nil {
panic(err)
}
for port := minPort; port <= maxPort; port += 1 {
ports = append(ports, port)
}
} else if portStrs := portRange2Regex().FindStringSubmatch(portsStr); portStrs != nil {
minPort, err := strconv.Atoi(portStrs[1])
if err != nil {
panic(err)
}
n, err := strconv.Atoi(portStrs[2])
if err != nil {
panic(err)
}
maxPort := minPort + n
for port := minPort; port <= maxPort; port += 1 {
ports = append(ports, port)
}
} else if portStrs := portRegex().FindStringSubmatch(portsStr); portStrs != nil {
port, err := strconv.Atoi(portStrs[1])
if err != nil {
panic(err)
}
ports = append(ports, port)
} else {
return nil, fmt.Errorf("Port must be either int min-max or int port (%s)", portsStr)
}
}
return ports, nil
}
func RequireExpandPorts(portsListStr string) []int {
ports, err := ExpandPorts(portsListStr)
if err != nil {
panic(err)
}
return ports
}
func CollapsePorts(ports []int) string {
parts := []string{}
slices.Sort(ports)
for i := 0; i < len(ports); {
j := i + 1
for j < len(ports) && (ports[j] == ports[j-1]+1 || ports[j] == ports[j-1]) {
j += 1
}
if ports[i] == ports[j-1] {
parts = append(parts, fmt.Sprintf("%d", ports[i]))
} else {
parts = append(parts, fmt.Sprintf("%d-%d", ports[i], ports[j-1]))
}
i = j
}
return strings.Join(parts, ",")
}
func ToSdkId(id Id) *sdk.Id {
sdkId, err := sdk.IdFromBytes(id.Bytes())
if err != nil {
panic(err)
}
return sdkId
}
type PostFunction = func() any
func RunPosts(ctx context.Context, posts ...PostFunction) {
for 0 < len(posts) {
// run all posts in parallel
next := make(chan any)
for _, post := range posts {
go HandleError(func() {
success := false
defer func() {
if !success {
select {
case <-ctx.Done():
case next <- nil:
}
}
}()
select {
case <-ctx.Done():
case next <- post():
success = true
}
})
}
var nextPosts []PostFunction
for range len(posts) {
select {
case <-ctx.Done():
return
case r := <-next:
if r != nil {
switch v := r.(type) {
case []PostFunction:
nextPosts = append(nextPosts, v...)
case PostFunction:
nextPosts = append(nextPosts, v)
}
}
}
}
posts = nextPosts
}
}