genValue appends one JSON value to buf, chosen from a weighted distribution that favors containers at shallow depth (to exercise nesting) and scalars at deep depth (to bound recursion).
(r *rand.Rand, depth int, buf *[]byte)
| 163 | // distribution that favors containers at shallow depth (to exercise |
| 164 | // nesting) and scalars at deep depth (to bound recursion). |
| 165 | func genValue(r *rand.Rand, depth int, buf *[]byte) { |
| 166 | if depth >= maxGenDepth { |
| 167 | // Scalars only at deep recursion. |
| 168 | switch r.Intn(4) { |
| 169 | case 0: |
| 170 | genString(r, buf) |
| 171 | case 1: |
| 172 | genNumber(r, buf) |
| 173 | case 2: |
| 174 | *buf = append(*buf, 't', 'r', 'u', 'e') |
| 175 | default: |
| 176 | *buf = append(*buf, 'n', 'u', 'l', 'l') |
| 177 | } |
| 178 | return |
| 179 | } |
| 180 | |
| 181 | roll := r.Intn(20) |
| 182 | switch { |
| 183 | case roll < 6: // 30% — object |
| 184 | genObject(r, depth, buf) |
| 185 | case roll < 11: // 25% — array |
| 186 | genArray(r, depth, buf) |
| 187 | case roll < 14: // 15% — string |
| 188 | genString(r, buf) |
| 189 | case roll < 18: // 20% — number |
| 190 | genNumber(r, buf) |
| 191 | case roll == 18: // 5% — bool |
| 192 | if r.Intn(2) == 0 { |
| 193 | *buf = append(*buf, 't', 'r', 'u', 'e') |
| 194 | } else { |
| 195 | *buf = append(*buf, 'f', 'a', 'l', 's', 'e') |
| 196 | } |
| 197 | default: // 5% — null |
| 198 | *buf = append(*buf, 'n', 'u', 'l', 'l') |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // genObject emits an object with 0–5 entries. Keys are drawn from a set |
| 203 | // that includes the dangerous cases: empty-string "", unicode (\uXXXX), |