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