-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.go
130 lines (105 loc) · 3.22 KB
/
number.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
package chaff
import (
"errors"
"math"
"github.com/ryanolee/go-chaff/rand"
)
type (
numberGeneratorType string
numberGenerator struct {
Type numberGeneratorType
Min float64
Max float64
MultipleOf float64
}
)
const (
infinitesimal = math.SmallestNonzeroFloat64
generatorTypeInteger numberGeneratorType = "integer"
generatorTypeNumber numberGeneratorType = "number"
defaultOffset = 10
)
// Parses the "type" keyword of a schema when it is a "number" or "integer"
// Example:
// {
// "type": "number",
// "minimum": 0,
// "maximum": 100
// "multipleOf": 10
// }
func parseNumber(node schemaNode, genType numberGeneratorType) (Generator, error) {
var min float64
var max float64
// Initial Validation
if node.Minimum != 0 && node.ExclusiveMinimum != 0 {
return nullGenerator{}, errors.New("cannot have both minimum and exclusive minimum")
}
if node.Maximum != 0 && node.ExclusiveMaximum != 0 {
return nullGenerator{}, errors.New("cannot have both maximum and exclusive maximum")
}
// Set min and max
if node.Minimum != 0 {
min = float64(node.Minimum)
} else if node.ExclusiveMinimum != 0 {
min = float64(node.ExclusiveMinimum) + infinitesimal
}
if node.Maximum != 0 {
max = float64(node.Maximum)
} else if node.ExclusiveMaximum != 0 {
max = float64(node.ExclusiveMaximum) - infinitesimal
} else if min != 0 {
max = min + defaultOffset
} else {
max = defaultOffset
}
// Validate min and max
if min > max {
return nullGenerator{}, errors.New("minimum cannot be greater than maximum")
}
// Validate multipleOf
if node.MultipleOf != 0 {
if node.MultipleOf <= 0 {
return nullGenerator{}, errors.New("multipleOf cannot be negative or zero")
}
multiplesInRange := countMultiplesInRange(min, max, node.MultipleOf)
if multiplesInRange == 0 {
return nullGenerator{}, errors.New("minimum and maximum do not allow for any multiples of multipleOf")
}
}
return &numberGenerator{
Type: genType,
Min: min,
Max: max,
MultipleOf: node.MultipleOf,
}, nil
}
func countMultiplesInRange(min float64, max float64, multiple float64) int {
if min == 0 {
return int(math.Floor(max / multiple))
}
return int(math.Floor(max / multiple)) - int(math.Floor(min / multiple))
}
func generateMultipleOf(rand rand.RandUtil, min float64, max float64, multiple float64) float64{
multiplesInRange := countMultiplesInRange(min, max, multiple)
if multiplesInRange == 0 {
return 0
}
lowerBound := math.Floor(min / multiple) * multiple
randomMultiple := float64(rand.RandomInt(1, multiplesInRange)) * multiple
return lowerBound + randomMultiple
}
func (g *numberGenerator) Generate(opts *GeneratorOptions) interface{} {
if g.Type == generatorTypeInteger && g.MultipleOf != 0 {
return int(generateMultipleOf(*opts.Rand, g.Min, g.Max, g.MultipleOf))
} else if g.Type == generatorTypeInteger && g.MultipleOf == 0 {
return int(math.Round(opts.Rand.RandomFloat(g.Min, g.Max)))
} else if g.Type == generatorTypeNumber && g.MultipleOf != 0 {
return generateMultipleOf(*opts.Rand, g.Min, g.Max, g.MultipleOf)
} else if g.Type == generatorTypeNumber && g.MultipleOf == 0 {
return opts.Rand.RandomFloat(g.Min, g.Max)
}
return 0
}
func (g *numberGenerator) String() string {
return "NumberGenerator"
}