-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr_of_type.go
64 lines (59 loc) · 1.52 KB
/
expr_of_type.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
package gocode
import (
"fmt"
"go/ast"
"reflect"
"strings"
)
// 表示一個型別
type TypeExpr struct {
Expr ast.Expr
}
func NewTypeExpr(expr ast.Expr) *TypeExpr {
return &TypeExpr{
Expr: expr,
}
}
func typeString(tag string, expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return NewIdent(t).TypeString()
case *ast.SelectorExpr:
return NewSelectorExpr(t).TypeString()
case *ast.StarExpr:
return NewStarExpr(t).TypeString()
case *ast.ArrayType:
return NewArrayType(t).TypeString()
case *ast.MapType:
return NewMapType(t).TypeString()
case *ast.FuncType:
return NewFuncType(t).TypeString()
case *ast.StructType:
// NewStruct(``, t).Output()
return "struct{}"
case *ast.Ellipsis:
return `...` + NewTypeExpr(t.Elt).TypeString()
case *ast.InterfaceType:
return `interface{}`
case *ast.ChanType:
return NewChanType(t).TypeString()
case *ast.ParenExpr:
return `(` + NewTypeExpr(t.X).TypeString() + `)`
case *ast.IndexListExpr: // 模板
strs := make([]string, len(t.Indices))
for i, v := range t.Indices {
strs[i] = NewTypeExpr(v).TypeString()
}
return fmt.Sprintf(`%s[%s]`, NewTypeExpr(t.X).TypeString(), strings.Join(strs, ", "))
case *ast.BasicLit:
return t.Value
case *ast.IndexExpr: // 模板
return fmt.Sprintf(`%s[%s]`, NewTypeExpr(t.X).TypeString(), NewTypeExpr(t.Index).TypeString())
default:
panic(`unknow ` + tag + `: ` + reflect.TypeOf(t).String())
}
}
// 返回類型字符串
func (e *TypeExpr) TypeString() string {
return typeString(`field`, e.Expr)
}