dump is the main workhorse for dumping a value. It uses the passed reflect value to figure out what kind of object we are dealing with and formats it appropriately. It is a recursive function, however circular data structures are detected and handled properly.
(v reflect.Value)
| 249 | // appropriately. It is a recursive function, however circular data structures |
| 250 | // are detected and handled properly. |
| 251 | func (d *dumpState) dump(v reflect.Value) { |
| 252 | // Handle invalid reflect values immediately. |
| 253 | kind := v.Kind() |
| 254 | if kind == reflect.Invalid { |
| 255 | d.w.Write(invalidAngleBytes) |
| 256 | return |
| 257 | } |
| 258 | |
| 259 | // Handle pointers specially. |
| 260 | if kind == reflect.Ptr { |
| 261 | d.indent() |
| 262 | d.dumpPtr(v) |
| 263 | return |
| 264 | } |
| 265 | |
| 266 | // Print type information unless already handled elsewhere. |
| 267 | if !d.ignoreNextType { |
| 268 | d.indent() |
| 269 | d.w.Write(openParenBytes) |
| 270 | d.w.Write([]byte(v.Type().String())) |
| 271 | d.w.Write(closeParenBytes) |
| 272 | d.w.Write(spaceBytes) |
| 273 | } |
| 274 | d.ignoreNextType = false |
| 275 | |
| 276 | // Display length and capacity if the built-in len and cap functions |
| 277 | // work with the value's kind and the len/cap itself is non-zero. |
| 278 | valueLen, valueCap := 0, 0 |
| 279 | switch v.Kind() { |
| 280 | case reflect.Array, reflect.Slice, reflect.Chan: |
| 281 | valueLen, valueCap = v.Len(), v.Cap() |
| 282 | case reflect.Map, reflect.String: |
| 283 | valueLen = v.Len() |
| 284 | } |
| 285 | if valueLen != 0 || !d.cs.DisableCapacities && valueCap != 0 { |
| 286 | d.w.Write(openParenBytes) |
| 287 | if valueLen != 0 { |
| 288 | d.w.Write(lenEqualsBytes) |
| 289 | printInt(d.w, int64(valueLen), 10) |
| 290 | } |
| 291 | if !d.cs.DisableCapacities && valueCap != 0 { |
| 292 | if valueLen != 0 { |
| 293 | d.w.Write(spaceBytes) |
| 294 | } |
| 295 | d.w.Write(capEqualsBytes) |
| 296 | printInt(d.w, int64(valueCap), 10) |
| 297 | } |
| 298 | d.w.Write(closeParenBytes) |
| 299 | d.w.Write(spaceBytes) |
| 300 | } |
| 301 | |
| 302 | // Call Stringer/error interfaces if they exist and the handle methods flag |
| 303 | // is enabled |
| 304 | if !d.cs.DisableMethods { |
| 305 | if (kind != reflect.Invalid) && (kind != reflect.Interface) { |
| 306 | if handled := handleMethods(d.cs, d.w, v); handled { |
| 307 | return |
| 308 | } |