-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
96 lines (74 loc) · 3.12 KB
/
generator.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
package chaff
import (
"fmt"
"github.com/ryanolee/go-chaff/internal/util"
"github.com/ryanolee/go-chaff/rand"
)
type (
Generator interface {
fmt.Stringer
Generate(*GeneratorOptions) interface{}
}
GeneratorOptions struct {
// The source of randomness to use for the given generation.
// Please note that some parts of the generators use different sources of randomness.
// ("regex" generation and "format" strings)
Rand *rand.RandUtil
// The default minimum number value
DefaultNumberMinimum int
// The default maximum number value
DefaultNumberMaximum int
// The default minimum String length
DefaultStringMinLength int
// The default maximum String length
DefaultStringMaxLength int
// The default minimum array length
DefaultArrayMinItems int
// The default maximum array length
// This will be set min + this inf the event a minimum value is set
DefaultArrayMaxItems int
// The default minimum object properties (Will be ignored if there are fewer properties available)
DefaultObjectMinProperties int
// The default maximum object properties (Will be ignored if there are fewer properties available)
DefaultObjectMaxProperties int
// The maximum number of references to resolve at once (Default: 10)
MaximumReferenceDepth int
// In the event that schemas are recursive there is a good chance the generator
// can run forever. This option will bypass the check for cyclic references
// Please defer to the MaximumReferenceDepth option if possible when using this
BypassCyclicReferenceCheck bool
// Used to keep track of references during a resolution cycle (Used internally and can be ignored)
ReferenceResolver referenceResolver
// Though technically in some cases a schema may allow for additional
// values it might not always be desireable. this option suppresses fallback_n values
// so that they will only appear to make up a "minimum value" forces them to
SuppressFallbackValues bool
}
)
func withGeneratorOptionsDefaults(options GeneratorOptions) *GeneratorOptions {
randUtil := options.Rand
if options.Rand == nil {
randUtil = rand.NewRandUtilFromTime()
}
return &GeneratorOptions{
// General
Rand: randUtil,
// Number
DefaultNumberMinimum: util.GetInt(options.DefaultNumberMinimum, 0),
DefaultNumberMaximum: util.GetInt(options.DefaultNumberMaximum, 100),
// String
DefaultStringMinLength: util.GetInt(options.DefaultStringMinLength, 0),
DefaultStringMaxLength: util.GetInt(options.DefaultStringMaxLength, 100),
// Array
DefaultArrayMinItems: util.GetInt(options.DefaultArrayMinItems, 0),
DefaultArrayMaxItems: util.GetInt(options.DefaultArrayMaxItems, 10),
// Object
DefaultObjectMinProperties: util.GetInt(options.DefaultObjectMinProperties, 0),
DefaultObjectMaxProperties: util.GetInt(options.DefaultObjectMaxProperties, 10),
SuppressFallbackValues: util.GetBool(options.SuppressFallbackValues, true),
// References
BypassCyclicReferenceCheck: util.GetBool(options.BypassCyclicReferenceCheck, false),
MaximumReferenceDepth: util.GetInt(options.MaximumReferenceDepth, 10),
ReferenceResolver: referenceResolver{},
}
}