SYS-REQ-115
(config Config, data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string)
| 1701 | |
| 1702 | // SYS-REQ-115 |
| 1703 | func arrayEachConfig(config Config, data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string) (offset int, err error) { |
| 1704 | if len(data) == 0 { |
| 1705 | return -1, MalformedObjectError |
| 1706 | } |
| 1707 | |
| 1708 | nT := nextTokenConfig(config, data) |
| 1709 | if nT == -1 { |
| 1710 | return -1, MalformedJsonError |
| 1711 | } |
| 1712 | |
| 1713 | // Guard: when ArrayEach is called without a key path, the addressed |
| 1714 | // root value must be an array. Without this guard, the main loop below |
| 1715 | // happily parses the first token of a non-array value (e.g. the opening |
| 1716 | // key of an object, or a bare number) as if it were an array element, |
| 1717 | // invoking the callback once with bogus data before eventually returning |
| 1718 | // MalformedArrayError. A caller performing side effects in the callback |
| 1719 | // would observe a spurious invocation on input that is not an array at |
| 1720 | // all. (SYS-REQ-029 partition: non-array root, no key path.) |
| 1721 | // When a key path IS provided, the keys block below already enforces the |
| 1722 | // same contract via its own `data[offset] != '['` check after resolving |
| 1723 | // the path, so the guard is only needed for the no-keys case. |
| 1724 | if len(keys) == 0 && data[nT] != '[' { |
| 1725 | return -1, MalformedArrayError |
| 1726 | } |
| 1727 | |
| 1728 | offset = nT + 1 |
| 1729 | |
| 1730 | if len(keys) > 0 { |
| 1731 | if offset = searchKeysConfig(config, data, keys...); offset == -1 { |
| 1732 | return offset, KeyPathNotFoundError |
| 1733 | } |
| 1734 | |
| 1735 | // Go to closest value |
| 1736 | nO := nextTokenConfig(config, data[offset:]) |
| 1737 | if nO == -1 { |
| 1738 | return offset, MalformedJsonError |
| 1739 | } |
| 1740 | |
| 1741 | offset += nO |
| 1742 | |
| 1743 | if data[offset] != '[' { |
| 1744 | return offset, MalformedArrayError |
| 1745 | } |
| 1746 | |
| 1747 | offset++ |
| 1748 | } |
| 1749 | |
| 1750 | nO := nextTokenConfig(config, data[offset:]) |
| 1751 | if nO == -1 { |
| 1752 | return offset, MalformedJsonError |
| 1753 | } |
| 1754 | |
| 1755 | offset += nO |
| 1756 | |
| 1757 | if data[offset] == ']' { |
| 1758 | return offset, nil |
| 1759 | } |
| 1760 |
no test coverage detected