1package main
2
3import "path/filepath"
4
5type deflayout struct {
6	targetDir string
7}
8
9func (l deflayout) packagePath(sub string) string {
10	return filepath.Join(l.targetDir, sub)
11}
12
13func (l deflayout) transformAST(ctx *sourceContext) (files, error) {
14	out := make(outputTree)
15
16	endpoints := out.addFile("endpoints/endpoints.go", "endpoints")
17	http := out.addFile("http/http.go", "http")
18	service := out.addFile("service/service.go", "service")
19
20	addImports(endpoints, ctx)
21	addImports(http, ctx)
22	addImports(service, ctx)
23
24	for _, typ := range ctx.types {
25		addType(service, typ)
26	}
27
28	for _, iface := range ctx.interfaces { //only one...
29		addStubStruct(service, iface)
30
31		for _, meth := range iface.methods {
32			addMethod(service, iface, meth)
33			addRequestStruct(endpoints, meth)
34			addResponseStruct(endpoints, meth)
35			addEndpointMaker(endpoints, iface, meth)
36		}
37
38		addEndpointsStruct(endpoints, iface)
39		addHTTPHandler(http, iface)
40
41		for _, meth := range iface.methods {
42			addDecoder(http, meth)
43			addEncoder(http, meth)
44		}
45
46		for name := range out {
47			out[name] = selectify(out[name], "service", iface.stubName().Name, l.packagePath("service"))
48			for _, meth := range iface.methods {
49				out[name] = selectify(out[name], "endpoints", meth.requestStructName().Name, l.packagePath("endpoints"))
50			}
51		}
52	}
53
54	for name := range out {
55		out[name] = selectify(out[name], "endpoints", "Endpoints", l.packagePath("endpoints"))
56
57		for _, typ := range ctx.types {
58			out[name] = selectify(out[name], "service", typ.Name.Name, l.packagePath("service"))
59		}
60	}
61
62	return formatNodes(out)
63}
64