This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
forked from aristanetworks/goarista
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty.go
165 lines (155 loc) · 4.21 KB
/
pretty.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (C) 2015 Arista Networks, Inc.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package test
import (
"fmt"
"os"
"reflect"
"sort"
"strconv"
"github.com/aristanetworks/goarista/areflect"
)
// PrettyPrint tries to display a human readable version of an interface
func PrettyPrint(v interface{}) string {
return PrettyPrintWithDepth(v, prettyPrintDepth)
}
// PrettyPrintWithDepth tries to display a human readable version of an interface
// and allows to define the depth of the print
func PrettyPrintWithDepth(v interface{}, depth int) string {
return prettyPrint(reflect.ValueOf(v), ptrSet{}, depth)
}
var prettyPrintDepth = 8
func init() {
d := os.Getenv("PPDEPTH")
if d, ok := strconv.Atoi(d); ok == nil && d >= 0 {
prettyPrintDepth = d
}
}
type ptrSet map[uintptr]struct{}
func prettyPrint(v reflect.Value, done ptrSet, depth int) string {
return prettyPrintWithType(v, done, depth, true)
}
func prettyPrintWithType(v reflect.Value, done ptrSet, depth int, showType bool) string {
if depth < 0 {
return "<max_depth>"
}
switch v.Kind() {
case reflect.Invalid:
return "nil"
case reflect.Bool:
return fmt.Sprintf("%t", v.Bool())
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Float32, reflect.Float64,
reflect.Int, reflect.Uint, reflect.Uintptr,
reflect.Complex64, reflect.Complex128:
i := areflect.ForceExport(v).Interface()
if showType {
return fmt.Sprintf("%s(%v)", v.Type().Name(), i)
}
return fmt.Sprintf("%v", i)
case reflect.String:
return fmt.Sprintf("%q", v.String())
case reflect.Ptr:
return "*" + prettyPrintWithType(v.Elem(), done, depth-1, showType)
case reflect.Interface:
return prettyPrintWithType(v.Elem(), done, depth-1, showType)
case reflect.Map:
var r []byte
r = append(r, []byte(v.Type().String())...)
r = append(r, '{')
var elems mapEntries
for _, k := range v.MapKeys() {
elem := &mapEntry{
k: prettyPrint(k, done, depth-1),
v: prettyPrint(v.MapIndex(k), done, depth-1),
}
elems.entries = append(elems.entries, elem)
}
sort.Sort(&elems)
for i, e := range elems.entries {
if i > 0 {
r = append(r, []byte(", ")...)
}
r = append(r, []byte(e.k)...)
r = append(r, ':')
r = append(r, []byte(e.v)...)
}
r = append(r, '}')
return string(r)
case reflect.Struct:
// Circular dependency?
if v.CanAddr() {
ptr := v.UnsafeAddr()
if _, ok := done[ptr]; ok {
return fmt.Sprintf("%s{<circular dependency>}", v.Type().String())
}
done[ptr] = struct{}{}
}
var r []byte
r = append(r, []byte(v.Type().String())...)
r = append(r, '{')
for i := 0; i < v.NumField(); i++ {
if i > 0 {
r = append(r, []byte(", ")...)
}
sf := v.Type().Field(i)
r = append(r, sf.Name...)
r = append(r, ':')
r = append(r, prettyPrint(v.Field(i), done, depth-1)...)
}
r = append(r, '}')
return string(r)
case reflect.Chan:
var ptr, bufsize string
if v.Pointer() == 0 {
ptr = "nil"
} else {
ptr = fmt.Sprintf("0x%x", v.Pointer())
}
if v.Cap() > 0 {
bufsize = fmt.Sprintf("[%d]", v.Cap())
}
return fmt.Sprintf("(%s)(%s)%s", v.Type().String(), ptr, bufsize)
case reflect.Func:
return "func(...)"
case reflect.Array, reflect.Slice:
l := v.Len()
var r []byte
if v.Type().Elem().Kind() == reflect.Uint8 && v.Kind() != reflect.Array {
b := areflect.ForceExport(v).Interface().([]byte)
r = append(r, []byte(`[]byte(`)...)
if b == nil {
r = append(r, []byte("nil")...)
} else {
r = append(r, []byte(fmt.Sprintf("%q", b))...)
}
r = append(r, ')')
return string(r)
}
r = append(r, []byte(v.Type().String())...)
r = append(r, '{')
for i := 0; i < l; i++ {
if i > 0 {
r = append(r, []byte(", ")...)
}
r = append(r, prettyPrintWithType(v.Index(i), done, depth-1, false)...)
}
r = append(r, '}')
return string(r)
case reflect.UnsafePointer:
var ptr string
if v.Pointer() == 0 {
ptr = "nil"
} else {
ptr = fmt.Sprintf("0x%x", v.Pointer())
}
if showType {
ptr = fmt.Sprintf("(unsafe.Pointer)(%s)", ptr)
}
return ptr
default:
panic(fmt.Errorf("Unhandled kind of reflect.Value: %v", v.Kind()))
}
}