-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_repeated.go
44 lines (40 loc) · 1.29 KB
/
gen_repeated.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
package fauxrpc
import (
"google.golang.org/protobuf/reflect/protoreflect"
)
func repeatedSimple(msg protoreflect.Message, fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
listVal := msg.NewField(fd)
itemCount := opts.fake().IntRange(0, 4)
for i := 0; i < itemCount; i++ {
if v := FieldValue(fd, opts.nested()); v != nil {
listVal.List().Append(*v)
}
}
return &listVal
}
// Repeated returns a fake repeated value given a field descriptor.
func Repeated(msg protoreflect.Message, fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
return repeatedSimple(msg, fd, opts)
}
rules := constraints.GetEnum()
if rules == nil {
return repeatedSimple(msg, fd, opts)
}
min, max := uint64(0), uint64(4)
if constraints.GetRepeated().MinItems != nil {
min = constraints.GetRepeated().GetMinItems()
}
if constraints.GetRepeated().MaxItems != nil {
max = constraints.GetRepeated().GetMaxItems()
}
listVal := msg.NewField(fd)
itemCount := opts.fake().IntRange(int(min), int(max))
for i := 0; i < itemCount; i++ {
if v := FieldValue(fd, opts.nested().withExtraFieldConstraints(constraints.GetRepeated().Items)); v != nil {
listVal.List().Append(*v)
}
}
return &listVal
}