-
Notifications
You must be signed in to change notification settings - Fork 2
/
printer.go
250 lines (227 loc) · 5.1 KB
/
printer.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"fmt"
"go/ast"
"go/token"
"io"
"strings"
)
func printFuncDecl(v *ast.FuncDecl) { fmt.Println(printFunc(v.Recv, v.Name, v.Type)) }
func printFunc(recv *ast.FieldList, name *ast.Ident, typ *ast.FuncType) string {
var s strings.Builder
s.WriteString("func ")
if name != nil {
if recv := fieldListType(recv); recv != "" {
s.WriteByte('(')
s.WriteString(recv)
s.WriteByte(')')
s.WriteByte(' ')
}
s.WriteString(name.Name)
}
s.WriteByte('(')
writeArgs(&s, describeFieldList(typ.Params))
s.WriteByte(')')
if typ.Results != nil && len(typ.Results.List) > 0 {
s.WriteByte(' ')
s.WriteByte('(')
writeArgs(&s, describeFieldList(typ.Results))
s.WriteByte(')')
}
return s.String()
}
func printValueSpec(v *ast.ValueSpec) { fmt.Println(printValue(v)) }
func printValue(v *ast.ValueSpec) string {
var s strings.Builder
t := describeType(v.Type)
// TODO(gbbr): doesn't work for untyped values (var A = ...)
for _, name := range v.Names {
if !ast.IsExported(name.Name) {
continue
}
if name.Obj != nil {
switch name.Obj.Kind {
case ast.Con:
s.WriteString("const ")
case ast.Var:
s.WriteString("var ")
}
}
s.WriteString(name.Name)
s.WriteByte(' ')
s.WriteString(t)
}
if s.Len() > 0 {
return s.String()
}
return ""
}
func printTypeSpec(v *ast.TypeSpec) { fmt.Println(printType(v)) }
func printType(v *ast.TypeSpec) string {
return fmt.Sprintf("type %s %s", v.Name.Name, describeType(v.Type))
}
// writeArgs pretty-prints a list of arguments (e.g. ["1", "2", "3"] => "1, 2, 3")
func writeArgs(s io.StringWriter, args []string) {
for i, arg := range args {
s.WriteString(arg)
if i < len(args)-1 {
s.WriteString(", ")
}
}
}
func fieldListType(list *ast.FieldList) string {
types := describeFieldList(list)
if len(types) == 0 {
return ""
}
return types[0]
}
func describeFieldList(list *ast.FieldList) []string {
if list == nil {
return nil
}
names := make([]string, 0, len(list.List))
for _, field := range list.List {
if field == nil {
continue
}
names = append(names, describeType(field.Type))
}
return names
}
func describeType(t ast.Expr) string {
var name string
ast.Inspect(t, func(n ast.Node) bool {
if n == nil {
return false
}
switch v := n.(type) {
case *ast.Ident:
name = v.Name
return false
case *ast.ChanType:
name = "chan"
if v.Arrow != token.NoPos {
switch v.Dir {
case ast.SEND:
name = "chan<-"
case ast.RECV:
name = "<-chan"
}
}
return false
case *ast.MapType:
name = "map[" + describeType(v.Key) + "]" + describeType(v.Value)
return false
case *ast.FuncType:
var s1 strings.Builder
writeArgs(&s1, describeFieldList(v.Params))
if v.Results != nil && len(v.Results.List) > 0 {
var s2 strings.Builder
writeArgs(&s2, describeFieldList(v.Results))
name = fmt.Sprintf("F(%v) (%v)", s1.String(), s2.String())
return false
}
name = fmt.Sprintf("func(%v)", s1.String())
return false
case *ast.InterfaceType:
name = describeInterfaceType(v)
return false
case *ast.StructType:
name = describeStructType(v)
return false
case *ast.ArrayType:
if v.Len == nil {
name = "[]" + describeType(v.Elt)
return false
}
switch x := v.Len.(type) {
case *ast.Ellipsis:
name = "[...]" + describeType(v.Elt)
return false
case *ast.BasicLit:
name = "[" + x.Value + "]" + describeType(v.Elt)
return false
case *ast.Ident:
name = "[" + x.Name + "]" + describeType(v.Elt)
return false
default:
// TODO: this can't stay
panic(fmt.Sprintf("unexpected array type %T: [%#v]%s\n", v.Len, v.Len, describeType(v.Elt)))
}
case *ast.Ellipsis:
name = "..." + describeType(v.Elt)
return false
case *ast.SelectorExpr:
name = describeType(v.X) + "." + describeType(v.Sel)
return false
case *ast.StarExpr:
name = "*" + describeType(v.X)
return false
default:
panic(fmt.Sprintf("unhandled type: %#v", t))
}
return true
})
return name
}
func describeStructType(v *ast.StructType) string {
if v.Fields == nil {
return "struct{}"
}
var s strings.Builder
s.WriteString("struct{")
for i, field := range v.Fields.List {
if len(field.Names) == 0 {
// should not be possible
continue
}
var j int
for _, name := range field.Names {
if !ast.IsExported(name.Name) {
continue
}
if j == 0 {
if i == 0 {
s.WriteByte('\n')
}
s.WriteByte('\t')
}
if j > 0 {
s.WriteString(", ")
}
s.WriteString(name.Name)
j++
}
if j == 0 {
continue
}
s.WriteByte(' ')
s.WriteString(describeType(field.Type))
s.WriteByte('\n')
}
s.WriteByte('}')
return s.String()
}
func describeInterfaceType(v *ast.InterfaceType) string {
if v.Methods == nil {
return "interface{}"
}
var s strings.Builder
s.WriteString("interface{")
for i, field := range v.Methods.List {
if i == 0 {
s.WriteByte('\n')
}
ft, ok := field.Type.(*ast.FuncType)
if !ok {
// should not be possible
continue
}
s.WriteByte('\t')
s.WriteString(printFunc(nil, field.Names[0], ft))
s.WriteByte('\n')
}
s.WriteByte('}')
return s.String()
}