MCPcopy Index your code
hub / github.com/expr-lang/expr / format

Method format

internal/spew/format.go:201–367  ·  view source on GitHub ↗

format is the main workhorse for providing the Formatter interface. 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)

Source from the content-addressed store, hash-verified

199// dealing with and formats it appropriately. It is a recursive function,
200// however circular data structures are detected and handled properly.
201func (f *formatState) format(v reflect.Value) {
202 // Handle invalid reflect values immediately.
203 kind := v.Kind()
204 if kind == reflect.Invalid {
205 f.fs.Write(invalidAngleBytes)
206 return
207 }
208
209 // Handle pointers specially.
210 if kind == reflect.Ptr {
211 f.formatPtr(v)
212 return
213 }
214
215 // Print type information unless already handled elsewhere.
216 if !f.ignoreNextType && f.fs.Flag('#') {
217 f.fs.Write(openParenBytes)
218 f.fs.Write([]byte(v.Type().String()))
219 f.fs.Write(closeParenBytes)
220 }
221 f.ignoreNextType = false
222
223 // Call Stringer/error interfaces if they exist and the handle methods
224 // flag is enabled.
225 if !f.cs.DisableMethods {
226 if (kind != reflect.Invalid) && (kind != reflect.Interface) {
227 if handled := handleMethods(f.cs, f.fs, v); handled {
228 return
229 }
230 }
231 }
232
233 switch kind {
234 case reflect.Invalid:
235 // Do nothing. We should never get here since invalid has already
236 // been handled above.
237
238 case reflect.Bool:
239 printBool(f.fs, v.Bool())
240
241 case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
242 printInt(f.fs, v.Int(), 10)
243
244 case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
245 printUint(f.fs, v.Uint(), 10)
246
247 case reflect.Float32:
248 printFloat(f.fs, v.Float(), 32)
249
250 case reflect.Float64:
251 printFloat(f.fs, v.Float(), 64)
252
253 case reflect.Complex64:
254 printComplex(f.fs, v.Complex(), 32)
255
256 case reflect.Complex128:
257 printComplex(f.fs, v.Complex(), 64)
258

Callers 4

TestAddedReflectValueFunction · 0.95
formatPtrMethod · 0.95
FormatMethod · 0.95
TestInvalidReflectValueFunction · 0.95

Calls 15

formatPtrMethod · 0.95
unpackValueMethod · 0.95
buildDefaultFormatMethod · 0.95
handleMethodsFunction · 0.85
printBoolFunction · 0.85
printIntFunction · 0.85
printUintFunction · 0.85
printFloatFunction · 0.85
printComplexFunction · 0.85
sortValuesFunction · 0.85
printHexPtrFunction · 0.85
WriteMethod · 0.80

Tested by 2

TestAddedReflectValueFunction · 0.76
TestInvalidReflectValueFunction · 0.76