forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.go
189 lines (170 loc) · 4.3 KB
/
formatter.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package gorgonia
import (
"bytes"
"fmt"
"reflect"
)
type mapFmt struct {
m reflect.Value // map
}
// FmtNodeMap is a convenience function to print map[*Node]<T>
//
// The fmt flag that makes it all nicely formatted is "-". Because a map consists of two types (key's type
// and val's type), and the Go fmt verb doesn't quite allow us to do something like "%ds", a hack is introduced
// to enable nicer printing of map[*Node]<T>
//
// Here's the hack:
// The "#" flag is used to indicate if the map will use the Node's ID or Name when formatting the map.
// %-v nodeName:%v
// %-#v nodeID:%v
// %-d nodeName:%x
// %-#d nodeID: %x
// %-p nodeName:%p
// %-#p nodeID:%p
//
// If the "-" flag is not found, then the formatter returns the default Go format for map[<T>]<T2>
func FmtNodeMap(m interface{}) mapFmt {
refVal := reflect.ValueOf(m)
if refVal.Kind() != reflect.Map {
panic("Only expect maps in FmtNodeMap")
}
t := refVal.Type()
keyType := t.Key()
var n *Node
if keyType != reflect.TypeOf(n) {
panic("Only expected map[*Node]<T>")
}
return mapFmt{
m: refVal,
}
}
func (mf mapFmt) defaultFmt(s fmt.State, c rune) {
var buf bytes.Buffer
buf.WriteRune('%')
for i := 0; i < 128; i++ {
if s.Flag(i) {
buf.WriteByte(byte(i))
}
}
if w, ok := s.Width(); ok {
fmt.Fprintf(&buf, "%d", w)
}
if p, ok := s.Precision(); ok {
fmt.Fprintf(&buf, ".%d", p)
}
buf.WriteRune(c)
fmt.Fprintf(s, buf.String(), mf.m)
}
func (mf mapFmt) format(s fmt.State, c rune) string {
var tmpl string
switch {
case c == 'v':
if s.Flag('#') {
tmpl = "\t%x: %v\n"
} else {
tmpl = "\t%s: %v\n"
}
case c == 'd':
if s.Flag('#') {
tmpl = "\t%x: %x\n"
} else {
tmpl = "\t%s: %x\n"
}
case c == 'p':
if s.Flag('#') {
tmpl = "\t%x: %p\n"
} else {
tmpl = "\t%s: %p\n"
}
default:
tmpl = "\t%s: %s\n"
}
return tmpl
}
func (mf mapFmt) Format(s fmt.State, c rune) {
refVal := mf.m
var n *Node
t := refVal.Type()
keyType := t.Key()
if keyType != reflect.TypeOf(n) {
panic("Only map[*Node]<T> is expected")
}
tmpl := mf.format(s, c)
keys := refVal.MapKeys()
if s.Flag('-') {
if s.Flag('#') {
// then key, will try its best to be a number
fmt.Fprintf(s, "map[Node.ID]%s {\n", t.Elem())
for i := 0; i < refVal.Len(); i++ {
key := keys[i]
val := refVal.MapIndex(key)
meth := key.MethodByName("ID")
id := meth.Call(nil)[0]
valType := val.Type()
if valType == reflect.TypeOf(n) {
switch c {
case 'd':
valMeth := val.MethodByName("ID")
valID := valMeth.Call(nil)[0]
fmt.Fprintf(s, tmpl, id, valID)
default:
strMeth := val.MethodByName("String")
str := strMeth.Call(nil)[0]
fmt.Fprintf(s, tmpl, id, str)
}
} else {
if _, ok := valType.MethodByName("Format"); ok {
fmt.Fprintf(s, "\t%x: ", id)
fmtMeth := val.MethodByName("Format")
fmtMeth.Call([]reflect.Value{reflect.ValueOf(s), reflect.ValueOf(c)})
fmt.Fprintf(s, "\n")
} else if _, ok := valType.MethodByName("String"); ok {
strMeth := val.MethodByName("String")
str := strMeth.Call(nil)[0]
fmt.Fprintf(s, tmpl, id, str)
} else {
fmt.Fprintf(s, tmpl, id, val)
}
}
}
s.Write([]byte("}"))
} else {
fmt.Fprintf(s, "map[Node.Name]%s {\n", t.Elem())
for i := 0; i < refVal.Len(); i++ {
key := keys[i]
val := refVal.MapIndex(key)
meth := key.MethodByName("String")
id := meth.Call(nil)[0]
valType := val.Type()
if valType == reflect.TypeOf(n) {
switch c {
case 'd':
valMeth := val.MethodByName("ID")
valID := valMeth.Call(nil)[0]
fmt.Fprintf(s, tmpl, id, valID)
default:
strMeth := val.MethodByName("String")
str := strMeth.Call(nil)[0]
fmt.Fprintf(s, tmpl, id, str)
}
} else {
if _, ok := valType.MethodByName("Format"); ok {
fmt.Fprintf(s, "\t%s: ", id)
fmtMeth := val.MethodByName("Format")
fmtMeth.Call([]reflect.Value{reflect.ValueOf(s), reflect.ValueOf(c)})
fmt.Fprintf(s, "\n")
} else if _, ok := valType.MethodByName("String"); ok {
strMeth := val.MethodByName("String")
str := strMeth.Call(nil)[0]
fmt.Fprintf(s, tmpl, id, str)
} else {
fmt.Fprintf(s, tmpl, id, val)
}
}
}
s.Write([]byte("}"))
}
return
}
mf.defaultFmt(s, c)
}