forked from go-kit/kit
/
deflayout.go
63 lines (48 loc) · 1.55 KB
/
deflayout.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
package main
import "path/filepath"
type deflayout struct {
targetDir string
}
func (l deflayout) packagePath(sub string) string {
return filepath.Join(l.targetDir, sub)
}
func (l deflayout) transformAST(ctx *sourceContext) (files, error) {
out := make(outputTree)
endpoints := out.addFile("endpoints/endpoints.go", "endpoints")
http := out.addFile("http/http.go", "http")
service := out.addFile("service/service.go", "service")
addImports(endpoints, ctx)
addImports(http, ctx)
addImports(service, ctx)
for _, typ := range ctx.types {
addType(service, typ)
}
for _, iface := range ctx.interfaces { //only one...
addStubStruct(service, iface)
for _, meth := range iface.methods {
addMethod(service, iface, meth)
addRequestStruct(endpoints, meth)
addResponseStruct(endpoints, meth)
addEndpointMaker(endpoints, iface, meth)
}
addEndpointsStruct(endpoints, iface)
addHTTPHandler(http, iface)
for _, meth := range iface.methods {
addDecoder(http, meth)
addEncoder(http, meth)
}
for name := range out {
out[name] = selectify(out[name], "service", iface.stubName().Name, l.packagePath("service"))
for _, meth := range iface.methods {
out[name] = selectify(out[name], "endpoints", meth.requestStructName().Name, l.packagePath("endpoints"))
}
}
}
for name := range out {
out[name] = selectify(out[name], "endpoints", "Endpoints", l.packagePath("endpoints"))
for _, typ := range ctx.types {
out[name] = selectify(out[name], "service", typ.Name.Name, l.packagePath("service"))
}
}
return formatNodes(out)
}