formatPtr handles formatting of pointers by indirecting them as necessary.
(v reflect.Value)
| 103 | |
| 104 | // formatPtr handles formatting of pointers by indirecting them as necessary. |
| 105 | func (f *formatState) formatPtr(v reflect.Value) { |
| 106 | // Display nil if top level pointer is nil. |
| 107 | showTypes := f.fs.Flag('#') |
| 108 | if v.IsNil() && (!showTypes || f.ignoreNextType) { |
| 109 | f.fs.Write(nilAngleBytes) |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | // Remove pointers at or below the current depth from map used to detect |
| 114 | // circular refs. |
| 115 | for k, depth := range f.pointers { |
| 116 | if depth >= f.depth { |
| 117 | delete(f.pointers, k) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Keep list of all dereferenced pointers to possibly show later. |
| 122 | pointerChain := make([]uintptr, 0) |
| 123 | |
| 124 | // Figure out how many levels of indirection there are by dereferencing |
| 125 | // pointers and unpacking interfaces down the chain while detecting circular |
| 126 | // references. |
| 127 | nilFound := false |
| 128 | cycleFound := false |
| 129 | indirects := 0 |
| 130 | ve := v |
| 131 | for ve.Kind() == reflect.Ptr { |
| 132 | if ve.IsNil() { |
| 133 | nilFound = true |
| 134 | break |
| 135 | } |
| 136 | indirects++ |
| 137 | addr := ve.Pointer() |
| 138 | pointerChain = append(pointerChain, addr) |
| 139 | if pd, ok := f.pointers[addr]; ok && pd < f.depth { |
| 140 | cycleFound = true |
| 141 | indirects-- |
| 142 | break |
| 143 | } |
| 144 | f.pointers[addr] = f.depth |
| 145 | |
| 146 | ve = ve.Elem() |
| 147 | if ve.Kind() == reflect.Interface { |
| 148 | if ve.IsNil() { |
| 149 | nilFound = true |
| 150 | break |
| 151 | } |
| 152 | ve = ve.Elem() |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Display type or indirection level depending on flags. |
| 157 | if showTypes && !f.ignoreNextType { |
| 158 | f.fs.Write(openParenBytes) |
| 159 | f.fs.Write(bytes.Repeat(asteriskBytes, indirects)) |
| 160 | f.fs.Write([]byte(ve.Type().String())) |
| 161 | f.fs.Write(closeParenBytes) |
| 162 | } else { |