-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_msg.go
82 lines (74 loc) · 2.02 KB
/
gen_msg.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
package fauxrpc
import (
"github.com/sudorandom/fauxrpc/private/registry"
"google.golang.org/protobuf/reflect/protoreflect"
)
const defaultMaxDepth = 5
// NewMessage creates a new message populated with fake data given a protoreflect.MessageDescriptor
func NewMessage(md protoreflect.MessageDescriptor, opts GenOptions) (protoreflect.ProtoMessage, error) {
if opts.MaxDepth == 0 {
opts.MaxDepth = defaultMaxDepth
}
opts.GetContext()
msg := registry.NewMessage(md).Interface()
err := setDataOnMessage(msg, opts)
if err != nil {
return nil, err
}
return msg, nil
}
// SetDataOnMessage generates fake data given a protoreflect.ProtoMessage and sets the field values.
func SetDataOnMessage(msg protoreflect.ProtoMessage, opts GenOptions) error {
if opts.MaxDepth == 0 {
opts.MaxDepth = defaultMaxDepth
}
return setDataOnMessage(msg, opts)
}
func setDataOnMessage(pm protoreflect.ProtoMessage, opts GenOptions) error {
if opts.MaxDepth <= 0 {
return nil
}
msg := pm.ProtoReflect()
desc := msg.Descriptor()
oneOfFields := map[protoreflect.FullName]struct{}{}
oneOfs := desc.Oneofs()
// gather one-of fields
for i := 0; i < oneOfs.Len(); i++ {
oneOf := oneOfs.Get(i)
fields := oneOf.Fields()
for i := 0; i < fields.Len(); i++ {
field := fields.Get(i)
oneOfFields[field.FullName()] = struct{}{}
}
// pick oneOf the fields to create data for
options := oneOf.Fields()
idx := opts.fake().IntRange(0, options.Len()-1)
field := options.Get(idx)
if v := FieldValue(field, opts.nested()); v != nil {
msg.Set(field, *v)
}
}
fields := desc.Fields()
for i := 0; i < fields.Len(); i++ {
field := fields.Get(i)
if _, ok := oneOfFields[field.FullName()]; ok {
continue
}
if field.IsList() {
if val := Repeated(msg, field, opts); val != nil {
msg.Set(field, *val)
}
continue
}
if field.IsMap() {
if val := Map(msg, field, opts); val != nil {
msg.Set(field, *val)
}
continue
}
if v := FieldValue(field, opts.nested()); v != nil {
msg.Set(field, *v)
}
}
return nil
}