-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_bytes.go
49 lines (43 loc) · 1.12 KB
/
gen_bytes.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
package fauxrpc
import (
"github.com/brianvoe/gofakeit/v7"
"google.golang.org/protobuf/reflect/protoreflect"
)
func generateBytesSimple() []byte {
return []byte(gofakeit.HipsterSentence(3))
}
// Bytes returns a fake []byte value given a field descriptor.
func Bytes(fd protoreflect.FieldDescriptor, opts GenOptions) []byte {
constraints := getFieldConstraints(fd, opts)
if constraints == nil {
return generateBytesSimple()
}
rules := constraints.GetBytes()
if rules == nil {
return generateBytesSimple()
}
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.Pattern != nil {
return []byte(opts.fake().Regex(*rules.Pattern))
}
if len(rules.In) > 0 {
return rules.In[opts.fake().IntRange(0, len(rules.In)-1)]
}
return []byte(opts.fake().Sentence(int(maxLen / uint64(4)))[minLen:maxLen])
}