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