-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_uint.go
180 lines (158 loc) · 4.28 KB
/
gen_uint.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
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
package fauxrpc
import (
"math"
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"google.golang.org/protobuf/reflect/protoreflect"
)
// UInt32 returns a fake uint32 value given a field descriptor.
func UInt32(fd protoreflect.FieldDescriptor, opts GenOptions) uint32 {
constraints := getFieldConstraints(fd, opts)
if constraints == nil {
return opts.fake().Uint32()
}
rules := constraints.GetUint32()
if rules == nil {
return opts.fake().Uint32()
}
if rules.Const != nil {
return *rules.Const
}
if len(rules.Example) > 0 {
return rules.Example[opts.fake().IntRange(0, len(rules.Example)-1)]
}
minVal, maxVal := uint32(0), uint32(math.MaxInt32)
if rules.GreaterThan != nil {
switch v := rules.GreaterThan.(type) {
case *validate.UInt32Rules_Gt:
minVal = v.Gt + 1
case *validate.UInt32Rules_Gte:
minVal = v.Gte
}
}
if rules.LessThan != nil {
switch v := rules.LessThan.(type) {
case *validate.UInt32Rules_Lt:
maxVal = v.Lt - 1
case *validate.UInt32Rules_Lte:
maxVal = v.Lte
}
}
if len(rules.In) > 0 {
return rules.In[opts.fake().IntRange(0, len(rules.In)-1)]
}
return uint32(opts.fake().IntRange(int(minVal), int(maxVal)))
}
// UInt64 returns a fake uint64 value given a field descriptor.
func UInt64(fd protoreflect.FieldDescriptor, opts GenOptions) uint64 {
constraints := getFieldConstraints(fd, opts)
if constraints == nil {
return opts.fake().Uint64()
}
rules := constraints.GetUint64()
if rules == nil {
return opts.fake().Uint64()
}
if rules.Const != nil {
return *rules.Const
}
if len(rules.Example) > 0 {
return rules.Example[opts.fake().IntRange(0, len(rules.Example)-1)]
}
minVal, maxVal := uint64(0), uint64(math.MaxInt64)
if rules.GreaterThan != nil {
switch v := rules.GreaterThan.(type) {
case *validate.UInt64Rules_Gt:
minVal = v.Gt + 1
case *validate.UInt64Rules_Gte:
minVal = v.Gte
}
}
if rules.LessThan != nil {
switch v := rules.LessThan.(type) {
case *validate.UInt64Rules_Lt:
maxVal = v.Lt - 1
case *validate.UInt64Rules_Lte:
maxVal = v.Lte
}
}
if len(rules.In) > 0 {
return rules.In[opts.fake().IntRange(0, len(rules.In)-1)]
}
return uint64(opts.fake().UintRange(uint(minVal), uint(maxVal)))
}
// Fixed32 returns a fake fixed32 value given a field descriptor.
func Fixed32(fd protoreflect.FieldDescriptor, opts GenOptions) uint32 {
constraints := getFieldConstraints(fd, opts)
if constraints == nil {
return opts.fake().Uint32()
}
rules := constraints.GetFixed32()
if rules == nil {
return opts.fake().Uint32()
}
if rules.Const != nil {
return *rules.Const
}
if len(rules.Example) > 0 {
return rules.Example[opts.fake().IntRange(0, len(rules.Example)-1)]
}
minVal, maxVal := uint32(0), uint32(math.MaxInt32)
if rules.GreaterThan != nil {
switch v := rules.GreaterThan.(type) {
case *validate.Fixed32Rules_Gt:
minVal = v.Gt + 1
case *validate.Fixed32Rules_Gte:
minVal = v.Gte
}
}
if rules.LessThan != nil {
switch v := rules.LessThan.(type) {
case *validate.Fixed32Rules_Lt:
maxVal = v.Lt - 1
case *validate.Fixed32Rules_Lte:
maxVal = v.Lte
}
}
if len(rules.In) > 0 {
return rules.In[opts.fake().IntRange(0, len(rules.In)-1)]
}
return uint32(opts.fake().IntRange(int(minVal), int(maxVal)))
}
// Fixed64 returns a fake fixed64 value given a field descriptor.
func Fixed64(fd protoreflect.FieldDescriptor, opts GenOptions) uint64 {
constraints := getFieldConstraints(fd, opts)
if constraints == nil {
return opts.fake().Uint64()
}
rules := constraints.GetFixed64()
if rules == nil {
return opts.fake().Uint64()
}
if rules.Const != nil {
return *rules.Const
}
if len(rules.Example) > 0 {
return rules.Example[opts.fake().IntRange(0, len(rules.Example)-1)]
}
minVal, maxVal := uint64(0), uint64(math.MaxInt64)
if rules.GreaterThan != nil {
switch v := rules.GreaterThan.(type) {
case *validate.Fixed64Rules_Gt:
minVal = v.Gt + 1
case *validate.Fixed64Rules_Gte:
minVal = v.Gte
}
}
if rules.LessThan != nil {
switch v := rules.LessThan.(type) {
case *validate.Fixed64Rules_Lt:
maxVal = v.Lt - 1
case *validate.Fixed64Rules_Lte:
maxVal = v.Lte
}
}
if len(rules.In) > 0 {
return rules.In[opts.fake().IntRange(0, len(rules.In)-1)]
}
return uint64(opts.fake().UintRange(uint(minVal), uint(maxVal)))
}