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

Method Format

pkg/util/json/random.go:245–338  ·  view source on GitHub ↗

Format implements JSON, and overrides underlying JSON object implementation in order to inject errors.

(buf *bytes.Buffer)

Source from the content-addressed store, hash-verified

243// Format implements JSON, and overrides underlying JSON object
244// implementation in order to inject errors.
245func (j *errJSON) Format(buf *bytes.Buffer) {
246 j.writeGarbage(garbage, buf) // Possibly prefix with garbage data.
247 defer func() {
248 j.writeGarbage(garbage, buf) // Possibly append garbage data.
249 }()
250
251 switch t := j.JSON.(type) {
252 default:
253 j.JSON.Format(buf)
254 case jsonObject:
255 if j.injectErr() {
256 buf.WriteByte('[') // Oops, array instead of object.
257 } else {
258 buf.WriteByte('{')
259 }
260 for i := range t {
261 if i != 0 {
262 buf.WriteString(", ")
263 }
264 // Skip element (which results in trailing or extra ",")
265 // if injectErr is true.
266 if !j.injectErr() {
267 ek := j.errJSON(t[i].k)
268 ev := j.errJSON(t[i].v)
269 buf.WriteString(asString(ek))
270 buf.WriteString(": ")
271 ev.Format(buf)
272 }
273 }
274 if j.injectErr() {
275 buf.WriteByte(']') // Oops, array instead of object.
276 } else {
277 buf.WriteByte('}')
278 }
279 case jsonArray:
280 if j.injectErr() {
281 buf.WriteByte('{') // Oops, object instead of array.
282 } else {
283 buf.WriteByte('[')
284 }
285 for i := range t {
286 if i != 0 {
287 buf.WriteString(", ")
288 }
289 // Skip element (which results in trailing or extra ",")
290 // if injectErr is true.
291 if !j.injectErr() {
292 j.errJSON(t[i]).Format(buf)
293 }
294 }
295 if j.injectErr() {
296 buf.WriteByte('}') // Oops, object instead of array.
297 } else {
298 buf.WriteByte(']')
299 }
300 case jsonString:
301 t.Format(buf)
302 if j.injectErr() {

Callers

nothing calls this directly

Calls 7

writeGarbageMethod · 0.95
injectErrMethod · 0.95
errJSONMethod · 0.95
asStringFunction · 0.85
TruncateMethod · 0.80
FormatMethod · 0.65
LenMethod · 0.65

Tested by

no test coverage detected