-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen.go
58 lines (45 loc) · 1.4 KB
/
gen.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
package fauxrpc
import (
"errors"
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"google.golang.org/protobuf/reflect/protoreflect"
)
var (
ErrNotFaked = errors.New("no data was faked either because there was no relevant data or the faker was just out of faker juice")
)
type ProtoFaker interface {
SetDataOnMessage(msg protoreflect.ProtoMessage, opts GenOptions) error
}
type fauxFaker struct{}
func NewFauxFaker() *fauxFaker {
return &fauxFaker{}
}
func (f *fauxFaker) SetDataOnMessage(msg protoreflect.ProtoMessage, opts GenOptions) error {
return SetDataOnMessage(msg, opts)
}
type multiFaker []ProtoFaker
func NewMultiFaker(fakers []ProtoFaker) multiFaker {
return multiFaker(fakers)
}
func (f multiFaker) SetDataOnMessage(msg protoreflect.ProtoMessage, opts GenOptions) error {
for _, faker := range f {
if err := faker.SetDataOnMessage(msg, opts); err != nil {
if !errors.Is(err, ErrNotFaked) {
return err
}
} else {
return nil
}
}
return ErrNotFaked
}
func (st GenOptions) withExtraFieldConstraints(constraints *validate.FieldConstraints) GenOptions {
st.extraFieldConstraints = constraints
return st
}
func getFieldConstraints(fd protoreflect.FieldDescriptor, opts GenOptions) *validate.FieldConstraints {
if constraints := getResolver().ResolveFieldConstraints(fd); constraints != nil {
return constraints
}
return opts.extraFieldConstraints
}