-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_strings.go
139 lines (126 loc) · 3.99 KB
/
gen_strings.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
package fauxrpc
import (
"math"
"strconv"
"strings"
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"google.golang.org/protobuf/reflect/protoreflect"
)
func randInt64GeometricDist(p float64, opts GenOptions) int64 {
return int64(math.Floor(math.Log(opts.fake().Float64()) / math.Log(1.0-p)))
}
func stringSimple(fd protoreflect.FieldDescriptor, opts GenOptions) string {
lowerName := strings.ToLower(string(fd.Name()))
switch {
case strings.Contains(lowerName, "firstname"):
return opts.fake().FirstName()
case strings.Contains(lowerName, "lastname"):
return opts.fake().LastName()
case strings.Contains(lowerName, "name"):
return opts.fake().FirstName()
case strings.Contains(lowerName, "fullname"):
return opts.fake().Name()
case strings.Contains(lowerName, "id"):
return opts.fake().UUID()
case strings.Contains(lowerName, "token"):
return opts.fake().UUID()
case strings.Contains(lowerName, "photo") && strings.Contains(lowerName, "url"):
return "https://picsum.photos/400"
case strings.Contains(lowerName, "url"):
return opts.fake().URL()
case strings.Contains(lowerName, "version"):
return opts.fake().AppVersion()
case strings.Contains(lowerName, "status"):
return opts.fake().RandomString([]string{"active", "inactive", "hidden", "archived", "deleted", "pending"})
}
return opts.fake().HipsterSentence(int(randInt64GeometricDist(0.5, opts) + 1))
}
// String returns a fake string value given a field descriptor.
func String(fd protoreflect.FieldDescriptor, opts GenOptions) string {
constraints := getFieldConstraints(fd, opts)
if constraints == nil {
return stringSimple(fd, opts)
}
rules := constraints.GetString_()
if rules == nil {
return stringSimple(fd, opts)
}
if rules == nil {
return stringSimple(fd, opts)
}
if rules.Const != nil {
return *rules.Const
}
if len(rules.Example) > 0 {
return rules.Example[opts.fake().IntRange(0, len(rules.Example)-1)]
}
minLen, maxLen := uint64(0), uint64(20)
if rules.Len != nil {
minLen = *rules.Len
maxLen = *rules.Len
}
if rules.MinLen != nil {
minLen = *rules.MinLen
}
if rules.MaxLen != nil {
maxLen = *rules.MaxLen
}
if rules.MinBytes != nil {
maxLen = *rules.MinBytes
}
if rules.MaxBytes != nil {
maxLen = *rules.MaxBytes
}
if rules.Pattern != nil {
return opts.fake().Regex(*rules.Pattern)
}
if len(rules.In) > 0 {
return opts.fake().RandomString(rules.In)
}
if rules.WellKnown != nil {
switch rules.WellKnown.(type) {
case *validate.StringRules_Email:
return opts.fake().Email()
case *validate.StringRules_Hostname:
return strings.ToLower(opts.fake().JobDescriptor())
case *validate.StringRules_Ip:
return opts.fake().IPv4Address()
case *validate.StringRules_Ipv4:
return opts.fake().IPv4Address()
case *validate.StringRules_Ipv6:
return opts.fake().IPv6Address()
case *validate.StringRules_Uri:
return opts.fake().URL()
case *validate.StringRules_Address:
return opts.fake().DomainName()
case *validate.StringRules_Uuid:
return opts.fake().UUID()
case *validate.StringRules_Tuuid:
return strings.ReplaceAll(opts.fake().UUID(), "-", "")
case *validate.StringRules_IpWithPrefixlen:
return opts.fake().IPv4Address() + "/30"
case *validate.StringRules_Ipv4WithPrefixlen:
return opts.fake().IPv4Address() + "/30"
case *validate.StringRules_Ipv6Prefix:
return opts.fake().IPv6Address() + "/64"
case *validate.StringRules_HostAndPort:
return strings.ToLower(opts.fake().JobDescriptor()) + ":" + strconv.FormatInt(int64(opts.fake().IntRange(443, 9000)), 10)
case *validate.StringRules_WellKnownRegex:
}
}
return generateHipsterText(minLen, maxLen, opts)
}
func generateHipsterText(minLen, maxLen uint64, opts GenOptions) string {
b := &strings.Builder{}
addMoreText := func() {
b.WriteString(opts.fake().HipsterSentence(int(randInt64GeometricDist(0.5, opts)) + 1))
}
addMoreText()
for uint64(b.Len()) < minLen {
addMoreText()
}
if uint64(b.Len()) > maxLen {
return b.String()[:maxLen-1]
}
return b.String()
}