-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_wellknown.go
141 lines (125 loc) · 4.12 KB
/
gen_wellknown.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package fauxrpc
import (
"strings"
"time"
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
)
func durationSimple(opts GenOptions) *durationpb.Duration {
duration := time.Duration(opts.fake().Uint64() % uint64(30*time.Hour*24))
return durationpb.New(duration)
}
// GoogleDuration generates a random google.protobuf.Duration value.
func GoogleDuration(fd protoreflect.FieldDescriptor, opts GenOptions) *durationpb.Duration {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
return durationSimple(opts)
}
rules := constraints.GetDuration()
if rules == nil {
return durationSimple(opts)
}
if rules.Const != nil {
return rules.Const
}
if len(rules.Example) > 0 {
return rules.Example[opts.fake().IntRange(0, len(rules.Example)-1)]
}
minVal, maxVal := time.Duration(0), time.Duration(30*24*time.Hour)
if rules.GreaterThan != nil {
switch v := rules.GreaterThan.(type) {
case *validate.DurationRules_Gt:
minVal = v.Gt.AsDuration() + 1
case *validate.DurationRules_Gte:
minVal = v.Gte.AsDuration()
}
}
if rules.LessThan != nil {
switch v := rules.LessThan.(type) {
case *validate.DurationRules_Lt:
maxVal = v.Lt.AsDuration() - 1
case *validate.DurationRules_Lte:
maxVal = v.Lte.AsDuration()
}
}
if len(rules.In) > 0 {
return rules.In[opts.fake().IntRange(0, len(rules.In)-1)]
}
return durationpb.New(time.Duration(opts.fake().IntRange(int(minVal), int(maxVal))))
}
func generateTimestampSimple(opts GenOptions) *timestamppb.Timestamp {
return timestamppb.New(opts.fake().Date())
}
// GoogleTimestamp generates a random google.protobuf.Timestamp value.
func GoogleTimestamp(fd protoreflect.FieldDescriptor, opts GenOptions) *timestamppb.Timestamp {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
return generateTimestampSimple(opts)
}
rules := constraints.GetTimestamp()
if rules == nil {
return generateTimestampSimple(opts)
}
if rules.Const != nil {
return rules.Const
}
if len(rules.Example) > 0 {
return rules.Example[opts.fake().IntRange(0, len(rules.Example)-1)]
}
minVal, maxVal := time.Now().Add(20*365*24*time.Hour), time.Now().Add(10*365*24*time.Hour)
if rules.GreaterThan != nil {
switch v := rules.GreaterThan.(type) {
case *validate.TimestampRules_Gt:
minVal = v.Gt.AsTime().Add(1)
case *validate.TimestampRules_Gte:
minVal = v.Gte.AsTime()
}
}
if rules.LessThan != nil {
switch v := rules.LessThan.(type) {
case *validate.TimestampRules_Lt:
maxVal = v.Lt.AsTime().Add(-1)
case *validate.TimestampRules_Lte:
maxVal = v.Lte.AsTime()
}
}
if rules.Within != nil {
minVal = time.Now().Add(-rules.Within.AsDuration())
maxVal = time.Now().Add(rules.Within.AsDuration())
}
min := minVal.UnixNano()
max := maxVal.UnixNano()
delta := max - min
return timestamppb.New(time.Unix(0, (opts.fake().Int64()%delta)+min))
}
func GoogleValue(fd protoreflect.FieldDescriptor, opts GenOptions) *structpb.Value {
options := []func() *structpb.Value{
func() *structpb.Value { return structpb.NewNullValue() },
func() *structpb.Value { return structpb.NewBoolValue(Bool(fd, opts)) },
func() *structpb.Value { return structpb.NewNumberValue(Float64(fd, opts)) },
func() *structpb.Value { return structpb.NewStringValue(String(fd, opts)) },
func() *structpb.Value {
list := &structpb.ListValue{}
itemCount := opts.fake().IntRange(0, 4)
for i := 0; i < itemCount; i++ {
list.Values = append(list.Values, GoogleValue(fd, opts.nested()))
}
return structpb.NewListValue(list)
},
func() *structpb.Value {
obj := &structpb.Struct{
Fields: map[string]*structpb.Value{},
}
itemCount := opts.fake().IntRange(0, 4)
for i := 0; i < itemCount; i++ {
obj.Fields[strings.ToLower(opts.fake().Word())] = GoogleValue(fd, opts.nested())
}
return structpb.NewStructValue(obj)
},
}
fn := options[opts.fake().IntRange(0, len(options)-1)]
return fn()
}