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