SYS-REQ-006, SYS-REQ-028, SYS-REQ-029, SYS-REQ-052, SYS-REQ-053, SYS-REQ-055, SYS-REQ-083 ArrayEach is used when iterating arrays, accepts a callback function with the same return arguments as `Get`.
(data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string)
| 1025 | // SYS-REQ-006, SYS-REQ-028, SYS-REQ-029, SYS-REQ-052, SYS-REQ-053, SYS-REQ-055, SYS-REQ-083 |
| 1026 | // ArrayEach is used when iterating arrays, accepts a callback function with the same return arguments as `Get`. |
| 1027 | func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string) (offset int, err error) { |
| 1028 | if len(data) == 0 { |
| 1029 | return -1, MalformedObjectError |
| 1030 | } |
| 1031 | |
| 1032 | nT := nextToken(data) |
| 1033 | if nT == -1 { |
| 1034 | return -1, MalformedJsonError |
| 1035 | } |
| 1036 | |
| 1037 | offset = nT + 1 |
| 1038 | |
| 1039 | if len(keys) > 0 { |
| 1040 | if offset = searchKeys(data, keys...); offset == -1 { |
| 1041 | return offset, KeyPathNotFoundError |
| 1042 | } |
| 1043 | |
| 1044 | // Go to closest value |
| 1045 | nO := nextToken(data[offset:]) |
| 1046 | if nO == -1 { |
| 1047 | return offset, MalformedJsonError |
| 1048 | } |
| 1049 | |
| 1050 | offset += nO |
| 1051 | |
| 1052 | if data[offset] != '[' { |
| 1053 | return offset, MalformedArrayError |
| 1054 | } |
| 1055 | |
| 1056 | offset++ |
| 1057 | } |
| 1058 | |
| 1059 | nO := nextToken(data[offset:]) |
| 1060 | if nO == -1 { |
| 1061 | return offset, MalformedJsonError |
| 1062 | } |
| 1063 | |
| 1064 | offset += nO |
| 1065 | |
| 1066 | if data[offset] == ']' { |
| 1067 | return offset, nil |
| 1068 | } |
| 1069 | |
| 1070 | for { |
| 1071 | v, t, o, e := Get(data[offset:]) |
| 1072 | |
| 1073 | if o == 0 { |
| 1074 | // When Get returns endOffset==0, it always means a parse error |
| 1075 | // (no valid value found at the current position). The former |
| 1076 | // e==nil/break branch was structurally unreachable because Get |
| 1077 | // never returns endOffset==0 without an error. |
| 1078 | return offset, e |
| 1079 | } |
| 1080 | |
| 1081 | // Pass the error to the callback — the callback signature declares |
| 1082 | // an err parameter, so callers who check it should see real errors. |
| 1083 | cb(v, t, offset+o-len(v), e) |
| 1084 |
searching dependent graphs…