/
handlers.go
342 lines (325 loc) · 15.5 KB
/
handlers.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2019 NDP Systèmes. All Rights Reserved.
// See LICENSE file for full licensing details.
package generate
import (
"fmt"
)
// specificMethodsHandlers are functions that populate the given modelData
// for specific methods.
var specificMethodsHandlers = map[string]func(astData *MethodASTData, modelData *modelData, depsMap *map[string]bool){
"Search": searchMethodHandler,
"SearchByName": searchByNameMethodHandler,
"Create": createMethodHandler,
"New": newMethodHandler,
"Write": writeMethodHandler,
"Copy": copyMethodHandler,
"CopyData": copyDataMethodHandler,
"CartesianProduct": cartesianProductMethodHandler,
"Sorted": sortedMethodHandler,
"Filtered": filteredMethodHandler,
"Aggregates": aggregatesMethodHandler,
"First": firstMethodHandler,
"All": allMethodHandler,
"DefaultGet": defaultGetMethodHandler,
}
// searchMethodHandler returns the specific methodData for the Search method.
func searchMethodHandler(astData *MethodASTData, modelData *modelData, _ *map[string]bool) {
name := "Search"
iReturnString := fmt.Sprintf("%sSet", modelData.Name)
returnString := fmt.Sprintf("%s.%sSet", PoolInterfacesPackage, modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ToDeclare: astData.ToDeclare,
ParamsTypes: fmt.Sprintf("%s.%sCondition", PoolQueryPackage, modelData.Name),
IParamsWithTypes: fmt.Sprintf("condition %s.%sCondition", PoolQueryPackage, modelData.Name),
ReturnString: returnString,
IReturnString: iReturnString,
})
modelData.Methods = append(modelData.Methods, methodData{
Name: name,
Doc: fmt.Sprintf("// Search returns a new %sSet filtering on the current one with the additional given Condition", modelData.Name),
ToDeclare: astData.ToDeclare,
Params: "condition",
ParamsWithType: fmt.Sprintf("condition %s.%sCondition", PoolQueryPackage, modelData.Name),
ReturnAsserts: fmt.Sprintf("resTyped := res.(models.RecordSet).Collection().Wrap(\"%s\").(%s)", modelData.Name, returnString),
Returns: "resTyped",
ReturnString: returnString,
Call: "Call",
})
}
// createMethodHandler returns the specific methodData for the Create method.
func createMethodHandler(astData *MethodASTData, modelData *modelData, _ *map[string]bool) {
name := "Create"
iReturnString := fmt.Sprintf("%sSet", modelData.Name)
returnString := fmt.Sprintf("%s.%sSet", PoolInterfacesPackage, modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ToDeclare: astData.ToDeclare,
ParamsTypes: fmt.Sprintf("%s.%sData", PoolInterfacesPackage, modelData.Name),
IParamsWithTypes: fmt.Sprintf("data %sData", modelData.Name),
ReturnString: returnString,
IReturnString: iReturnString,
})
modelData.Methods = append(modelData.Methods, methodData{
Name: name,
Doc: fmt.Sprintf(`// Create inserts a %s record in the database from the given data.
// Returns the created %sSet.`,
modelData.Name, modelData.Name),
ToDeclare: astData.ToDeclare,
Params: "data",
ParamsWithType: fmt.Sprintf("data %s.%sData", PoolInterfacesPackage, modelData.Name),
ReturnAsserts: fmt.Sprintf("resTyped := res.(models.RecordSet).Collection().Wrap(\"%s\").(%s)", modelData.Name, returnString),
Returns: "resTyped",
ReturnString: returnString,
Call: "Call",
})
}
// newMethodHandler returns the specific methodData for the New method.
func newMethodHandler(astData *MethodASTData, modelData *modelData, _ *map[string]bool) {
name := "New"
iReturnString := fmt.Sprintf("%sSet", modelData.Name)
returnString := fmt.Sprintf("%s.%sSet", PoolInterfacesPackage, modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ToDeclare: astData.ToDeclare,
ParamsTypes: fmt.Sprintf("%s.%sData", PoolInterfacesPackage, modelData.Name),
IParamsWithTypes: fmt.Sprintf("data %sData", modelData.Name),
ReturnString: returnString,
IReturnString: iReturnString,
})
modelData.Methods = append(modelData.Methods, methodData{
Name: name,
Doc: fmt.Sprintf(`// New creates a %s record in memory from the given data.
// Such %sSet has a negative ID and cannot be reloaded from the database`,
modelData.Name, modelData.Name),
ToDeclare: astData.ToDeclare,
Params: "data",
ParamsWithType: fmt.Sprintf("data %s.%sData", PoolInterfacesPackage, modelData.Name),
ReturnAsserts: fmt.Sprintf("resTyped := res.(models.RecordSet).Collection().Wrap(\"%s\").(%s)", modelData.Name, returnString),
Returns: "resTyped",
ReturnString: returnString,
Call: "Call",
})
}
// writeMethodHandler returns the specific methodData for the Write method.
func writeMethodHandler(astData *MethodASTData, modelData *modelData, _ *map[string]bool) {
name := "Write"
returnString := "bool"
iReturnString := "bool"
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ToDeclare: astData.ToDeclare,
IParamsWithTypes: fmt.Sprintf("data %sData", modelData.Name),
ParamsTypes: fmt.Sprintf("%s.%sData", PoolInterfacesPackage, modelData.Name),
ReturnString: returnString,
IReturnString: iReturnString,
})
modelData.Methods = append(modelData.Methods, methodData{
Name: name,
Doc: fmt.Sprintf(`// Write is the base implementation of the 'Write' method which updates
// %s records in the database with the given data.`, modelData.Name),
ToDeclare: astData.ToDeclare,
Params: "data",
ParamsWithType: fmt.Sprintf("data %s.%sData", PoolInterfacesPackage, modelData.Name),
ReturnAsserts: "resTyped, _ := res.(bool)",
Returns: "resTyped",
ReturnString: returnString,
Call: "Call",
})
}
// copyMethodHandler returns the specific methodData for the Copy method.
func copyMethodHandler(astData *MethodASTData, modelData *modelData, _ *map[string]bool) {
name := "Copy"
returnString := fmt.Sprintf("%s.%sSet", PoolInterfacesPackage, modelData.Name)
iReturnString := fmt.Sprintf("%sSet", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ToDeclare: astData.ToDeclare,
ParamsTypes: fmt.Sprintf("%s.%sData", PoolInterfacesPackage, modelData.Name),
IParamsWithTypes: fmt.Sprintf("overrides %sData", modelData.Name),
ReturnString: returnString,
IReturnString: iReturnString,
})
modelData.Methods = append(modelData.Methods, methodData{
Name: name,
Doc: fmt.Sprintf(`// Copy duplicates the given %s record, overridding values with overrides.`, modelData.Name),
ToDeclare: astData.ToDeclare,
Params: "overrides",
ParamsWithType: fmt.Sprintf("overrides %s.%sData", PoolInterfacesPackage, modelData.Name),
ReturnAsserts: fmt.Sprintf("resTyped := res.(models.RecordSet).Collection().Wrap(\"%s\").(%s)", modelData.Name, returnString),
Returns: "resTyped",
ReturnString: returnString,
Call: "Call",
})
}
// copyDataMethodHandler returns the specific methodData for the CopyData method.
func copyDataMethodHandler(astData *MethodASTData, modelData *modelData, _ *map[string]bool) {
name := "CopyData"
returnString := fmt.Sprintf("%s.%sData", PoolInterfacesPackage, modelData.Name)
iReturnString := fmt.Sprintf("%sData", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ToDeclare: astData.ToDeclare,
ParamsTypes: fmt.Sprintf("%s.%sData", PoolInterfacesPackage, modelData.Name),
IParamsWithTypes: fmt.Sprintf("overrides %sData", modelData.Name),
ReturnString: returnString,
IReturnString: iReturnString,
})
modelData.Methods = append(modelData.Methods, methodData{
Name: name,
Doc: `// CopyData copies given record's data with all its fields values, overriding values with overrides.`,
ToDeclare: astData.ToDeclare,
Params: "overrides",
ParamsWithType: fmt.Sprintf("overrides %s.%sData", PoolInterfacesPackage, modelData.Name),
ReturnAsserts: "resTyped, _ := res.(models.RecordData)",
Returns: fmt.Sprintf("resTyped.Underlying().Wrap().(%s.%sData)", PoolInterfacesPackage, modelData.Name),
ReturnString: returnString,
Call: "Call",
})
}
// searchByNameMethodHandler returns the specific methodData for the Search method.
func searchByNameMethodHandler(astData *MethodASTData, modelData *modelData, depsMap *map[string]bool) {
name := "SearchByName"
returnString := fmt.Sprintf("%s.%sSet", PoolInterfacesPackage, modelData.Name)
iReturnString := fmt.Sprintf("%sSet", modelData.Name)
(*depsMap)["github.com/erlangs/okoo/src/models/operator"] = true
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ToDeclare: astData.ToDeclare,
ParamsTypes: fmt.Sprintf("string, operator.Operator, %s.%sCondition, int", PoolQueryPackage, modelData.Name),
IParamsWithTypes: fmt.Sprintf("name string, op operator.Operator, additionalCond %s.%sCondition, limit int", PoolQueryPackage, modelData.Name),
ReturnString: returnString,
IReturnString: iReturnString,
})
modelData.Methods = append(modelData.Methods, methodData{
Name: name,
Doc: fmt.Sprintf(`// SearchByName searches for %s records that have a display name matching the given
// "name" pattern when compared with the given "op" operator, while also
// matching the optional search condition ("additionalCond").
//
// This is used for example to provide suggestions based on a partial
// value for a relational field. Sometimes be seen as the inverse
// function of NameGet but it is not guaranteed to be.`, modelData.Name),
ToDeclare: astData.ToDeclare,
Params: "name, op, additionalCond, limit",
ParamsWithType: fmt.Sprintf("name string, op operator.Operator, additionalCond %s.%sCondition, limit int", PoolQueryPackage, modelData.Name),
ReturnAsserts: fmt.Sprintf("resTyped := res.(models.RecordSet).Collection().Wrap(\"%s\").(%s)", modelData.Name, returnString),
Returns: "resTyped",
ReturnString: returnString,
Call: "Call",
})
}
// firstMethodHandler returns the specific methodData for the First method.
func firstMethodHandler(astData *MethodASTData, modelData *modelData, _ *map[string]bool) {
name := "First"
returnString := fmt.Sprintf("%s.%sData", PoolInterfacesPackage, modelData.Name)
iReturnString := fmt.Sprintf("%sData", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ToDeclare: astData.ToDeclare,
ReturnString: returnString,
IReturnString: iReturnString,
})
}
// allMethodHandler returns the specific methodData for the First method.
func allMethodHandler(astData *MethodASTData, modelData *modelData, _ *map[string]bool) {
name := "All"
returnString := fmt.Sprintf("[]%s.%sData", PoolInterfacesPackage, modelData.Name)
iReturnString := fmt.Sprintf("[]%sData", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ToDeclare: astData.ToDeclare,
ReturnString: returnString,
IReturnString: iReturnString,
})
}
// cartesianProductMethodHandler returns the specific methodData for the CartesianProduct method.
func cartesianProductMethodHandler(astData *MethodASTData, modelData *modelData, _ *map[string]bool) {
name := "CartesianProduct"
returnString := fmt.Sprintf("[]%s.%sSet", PoolInterfacesPackage, modelData.Name)
iReturnString := fmt.Sprintf("[]%sSet", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ToDeclare: astData.ToDeclare,
ParamsTypes: fmt.Sprintf("...%s.%sSet", PoolInterfacesPackage, modelData.Name),
IParamsWithTypes: fmt.Sprintf("others...%sSet", modelData.Name),
ReturnString: returnString,
IReturnString: iReturnString,
})
}
// sortedMethodHandler returns the specific methodData for the Sorted method.
func sortedMethodHandler(astData *MethodASTData, modelData *modelData, _ *map[string]bool) {
name := "Sorted"
returnString := fmt.Sprintf("%s.%sSet", PoolInterfacesPackage, modelData.Name)
iReturnString := fmt.Sprintf("%sSet", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ToDeclare: astData.ToDeclare,
ParamsTypes: fmt.Sprintf("func(%s.%sSet, %s.%sSet) bool", PoolInterfacesPackage, modelData.Name, PoolInterfacesPackage, modelData.Name),
IParamsWithTypes: fmt.Sprintf("less func(%sSet, %sSet) bool", modelData.Name, modelData.Name),
ReturnString: returnString,
IReturnString: iReturnString,
})
}
// filteredMethodHandler returns the specific methodData for the Sorted method.
func filteredMethodHandler(astData *MethodASTData, modelData *modelData, _ *map[string]bool) {
name := "Filtered"
returnString := fmt.Sprintf("%s.%sSet", PoolInterfacesPackage, modelData.Name)
iReturnString := fmt.Sprintf("%sSet", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ToDeclare: astData.ToDeclare,
ParamsTypes: fmt.Sprintf("func(%s.%sSet) bool", PoolInterfacesPackage, modelData.Name),
IParamsWithTypes: fmt.Sprintf("test func(%sSet) bool", modelData.Name),
ReturnString: returnString,
IReturnString: iReturnString,
})
}
// aggregatesMethodHandler returns the specific methodData for the Aggregates method.
func aggregatesMethodHandler(astData *MethodASTData, modelData *modelData, _ *map[string]bool) {
returnString := fmt.Sprintf("[]%s.%sGroupAggregateRow", PoolInterfacesPackage, modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: "Aggregates",
ToDeclare: false,
ParamsTypes: "...models.FieldName",
IParamsWithTypes: "fieldNames ...models.FieldName",
ReturnString: returnString,
IReturnString: fmt.Sprintf("[]%sGroupAggregateRow", modelData.Name),
})
modelData.Methods = append(modelData.Methods, methodData{
Name: "Aggregates",
Doc: "// Aggregates returns the result of this RecordSet query, which must by a grouped query.",
ToDeclare: false,
Params: "fieldNames",
ParamsWithType: "fieldNames ...models.FieldName",
ReturnAsserts: fmt.Sprintf("resTyped, _ := res.(%s)", returnString),
Returns: "resTyped",
ReturnString: returnString,
Call: "Call",
})
}
// defaultGetMethodHandler returns the specific methodData for the DefaultGet method.
func defaultGetMethodHandler(astData *MethodASTData, modelData *modelData, _ *map[string]bool) {
name := "DefaultGet"
returnString := fmt.Sprintf("%s.%sData", PoolInterfacesPackage, modelData.Name)
iReturnString := fmt.Sprintf("%sData", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ToDeclare: astData.ToDeclare,
IParamsWithTypes: "",
ParamsTypes: "",
ReturnString: returnString,
IReturnString: iReturnString,
})
modelData.Methods = append(modelData.Methods, methodData{
Name: name,
Doc: fmt.Sprintf(`// DefaultGet returns a %sData with the default values for the model.`, modelData.Name),
ToDeclare: astData.ToDeclare,
Params: "",
ParamsWithType: "",
ReturnAsserts: "resTyped, _ := res.(models.RecordData)",
Returns: fmt.Sprintf("resTyped.Underlying().Wrap().(%s.%sData)", PoolInterfacesPackage, modelData.Name),
ReturnString: returnString,
Call: "Call",
})
}