(key string)
| 411 | } |
| 412 | |
| 413 | func (j *jsonEncoded) FetchValKey(key string) (JSON, error) { |
| 414 | if dec := j.alreadyDecoded(); dec != nil { |
| 415 | return dec.FetchValKey(key) |
| 416 | } |
| 417 | |
| 418 | if j.Type() == ObjectJSONType { |
| 419 | // TODO(justin): This is not as absolutely efficient as it could be - every |
| 420 | // lookup we have to seek to find the actual location of the key. We could |
| 421 | // be caching the locations of all the intermediate keys that we have to |
| 422 | // scan in order to get to this one, in case we need to look them up later, |
| 423 | // or maybe there's something fancier we could do if we know the locations |
| 424 | // of the offsets by strategically positioning our binary search guesses to |
| 425 | // land on them. |
| 426 | var searchErr error |
| 427 | i := sort.Search(j.containerLen, func(idx int) bool { |
| 428 | data, _, err := j.objectGetNthDataRange(idx) |
| 429 | if err != nil { |
| 430 | searchErr = err |
| 431 | return false |
| 432 | } |
| 433 | return string(data) >= key |
| 434 | }) |
| 435 | if searchErr != nil { |
| 436 | return nil, searchErr |
| 437 | } |
| 438 | // The sort.Search API implies that we have to double-check if the key we |
| 439 | // landed on is the one we were searching for in the first place. |
| 440 | if i >= j.containerLen { |
| 441 | return nil, nil |
| 442 | } |
| 443 | |
| 444 | data, _, err := j.objectGetNthDataRange(i) |
| 445 | if err != nil { |
| 446 | return nil, err |
| 447 | } |
| 448 | |
| 449 | if string(data) == key { |
| 450 | return j.objectNthValue(i) |
| 451 | } |
| 452 | } |
| 453 | return nil, nil |
| 454 | } |
| 455 | |
| 456 | // shallowDecode decodes only the keys of an object, and doesn't decode any |
| 457 | // elements of an array. It can be used to save a decode-encode cycle for |
no test coverage detected