Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- +// formatFakePointers controls whether to substitute pointer addresses with nil.
- +// This is used for deterministic testing.
- +var formatFakePointers = false
- var stringerIface = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
- -func prettyPrint(v reflect.Value, useStringer bool) string {
- - return formatAny(v, formatConfig{useStringer, true, true, true}, nil)
- +// Format formats the value v as a string.
- +//
- +// This is similar to fmt.Sprintf("%+v", v) except this:
- +// * Prints the type unless it can be elided
- +// * Avoids printing struct fields that are zero
- +// * Prints a nil-slice as being nil, not empty
- +// * Prints map entries in deterministic order
- +func Format(v reflect.Value, useStringer bool) string {
- + return formatAny(v, formatConfig{useStringer, true, true, !formatFakePointers}, nil)
- }
- type formatConfig struct {
- @@ -69,12 +36,6 @@ type formatConfig struct {
- realPointers bool // Should we print the real address of pointers?
- }
- -// formatAny prints the value v in a pretty formatted manner.
- -// This is similar to fmt.Sprintf("%+v", v) except this:
- -// * Prints the type unless it can be elided.
- -// * Avoids printing struct fields that are zero.
- -// * Prints a nil-slice as being nil, not empty.
- -// * Prints map entries in deterministic order.
- func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) string {
- // TODO: Should this be a multi-line printout in certain situations?
- @@ -90,20 +51,21 @@ func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) str
- switch v.Kind() {
- case reflect.Bool:
- - return fmt.Sprint(v.Bool())
- + return formatPrimitive(v.Type(), v.Bool(), conf)
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- - return fmt.Sprint(v.Int())
- + return formatPrimitive(v.Type(), v.Int(), conf)
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- if v.Type().PkgPath() == "" || v.Kind() == reflect.Uintptr {
- - return formatHex(v.Uint()) // Unnamed uints are usually bytes or words
- + // Unnamed uints are usually bytes or words, so use hexadecimal.
- + return formatPrimitive(v.Type(), formatHex(v.Uint()), conf)
- }
- - return fmt.Sprint(v.Uint()) // Named uints are usually enumerations
- + return formatPrimitive(v.Type(), v.Uint(), conf)
- case reflect.Float32, reflect.Float64:
- - return fmt.Sprint(v.Float())
- + return formatPrimitive(v.Type(), v.Float(), conf)
- case reflect.Complex64, reflect.Complex128:
- - return fmt.Sprint(v.Complex())
- + return formatPrimitive(v.Type(), v.Complex(), conf)
- case reflect.String:
- - return fmt.Sprintf("%q", v)
- + return formatPrimitive(v.Type(), fmt.Sprintf("%q", v), conf)
- case reflect.UnsafePointer, reflect.Chan, reflect.Func:
- return formatPointer(v, conf)
- case reflect.Ptr:
- @@ -166,7 +128,7 @@ func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) str
- var ss []string
- subConf := conf
- subConf.printType = v.Type().Elem().Kind() == reflect.Interface
- - for _, k := range sortKeys(v.MapKeys()) {
- + for _, k := range SortKeys(v.MapKeys()) {
- sk := formatAny(k, formatConfig{realPointers: conf.realPointers}, visited)
- sv := formatAny(v.MapIndex(k), subConf, visited)
- ss = append(ss, fmt.Sprintf("%s: %s", sk, sv))
- @@ -200,6 +162,13 @@ func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) str
- }
- }
- +func formatPrimitive(t reflect.Type, v interface{}, conf formatConfig) string {
- + if conf.printType && t.PkgPath() != "" {
- + return fmt.Sprintf("%v(%v)", t, v)
- + }
- + return fmt.Sprintf("%v", v)
- +}
- +
- func formatPointer(v reflect.Value, conf formatConfig) string {
- p := v.Pointer()
- if !conf.realPointers {
- @@ -282,100 +251,8 @@ func isZero(v reflect.Value) bool {
- return false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement