/
gong.go
564 lines (458 loc) · 15 KB
/
gong.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package x
import (
"errors"
"fmt"
"time"
test2_models "github.com/fullstack-lang/gong/test2/go/models"
)
// errUnkownEnum is returns when a value cannot match enum values
var errUnkownEnum = errors.New("unkown enum")
// needed to avoid when fmt package is not needed by generated code
var __dummy__fmt_variable fmt.Scanner
// swagger:ignore
type __void any
// needed for creating set of instances in the stage
var __member __void
// GongStructInterface is the interface met by GongStructs
// It allows runtime reflexion of instances (without the hassle of the "reflect" package)
type GongStructInterface interface {
GetName() (res string)
// GetID() (res int)
// GetFields() (res []string)
// GetFieldStringValue(fieldName string) (res string)
}
// StageStruct enables storage of staged instances
// swagger:ignore
type StageStruct struct {
// each imported packages to the x package is compiled to
// a field to this stage struct of the imported package
// name deconflixion relies on the aliasing in the import
//
// packages imports within the module construe a directed acyclic graph (DAG)
// where each package is represented only once
// when a commit is performed, the first stage of the commit is performed
// recursively on all depdendant stages in the DAG. One stage can be present
// reached through multiple path but it performs the local first stage commit
// only once
//
GONG_Imported_Package_test2_models *test2_models.StageStruct
path string
// insertion point for definition of arrays registering instances
As map[*A]any
As_mapString map[string]*A
OnAfterACreateCallback OnAfterCreateInterface[A]
OnAfterAUpdateCallback OnAfterUpdateInterface[A]
OnAfterADeleteCallback OnAfterDeleteInterface[A]
OnAfterAReadCallback OnAfterReadInterface[A]
AllModelsStructCreateCallback AllModelsStructCreateInterface
AllModelsStructDeleteCallback AllModelsStructDeleteInterface
BackRepo BackRepoInterface
// if set will be called before each commit to the back repo
OnInitCommitCallback OnInitCommitInterface
OnInitCommitFromFrontCallback OnInitCommitInterface
OnInitCommitFromBackCallback OnInitCommitInterface
// store the number of instance per gongstruct
Map_GongStructName_InstancesNb map[string]int
// store meta package import
MetaPackageImportPath string
MetaPackageImportAlias string
// to be removed after fix of [issue](https://github.com/golang/go/issues/57559)
// map to enable docLink renaming when an identifier is renamed
Map_DocLink_Renaming map[string]GONG__Identifier
// the to be removed stops here
}
type GONG__Identifier struct {
Ident string
Type GONG__ExpressionType
}
type OnInitCommitInterface interface {
BeforeCommit(stage *StageStruct)
}
// OnAfterCreateInterface callback when an instance is updated from the front
type OnAfterCreateInterface[Type Gongstruct] interface {
OnAfterCreate(stage *StageStruct,
instance *Type)
}
// OnAfterReadInterface callback when an instance is updated from the front
type OnAfterReadInterface[Type Gongstruct] interface {
OnAfterRead(stage *StageStruct,
instance *Type)
}
// OnAfterUpdateInterface callback when an instance is updated from the front
type OnAfterUpdateInterface[Type Gongstruct] interface {
OnAfterUpdate(stage *StageStruct, old, new *Type)
}
// OnAfterDeleteInterface callback when an instance is updated from the front
type OnAfterDeleteInterface[Type Gongstruct] interface {
OnAfterDelete(stage *StageStruct,
staged, front *Type)
}
type BackRepoInterface interface {
Commit(stage *StageStruct)
Checkout(stage *StageStruct)
Backup(stage *StageStruct, dirPath string)
Restore(stage *StageStruct, dirPath string)
BackupXL(stage *StageStruct, dirPath string)
RestoreXL(stage *StageStruct, dirPath string)
// insertion point for Commit and Checkout signatures
CommitA(a *A)
CheckoutA(a *A)
GetLastCommitFromBackNb() uint
GetLastPushFromFrontNb() uint
}
func NewStage(path string) (stage *StageStruct) {
stage = &StageStruct{ // insertion point for array initiatialisation
As: make(map[*A]any),
As_mapString: make(map[string]*A),
// end of insertion point
Map_GongStructName_InstancesNb: make(map[string]int),
path: path,
// to be removed after fix of [issue](https://github.com/golang/go/issues/57559)
Map_DocLink_Renaming: make(map[string]GONG__Identifier),
// the to be removed stops here
}
return
}
func (stage *StageStruct) GetPath() string {
return stage.path
}
func (stage *StageStruct) CommitWithSuspendedCallbacks() {
tmp := stage.OnInitCommitFromBackCallback
stage.OnInitCommitFromBackCallback = nil
stage.Commit()
stage.OnInitCommitFromBackCallback = tmp
}
func (stage *StageStruct) Commit() {
if stage.BackRepo != nil {
stage.BackRepo.Commit(stage)
}
// insertion point for computing the map of number of instances per gongstruct
stage.Map_GongStructName_InstancesNb["A"] = len(stage.As)
}
func (stage *StageStruct) Checkout() {
if stage.BackRepo != nil {
stage.BackRepo.Checkout(stage)
}
// insertion point for computing the map of number of instances per gongstruct
stage.Map_GongStructName_InstancesNb["A"] = len(stage.As)
}
// backup generates backup files in the dirPath
func (stage *StageStruct) Backup(dirPath string) {
if stage.BackRepo != nil {
stage.BackRepo.Backup(stage, dirPath)
}
}
// Restore resets Stage & BackRepo and restores their content from the restore files in dirPath
func (stage *StageStruct) Restore(dirPath string) {
if stage.BackRepo != nil {
stage.BackRepo.Restore(stage, dirPath)
}
}
// backup generates backup files in the dirPath
func (stage *StageStruct) BackupXL(dirPath string) {
if stage.BackRepo != nil {
stage.BackRepo.BackupXL(stage, dirPath)
}
}
// Restore resets Stage & BackRepo and restores their content from the restore files in dirPath
func (stage *StageStruct) RestoreXL(dirPath string) {
if stage.BackRepo != nil {
stage.BackRepo.RestoreXL(stage, dirPath)
}
}
// insertion point for cumulative sub template with model space calls
// Stage puts a to the model stage
func (a *A) Stage(stage *StageStruct) *A {
stage.As[a] = __member
stage.As_mapString[a.Name] = a
return a
}
// Unstage removes a off the model stage
func (a *A) Unstage(stage *StageStruct) *A {
delete(stage.As, a)
delete(stage.As_mapString, a.Name)
return a
}
// UnstageVoid removes a off the model stage
func (a *A) UnstageVoid(stage *StageStruct) {
delete(stage.As, a)
delete(stage.As_mapString, a.Name)
}
// commit a to the back repo (if it is already staged)
func (a *A) Commit(stage *StageStruct) *A {
if _, ok := stage.As[a]; ok {
if stage.BackRepo != nil {
stage.BackRepo.CommitA(a)
}
}
return a
}
func (a *A) CommitVoid(stage *StageStruct) {
a.Commit(stage)
}
// Checkout a to the back repo (if it is already staged)
func (a *A) Checkout(stage *StageStruct) *A {
if _, ok := stage.As[a]; ok {
if stage.BackRepo != nil {
stage.BackRepo.CheckoutA(a)
}
}
return a
}
// for satisfaction of GongStruct interface
func (a *A) GetName() (res string) {
return a.Name
}
// swagger:ignore
type AllModelsStructCreateInterface interface { // insertion point for Callbacks on creation
CreateORMA(A *A)
}
type AllModelsStructDeleteInterface interface { // insertion point for Callbacks on deletion
DeleteORMA(A *A)
}
func (stage *StageStruct) Reset() { // insertion point for array reset
stage.As = make(map[*A]any)
stage.As_mapString = make(map[string]*A)
}
func (stage *StageStruct) Nil() { // insertion point for array nil
stage.As = nil
stage.As_mapString = nil
}
func (stage *StageStruct) Unstage() { // insertion point for array nil
for a := range stage.As {
a.Unstage(stage)
}
}
// Gongstruct is the type parameter for generated generic function that allows
// - access to staged instances
// - navigation between staged instances by going backward association links between gongstruct
// - full refactoring of Gongstruct identifiers / fields
type Gongstruct interface {
// insertion point for generic types
A
}
type GongtructBasicField interface {
int | float64 | bool | string | time.Time | time.Duration
}
// Gongstruct is the type parameter for generated generic function that allows
// - access to staged instances
// - navigation between staged instances by going backward association links between gongstruct
// - full refactoring of Gongstruct identifiers / fields
type PointerToGongstruct interface {
// insertion point for generic types
*A
GetName() string
CommitVoid(*StageStruct)
UnstageVoid(stage *StageStruct)
}
type GongstructSet interface {
map[any]any |
// insertion point for generic types
map[*A]any |
map[*any]any // because go does not support an extra "|" at the end of type specifications
}
type GongstructMapString interface {
map[any]any |
// insertion point for generic types
map[string]*A |
map[*any]any // because go does not support an extra "|" at the end of type specifications
}
// GongGetSet returns the set staged GongstructType instances
// it is usefull because it allows refactoring of gong struct identifier
func GongGetSet[Type GongstructSet](stage *StageStruct) *Type {
var ret Type
switch any(ret).(type) {
// insertion point for generic get functions
case map[*A]any:
return any(&stage.As).(*Type)
default:
return nil
}
}
// GongGetMap returns the map of staged GongstructType instances
// it is usefull because it allows refactoring of gong struct identifier
func GongGetMap[Type GongstructMapString](stage *StageStruct) *Type {
var ret Type
switch any(ret).(type) {
// insertion point for generic get functions
case map[string]*A:
return any(&stage.As_mapString).(*Type)
default:
return nil
}
}
// GetGongstructInstancesSet returns the set staged GongstructType instances
// it is usefull because it allows refactoring of gongstruct identifier
func GetGongstructInstancesSet[Type Gongstruct](stage *StageStruct) *map[*Type]any {
var ret Type
switch any(ret).(type) {
// insertion point for generic get functions
case A:
return any(&stage.As).(*map[*Type]any)
default:
return nil
}
}
// GetGongstructInstancesSetFromPointerType returns the set staged GongstructType instances
// it is usefull because it allows refactoring of gongstruct identifier
func GetGongstructInstancesSetFromPointerType[Type PointerToGongstruct](stage *StageStruct) *map[Type]any {
var ret Type
switch any(ret).(type) {
// insertion point for generic get functions
case *A:
return any(&stage.As).(*map[Type]any)
default:
return nil
}
}
// GetGongstructInstancesMap returns the map of staged GongstructType instances
// it is usefull because it allows refactoring of gong struct identifier
func GetGongstructInstancesMap[Type Gongstruct](stage *StageStruct) *map[string]*Type {
var ret Type
switch any(ret).(type) {
// insertion point for generic get functions
case A:
return any(&stage.As_mapString).(*map[string]*Type)
default:
return nil
}
}
// GetAssociationName is a generic function that returns an instance of Type
// where each association is filled with an instance whose name is the name of the association
//
// This function can be handy for generating navigation function that are refactorable
func GetAssociationName[Type Gongstruct]() *Type {
var ret Type
switch any(ret).(type) {
// insertion point for instance with special fields
case A:
return any(&A{
// Initialisation of associations
}).(*Type)
default:
return nil
}
}
// GetPointerReverseMap allows backtrack navigation of any Start.Fieldname
// associations (0..1) that is a pointer from one staged Gongstruct (type Start)
// instances to another (type End)
//
// The function provides a map with keys as instances of End and values to arrays of *Start
// the map is construed by iterating over all Start instances and populationg keys with End instances
// and values with slice of Start instances
func GetPointerReverseMap[Start, End Gongstruct](fieldname string, stage *StageStruct) map[*End][]*Start {
var ret Start
switch any(ret).(type) {
// insertion point of functions that provide maps for reverse associations
// reverse maps of direct associations of A
case A:
switch fieldname {
// insertion point for per direct association field
}
}
return nil
}
// GetSliceOfPointersReverseMap allows backtrack navigation of any Start.Fieldname
// associations (0..N) between one staged Gongstruct instances and many others
//
// The function provides a map with keys as instances of End and values to *Start instances
// the map is construed by iterating over all Start instances and populating keys with End instances
// and values with the Start instances
func GetSliceOfPointersReverseMap[Start, End Gongstruct](fieldname string, stage *StageStruct) map[*End]*Start {
var ret Start
switch any(ret).(type) {
// insertion point of functions that provide maps for reverse associations
// reverse maps of direct associations of A
case A:
switch fieldname {
// insertion point for per direct association field
}
}
return nil
}
// GetGongstructName returns the name of the Gongstruct
// this can be usefull if one want program robust to refactoring
func GetGongstructName[Type Gongstruct]() (res string) {
var ret Type
switch any(ret).(type) {
// insertion point for generic get gongstruct name
case A:
res = "A"
}
return res
}
// GetPointerToGongstructName returns the name of the Gongstruct
// this can be usefull if one want program robust to refactoring
func GetPointerToGongstructName[Type PointerToGongstruct]() (res string) {
var ret Type
switch any(ret).(type) {
// insertion point for generic get gongstruct name
case *A:
res = "A"
}
return res
}
// GetFields return the array of the fields
func GetFields[Type Gongstruct]() (res []string) {
var ret Type
switch any(ret).(type) {
// insertion point for generic get gongstruct name
case A:
res = []string{"Name"}
}
return
}
type ReverseField struct {
GongstructName string
Fieldname string
}
func GetReverseFields[Type Gongstruct]() (res []ReverseField) {
res = make([]ReverseField, 0)
var ret Type
switch any(ret).(type) {
// insertion point for generic get gongstruct name
case A:
var rf ReverseField
_ = rf
}
return
}
// GetFieldsFromPointer return the array of the fields
func GetFieldsFromPointer[Type PointerToGongstruct]() (res []string) {
var ret Type
switch any(ret).(type) {
// insertion point for generic get gongstruct name
case *A:
res = []string{"Name"}
}
return
}
func GetFieldStringValueFromPointer[Type PointerToGongstruct](instance Type, fieldName string) (res string) {
switch inferedInstance := any(instance).(type) {
// insertion point for generic get gongstruct field value
case *A:
switch fieldName {
// string value of fields
case "Name":
res = inferedInstance.Name
}
default:
_ = inferedInstance
}
return
}
func GetFieldStringValue[Type Gongstruct](instance Type, fieldName string) (res string) {
switch inferedInstance := any(instance).(type) {
// insertion point for generic get gongstruct field value
case A:
switch fieldName {
// string value of fields
case "Name":
res = inferedInstance.Name
}
default:
_ = inferedInstance
}
return
}
// Last line of the template