-
Notifications
You must be signed in to change notification settings - Fork 3
/
types.go
53 lines (48 loc) · 1.12 KB
/
types.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
package humanize
import (
"fmt"
"go/ast"
)
// Type is the interface for all types without name
type Type interface {
fmt.Stringer
// Package return the package name of the type
Package() *Package
Equal(Type) bool
}
func newType(p *Package, f *File, e ast.Expr) Type {
switch t := e.(type) {
case *ast.Ident:
return getIdent(p, f, t)
case *ast.StarExpr:
return getStar(p, f, t)
case *ast.ArrayType:
return getArray(p, f, t)
case *ast.MapType:
return getMap(p, f, t)
case *ast.StructType:
return getStruct(p, f, t)
case *ast.SelectorExpr:
return getSelector(p, f, t)
case *ast.ChanType:
return getChannel(p, f, t)
case *ast.FuncType:
return getFunc(p, f, t)
case *ast.InterfaceType:
return getInterface(p, f, t)
default:
return nil
}
}
// FindTypeName try to find type name based on the type. it can fail for
// anonymous types, so watch about the result
func FindTypeName(t Type) (*TypeName, error) {
switch v := t.(type) {
case *IdentType:
return t.Package().FindType(v.Ident)
case *StarType:
return FindTypeName(v.Target)
default:
return nil, fmt.Errorf("%T is not supported yet", t)
}
}