prettyPrintFirstValue returns a string representation of the first decodable value in the provided byte slice, along with the remaining byte slice after decoding. Ascending will be the default direction (when dir is the 0 value) for all values.
(dir Direction, b []byte)
| 2227 | // Ascending will be the default direction (when dir is the 0 value) for all |
| 2228 | // values. |
| 2229 | func prettyPrintFirstValue(dir Direction, b []byte) ([]byte, string, error) { |
| 2230 | var err error |
| 2231 | switch typ := PeekType(b); typ { |
| 2232 | case Null: |
| 2233 | b, _ = DecodeIfNull(b) |
| 2234 | return b, "NULL", nil |
| 2235 | case True: |
| 2236 | return b[1:], "True", nil |
| 2237 | case False: |
| 2238 | return b[1:], "False", nil |
| 2239 | case Array: |
| 2240 | return b[1:], "Arr", nil |
| 2241 | case ArrayKeyAsc, ArrayKeyDesc: |
| 2242 | encDir := Ascending |
| 2243 | if typ == ArrayKeyDesc { |
| 2244 | encDir = Descending |
| 2245 | } |
| 2246 | var build strings.Builder |
| 2247 | buf, err := ValidateAndConsumeArrayKeyMarker(b, encDir) |
| 2248 | if err != nil { |
| 2249 | return nil, "", err |
| 2250 | } |
| 2251 | build.WriteString("ARRAY[") |
| 2252 | first := true |
| 2253 | // Use the array key decoding logic, but instead of calling out |
| 2254 | // to keyside.Decode, just make a recursive call. |
| 2255 | for { |
| 2256 | if len(buf) == 0 { |
| 2257 | return nil, "", errors.AssertionFailedf("invalid array (unterminated)") |
| 2258 | } |
| 2259 | if IsArrayKeyDone(buf, encDir) { |
| 2260 | buf = buf[1:] |
| 2261 | break |
| 2262 | } |
| 2263 | var next string |
| 2264 | if IsNextByteArrayEncodedNull(buf, dir) { |
| 2265 | next = "NULL" |
| 2266 | buf = buf[1:] |
| 2267 | } else { |
| 2268 | buf, next, err = prettyPrintFirstValue(dir, buf) |
| 2269 | if err != nil { |
| 2270 | return nil, "", err |
| 2271 | } |
| 2272 | } |
| 2273 | if !first { |
| 2274 | build.WriteString(",") |
| 2275 | } |
| 2276 | build.WriteString(next) |
| 2277 | first = false |
| 2278 | } |
| 2279 | build.WriteString("]") |
| 2280 | return buf, build.String(), nil |
| 2281 | case NotNull: |
| 2282 | b, _ = DecodeIfNotNull(b) |
| 2283 | return b, "!NULL", nil |
| 2284 | case Int: |
| 2285 | var i int64 |
| 2286 | if dir == Descending { |
no test coverage detected
searching dependent graphs…