-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdef.go
156 lines (142 loc) · 3.99 KB
/
def.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"go/ast"
"go/types"
)
// unsealedError corresponds to a declared sum type whose interface is not
// sealed. A sealed interface requires at least one unexported method.
type unsealedError struct {
Decl sumTypeDecl
}
func (e unsealedError) Error() string {
return fmt.Sprintf(
"%s: interface '%s' is not sealed "+
"(sealing requires at least one unexported method)",
e.Decl.Location(), e.Decl.TypeName)
}
// notFoundError corresponds to a declared sum type whose type definition
// could not be found in the same Go package.
type notFoundError struct {
Decl sumTypeDecl
}
func (e notFoundError) Error() string {
return fmt.Sprintf("%s: type '%s' is not defined", e.Decl.Location(), e.Decl.TypeName)
}
// notInterfaceError corresponds to a declared sum type that does not
// correspond to an interface.
type notInterfaceError struct {
Decl sumTypeDecl
}
func (e notInterfaceError) Error() string {
return fmt.Sprintf("%s: type '%s' is not an interface", e.Decl.Location(), e.Decl.TypeName)
}
// sumTypeDef corresponds to the definition of a Go interface that is
// interpreted as a sum type. Its variants are determined by finding all types
// that implement said interface in the same package.
type sumTypeDef struct {
Decl sumTypeDecl
Ty *types.Interface
Variants []types.Object
}
// findSumTypeDefs attempts to find a Go type definition for each of the given
// sum type declarations. If no such sum type definition could be found for
// any of the given declarations, then an error is returned.
func findSumTypeDefs(decls []sumTypeDecl) ([]sumTypeDef, []error) {
var defs []sumTypeDef
var errs []error
for _, decl := range decls {
def, err := newSumTypeDef(decl.Package.Types, decl)
if err != nil {
errs = append(errs, err)
continue
}
if def == nil {
errs = append(errs, notFoundError{decl})
continue
}
defs = append(defs, *def)
}
return defs, errs
}
// newSumTypeDef attempts to extract a sum type definition from a single
// package. If no such type corresponds to the given decl, then this function
// returns a nil def and a nil error.
//
// If the decl corresponds to a type that isn't an interface containing at
// least one unexported method, then this returns an error.
func newSumTypeDef(pkg *types.Package, decl sumTypeDecl) (*sumTypeDef, error) {
obj := pkg.Scope().Lookup(decl.TypeName)
if obj == nil {
return nil, nil
}
iface, ok := obj.Type().Underlying().(*types.Interface)
if !ok {
return nil, notInterfaceError{decl}
}
hasUnexported := false
for i := 0; i < iface.NumMethods(); i++ {
if !iface.Method(i).Exported() {
hasUnexported = true
break
}
}
if !hasUnexported {
return nil, unsealedError{decl}
}
def := &sumTypeDef{
Decl: decl,
Ty: iface,
}
for _, name := range pkg.Scope().Names() {
obj, ok := pkg.Scope().Lookup(name).(*types.TypeName)
if !ok {
continue
}
ty := obj.Type()
if types.Identical(ty.Underlying(), iface) {
continue
}
if types.Implements(ty, iface) || types.Implements(types.NewPointer(ty), iface) {
def.Variants = append(def.Variants, obj)
}
}
return def, nil
}
func (def *sumTypeDef) String() string {
return def.Decl.TypeName
}
// missing returns a list of variants in this sum type that are not in the
// given list of types.
func (def *sumTypeDef) missing(tys []types.Type) []types.Object {
// TODO(ag): This is O(n^2). Fix that. /shrug
var missing []types.Object
for _, v := range def.Variants {
found := false
varty := indirect(v.Type())
for _, ty := range tys {
ty = indirect(ty)
if types.Identical(varty, ty) {
found = true
}
}
if !found {
missing = append(missing, v)
}
}
return missing
}
// indirect dereferences through an arbitrary number of pointer types.
func indirect(ty types.Type) types.Type {
if ty, ok := ty.(*types.Pointer); ok {
return indirect(ty.Elem())
}
return ty
}
func pkgFiles(pkg *ast.Package) []*ast.File {
var files []*ast.File
for _, file := range pkg.Files {
files = append(files, file)
}
return files
}