-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
143 lines (123 loc) · 2.87 KB
/
utils.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
package stableinterfaces
import (
"crypto/sha256"
"encoding/base32"
"fmt"
gonanoid "github.com/matoous/go-nanoid/v2"
"github.com/segmentio/ksuid"
"os"
"regexp"
"strconv"
"strings"
)
func getEnvOrDefault(env, defaultVal string) string {
e := os.Getenv(env)
if e == "" {
return defaultVal
} else {
return e
}
}
func getEnvOrDefaultInt(env string, defaultVal int64) int64 {
e := os.Getenv(env)
if e == "" {
return defaultVal
} else {
intVal, err := strconv.ParseInt(e, 10, 16)
if err != nil {
return 0
}
return intVal
}
}
// Entirely gpt-4 generated, seems to work!
func expandRangePattern(input string) ([]string, error) {
// Find all matches for the pattern `{number..number}`
re := regexp.MustCompile(`\{(\d+)\.\.(\d+)\}`)
matches := re.FindAllStringSubmatch(input, -1)
// If no matches, return the input as the only element in the slice
if len(matches) == 0 {
return []string{input}, nil
}
// Assuming we only handle the first range for simplicity
// Further logic could be added to handle multiple ranges
for _, match := range matches {
start, err := strconv.Atoi(match[1])
if err != nil {
return nil, err
}
end, err := strconv.Atoi(match[2])
if err != nil {
return nil, err
}
// Generate the expanded strings
var expandedStrings []string
for i := start; i <= end; i++ {
expandedString := re.ReplaceAllString(input, strconv.Itoa(i))
expandedStrings = append(expandedStrings, expandedString)
}
return expandedStrings, nil
}
return nil, nil
}
// extracted from franz-go
func Murmur2(b []byte) uint32 {
const (
seed uint32 = 0x9747b28c
m uint32 = 0x5bd1e995
r = 24
)
h := seed ^ uint32(len(b))
for len(b) >= 4 {
k := uint32(b[3])<<24 + uint32(b[2])<<16 + uint32(b[1])<<8 + uint32(b[0])
b = b[4:]
k *= m
k ^= k >> r
k *= m
h *= m
h ^= k
}
switch len(b) {
case 3:
h ^= uint32(b[2]) << 16
fallthrough
case 2:
h ^= uint32(b[1]) << 8
fallthrough
case 1:
h ^= uint32(b[0])
h *= m
}
h ^= h >> 13
h *= m
h ^= h >> 15
return h
}
func instanceInternalIDToShard(internalID string, numShards int) uint32 {
return Murmur2([]byte(internalID)) % uint32(numShards)
}
// TruncatedSHA256 truncates the SHA256 hash to 32 characters
func TruncatedSHA256(id string) (string, error) {
h := sha256.New()
_, err := h.Write([]byte(id))
if err != nil {
return "", fmt.Errorf("error in h.Write: %w", err)
}
// Use a truncated hash
return strings.ReplaceAll(base32.StdEncoding.EncodeToString(h.Sum(nil)[:32]), "=", ""), nil
}
func ptr[T any](s T) *T {
return &s
}
func genRandomID(prefix string) string {
return prefix + gonanoid.MustGenerate("abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMONPQRSTUVWXYZ0123456789", 22)
}
func genKSortedID(prefix string) string {
return prefix + ksuid.New().String()
}
func deref[T any](ref *T, fallback T) T {
if ref == nil {
return fallback
}
return *ref
}