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

Function formatEncodedJSONObject

pkg/util/json/encoded_format.go:119–188  ·  view source on GitHub ↗
(buf *bytes.Buffer, containerHeader uint32, b []byte)

Source from the content-addressed store, hash-verified

117}
118
119func formatEncodedJSONObject(buf *bytes.Buffer, containerHeader uint32, b []byte) ([]byte, error) {
120 length := int(containerHeader & containerHeaderLenMask)
121
122 // There are `length` key JEntries and then `length` value JEntries in the
123 // object.
124 keyOff, valOff := uint32(0), uint32(0)
125 keyEntB, valEntB := b, b
126 // Skip past the key JEntries to find the first value JEntry, permitting
127 // parallel iteration through the key JEntries, value JEntries, keys, and
128 // values below.
129 var err error
130 for i := 0; i < length; i++ {
131 // Decode (and ignore) key JEntry.
132 var keyEnt jEntry
133 valEntB, keyEnt, err = decodeJEntry(valEntB, valOff)
134 if err != nil {
135 return nil, err
136 }
137 if keyEnt.typCode != stringTag {
138 return nil, errors.AssertionFailedf(
139 "key encoded as non-string: %d", errors.Safe(keyEnt.typCode))
140 }
141 valOff += keyEnt.length
142 }
143 // Skip past the value JEntries to find the first key.
144 valEntsSize := jEntrySize * length
145 if len(valEntB) < valEntsSize {
146 return nil, errors.AssertionFailedf("insufficient data to skip JSON object value JEntries")
147 }
148 keyB := valEntB[valEntsSize:]
149 // Skip past the keys to find the first value.
150 if len(keyB) < int(valOff) {
151 return nil, errors.AssertionFailedf("insufficient data to skip JSON object keys")
152 }
153 valB := keyB[valOff:]
154
155 // Iterate through the keys and values in parallel, formatting them.
156 buf.WriteByte('{')
157 for i := 0; i < length; i++ {
158 if i != 0 {
159 buf.WriteString(", ")
160 }
161 // Decode key JEntry i.
162 var keyEnt jEntry
163 keyEntB, keyEnt, err = decodeJEntry(keyEntB, keyOff)
164 if err != nil {
165 return nil, err
166 }
167 keyOff += keyEnt.length
168 // Decode value JEntry i.
169 var valEnt jEntry
170 valEntB, valEnt, err = decodeJEntry(valEntB, valOff)
171 if err != nil {
172 return nil, err
173 }
174 valOff += valEnt.length
175 // Decode and format key i.
176 keyS := unsafeJSONString(keyB[:keyEnt.length])

Callers 1

formatEncodedJSONFunction · 0.85

Calls 4

decodeJEntryFunction · 0.85
unsafeJSONStringFunction · 0.85
formatEncodedJSONValueFunction · 0.85
FormatMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…