MCPcopy Create free account
hub / github.com/buger/jsonparser / pickExistingPath

Function pickExistingPath

reference_oracle_test.go:270–300  ·  view source on GitHub ↗

--------------------------------------------------------------------------- Path picker — walks a random Go value tree to produce a path that EXISTS. Returns the path components and the Go value at that path. ---------------------------------------------------------------------------

(r *mathrand.Rand, v interface{})

Source from the content-addressed store, hash-verified

268// ---------------------------------------------------------------------------
269
270func pickExistingPath(r *mathrand.Rand, v interface{}) ([]string, interface{}) {
271 cur := v
272 var path []string
273 // Bound the walk so we don't infinite-loop on cyclic data (json.Marshal
274 // rejects cycles anyway, so this is defensive only).
275 for i := 0; i < 16; i++ {
276 switch node := cur.(type) {
277 case map[string]interface{}:
278 if len(node) == 0 || r.Intn(3) == 0 {
279 return path, cur
280 }
281 keys := make([]string, 0, len(node))
282 for k := range node {
283 keys = append(keys, k)
284 }
285 k := keys[r.Intn(len(keys))]
286 path = append(path, k)
287 cur = node[k]
288 case []interface{}:
289 if len(node) == 0 || r.Intn(3) == 0 {
290 return path, cur
291 }
292 idx := r.Intn(len(node))
293 path = append(path, fmt.Sprintf("[%d]", idx))
294 cur = node[idx]
295 default:
296 return path, cur
297 }
298 }
299 return path, cur
300}
301
302// pickAdversarialPath produces a path that may NOT exist in v, including
303// out-of-range indices and empty components — exactly the inputs that

Callers 3

pickAdversarialPathFunction · 0.85
TestOracleGetRoundTripFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected