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

Function arrayEachConfig

parser.go:1609–1706  ·  view source on GitHub ↗

SYS-REQ-115

(config Config, data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string)

Source from the content-addressed store, hash-verified

1607
1608// SYS-REQ-115
1609func arrayEachConfig(config Config, data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string) (offset int, err error) {
1610 if len(data) == 0 {
1611 return -1, MalformedObjectError
1612 }
1613
1614 nT := nextTokenConfig(config, data)
1615 if nT == -1 {
1616 return -1, MalformedJsonError
1617 }
1618
1619 // Guard: when ArrayEach is called without a key path, the addressed
1620 // root value must be an array. Without this guard, the main loop below
1621 // happily parses the first token of a non-array value (e.g. the opening
1622 // key of an object, or a bare number) as if it were an array element,
1623 // invoking the callback once with bogus data before eventually returning
1624 // MalformedArrayError. A caller performing side effects in the callback
1625 // would observe a spurious invocation on input that is not an array at
1626 // all. (SYS-REQ-029 partition: non-array root, no key path.)
1627 // When a key path IS provided, the keys block below already enforces the
1628 // same contract via its own `data[offset] != '['` check after resolving
1629 // the path, so the guard is only needed for the no-keys case.
1630 if len(keys) == 0 && data[nT] != '[' {
1631 return -1, MalformedArrayError
1632 }
1633
1634 offset = nT + 1
1635
1636 if len(keys) > 0 {
1637 if offset = searchKeysConfig(config, data, keys...); offset == -1 {
1638 return offset, KeyPathNotFoundError
1639 }
1640
1641 // Go to closest value
1642 nO := nextTokenConfig(config, data[offset:])
1643 if nO == -1 {
1644 return offset, MalformedJsonError
1645 }
1646
1647 offset += nO
1648
1649 if data[offset] != '[' {
1650 return offset, MalformedArrayError
1651 }
1652
1653 offset++
1654 }
1655
1656 nO := nextTokenConfig(config, data[offset:])
1657 if nO == -1 {
1658 return offset, MalformedJsonError
1659 }
1660
1661 offset += nO
1662
1663 if data[offset] == ']' {
1664 return offset, nil
1665 }
1666

Callers 3

searchKeysConfigFunction · 0.85
ArrayEachFunction · 0.85
ArrayEachMethod · 0.85

Calls 3

nextTokenConfigFunction · 0.85
searchKeysConfigFunction · 0.85
GetMethod · 0.45

Tested by

no test coverage detected