-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_value.go
46 lines (40 loc) · 1.19 KB
/
random_value.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
package mutator
import "math/rand"
// Random value generator based on type
func randomValue(propType int, maxDepth, currentDepth int, m *Mutator) interface{} {
switch propType {
case 0: // STRING
return randomPropertyName()
case 1: // NUMBER
return rand.Intn(1000)
case 2: // BOOL
return rand.Intn(2) == 1
case 3: // OBJECT
if currentDepth < maxDepth {
return m.generateRandomJSON(maxDepth, currentDepth+1)
}
return randomPropertyName()
default:
return nil
}
}
// Generates a random JSON object
func (m *Mutator) generateRandomJSON(maxDepth, currentDepth int) map[string]interface{} {
numProps := maxDepth // Number of properties per object (1 to 5)
jsonObj := make(map[string]interface{})
for i := 0; i < numProps; i++ {
propName := randomPropertyName()
propType := rand.Intn(4) // Type: 0-String, 1-Number, 2-Bool, 3-Object
jsonObj[propName] = randomValue(propType, maxDepth, currentDepth, m)
}
return jsonObj
}
// Random property name generator
func randomPropertyName() string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]rune, rand.Intn(10)+1)
for i := range b {
b[i] = rune(letters[rand.Intn(len(letters))])
}
return string(b)
}