SYS-REQ-115
(config Config, data []byte, callback func(key []byte, value []byte, dataType ValueType, offset int) error, keys ...string)
| 1794 | |
| 1795 | // SYS-REQ-115 |
| 1796 | func objectEachConfig(config Config, data []byte, callback func(key []byte, value []byte, dataType ValueType, offset int) error, keys ...string) (err error) { |
| 1797 | offset := 0 |
| 1798 | |
| 1799 | // Descend to the desired key, if requested |
| 1800 | if len(keys) > 0 { |
| 1801 | if off := searchKeysConfig(config, data, keys...); off == -1 { |
| 1802 | return KeyPathNotFoundError |
| 1803 | } else { |
| 1804 | offset = off |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | // Validate and skip past opening brace |
| 1809 | if off := nextTokenConfig(config, data[offset:]); off == -1 { |
| 1810 | return MalformedObjectError |
| 1811 | } else if offset += off; data[offset] != '{' { |
| 1812 | return MalformedObjectError |
| 1813 | } else { |
| 1814 | offset++ |
| 1815 | } |
| 1816 | |
| 1817 | // Skip to the first token inside the object, or stop if we find the ending brace |
| 1818 | if off := nextTokenConfig(config, data[offset:]); off == -1 { |
| 1819 | return MalformedJsonError |
| 1820 | } else if offset += off; data[offset] == '}' { |
| 1821 | return nil |
| 1822 | } |
| 1823 | |
| 1824 | // Loop pre-condition: data[offset] points to what should be either the next entry's key, |
| 1825 | // or the closing brace (if it's anything else, the JSON is malformed). |
| 1826 | // Every iteration either returns or advances offset past a token, so the loop |
| 1827 | // always exits via return; the former `offset < len(data)` guard was structurally |
| 1828 | // always true because internal nextToken/stringEnd calls return errors before |
| 1829 | // offset can reach len(data). |
| 1830 | for { |
| 1831 | // Step 1: find the next key |
| 1832 | var key []byte |
| 1833 | |
| 1834 | // Check what the the next token is: start of string, end of object, or something else (error) |
| 1835 | switch data[offset] { |
| 1836 | case '"': |
| 1837 | offset++ // accept as string and skip opening quote |
| 1838 | case '\'': |
| 1839 | if !config.AllowSingleQuotes { |
| 1840 | return MalformedObjectError |
| 1841 | } |
| 1842 | offset++ // accept as string and skip opening quote |
| 1843 | case '}': |
| 1844 | return nil // we found the end of the object; stop and return success |
| 1845 | default: |
| 1846 | return MalformedObjectError |
| 1847 | } |
| 1848 | |
| 1849 | // Find the end of the key string |
| 1850 | var keyEscaped bool |
| 1851 | quote := data[offset-1] |
| 1852 | if off, esc := stringEndConfig(config, data[offset:], quote); off == -1 { |
| 1853 | return MalformedJsonError |
no test coverage detected