-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmultiple_type.go
More file actions
49 lines (41 loc) · 1.12 KB
/
Copy pathmultiple_type.go
File metadata and controls
49 lines (41 loc) · 1.12 KB
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
package chaff
import (
"fmt"
"strings"
"github.com/thoas/go-funk"
)
type (
multipleTypeGenerator struct {
generators []Generator
}
)
// Parses the "type" keyword of a schema when it is an array
// Example:
//
// {
// "type": ["string", "number"]
// }
func parseMultipleType(node schemaNode, metadata *parserMetadata) (multipleTypeGenerator, error) {
generators := []Generator{}
for _, nodeType := range node.Type.MultipleTypes {
generator, err := parseType(nodeType, node, metadata)
if err != nil {
generators = append(generators, nullGenerator{})
} else {
generators = append(generators, generator)
}
}
return multipleTypeGenerator{
generators: generators,
}, nil
}
func (g multipleTypeGenerator) Generate(opts *GeneratorOptions) interface{} {
generator := g.generators[opts.Rand.RandomInt(0, len(g.generators))]
return generator.Generate(opts)
}
func (g multipleTypeGenerator) String() string {
formattedGenerators := funk.Map(g.generators, func(generator Generator) string {
return generator.String()
}).([]string)
return fmt.Sprintf("MultiTypeGenerator{%s}", strings.Join(formattedGenerators, ","))
}