MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / Exists

Method Exists

pkg/util/json/encoded.go:618–664  ·  view source on GitHub ↗
(key string)

Source from the content-addressed store, hash-verified

616}
617
618func (j *jsonEncoded) Exists(key string) (bool, error) {
619 if dec := j.alreadyDecoded(); dec != nil {
620 return dec.Exists(key)
621 }
622
623 switch j.typ {
624 case ObjectJSONType:
625 v, err := j.FetchValKey(key)
626 if err != nil {
627 return false, err
628 }
629 return v != nil, nil
630 case ArrayJSONType:
631 iter := j.iterArrayValues()
632 for {
633 nextJEntry, data, ok, err := iter.nextEncoded()
634 if err != nil {
635 return false, err
636 }
637 if !ok {
638 return false, nil
639 }
640 next, err := newEncoded(nextJEntry, data)
641 if err != nil {
642 return false, err
643 }
644 // This is a minor optimization - we know that newEncoded always returns a
645 // jsonEncoded if it's decoding a string, and we can save actually
646 // allocating that string for this check by not forcing a decode into a
647 // jsonString. This operates on two major assumptions:
648 // 1. newEncoded returns a jsonEncoded (and not a jsonString) for string
649 // types and
650 // 2. the `value` field on such a jsonEncoded directly corresponds to the string.
651 // This is tested sufficiently that if either of those assumptions is
652 // broken it will be caught.
653 if next.Type() == StringJSONType && string(next.(*jsonEncoded).value) == key {
654 return true, nil
655 }
656 }
657 default:
658 s, err := j.decode()
659 if err != nil {
660 return false, err
661 }
662 return s.Exists(key)
663 }
664}
665
666func (j *jsonEncoded) FetchValKeyOrIdx(key string) (JSON, error) {
667 switch j.typ {

Callers

nothing calls this directly

Calls 8

alreadyDecodedMethod · 0.95
FetchValKeyMethod · 0.95
iterArrayValuesMethod · 0.95
decodeMethod · 0.95
newEncodedFunction · 0.85
ExistsMethod · 0.65
TypeMethod · 0.65
nextEncodedMethod · 0.45

Tested by

no test coverage detected