-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.go
96 lines (84 loc) · 2.08 KB
/
funcs.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
package gen
import (
"math"
"math/rand"
"strconv"
"strings"
"github.com/google/uuid"
)
// Pad returns a padding string of spaces sized to fill the difference in length
// between string a and string b.
func Pad(a, b string) string {
cl := 0
if len(a) < len(b) {
cl = len(b) - len(a)
}
out := make([]byte, cl)
for i := range out {
out[i] = ' '
}
return string(out)
}
// IsBaseType returns whether or not the given type string is one of Go's built
// in types.
func IsBaseType(k string) bool {
return k == "bool" ||
k == "int" ||
k == "int8" ||
k == "int16" ||
k == "int32" ||
k == "int64" ||
k == "uint" ||
k == "uint8" ||
k == "uint16" ||
k == "uint32" ||
k == "uint64" ||
k == "float32" ||
k == "float64" ||
k == "complex64" ||
k == "complex128" ||
k == "string" ||
k == "byte" ||
k == "rune" ||
k == "interface{}"
}
func Quote(val string) string {
if len(val) == 0 {
return `""`
}
if len(val) == 1 && val[0] == '"' {
return `"\""`
}
return `"` + strings.Trim(val, `"`) + `"`
}
func DefaultValue(kind string) string {
switch kind {
case "bool":
return strconv.FormatBool(rand.Intn(1) == 1)
case "int", "int32", "rune":
return strconv.FormatInt(int64(rand.Int31n(math.MaxInt32)), 10)
case "int8":
return strconv.FormatInt(int64(rand.Intn(math.MaxInt8)), 10)
case "int16":
return strconv.FormatInt(int64(rand.Intn(math.MaxInt16)), 10)
case "int64":
return strconv.FormatInt(rand.Int63n(math.MaxInt64), 10)
case "uint", "uint32", "complex64", "complex128":
return strconv.FormatUint(uint64(rand.Uint32()), 10)
case "uint8", "byte":
return strconv.FormatUint(uint64(rand.Intn(math.MaxUint8)), 10)
case "uint16":
return strconv.FormatUint(uint64(rand.Intn(math.MaxUint16)), 10)
case "uint64":
return strconv.FormatUint(rand.Uint64(), 10)
case "float32":
return strconv.FormatFloat(float64(rand.Float32()), 'f', 3, 32)
case "float64":
return strconv.FormatFloat(rand.Float64(), 'f', 3, 64)
case "string":
return `"` + uuid.New().String() + `"`
case "interface{}":
return `"` + uuid.New().String() + `"`
}
panic("wat")
}