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

Function arrayEachConfig

parser.go:1649–1746  ·  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

1647
1648// SYS-REQ-115
1649func arrayEachConfig(config Config, data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string) (offset int, err error) {
1650 if len(data) == 0 {
1651 return -1, MalformedObjectError
1652 }
1653
1654 nT := nextTokenConfig(config, data)
1655 if nT == -1 {
1656 return -1, MalformedJsonError
1657 }
1658
1659 // Guard: when ArrayEach is called without a key path, the addressed
1660 // root value must be an array. Without this guard, the main loop below
1661 // happily parses the first token of a non-array value (e.g. the opening
1662 // key of an object, or a bare number) as if it were an array element,
1663 // invoking the callback once with bogus data before eventually returning
1664 // MalformedArrayError. A caller performing side effects in the callback
1665 // would observe a spurious invocation on input that is not an array at
1666 // all. (SYS-REQ-029 partition: non-array root, no key path.)
1667 // When a key path IS provided, the keys block below already enforces the
1668 // same contract via its own `data[offset] != '['` check after resolving
1669 // the path, so the guard is only needed for the no-keys case.
1670 if len(keys) == 0 && data[nT] != '[' {
1671 return -1, MalformedArrayError
1672 }
1673
1674 offset = nT + 1
1675
1676 if len(keys) > 0 {
1677 if offset = searchKeysConfig(config, data, keys...); offset == -1 {
1678 return offset, KeyPathNotFoundError
1679 }
1680
1681 // Go to closest value
1682 nO := nextTokenConfig(config, data[offset:])
1683 if nO == -1 {
1684 return offset, MalformedJsonError
1685 }
1686
1687 offset += nO
1688
1689 if data[offset] != '[' {
1690 return offset, MalformedArrayError
1691 }
1692
1693 offset++
1694 }
1695
1696 nO := nextTokenConfig(config, data[offset:])
1697 if nO == -1 {
1698 return offset, MalformedJsonError
1699 }
1700
1701 offset += nO
1702
1703 if data[offset] == ']' {
1704 return offset, nil
1705 }
1706

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