-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgo_field.go
78 lines (56 loc) · 1.31 KB
/
go_field.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
package gocoder
import (
"go/ast"
"go/token"
"strings"
)
type GoField struct {
rootExpr *GoExpr
astExpr *ast.Field
goNames []*GoIdent
fieldType *GoType
}
func newGoField(rootExpr *GoExpr, field *ast.Field) *GoField {
g := &GoField{
rootExpr: rootExpr,
astExpr: field,
}
g.load()
return g
}
func (p *GoField) NumName() int {
return len(p.astExpr.Names)
}
func (p *GoField) IsExported() bool {
if len(p.astExpr.Names) == 0 {
return true
}
return p.astExpr.Names[0].IsExported()
}
func (p *GoField) Name(i int) *GoIdent {
return p.goNames[i]
}
func (p *GoField) Type() *GoType {
return p.fieldType
}
func (p *GoField) Tag() StructTag {
if p.astExpr.Tag == nil {
return StructTag("")
}
tag := strings.Trim(p.astExpr.Tag.Value, "\"")
tag = strings.Trim(tag, "`")
return StructTag(tag)
}
func (p *GoField) load() {
for i := 0; i < len(p.astExpr.Names); i++ {
p.goNames = append(p.goNames, newGoIdent(p.rootExpr, p.astExpr.Names[i]))
}
p.fieldType = newGoType(p.rootExpr, p.astExpr, p.astExpr.Type)
}
func (p *GoField) Position() (token.Position, token.Position) {
return p.rootExpr.astFileSet.Position(p.astExpr.Pos()), p.rootExpr.astFileSet.Position(p.astExpr.End())
}
func (p *GoField) Print() error {
return ast.Print(p.rootExpr.astFileSet, p.astExpr)
}
func (p *GoField) goNode() {}