-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_float.go
89 lines (79 loc) · 2.1 KB
/
gen_float.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
package fauxrpc
import (
"math"
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"google.golang.org/protobuf/reflect/protoreflect"
)
// Float32 returns a fake float32 value given a field descriptor.
func Float32(fd protoreflect.FieldDescriptor, opts GenOptions) float32 {
constraints := getFieldConstraints(fd, opts)
if constraints == nil {
return opts.fake().Float32()
}
rules := constraints.GetFloat()
if rules == nil {
return opts.fake().Float32()
}
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 := float32(0), float32(math.MaxFloat32)
if rules.GreaterThan != nil {
switch v := rules.GreaterThan.(type) {
case *validate.FloatRules_Gt:
minVal = v.Gt + 1
case *validate.FloatRules_Gte:
minVal = v.Gte
}
}
if rules.LessThan != nil {
switch v := rules.LessThan.(type) {
case *validate.FloatRules_Lt:
maxVal = v.Lt - 1
case *validate.FloatRules_Lte:
maxVal = v.Lte
}
}
if len(rules.In) > 0 {
return rules.In[opts.fake().IntRange(0, len(rules.In)-1)]
}
return opts.fake().Float32Range(minVal, maxVal)
}
// Float64 returns a fake float64 value given a field descriptor.
func Float64(fd protoreflect.FieldDescriptor, opts GenOptions) float64 {
constraints := getFieldConstraints(fd, opts)
if constraints == nil {
return opts.fake().Float64()
}
rules := constraints.GetDouble()
if rules == nil {
return opts.fake().Float64()
}
if rules.Const != nil {
return *rules.Const
}
minVal, maxVal := float64(0), float64(math.MaxFloat64)
if rules.GreaterThan != nil {
switch v := rules.GreaterThan.(type) {
case *validate.DoubleRules_Gt:
minVal = v.Gt + 1
case *validate.DoubleRules_Gte:
minVal = v.Gte
}
}
if rules.LessThan != nil {
switch v := rules.LessThan.(type) {
case *validate.DoubleRules_Lt:
maxVal = v.Lt - 1
case *validate.DoubleRules_Lte:
maxVal = v.Lte
}
}
if len(rules.In) > 0 {
return rules.In[opts.fake().IntRange(0, len(rules.In)-1)]
}
return opts.fake().Float64Range(minVal, maxVal)
}