-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.go
401 lines (358 loc) · 12.7 KB
/
entity.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package entitygen
import (
"fmt"
j "github.com/dave/jennifer/jen"
)
func (g *generator) entityFile(entity *entitySpec) *genFile {
// Create a Go file with header
file := j.NewFile(g.pkgName)
file.HeaderComment(headerComment)
g.entityStruct(entity, file)
g.entityRepositoryStruct(entity, file)
g.entityRepositoryMethods(entity, file)
return &genFile{FileName: entity.FileName, File: file}
}
func (g *generator) entityStruct(entity *entitySpec, file *j.File) {
// Comment
if entity.GoDesc != "" {
file.Comment(fmt.Sprintf("%s - %s", entity.GoName, entity.GoDesc))
}
// Fields
file.Type().Id(entity.GoName).Struct(g.fields(entity)...).Line()
// Methods
file.
Comment("TrackChanges internally stores entity actual state to track changes for the Update operation.").Line().
Func().Params(j.Id("e").Op("*").Id(entity.GoName)).Id("TrackChanges").Params().
Block(
j.Id("clone").Clone().Op(":=").Op("*").Id("e"),
j.Id("e").Dot("original").Op("=").Op("&").Id("clone"),
)
file.
Comment("resetChanges after the Update operation.").Line().
Func().Params(j.Id("e").Op("*").Id(entity.GoName)).Id("resetChanges").Params().
Block(
j.Id("e").Dot("original").Op("=").Nil(),
)
}
func (g *generator) fields(entity *entitySpec) []j.Code {
var fields []j.Code
// Self reference, to store original state for updates
originalComment := "original field stores snapshot of the entity state for the Update operation, see TrackChanges method."
fields = append(fields, j.Comment(originalComment).Line().Id("original").Op("*").Id(entity.GoName))
for _, field := range entity.Fields {
fields = append(fields, g.field(field))
}
return fields
}
func (*generator) field(field *fieldSpec) *j.Statement {
code := j.Empty()
if field.GoDesc != "" {
code = code.Comment(fmt.Sprintf("%s - %s", field.GoName, field.GoDesc))
}
return code.Id(field.GoName).Add(field.GoType).Tag(field.Tags)
}
func (*generator) entityRepositoryStruct(entity *entitySpec, file *j.File) {
// Struct
file.Type().Id(entity.RepositoryName).Struct(clientParam.Clone())
// Constructor
file.Func().Id("new" + entity.RepositoryName).Params(clientParam.Clone()).Op("*").Id(entity.RepositoryName).Block(
j.Return(j.Op("&").Id(entity.RepositoryName).Values(j.Dict{clientVar.Clone(): clientVar.Clone()})),
)
}
func (g *generator) entityRepositoryMethods(entity *entitySpec, file *j.File) {
g.createMethod(entity, file)
g.updateMethod(entity, file)
g.deleteMethod(entity, file)
g.allMethod(entity, file)
g.byIDMethod(entity, file)
g.lookupHelperMethods(entity, file)
g.createPayloadMethod(entity, file)
g.updatePayloadMethod(entity, file)
}
func (g *generator) createMethod(entity *entitySpec, file *j.File) {
resultType := j.Id(entity.GoName)
file.
Comment("Create entity. After successful operation, the new primary ID will be set to the original entity.").Line().
Add(g.entityRepoMethod(entity, "Create")).
Params(entityVar.Clone().Op("*").Add(resultType.Clone())).
// Returns
Op("*").Qual(webAPIPkg, "APIRequest").Types(resultType.Clone()).
// Body
Block(
j.List(payloadVar.Clone(), j.Id("err")).Op(":=").Id("r").Dot("createPayload").Call(entityVar.Clone()),
j.If(j.Id("err").Op("!=").Nil()).Block(
j.Return(
j.Qual(webAPIPkg, "NewAPIRequestError").Types(resultType.Clone()).Call(
entityVar.Clone(), j.Id("err"),
),
),
),
j.Line(),
pathVar.Clone().Op(":=").Add(g.collectionPath(entity)),
resultVar.Clone().Op(":=").Add(entityVar.Clone()),
httpReqVar.Clone().Op(":=").Add(g.httpRequest(resultVar.Clone(), "Post", payloadVar.Clone())),
httpReqVar.Clone().Dot("Header").Call(j.Lit("Prefer"), j.Lit("return=representation")),
httpReqVar.Clone().Dot("ExpectStatus").Call(j.Qual("net/http", "StatusCreated")),
j.Return(
j.Qual(webAPIPkg, "NewAPIRequest").Call(resultVar.Clone(), httpReqVar.Clone()),
),
).
Line()
}
func (g *generator) updateMethod(entity *entitySpec, file *j.File) {
resultType := j.Id(entity.GoName)
file.
Comment("Update entity. A diff of modifications is generated and saved via API.").Line().
Comment("Before making changes, it is necessary to call the TrackChanges entity method.").Line().
Add(g.entityRepoMethod(entity, "Update")).
Params(entityVar.Clone().Op("*").Add(resultType.Clone())).
// Returns
Op("*").Qual(webAPIPkg, "APIRequest").Types(resultType.Clone()).
// Body
Block(
j.Line(),
j.If(j.Id("entity").Dot("original").Op("==").Nil()).Block(
j.Id("err").Op(":=").Qual("errors", "New").
Call(j.Lit(`changes are not tracked: use "TrackChanges" entity method to track changes and allow "Update" operation`)),
j.Return(
j.Qual(webAPIPkg, "NewAPIRequestError").Types(resultType.Clone()).Call(
entityVar.Clone(), j.Id("err"),
),
),
),
j.Line(),
j.List(payloadVar.Clone(), j.Id("err")).Op(":=").
Id("r").Dot("updatePayload").Call(entityVar.Clone().Dot("original"), entityVar.Clone()),
j.If(j.Id("err").Op("!=").Nil()).Block(
j.Return(
j.Qual(webAPIPkg, "NewAPIRequestError").Types(resultType.Clone()).Call(
entityVar.Clone(), j.Id("err"),
),
),
),
j.Line(),
idVar.Clone().Op(":=").Id("entity").Dot(entity.PrimaryIDField.GoName),
pathVar.Clone().Op(":=").Add(g.entityPathByID(entity)),
resultVar.Clone().Op(":=").Add(entityVar.Clone()),
httpReqVar.Clone().Op(":=").Add(g.httpRequest(resultVar.Clone(), "Patch", payloadVar)),
httpReqVar.Clone().Dot("Header").Call(j.Lit("If-Match"), j.Lit("*")).Comment("prevent create if entity not exists"),
httpReqVar.Clone().Dot("Header").Call(j.Lit("Prefer"), j.Lit("return=representation")),
httpReqVar.Clone().Dot("OnSuccess").Call(
j.Func().
Params(
j.Id("ctx").Qual("context", "Context"),
j.Id("c").Op("*").Add(resultType.Clone()),
).
Id("error").Block(
j.Id("c").Dot("resetChanges").Call(),
j.Return(j.Nil()),
),
),
j.Return(
j.Qual(webAPIPkg, "NewAPIRequest").Call(resultVar.Clone(), httpReqVar.Clone()),
),
).
Line()
}
func (g *generator) deleteMethod(entity *entitySpec, file *j.File) {
file.
Comment("Delete entity by the ID.").Line().
Add(g.entityRepoMethod(entity, "Delete")).
Params(idVar.Clone().String()).
// Returns
Op("*").Qual(webAPIPkg, "APIRequest").Types(j.Qual(webAPIPkg, "NoResult")).
// Body
Block(
pathVar.Clone().Op(":=").Add(g.entityPathByID(entity)),
httpReqVar.Clone().Op(":=").Add(g.httpRequest(j.Op("&").Qual(webAPIPkg, "NoResult").Block(), "Delete", j.Nil())),
httpReqVar.Clone().Dot("ExpectStatus").Call(j.Qual("net/http", "StatusNoContent")),
j.Return(
j.Qual(webAPIPkg, "NewAPIRequest").Call(j.Op("&").Qual(webAPIPkg, "NoResult").Block(), httpReqVar.Clone()),
),
).
Line()
}
func (g *generator) allMethod(entity *entitySpec, file *j.File) {
resultType := j.Qual(webAPIPkg, "Collection").Types(j.Id(entity.GoName))
file.Add(g.entityRepoMethod(entity, "All")).
Params().
// Returns
Op("*").Qual(webAPIPkg, "APIRequest").Types(resultType.Clone()).
// Body
Block(
pathVar.Clone().Op(":=").Add(g.collectionPath(entity)),
resultVar.Clone().Op(":=").Op("&").Add(resultType.Clone()).Block(),
httpReqVar.Clone().Op(":=").Add(g.httpRequest(resultVar.Clone(), "Get", j.Nil())),
j.Return(
j.Qual(webAPIPkg, "NewAPIRequest").Call(resultVar.Clone(), httpReqVar.Clone()),
),
).
Line()
}
func (g *generator) byIDMethod(entity *entitySpec, file *j.File) {
if entity.PrimaryIDField == nil {
return
}
resultType := j.Id(entity.GoName)
file.Add(g.entityRepoMethod(entity, "ByID")).
Params(idVar.Clone().String()).
// Returns
Op("*").Qual(webAPIPkg, "APIRequest").Types(resultType.Clone()).
// Body
Block(
pathVar.Clone().Op(":=").Add(g.entityPathByID(entity)),
resultVar.Clone().Op(":=").Op("&").Add(resultType.Clone()).Block(),
httpReqVar.Clone().Op(":=").Add(g.httpRequest(resultVar.Clone(), "Get", j.Nil())),
j.Return(
j.Qual(webAPIPkg, "NewAPIRequest").Call(resultVar.Clone(), httpReqVar.Clone()),
),
).
Line()
}
func (g *generator) lookupHelperMethods(entity *entitySpec, file *j.File) {
for _, field := range entity.Fields {
if field.APIType == lookupFieldType {
g.lookupHelperMethodsForField(entity, field, file)
}
}
}
func (g *generator) lookupHelperMethodsForField(entity *entitySpec, field *fieldSpec, file *j.File) {
resultType := j.Qual(webAPIPkg, "Collection").Types(j.Id(entity.GoName))
file.Add(g.entityRepoMethod(entity, "By"+field.GoName)).
Params(idVar.Clone().String()).
// Returns
Op("*").Qual(webAPIPkg, "APIRequest").Types(resultType.Clone()).
// Body
Block(
pathVar.Clone().Op(":=").Add(g.collectionPath(entity)),
resultVar.Clone().Op(":=").Op("&").Add(resultType.Clone()).Block(),
httpReqVar.Clone().Op(":=").Add(g.httpRequest(resultVar.Clone(), "Get", j.Nil())),
httpReqVar.Clone().Dot("Filter").
Call(j.
Lit(field.LogicalName+" eq '").
Op("+").
Add(j.Qual(webAPIPkg, "ID").Call(idVar.Clone())).
Op("+").
Lit("'"),
),
j.Return(
j.Qual(webAPIPkg, "NewAPIRequest").Call(resultVar.Clone(), httpReqVar.Clone()),
),
).
Line()
}
func (g *generator) createPayloadMethod(entity *entitySpec, file *j.File) {
// Init payload variable, create a map
mapType := j.Map(j.String()).Any()
blocks := []j.Code{
payloadVar.Clone().Op(":=").Add(mapType).Clone().Block(),
}
// Generate diff for each field
for _, field := range entity.Fields {
if field.IsPrimaryID {
continue
}
if fieldBlocks := g.payloadFieldSetter(field, entityVar); len(fieldBlocks) > 1 {
blocks = append(blocks, j.Block(fieldBlocks...))
} else {
blocks = append(blocks, fieldBlocks...)
}
}
// Return
blocks = append(blocks, j.Return(payloadVar.Clone(), j.Nil()))
// Compose method
file.
Comment("createPayload method generates payload for the Create operation.").Line().
Add(g.entityRepoMethod(entity, "createPayload")).
Params(entityVar.Clone().Op("*").Id(entity.GoName)).
// Returns
Parens(j.List(mapType.Clone(), j.Id("error"))).
// Body
Block(blocks...).
Line()
}
func (g *generator) updatePayloadMethod(entity *entitySpec, file *j.File) {
// Init payload variable, create a map
mapType := j.Map(j.String()).Any()
blocks := []j.Code{
payloadVar.Clone().Op(":=").Add(mapType).Clone().Block(),
}
// Generate diff for each field
for _, field := range entity.Fields {
if field.IsPrimaryID {
continue
}
blocks = append(blocks, g.compareFieldValues(field).Block(g.payloadFieldSetter(field, modifiedVar)...))
}
// Return
blocks = append(blocks, j.Return(payloadVar.Clone(), j.Nil()))
// Compose method
file.
Comment("updatePayload method generates diff of original and modified entity state for the Update operation.").Line().
Add(g.entityRepoMethod(entity, "updatePayload")).
Params(originalVar.Clone(), modifiedVar.Clone().Op("*").Id(entity.GoName)).
// Returns
Parens(j.List(mapType.Clone(), j.Id("error"))).
// Body
Block(blocks...).
Line()
}
func (*generator) payloadFieldSetter(field *fieldSpec, entityValueVar *j.Statement) (codes []j.Code) {
value := entityValueVar.Clone().Dot(field.GoName)
switch field.APIType {
case lookupFieldType:
fullIDVar := j.Id("idOrNil")
codes = append(codes,
j.List(fullIDVar.Clone(), j.Id("err")).Op(":=").Id("lookupFullIDOrNil").Call(value.Clone()),
j.If(j.Id("err").Op("!=").Nil()).Block(
j.Return(j.Nil(), j.Id("err")),
),
payloadVar.Clone().
Index(j.Lit(field.SchemaName+"@odata.bind")).Op("=").
Add(fullIDVar.Clone()),
)
default:
codes = append(codes,
payloadVar.Clone().
Index(j.Lit(field.LogicalName)).Op("=").
Add(value.Clone()),
)
}
return codes
}
func (*generator) compareFieldValues(field *fieldSpec) *j.Statement {
var originalValue, modifiedValue *j.Statement
switch field.APIType {
case lookupFieldType:
originalValue = originalVar.Clone().Dot(field.GoName).Clone().Dot("ID").Call()
modifiedValue = modifiedVar.Clone().Dot(field.GoName).Clone().Dot("ID").Call()
default:
originalValue = originalVar.Clone().Dot(field.GoName)
modifiedValue = modifiedVar.Clone().Dot(field.GoName)
}
return j.If(originalValue.Clone().Op("!=").Add(modifiedValue.Clone()))
}
func (*generator) entityRepoMethod(entity *entitySpec, method string) *j.Statement {
return j.Func().Params(j.Id("r").Op("*").Id(entity.RepositoryName)).Id(method)
}
func (g *generator) entityPathByID(entity *entitySpec) *j.Statement {
return g.collectionPath(entity).
Op("+").
Lit("(").
Op("+").
Qual(webAPIPkg, "ID").Call(idVar.Clone()).
Op("+").
Lit(")")
}
func (*generator) collectionPath(entity *entitySpec) *j.Statement {
return j.Lit(entity.EntitySetName)
}
func (*generator) httpRequest(result *j.Statement, method string, body *j.Statement) *j.Statement {
return j.Qual(webAPIPkg, "NewHTTPRequest").Call(
result,
j.Id("r.client"),
j.Qual("net/http", "Method"+method),
j.Id("path"),
body,
)
}