-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtemplate-structures.go
242 lines (216 loc) · 6.46 KB
/
template-structures.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"fmt"
"strings"
)
type TemplateCommon struct {
BasePackage string
BasePackageImport string
BasePackageName string
EndpointPackage string
EndpointPackageName string
EndpointPrefix string
InterfaceName string
InterfaceNameLcase string
}
type TemplateParam struct {
PublicName string
Name string
Type string
IsContext bool
}
func createTemplateParam(p Param) TemplateParam {
return TemplateParam{
Type: p.typ.String(),
}
}
type TemplateMethod struct {
TemplateCommon
HasContextParam bool
ContextParamName string
HasErrorResult bool
ErrorResultName string
HasMoreThanOneResult bool
LocalName string
MethodName string
MethodNameLcase string
MethodArguments string
MethodResults string
MethodResultNamesStr string
MethodArgumentNamesStr string
MethodArgumentNames []string
MethodResultNames []string
Params []TemplateParam
Results []TemplateParam
}
func publicVariableName(str string) string {
firstLetter := string([]byte{str[0]})
rest := ""
if len(str) > 1 {
rest = str[1:]
}
return strings.ToUpper(firstLetter) + rest
}
func privateVariableName(str string) string {
firstLetter := string([]byte{str[0]})
rest := ""
if len(str) > 1 {
rest = str[1:]
}
return strings.ToLower(firstLetter) + rest
}
func createTemplateMethods(basePackage, endpointPackage *Import, interf Interface, methods []Method, reseveredNames []string) []TemplateMethod {
results := make([]TemplateMethod, 0, len(methods))
for _, meth := range methods {
var names []string
names = append(names, reseveredNames...)
names = append(names, meth.usedNames()...)
var params []TemplateParam
var methodsResults []TemplateParam
var paramNames []string
for _, p := range meth.params {
// skip the context name, as this is primarily used to retrieve
// values after transport.
if !meth.hasContextParam || meth.contextParamName != p.names[0] {
paramNames = append(paramNames, p.names...)
}
for _, n := range p.names {
param := TemplateParam{
PublicName: publicVariableName(n),
Name: n,
Type: p.typ.String(),
}
if param.Type == "context.Context" {
param.IsContext = true
}
params = append(params, param)
}
}
var resultNames []string
for _, r := range meth.results {
resultNames = append(resultNames, r.names...)
for _, n := range r.names {
methodsResults = append(methodsResults, TemplateParam{
PublicName: publicVariableName(n),
Name: n,
Type: r.typ.String(),
})
}
}
contextParamName := "_ctx"
if meth.hasContextParam {
contextParamName = meth.contextParamName
}
errorResultName := "err"
if meth.hasErrResult {
errorResultName = meth.errorResultName
}
lcaseName := determineLocalName(strings.ToLower(interf.name), reseveredNames)
results = append(results, TemplateMethod{
TemplateCommon: TemplateCommon{
BasePackage: basePackage.path,
BasePackageImport: basePackage.ImportSpec(),
BasePackageName: basePackage.name,
EndpointPackage: endpointPackage.path,
EndpointPackageName: endpointPackage.name,
EndpointPrefix: fmt.Sprintf("/%s", strings.ToLower(interf.name)),
InterfaceName: interf.name,
InterfaceNameLcase: privateVariableName(interf.name),
},
HasContextParam: meth.hasContextParam,
ContextParamName: contextParamName,
HasErrorResult: meth.hasErrResult,
ErrorResultName: errorResultName,
HasMoreThanOneResult: meth.moreThanOneResult,
MethodName: meth.name,
MethodNameLcase: privateVariableName(meth.name),
LocalName: lcaseName,
MethodArguments: meth.methodArguments(),
MethodResults: meth.methodResults(),
MethodArgumentNamesStr: meth.methodArgumentNames(),
MethodResultNamesStr: meth.methodResultNames(),
MethodArgumentNames: paramNames,
MethodResultNames: resultNames,
Params: params,
Results: methodsResults,
})
}
return results
}
type TemplateBase struct {
TemplateCommon
Imports []string
ImportsWithoutTime []string
ExtraImports []string
Methods []TemplateMethod
ExtraInterfaces []TemplateParam
}
func createTemplateBase(basePackage, endpointPackage *Import, i Interface, oimps []*Import) TemplateBase {
// imps := filteredImports(i, oimps)
imps := oimps
names := make([]string, 0, len(imps))
for _, i := range imps {
names = append(names, i.name)
}
var impSpecs []string
var impSpecsWithoutTime []string
var extraImpSpecs []string
for _, i := range imps {
if !i.isParam && i.isEmbeded {
extraImpSpecs = append(extraImpSpecs, i.ImportSpec())
// skip non-params for these imports
continue
}
if i.isParam {
impSpecs = append(impSpecs, i.ImportSpec())
if i.path != "time" {
impSpecsWithoutTime = append(impSpecsWithoutTime, i.ImportSpec())
}
}
}
var extraInterfaces []TemplateParam
for _, t := range i.types {
var publicNamePieces = strings.Split(t.String(), ".")
if len(publicNamePieces) < 1 {
panic("This type is empty?!")
}
var publicName = publicNamePieces[len(publicNamePieces)-1]
extraInterfaces = append(extraInterfaces, TemplateParam{
PublicName: publicName,
Name: publicName,
Type: t.String(),
})
}
return TemplateBase{
TemplateCommon: TemplateCommon{
BasePackage: basePackage.path,
BasePackageImport: basePackage.ImportSpec(),
BasePackageName: basePackage.name,
EndpointPackage: endpointPackage.path,
EndpointPackageName: endpointPackage.name,
EndpointPrefix: fmt.Sprintf("/%s", strings.ToLower(i.name)),
InterfaceName: i.name,
InterfaceNameLcase: privateVariableName(i.name),
},
Imports: impSpecs,
ImportsWithoutTime: impSpecsWithoutTime,
ExtraImports: extraImpSpecs,
Methods: createTemplateMethods(basePackage, endpointPackage, i, i.methods, names),
ExtraInterfaces: extraInterfaces,
}
}
func filteredExtraImports(i Interface, imps []Import) []Import {
var res []Import
var tmp []string
for _, imp := range imps {
for _, t := range i.types {
if strings.HasPrefix(t.String(), fmt.Sprintf("%s.", imp.name)) {
if !sliceContains(tmp, imp.ImportSpec()) {
res = append(res, imp)
tmp = append(tmp, imp.ImportSpec())
}
}
}
}
return res
}