-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.go
49 lines (42 loc) · 876 Bytes
/
convert.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
package rsmq
import (
"math/rand"
"strconv"
"strings"
)
func convertStringToInt[n int | int64](s any) n {
r, err := strconv.ParseInt(s.(string), 10, 0)
if err != nil {
panic(err)
}
return n(r)
}
func convertStringToUint[n uint | uint64](s any) n {
r, err := strconv.ParseUint(s.(string), 10, 0)
if err != nil {
panic(err)
}
return n(r)
}
func convertIntToUint(s any) uint64 {
return uint64(s.(int64))
}
func convertStringToUnsignedOrDefault[n uint | uint64](s any, def n) n {
if s == nil {
return def
}
r, err := strconv.ParseUint(s.(string), 10, 0)
if err != nil {
panic(err)
}
return n(r)
}
const idLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
func makeID(n int) string {
sb := strings.Builder{}
ln := len(idLetters)
for i := 0; i < n; i++ {
sb.WriteByte(idLetters[rand.Int()%ln])
}
return sb.String()
}