-
Notifications
You must be signed in to change notification settings - Fork 12
/
ast.go
68 lines (62 loc) · 1.93 KB
/
ast.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
package main
import (
"go/ast"
"go/token"
"strings"
)
// genDecl processes one declaration clause.
func (f *File) genDecl(node ast.Node) bool {
decl, ok := node.(*ast.GenDecl)
// Token must be in IMPORT, CONST, TYPE, VAR
if !ok || decl.Tok != token.TYPE {
// We only care about const declarations.
return true
}
// The name of the type of the constants we are declaring.
// Can change if this is a multi-element declaration.
typ := ""
// Loop over the elements of the declaration. Each element is a ValueSpec:
// a list of names possibly followed by a type, possibly followed by values.
// If the type and value are both missing, we carry down the type (and value,
// but the "go/types" package takes care of that).
for _, spec := range decl.Specs {
tspec := spec.(*ast.TypeSpec) // Guaranteed to succeed as this is TYPE.
typ = tspec.Name.Name
sExpr, ok := tspec.Type.(*ast.SelectorExpr)
if !ok {
continue
}
if sExpr.X.(*ast.Ident).Name == "sync" && sExpr.Sel.Name == "Map" {
if typ != f.typeInfo.mapName {
// This is not the type we're looking for.
continue
}
v := Value{
originalName: typ,
str: typ,
keyImport: f.typeInfo.keyImport,
keyType: f.typeInfo.keyType,
keyIsPointer: f.typeInfo.keyIsPointer,
keyTypePrefix: f.typeInfo.keyTypePrefix,
valueImport: f.typeInfo.valueImport,
valueType: f.typeInfo.valueType,
valueIsPointer: f.typeInfo.valueIsPointer,
valueTypePrefix: f.typeInfo.valueTypePrefix,
}
if c := tspec.Comment; f.lineComment && c != nil && len(c.List) == 1 {
v.name = strings.TrimSpace(c.Text())
} else {
v.name = strings.TrimPrefix(v.originalName, f.trimPrefix)
}
v.mapName = v.name
if strings.TrimSpace(v.keyType) == "" {
v.keyType = "interface{}"
}
if strings.TrimSpace(v.valueType) == "" {
v.valueType = "interface{}"
}
f.values = append(f.values, v)
}
}
return false
}