forked from go-kit/kit
/
sourcecontext.go
53 lines (46 loc) · 1.04 KB
/
sourcecontext.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 main
import (
"fmt"
"go/ast"
"go/token"
)
type sourceContext struct {
pkg *ast.Ident
imports []*ast.ImportSpec
interfaces []iface
types []*ast.TypeSpec
}
func (sc *sourceContext) validate() error {
if len(sc.interfaces) != 1 {
return fmt.Errorf("found %d interfaces, expecting exactly 1", len(sc.interfaces))
}
for _, i := range sc.interfaces {
for _, m := range i.methods {
if len(m.results) < 1 {
return fmt.Errorf("method %q of interface %q has no result types", m.name, i.name)
}
}
}
return nil
}
func (sc *sourceContext) importDecls() (decls []ast.Decl) {
have := map[string]struct{}{}
notHave := func(is *ast.ImportSpec) bool {
if _, has := have[is.Path.Value]; has {
return false
}
have[is.Path.Value] = struct{}{}
return true
}
for _, is := range sc.imports {
if notHave(is) {
decls = append(decls, importFor(is))
}
}
for _, is := range fetchImports() {
if notHave(is) {
decls = append(decls, &ast.GenDecl{Tok: token.IMPORT, Specs: []ast.Spec{is}})
}
}
return
}