-
Notifications
You must be signed in to change notification settings - Fork 0
/
patcher.go
707 lines (628 loc) · 19.3 KB
/
patcher.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
package patch
import (
"bytes"
"fmt"
"github.com/hopeio/cherry/tools/protoc/protoc-gen-go-patch/patch/ident"
"go/ast"
"go/format"
"go/parser"
"go/token"
"go/types"
"log"
"path/filepath"
"regexp"
"strings"
gopb "github.com/hopeio/cherry/protobuf/utils/patch"
"golang.org/x/tools/go/ast/astutil"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/pluginpb"
)
// Patcher patches a set of generated Go Protobuf files with additional features:
// - go_name (Field option) overrides the name of a synthesized struct field and getters.
// - go_tags (Field option) lets you add additional struct tags to a field.
// - go_oneof_name (Oneof option) overrides the name of a oneof field, including wrapper types and getters.
// - go_oneof_tags (Oneof option) lets you specify additional struct tags on a oneof field.
// - go_message_name (Message option) overrides the name of the synthesized struct.
// - go_enum_name (Enum option) overrides the name of an enum type.
// - go_value_name (EnumValue option) overrides the name of an enum const.
type Patcher struct {
gen *protogen.Plugin
fset *token.FileSet
files []*ast.File
filesByName map[string]*ast.File
info *types.Info
packages []*Package
packagesByPath map[string]*Package
packagesByName map[string]*Package
renames map[protogen.GoIdent]string
typeRenames map[protogen.GoIdent]string
valueRenames map[protogen.GoIdent]string
fieldRenames map[protogen.GoIdent]string
methodRenames map[protogen.GoIdent]string
objectRenames map[types.Object]string
tags map[protogen.GoIdent]string
fieldTags map[types.Object]string
fileOptions *gopb.FileOptions
}
// NewPatcher returns an initialized Patcher for gen.
func NewPatcher(gen *protogen.Plugin) (*Patcher, error) {
p := &Patcher{
gen: gen,
packagesByPath: make(map[string]*Package),
packagesByName: make(map[string]*Package),
renames: make(map[protogen.GoIdent]string),
typeRenames: make(map[protogen.GoIdent]string),
valueRenames: make(map[protogen.GoIdent]string),
fieldRenames: make(map[protogen.GoIdent]string),
methodRenames: make(map[protogen.GoIdent]string),
objectRenames: make(map[types.Object]string),
tags: make(map[protogen.GoIdent]string),
fieldTags: make(map[types.Object]string),
}
return p, p.scan()
}
func (p *Patcher) scan() error {
for _, f := range p.gen.Files {
p.scanFile(f)
}
return nil
}
func (p *Patcher) scanFile(f *protogen.File) {
log.Printf("\nScan proto:\t%s", f.Desc.Path())
_ = p.getPackage(string(f.GoImportPath), string(f.GoPackageName), true)
p.fileOptions = fileOptions(f)
for _, e := range f.Enums {
p.scanEnum(e)
}
for _, m := range f.Messages {
p.scanMessage(m, nil)
}
for _, e := range f.Extensions {
p.scanExtension(e)
}
// TODO: scan gRPC services
}
func (p *Patcher) scanEnum(e *protogen.Enum) {
opts := enumOptions(e)
newName := opts.GetName()
if newName != "" {
p.RenameType(e.GoIdent, newName) // Enum type
p.RenameValue(ident.WithSuffix(e.GoIdent, "_name"), newName+"_name") // Enum name map
p.RenameValue(ident.WithSuffix(e.GoIdent, "_value"), newName+"_value") // Enum value map
}
stringerName := opts.GetStringerName()
if stringerName != "" {
p.RenameMethod(ident.WithChild(e.GoIdent, "String"), stringerName)
}
for _, v := range e.Values {
p.scanEnumValue(v)
}
}
func (p *Patcher) scanEnumValue(v *protogen.EnumValue) {
e := v.Parent
opts := valueOptions(v)
newName := opts.GetName()
if newName == "" && p.isRenamed(e.GoIdent) {
newName = replacePrefix(v.GoIdent.GoName, e.GoIdent.GoName, p.nameFor(e.GoIdent))
} else {
if p.fileOptions.GetNoEnumPrefix() {
newName = replacePrefix(v.GoIdent.GoName, e.GoIdent.GoName+"_", "")
}
}
if newName != "" {
p.RenameValue(v.GoIdent, newName) // Value const
}
}
func (p *Patcher) scanMessage(m *protogen.Message, parent *protogen.Message) {
opts := messageOptions(m)
newName := opts.GetName()
if newName == "" && parent != nil && p.isRenamed(parent.GoIdent) {
newName = replacePrefix(m.GoIdent.GoName, parent.GoIdent.GoName, p.nameFor(parent.GoIdent))
}
if newName != "" {
p.RenameType(m.GoIdent, newName) // Message struct
}
for _, o := range m.Oneofs {
p.scanOneof(o)
}
for _, f := range m.Fields {
p.scanField(f)
}
for _, mm := range m.Messages {
p.scanMessage(mm, m)
}
}
func replacePrefix(s, prefix, with string) string {
return with + strings.TrimPrefix(s, prefix)
}
func (p *Patcher) scanOneof(o *protogen.Oneof) {
m := o.Parent
opts := oneofOptions(o)
newName := opts.GetName()
if newName == "" && p.isRenamed(m.GoIdent) {
// Implicitly rename this oneof field because its parent message was renamed.
newName = o.GoName
}
if newName != "" {
p.RenameField(ident.WithChild(m.GoIdent, o.GoName), newName) // Oneof
p.RenameMethod(ident.WithChild(m.GoIdent, "Get"+o.GoName), "Get"+newName) // Getter
ifName := ident.WithPrefix(o.GoIdent, "is")
newIfName := "is" + p.nameFor(m.GoIdent) + "_" + newName
p.RenameType(ifName, newIfName) // Interface type (e.g. isExample_Person)
p.RenameMethod(ident.WithChild(ifName, ifName.GoName), newIfName) // Interface method
}
tags := opts.GetTags()
if tags != "" {
p.Tag(ident.WithChild(m.GoIdent, o.GoName), tags)
}
}
func (p *Patcher) scanField(f *protogen.Field) {
m := f.Parent
o := f.Oneof
opts := fieldOptions(f)
newName := opts.GetName()
if newName == "" && o != nil && (p.isRenamed(m.GoIdent) || p.isRenamed(o.GoIdent)) {
// Implicitly rename this oneof field because its parent(s) were renamed.
newName = f.GoName
}
if newName != "" {
if o != nil {
p.RenameType(f.GoIdent, p.nameFor(m.GoIdent)+"_"+newName) // Oneof wrapper struct
p.RenameField(ident.WithChild(f.GoIdent, f.GoName), newName) // Oneof wrapper field
ifName := ident.WithPrefix(o.GoIdent, "is")
p.RenameMethod(ident.WithChild(f.GoIdent, ifName.GoName), p.nameFor(ifName)) // Oneof interface method
} else {
p.RenameField(ident.WithChild(m.GoIdent, f.GoName), newName) // Field
}
p.RenameMethod(ident.WithChild(m.GoIdent, "Get"+f.GoName), "Get"+newName) // Getter
}
tags := opts.GetTags()
if tags != "" {
if o != nil {
p.Tag(ident.WithChild(f.GoIdent, f.GoName), tags) // Oneof wrapper field tags
} else {
p.Tag(ident.WithChild(m.GoIdent, f.GoName), tags) // Field tags
}
}
}
func (p *Patcher) scanExtension(f *protogen.Field) {
opts := fieldOptions(f)
newName := opts.GetName()
if newName != "" {
id := f.GoIdent
id.GoName = f.GoName
p.RenameValue(ident.WithPrefix(id, "E_"), newName)
}
}
// RenameType renames the Go type specified by id to newName.
// The id argument specifies a GoName from GoImportPath, e.g.: "github.com/org/repo/example".FooMessage
// To rename a package-level identifier such as a type, var, or const, specify just the name, e.g. "Message" or "Enum_VALUE".
// newName should be the unqualified name.
// The value of id.GoName should be the original generated type name, not a renamed type.
func (p *Patcher) RenameType(id protogen.GoIdent, newName string) {
p.renames[id] = newName
p.typeRenames[id] = newName
log.Printf("Rename type:\t%s.%s → %s", id.GoImportPath, id.GoName, newName)
}
// RenameValue renames the Go value (const or var) specified by id to newName.
// The id argument specifies a GoName from GoImportPath, e.g.: "github.com/org/repo/example".FooValue
// newName should be the unqualified name.
// The value of id.GoName should be the original generated type name, not a renamed type.
func (p *Patcher) RenameValue(id protogen.GoIdent, newName string) {
p.renames[id] = newName
p.valueRenames[id] = newName
log.Printf("Rename value:\t%s.%s → %s", id.GoImportPath, id.GoName, newName)
}
// RenameField renames the Go struct field specified by id to newName.
// The id argument specifies a GoName from GoImportPath, e.g.: "github.com/org/repo/example".FooMessage.BarField
// newName should be the unqualified name (after the dot).
// The value of id.GoName should be the original generated identifier name, not a renamed identifier.
func (p *Patcher) RenameField(id protogen.GoIdent, newName string) {
p.renames[id] = newName
p.fieldRenames[id] = newName
log.Printf("Rename field:\t%s.%s → %s", id.GoImportPath, id.GoName, newName)
}
// RenameMethod renames the Go struct or interface method specified by id to newName.
// The id argument specifies a GoName from GoImportPath, e.g.: "github.com/org/repo/example".FooMessage.GetBarField
// The new name should be the unqualified name (after the dot).
// The value of id.GoName should be the original generated identifier name, not a renamed identifier.
func (p *Patcher) RenameMethod(id protogen.GoIdent, newName string) {
p.renames[id] = newName
p.methodRenames[id] = newName
log.Printf("Rename method:\t%s.%s → %s", id.GoImportPath, id.GoName, newName)
}
func (p *Patcher) isRenamed(id protogen.GoIdent) bool {
_, ok := p.renames[id]
return ok
}
func (p *Patcher) nameFor(id protogen.GoIdent) string {
if name, ok := p.renames[id]; ok {
return name
}
return ident.LeafName(id)
}
// Tag adds the specified struct tags to the field specified by selector,
// in the form of "Message.Field". The tags argument should omit outer backticks (`).
// The value of id.GoName should be the original generated identifier name, not a renamed identifier.
// The struct tags will be applied when Patch is called.
func (p *Patcher) Tag(id protogen.GoIdent, tags string) {
p.tags[id] = tags
log.Printf("Tags:\t%s.%s `%s`", id.GoImportPath, id.GoName, tags)
}
// Patch applies the patch(es) in p the Go files in res.
// Clone res before calling Patch if you want to retain an unmodified copy.
// The behavior of calling Patch multiple times is currently undefined.
func (p *Patcher) Patch(res *pluginpb.CodeGeneratorResponse) error {
if err := p.parseGoFiles(res); err != nil {
return err
}
if err := p.checkGoFiles(); err != nil {
return err
}
if err := p.patchGoFiles(); err != nil {
return err
}
return p.serializeGoFiles(res)
}
func (p *Patcher) parseGoFiles(res *pluginpb.CodeGeneratorResponse) error {
p.fset = token.NewFileSet()
p.files = nil
p.filesByName = make(map[string]*ast.File)
for _, rf := range res.File {
if rf.Name == nil || !strings.HasSuffix(*rf.Name, ".go") || rf.Content == nil {
continue
}
f, err := p.parseGoFile(*rf.Name, *rf.Content)
if err != nil {
return err
}
// TODO: should we cache these?
p.files = append(p.files, f)
p.filesByName[*rf.Name] = f
// FIXME: this will break if the package’s implicit name differs from the types.Package name.
if pkg, ok := p.packagesByName[f.Name.Name]; ok {
pkg.AddFile(*rf.Name, f)
} else {
return fmt.Errorf("unknown package: %s", f.Name.Name)
}
}
return nil
}
func (p *Patcher) checkGoFiles() error {
// Type-check Go packages first to find any missing identifiers.
if err := p.checkPackages(); err != nil {
return err
}
var recheck bool
// Find missing type declarations.
for id := range p.typeRenames {
if obj, _ := p.find(id); obj != nil {
continue
}
if err := p.synthesize(id); err != nil {
return err
}
recheck = true
}
// Find missing value declarations.
for id := range p.valueRenames {
if obj, _ := p.find(id); obj != nil {
continue
}
if err := p.synthesize(id); err != nil {
return err
}
recheck = true
}
// Re-type-check if necessary.
if recheck {
if err := p.checkPackages(); err != nil {
return err
}
}
// Map renames.
for id, name := range p.renames {
obj, _ := p.find(id)
if obj == nil {
continue
}
p.objectRenames[obj] = name
}
// Map struct tags.
for id, tags := range p.tags {
obj, _ := p.find(id)
if obj == nil {
continue
}
p.fieldTags[obj] = tags
}
return nil
}
func (p *Patcher) parseGoFile(filename string, src interface{}) (*ast.File, error) {
f, err := parser.ParseFile(p.fset, filename, src, parser.ParseComments)
if err != nil {
return nil, err
}
log.Printf("\nParse Go:\t%s\n", filename)
return f, nil
}
func (p *Patcher) checkPackages() error {
p.info = &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
}
for _, pkg := range p.packages {
pkg.Reset()
}
for _, pkg := range p.packages {
err := pkg.Check(importer{p}, p.fset, p.info)
if err != nil {
return err
}
}
return nil
}
func (p *Patcher) synthesize(id protogen.GoIdent) error {
pkg := p.getPackage(string(id.GoImportPath), id.GoName, true)
// Already synthesized?
filename := pkg.pkg.Name() + "/" + id.GoName + ".synthetic.go"
if f := pkg.File(filename); f != nil {
return nil
}
// Synthesize a Go file for this identifier.
b := &bytes.Buffer{}
fmt.Fprintf(b, "package %s\n\n", pkg.pkg.Name())
names := strings.Split(id.GoName, ".")
if len(names) == 1 {
// Type or value.
// Synthesize a Go type as a map so subscript access works, e.g.: foo[key]
fmt.Fprintf(b, "type %s map[interface{}]interface{}\n", names[0])
} else {
// Field or method.
// Synthesize a Go method so a non-call expr works, e.g.: foo.Method
fmt.Fprintf(b, "func (%s) %s() {}\n", names[0], names[1])
}
log.Printf("\nGenerated Go code: %s\n\n%s\n", filename, b.String())
// Parse and add it to pkg.
f, err := p.parseGoFile(filename, b)
if err != nil {
return err
}
return pkg.AddFile(filename, f)
}
// find finds id in all parsed Go packages, along with any ancestor(s),
// or nil if the id is not found.
func (p *Patcher) find(id protogen.GoIdent) (obj types.Object, ancestors []types.Object) {
pkg := p.getPackage(string(id.GoImportPath), "", false)
if pkg == nil {
return
}
return pkg.Find(id)
}
// getPackage finds a getPackage with path, or creates it if it doesn’t exist.
// If name is empty, getPackage will use the last path element as the package name.
func (p *Patcher) getPackage(path, name string, create bool) *Package {
if pkg, ok := p.packagesByPath[path]; ok {
return pkg
}
if !create {
return nil
}
if name == "" {
name = filepath.Base(path)
}
pkg := NewPackage(path, name)
name = pkg.pkg.Name() // Get real name
p.packages = append(p.packages, pkg)
p.packagesByPath[path] = pkg
p.packagesByName[name] = pkg
return pkg
}
func (p *Patcher) serializeGoFiles(res *pluginpb.CodeGeneratorResponse) error {
for _, rf := range res.File {
if rf.Name == nil || !strings.HasSuffix(*rf.Name, ".go") || rf.Content == nil {
continue
}
log.Printf("\nSerialize:\t%s\n", *rf.Name)
f := p.filesByName[*rf.Name]
if f == nil {
continue // Should never happen
}
var b strings.Builder
err := format.Node(&b, p.fset, f)
if err != nil {
return err
}
content := b.String()
rf.Content = &content
}
return nil
}
func (p *Patcher) patchGoFiles() error {
log.Printf("\nDefs")
for id, obj := range p.info.Defs {
p.patchIdent(id, obj)
}
log.Printf("\nUses\n")
for id, obj := range p.info.Uses {
p.patchIdent(id, obj)
}
log.Printf("\nUnresolved\n")
for _, f := range p.files {
for _, id := range f.Unresolved {
p.patchIdent(id, nil)
}
}
return nil
}
func (p *Patcher) patchIdent(id *ast.Ident, obj types.Object) {
// Renames
name := p.objectRenames[obj]
if name != "" {
log.Printf("Rename %s:\t%q.%s → %s", typeString(obj), obj.Pkg().Path(), id.Name, name)
p.patchComments(id, name)
id.Name = name
}
// Struct tags
tags := p.fieldTags[obj]
if tags != "" && id.Obj != nil {
v, ok := id.Obj.Decl.(*ast.Field)
if !ok {
log.Printf("Warning: struct tags declared for non-field object: %v `%s`", obj, tags)
} else {
if v.Tag == nil {
v.Tag = &ast.BasicLit{}
}
v.Tag.Value = mergeTags(v.Tag.Value, tags, p.fileOptions.GetNonOmitempty())
log.Printf("Add tags:\t%q.%s %s", obj.Pkg().Path(), id.Name, v.Tag.Value)
}
}
}
// mergeTags overrides tags
func mergeTags(oldTag, newTag string, nonOmit bool) string {
var tagKeys []string
var kvMap = make(map[string]string)
oldTag = strings.TrimSpace(strings.Trim(oldTag, "`"))
tagKeys = findTags(oldTag, tagKeys, kvMap)
if value, ok := kvMap["json"]; ok && nonOmit {
kvMap["json"] = value[:(len(value)-len(`,omitempty"`))] + `"`
}
newTag = strings.TrimSpace(strings.Trim(newTag, "`"))
tagKeys = findTags(newTag, tagKeys, kvMap)
var builder strings.Builder
for i, tag := range tagKeys {
builder.WriteString(tag)
builder.WriteRune(':')
builder.WriteString(kvMap[tag])
if i < len(tagKeys)-1 {
builder.WriteRune(' ')
}
}
return "`" + builder.String() + "`"
}
// findTags generate key-value map and record key sequence
func findTags(tag string, tagKeys []string, kvMap map[string]string) []string {
for tag != "" {
// Skip leading space.
i := 0
for i < len(tag) && tag[i] == ' ' {
i++
}
tag = tag[i:]
if tag == "" {
break
}
// Scan to colon. A space, a quote or a control character is a syntax error.
// Strictly speaking, control chars include the range [0x7f, 0x9f], not just
// [0x00, 0x1f], but in practice, we ignore the multi-byte control characters
// as it is simpler to inspect the tag's bytes than the tag's runes.
i = 0
for i < len(tag) && tag[i] > ' ' && tag[i] != ':' && tag[i] != '"' && tag[i] != 0x7f {
i++
}
if i == 0 || i+1 >= len(tag) || tag[i] != ':' || tag[i+1] != '"' {
tag = tag[i:]
continue
}
key := tag[:i]
tag = tag[i+1:]
// Scan quoted string to find value.
i = 1
for i < len(tag) && tag[i] != '"' {
if tag[i] == '\\' {
i++
}
i++
}
if i >= len(tag) {
break
}
value := tag[:i+1]
tag = tag[i+1:]
if _, ok := kvMap[key]; !ok {
tagKeys = append(tagKeys, key)
}
kvMap[key] = value
}
return tagKeys
}
func (p *Patcher) patchComments(id *ast.Ident, repl string) {
doc, comment := p.findCommentGroups(id)
if doc == nil && comment == nil {
return
}
x, err := regexp.Compile(`\b` + regexp.QuoteMeta(id.Name) + `\b`)
if err != nil {
return
}
log.Printf("Comment:\t%v → %s", x, repl)
patchCommentGroup(doc, x, repl)
patchCommentGroup(comment, x, repl)
}
// Borrowed from https://github.com/golang/tools/blob/HEAD/refactor/rename/rename.go#L543
func (p *Patcher) findCommentGroups(id *ast.Ident) (doc *ast.CommentGroup, comment *ast.CommentGroup) {
tf := p.fset.File(id.Pos())
if tf == nil {
return
}
f := p.filesByName[tf.Name()]
if f == nil {
return
}
nodes, _ := astutil.PathEnclosingInterval(f, id.Pos(), id.End())
for _, node := range nodes {
switch decl := node.(type) {
case *ast.FuncDecl:
return decl.Doc, nil
case *ast.Field:
return decl.Doc, decl.Comment
case *ast.GenDecl:
return decl.Doc, nil
// For {Type,Value}Spec, if the doc on the spec is absent, search for the enclosing GenDecl.
case *ast.TypeSpec:
if decl.Doc != nil {
return decl.Doc, decl.Comment
}
case *ast.ValueSpec:
if decl.Doc != nil {
return decl.Doc, decl.Comment
}
case *ast.Ident:
default:
return
}
}
return
}
func patchCommentGroup(c *ast.CommentGroup, x *regexp.Regexp, repl string) {
if c == nil {
return
}
for _, c := range c.List {
c.Text = x.ReplaceAllString(c.Text, repl)
}
}
func typeString(obj types.Object) string {
switch obj.(type) {
case *types.PkgName:
return "package name"
case *types.TypeName:
return "type"
case *types.Var:
if obj.Parent() == nil {
return "field"
}
return "var"
case *types.Const:
return "const"
case *types.Func:
if obj.Parent() == nil {
return "method"
}
return "func"
case nil:
return "(nil)"
}
return obj.Type().String()
}