/
ref.go
62 lines (50 loc) · 1017 Bytes
/
ref.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
package types
import (
"fmt"
"go/ast"
"go/types"
"strings"
)
type TypeName interface {
Pkg() *types.Package
Name() string
String() string
Exported() bool
}
var _ TypeName = &types.TypeName{}
func Ref(pkgPath string, name string) TypeName {
return &ref{pkgPath: pkgPath, name: name}
}
func ParseRef(ref string) (TypeName, error) {
parts := strings.Split(ref, ".")
if len(parts) == 1 {
return nil, fmt.Errorf("unsupported ref: %s", ref)
}
return Ref(strings.Join(parts[0:len(parts)-1], "."), parts[len(parts)-1]), nil
}
func MustParseRef(ref string) TypeName {
r, err := ParseRef(ref)
if err != nil {
panic(nil)
}
return r
}
type ref struct {
pkgPath string
name string
}
func (ref) Underlying() types.Type {
return nil
}
func (r *ref) String() string {
return r.pkgPath + "." + r.name
}
func (r *ref) Pkg() *types.Package {
return types.NewPackage(r.pkgPath, "")
}
func (r *ref) Name() string {
return r.name
}
func (r *ref) Exported() bool {
return ast.IsExported(r.name)
}