-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.go
56 lines (51 loc) · 1.03 KB
/
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
package gocode
import (
"go/ast"
"reflect"
"strings"
)
type Field struct {
AST *ast.Field
Name string
}
func NewField(name string, filed *ast.Field) *Field {
return &Field{
AST: filed,
Name: name,
}
}
func (f *Field) String() string {
return f.Output(" ")
}
func (f *Field) IsExport() bool {
if f.Name == `` {
s := NewTypeExpr(f.AST.Type).TypeString()
s = strings.TrimLeft(s, `*`)
return IsExport(s)
}
return IsExport(f.Name)
}
// 輸出字段定義
func (f *Field) Output(indent string) string {
s := f.Name
if s != `` {
s += indent
}
node := f.AST
s += NewTypeExpr(node.Type).TypeString()
return s
}
// 輸出接口定義
func (f *Field) OutputInterface() string {
node := f.AST
switch t := node.Type.(type) {
case *ast.Ident: // 標識,基礎類型
return NewIdent(t).TypeString()
case *ast.SelectorExpr:
return NewSelectorExpr(t).TypeString()
case *ast.FuncType: // 函數
return f.Name + NewFuncType(t).ParamsAndResults()
default:
panic(`unknow field t type: ` + reflect.TypeOf(t).String())
}
}