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

Function setPathSemanticallyValid

reference_oracle_test.go:409–446  ·  view source on GitHub ↗

setPathSemanticallyValid reports whether applying Set/Delete with `path` to a doc marshaled from `v` is well-defined: each array-index component must address an array element of an existing array (in-range), each object-key component must address an object field. Out-of-range indices on nested array

(v interface{}, path []string)

Source from the content-addressed store, hash-verified

407// known bugs (D6, D7, D8 in the file header) — callers should NOT assert
408// strict round-trip correctness on those paths.
409func setPathSemanticallyValid(v interface{}, path []string) bool {
410 cur := v
411 for i, comp := range path {
412 isArrayIdx := len(comp) > 0 && comp[0] == '['
413 switch node := cur.(type) {
414 case map[string]interface{}:
415 if isArrayIdx {
416 return false // D6: array-index syntax on object — undefined
417 }
418 if existing, ok := node[comp]; ok {
419 cur = existing
420 } else {
421 // New key only well-defined if it's the LAST component
422 // (Set creates one new key; multi-component paths through
423 // a new key are undefined).
424 return i == len(path)-1
425 }
426 case []interface{}:
427 if !isArrayIdx {
428 return false // object-key syntax on array — undefined
429 }
430 idxStr := strings.TrimSuffix(strings.TrimPrefix(comp, "["), "]")
431 idx, err := strconv.Atoi(idxStr)
432 if err != nil || idx < 0 {
433 return false
434 }
435 if idx >= len(node) {
436 // D7: out-of-range index on nested array — known data-loss bug.
437 return false
438 }
439 cur = node[idx]
440 default:
441 // Trying to descend into a scalar — undefined.
442 return false
443 }
444 }
445 return true
446}
447
448// ---------------------------------------------------------------------------
449// Property 1: Get round-trip — jsonparser.Get matches encoding/json.

Callers 1

TestOracleSetRoundTripFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected