@@ -2,8 +2,9 @@ package fuzz
2
2
3
3
import (
4
4
"fmt"
5
- "google.golang.org/protobuf/reflect/protoreflect"
6
5
"math"
6
+
7
+ "google.golang.org/protobuf/reflect/protoreflect"
7
8
"pgregory.net/rapid"
8
9
)
9
10
@@ -66,7 +67,7 @@ func (g *generator) generate() {
66
67
case true :
67
68
g .field (fd )
68
69
default :
69
- genField := rapid .Bool ().Draw (g .t , fmt .Sprintf ("skip field generation: %s" , fd .FullName ())).( bool )
70
+ genField := rapid .Bool ().Draw (g .t , fmt .Sprintf ("skip field generation: %s" , fd .FullName ()))
70
71
if ! genField {
71
72
continue
72
73
}
@@ -83,7 +84,7 @@ func (g *generator) field(fd protoreflect.FieldDescriptor) {
83
84
return
84
85
}
85
86
// check if we can set an invalid value
86
- if g .invalidValue && rapid .Bool ().Draw (g .t , fmt .Sprintf ("generate invalid value for field %s" , fd .FullName ())).( bool ) {
87
+ if g .invalidValue && rapid .Bool ().Draw (g .t , fmt .Sprintf ("generate invalid value for field %s" , fd .FullName ())) {
87
88
g .m .Set (fd , protoreflect.Value {})
88
89
}
89
90
@@ -103,7 +104,7 @@ func isOneof(fd protoreflect.FieldDescriptor) bool {
103
104
104
105
func (g * generator ) list (fd protoreflect.FieldDescriptor ) {
105
106
list := g .m .NewField (fd ).List ()
106
- length := rapid .IntRange (0 , g .maxListLength ).Draw (g .t , fmt .Sprintf ("list length for %s" , fd .FullName ())).( int )
107
+ length := rapid .IntRange (0 , g .maxListLength ).Draw (g .t , fmt .Sprintf ("list length for %s" , fd .FullName ()))
107
108
108
109
for i := 0 ; i < length ; i ++ {
109
110
switch fd .Kind () {
@@ -124,7 +125,7 @@ func (g *generator) mapp(fd protoreflect.FieldDescriptor) {
124
125
125
126
mapValue := g .m .NewField (fd ).Map ()
126
127
127
- length := rapid .IntRange (0 , g .maxMapLength ).Draw (g .t , "map length for " + string (fd .FullName ())).( int )
128
+ length := rapid .IntRange (0 , g .maxMapLength ).Draw (g .t , "map length for " + string (fd .FullName ()))
128
129
129
130
for i := 0 ; i < length ; i ++ {
130
131
keyValue := protoreflect .MapKey (g .valueFor (keyDesc ))
@@ -161,43 +162,43 @@ func (g *generator) valueFor(fd protoreflect.FieldDescriptor) protoreflect.Value
161
162
switch fd .Kind () {
162
163
// bool kind
163
164
case protoreflect .BoolKind :
164
- value := rapid .Bool ().Draw (g .t , label (fd )).( bool )
165
+ value := rapid .Bool ().Draw (g .t , label (fd ))
165
166
return protoreflect .ValueOfBool (value )
166
167
// int32 kinds
167
168
case protoreflect .Int32Kind , protoreflect .Sint32Kind , protoreflect .Sfixed32Kind :
168
- value := rapid .Int32 ().Draw (g .t , label (fd )).( int32 )
169
+ value := rapid .Int32 ().Draw (g .t , label (fd ))
169
170
return protoreflect .ValueOfInt32 (value )
170
171
// int64 kinds
171
172
case protoreflect .Int64Kind , protoreflect .Sint64Kind , protoreflect .Sfixed64Kind :
172
- value := rapid .Int64 ().Draw (g .t , label (fd )).( int64 )
173
+ value := rapid .Int64 ().Draw (g .t , label (fd ))
173
174
return protoreflect .ValueOfInt64 (value )
174
175
// uint32 kinds
175
176
case protoreflect .Uint32Kind , protoreflect .Fixed32Kind :
176
- value := rapid .Uint32 ().Draw (g .t , label (fd )).( uint32 )
177
+ value := rapid .Uint32 ().Draw (g .t , label (fd ))
177
178
return protoreflect .ValueOfUint32 (value )
178
179
// uint64 kinds
179
180
case protoreflect .Uint64Kind , protoreflect .Fixed64Kind :
180
- value := rapid .Uint64 ().Draw (g .t , label (fd )).( uint64 )
181
+ value := rapid .Uint64 ().Draw (g .t , label (fd ))
181
182
return protoreflect .ValueOfUint64 (value )
182
183
// float32 kind
183
184
case protoreflect .FloatKind :
184
- value := rapid .Float32Max (math .MaxFloat32 ).Draw (g .t , label (fd )).( float32 )
185
+ value := rapid .Float32Max (math .MaxFloat32 ).Draw (g .t , label (fd ))
185
186
return protoreflect .ValueOfFloat32 (value )
186
187
// float64 kind
187
188
case protoreflect .DoubleKind :
188
- value := rapid .Float64 ().Draw (g .t , label (fd )).( float64 )
189
+ value := rapid .Float64 ().Draw (g .t , label (fd ))
189
190
return protoreflect .ValueOfFloat64 (value )
190
191
// string kind
191
192
case protoreflect .StringKind :
192
- value := rapid .String ().Draw (g .t , label (fd )).( string )
193
+ value := rapid .String ().Draw (g .t , label (fd ))
193
194
return protoreflect .ValueOfString (value )
194
195
// bytes kind
195
196
case protoreflect .BytesKind :
196
197
value := randomBytes (g .t , fd )
197
198
return protoreflect .ValueOfBytes (value )
198
199
// enum kind
199
200
case protoreflect .EnumKind :
200
- enumIndex := rapid .IntRange (0 , fd .Enum ().Values ().Len ()- 1 ).Draw (g .t , "random enum index for " + string (fd .FullName ())).( int )
201
+ enumIndex := rapid .IntRange (0 , fd .Enum ().Values ().Len ()- 1 ).Draw (g .t , "random enum index for " + string (fd .FullName ()))
201
202
enum := fd .Enum ().Values ().Get (enumIndex )
202
203
return protoreflect .ValueOfEnum (enum .Number ())
203
204
default :
@@ -231,7 +232,7 @@ func (g *generator) decideOneofs() {
231
232
md := g .typ .Descriptor ()
232
233
for i := 0 ; i < md .Oneofs ().Len (); i ++ {
233
234
oneof := md .Oneofs ().Get (i )
234
- index := rapid .IntRange (0 , oneof .Fields ().Len ()- 1 ).Draw (g .t , "deciding oneof field for: " + string (oneof .FullName ())).( int )
235
+ index := rapid .IntRange (0 , oneof .Fields ().Len ()- 1 ).Draw (g .t , "deciding oneof field for: " + string (oneof .FullName ()))
235
236
decidedFd := oneof .Fields ().Get (index )
236
237
g .pickedOneofs [oneof .FullName ()] = decidedFd .FullName ()
237
238
}
@@ -248,6 +249,6 @@ func label(fd protoreflect.FieldDescriptor) string {
248
249
}
249
250
250
251
func randomBytes (t * rapid.T , fd protoreflect.FieldDescriptor ) []byte {
251
- size := rapid .IntRange (0 , MaxBytesArraySize ).Draw (t , "bytes slice size for %s" + string (fd .FullName ())).( int )
252
- return rapid .SliceOfN (rapid .Byte (), 0 , size ).Draw (t , "bytes slice for %s" + string (fd .FullName ())).([] byte )
252
+ size := rapid .IntRange (0 , MaxBytesArraySize ).Draw (t , "bytes slice size for %s" + string (fd .FullName ()))
253
+ return rapid .SliceOfN (rapid .Byte (), 0 , size ).Draw (t , "bytes slice for %s" + string (fd .FullName ()))
253
254
}
0 commit comments