-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_iface.go
165 lines (131 loc) · 3.98 KB
/
generate_iface.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
157
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"strconv"
"strings"
"github.com/diamondburned/cchat/repository"
)
// WriteInterface writes an interface.
func (g *Generator) WriteInterface(pkg repository.Package, iface repository.Interface) {
methods := interfaceMethods(pkg, iface)
service := g.clone()
// Write services.
if len(methods.others) > 0 {
name := iface.Name + "Service"
service.WriteComment(repository.Comment{Raw: fmt.Sprintf(
"%s is the service containing call methods for implementations of interface %s.",
name, iface.Name,
)})
service.body.WriteString("rpc_service ")
service.body.WriteString(iface.Name + "Service")
service.body.WriteString(" {\n")
tail := service.clone()
for _, method := range methods.others {
service.writeServiceMethod(tail, iface.Name, method)
}
service.body.WriteByte('}')
service.WriteLine()
service.WriteLine()
service.takeFrom(tail)
}
tail := g.clone()
g.WriteComment(iface.Comment)
g.body.WriteString("table ")
g.body.WriteString(iface.Name)
if len(methods.services) > 0 {
g.body.WriteByte(' ')
g.body.WriteString("(services: ")
g.body.WriteString(strconv.Quote(strings.Join(methods.services, ", ")))
g.body.WriteString(")")
}
g.body.WriteString(" {")
if len(methods.fields) > 0 || len(methods.asserters) > 0 {
g.WriteLine()
}
for _, getter := range methods.fields {
fieldComment(&g.body, getter.Comment)
field := repository.NamedType{
Name: getter.Name,
Type: getter.Returns[0].Type,
}
if len(getter.Returns) > 1 {
var returns = make([]repository.StructField, len(getter.Returns))
for i, ret := range getter.Returns {
returns[i].NamedType = ret
}
field.Type = iface.Name + field.Name
tail.WriteComment(repository.Comment{
Raw: fmt.Sprintf(
"%s is the return of method %s for interface %s.",
field.Type, field.Name, iface.Name,
),
})
tail.writeStruct(field.Type, returns)
}
g.writeField(field, fieldOpts{})
}
if len(methods.asserters) > 0 {
g.WriteLine()
fieldComment(&g.body, repository.Comment{Raw: "Asserters"})
}
for _, field := range methods.asserters {
fieldType := repository.NamedType{
Name: PascalToSnake(field.ChildType),
Type: field.ChildType,
}
g.writeField(fieldType, fieldOpts{})
}
g.body.WriteByte('}')
g.WriteLine()
g.WriteLine()
g.takeFrom(service)
g.takeFrom(tail)
}
type methods struct {
fields []repository.GetterMethod // no parameters
asserters []repository.AsserterMethod
others []repository.Method
pkg repository.Package
services []string
}
func newMethods(pkg repository.Package) methods { return methods{pkg: pkg} }
func interfaceMethods(pkg repository.Package, iface repository.Interface) methods {
methods := newMethods(pkg)
methods.load(iface)
return methods
}
func (methods *methods) load(iface repository.Interface) {
for _, method := range iface.Methods {
switch method := method.(type) {
case repository.GetterMethod:
if len(method.Parameters) == 0 {
methods.fields = append(methods.fields, method)
continue
}
case repository.AsserterMethod:
methods.asserters = append(methods.asserters, method)
continue
}
methods.others = append(methods.others, method)
}
if len(methods.others) > 0 {
methods.services = append(methods.services, iface.Name+"Service")
}
// Load embedded interfaces recursively into another methods list, and then
// prepend that.
var child = newMethods(methods.pkg)
for _, embedded := range iface.Embeds {
iface := child.pkg.Interface(embedded.InterfaceName)
if iface != nil {
child.load(*iface)
}
}
methods.fields = append(child.fields, methods.fields...)
methods.asserters = append(child.asserters, methods.asserters...)
// We have methods that we cannot inherit directly, which means that a
// Service will be required. However, since that would lead to namespace
// conflicts, we'll just have to indicate that there's inheritance and not
// copy over the methods.
methods.services = append(methods.services, child.services...)
return
}