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

Method shallowDecode

pkg/util/json/encoded.go:459–510  ·  view source on GitHub ↗

shallowDecode decodes only the keys of an object, and doesn't decode any elements of an array. It can be used to save a decode-encode cycle for certain operations (say, key deletion).

()

Source from the content-addressed store, hash-verified

457// elements of an array. It can be used to save a decode-encode cycle for
458// certain operations (say, key deletion).
459func (j *jsonEncoded) shallowDecode() (JSON, error) {
460 if dec := j.alreadyDecoded(); dec != nil {
461 return dec, nil
462 }
463
464 switch j.typ {
465 case NumberJSONType, StringJSONType, TrueJSONType, FalseJSONType, NullJSONType:
466 return j.decode()
467 case ArrayJSONType:
468 iter := j.iterArrayValues()
469 result := make(jsonArray, j.containerLen)
470 for i := 0; i < j.containerLen; i++ {
471 entry, next, _, err := iter.nextEncoded()
472 if err != nil {
473 return nil, err
474 }
475 result[i], err = newEncoded(entry, next)
476 if err != nil {
477 return nil, err
478 }
479 }
480 return result, nil
481 case ObjectJSONType:
482 iter, err := j.iterObject()
483 if err != nil {
484 return nil, err
485 }
486 result := make(jsonObject, j.containerLen)
487 for i := 0; i < j.containerLen; i++ {
488 nextKey, entry, nextValue, _, err := iter.nextEncoded()
489 if err != nil {
490 return nil, err
491 }
492 v, err := newEncoded(entry, nextValue)
493 if err != nil {
494 return nil, err
495 }
496 result[i] = jsonKeyValuePair{
497 k: jsonString(nextKey),
498 v: v,
499 }
500 }
501 j.mu.Lock()
502 defer j.mu.Unlock()
503 if j.mu.cachedDecoded == nil {
504 j.mu.cachedDecoded = result
505 }
506 return result, nil
507 default:
508 return nil, errors.AssertionFailedf("unknown json type: %v", errors.Safe(j.typ))
509 }
510}
511
512func (j *jsonEncoded) mustDecode() JSON {
513 decoded, err := j.shallowDecode()

Callers 15

StripNullsMethod · 0.95
ObjectIterMethod · 0.95
mustDecodeMethod · 0.95
CompareMethod · 0.95
RemoveIndexMethod · 0.95
ConcatMethod · 0.95
RemoveStringMethod · 0.95
RemovePathMethod · 0.95
doRemovePathMethod · 0.95
toGoReprMethod · 0.95
tryDecodeMethod · 0.95
setValKeyOrIdxFunction · 0.80

Calls 9

alreadyDecodedMethod · 0.95
decodeMethod · 0.95
iterArrayValuesMethod · 0.95
iterObjectMethod · 0.95
newEncodedFunction · 0.85
jsonStringTypeAlias · 0.85
nextEncodedMethod · 0.45
LockMethod · 0.45
UnlockMethod · 0.45

Tested by

no test coverage detected