/
generate.go
1089 lines (974 loc) · 36.8 KB
/
generate.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 NDP Systèmes. All Rights Reserved.
// See LICENSE file for full licensing details.
package generate
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"text/template"
"github.com/labneco/doxa/doxa/tools/strutils"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/imports"
)
// A fieldData describes a field in a RecordSet
type fieldData struct {
Name string
JSON string
RelModel string
Type string
SanType string
IsRS bool
MixinField bool
EmbedField bool
}
// A methodData describes a method in a RecordSet
type methodData struct {
Name string
Doc string
Params string
ParamsWithType string
ParamsTypes string
ReturnAsserts string
Returns string
ReturnString string
Call string
ToDeclare bool
}
// an operatorDef defines an operator func
type operatorDef struct {
Name string
Multi bool
}
// An fieldType holds the name and valid operators on a field type
type fieldType struct {
Type string
SanType string
IsRS bool
Operators []operatorDef
}
// A modelData describes a RecordSet model
type modelData struct {
Name string
SnakeName string
ModelsPackageName string
QueryPackageName string
ModelType string
IsModelMixin bool
Deps []string
RelModels []string
Fields []fieldData
Methods []methodData
AllMethods []methodData
ConditionFuncs []string
Types []fieldType
}
// sort sorts all slices fields of this modelData so that the generated code is always the same.
func (m *modelData) sort() {
sort.Strings(m.Deps)
sort.Slice(m.Fields, func(i, j int) bool {
return m.Fields[i].Name < m.Fields[j].Name
})
sort.Slice(m.Methods, func(i, j int) bool {
return m.Methods[i].Name < m.Methods[j].Name
})
sort.Slice(m.AllMethods, func(i, j int) bool {
return m.AllMethods[i].Name < m.AllMethods[j].Name
})
sort.Strings(m.Deps)
sort.Strings(m.RelModels)
sort.Slice(m.Types, func(i, j int) bool {
return m.Types[i].Type < m.Types[j].Type
})
}
// specificMethodsHandlers are functions that populate the given modelData
// for specific methods.
var specificMethodsHandlers = map[string]func(modelData *modelData, depsMap *map[string]bool){
"Search": searchMethodHandler,
"SearchByName": searchByNameMethodHandler,
"First": firstMethodHandler,
"All": allMethodHandler,
"Create": createMethodHandler,
"Write": writeMethodHandler,
"Copy": copyMethodHandler,
"CartesianProduct": cartesianProductMethodHandler,
"Sorted": sortedMethodHandler,
"Filtered": filteredMethodHandler,
}
// searchMethodHandler returns the specific methodData for the Search method.
func searchMethodHandler(modelData *modelData, depsMap *map[string]bool) {
name := "Search"
returnString := fmt.Sprintf("%sSet", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ParamsTypes: fmt.Sprintf("%s.%sCondition", PoolQueryPackage, modelData.Name),
ReturnString: returnString,
})
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: false,
Params: "condition",
ParamsWithType: fmt.Sprintf("condition %s.%sCondition", PoolQueryPackage, modelData.Name),
ReturnAsserts: "resTyped := res.(models.RecordSet).Collection()",
Returns: fmt.Sprintf("%sSet{RecordCollection: resTyped}", modelData.Name),
ReturnString: returnString,
Call: "Call",
})
}
// firstMethodHandler returns the specific methodData for the First method.
func firstMethodHandler(modelData *modelData, depsMap *map[string]bool) {
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: "First",
ParamsTypes: "",
ReturnString: fmt.Sprintf("%sData", modelData.Name),
})
}
// allMethodHandler returns the specific methodData for the All method.
func allMethodHandler(modelData *modelData, depsMap *map[string]bool) {
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: "All",
ParamsTypes: "",
ReturnString: fmt.Sprintf("[]%sData", modelData.Name),
})
}
// createMethodHandler returns the specific methodData for the Create method.
func createMethodHandler(modelData *modelData, depsMap *map[string]bool) {
name := "Create"
returnString := fmt.Sprintf("%sSet", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ParamsTypes: fmt.Sprintf("*%sData", modelData.Name),
ReturnString: returnString,
})
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: false,
Params: "data",
ParamsWithType: fmt.Sprintf("data *%sData", modelData.Name),
ReturnAsserts: "resTyped := res.(models.RecordSet).Collection()",
Returns: fmt.Sprintf("%sSet{RecordCollection: resTyped}", modelData.Name),
ReturnString: returnString,
Call: "Call",
})
}
// writeMethodHandler returns the specific methodData for the Write method.
func writeMethodHandler(modelData *modelData, depsMap *map[string]bool) {
name := "Write"
returnString := "bool"
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ParamsTypes: fmt.Sprintf("*%sData, ...models.FieldNamer", modelData.Name),
ReturnString: returnString,
})
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.
//
// Only fields with non zero values or fields passed in the 'fieldsToReset' arg are updated`, modelData.Name),
ToDeclare: false,
Params: "data, fieldsToReset",
ParamsWithType: fmt.Sprintf("data *%sData, fieldsToReset ...models.FieldNamer", modelData.Name),
ReturnAsserts: "resTyped, _ := res.(bool)",
Returns: "resTyped",
ReturnString: returnString,
Call: "Call",
})
}
// copyMethodHandler returns the specific methodData for the Copy method.
func copyMethodHandler(modelData *modelData, depsMap *map[string]bool) {
name := "Copy"
returnString := fmt.Sprintf("%sSet", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ParamsTypes: fmt.Sprintf("*%sData, ...models.FieldNamer", modelData.Name),
ReturnString: returnString,
})
modelData.Methods = append(modelData.Methods, methodData{
Name: name,
Doc: fmt.Sprintf(`// Copy duplicates the given %s record, overridding values with overrides.
//
// Only fields with non zero values of overrides or fields passed in the 'fieldsToReset' arg are updated`, modelData.Name),
ToDeclare: false,
Params: "overrides, fieldsToReset",
ParamsWithType: fmt.Sprintf("overrides *%sData, fieldsToReset ...models.FieldNamer", modelData.Name),
ReturnAsserts: "resTyped := res.(models.RecordSet).Collection()",
Returns: fmt.Sprintf("%sSet{RecordCollection: resTyped}", modelData.Name),
ReturnString: returnString,
Call: "Call",
})
}
// searchByNameMethodHandler returns the specific methodData for the Search method.
func searchByNameMethodHandler(modelData *modelData, depsMap *map[string]bool) {
name := "SearchByName"
returnString := fmt.Sprintf("%sSet", modelData.Name)
(*depsMap)["github.com/labneco/doxa/doxa/models/operator"] = true
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ParamsTypes: fmt.Sprintf("string, operator.Operator, %s.%sCondition, int", PoolQueryPackage, modelData.Name),
ReturnString: returnString,
})
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: false,
Params: "name, op, additionalCond, limit",
ParamsWithType: fmt.Sprintf("name string, op operator.Operator, additionalCond %s.%sCondition, limit int", PoolQueryPackage, modelData.Name),
ReturnAsserts: "resTyped := res.(models.RecordSet).Collection()",
Returns: fmt.Sprintf("%sSet{RecordCollection: resTyped}", modelData.Name),
ReturnString: returnString,
Call: "Call",
})
}
// cartesianProductMethodHandler returns the specific methodData for the CartesianProduct method.
func cartesianProductMethodHandler(modelData *modelData, depsMap *map[string]bool) {
name := "CartesianProduct"
returnString := fmt.Sprintf("[]%sSet", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ParamsTypes: fmt.Sprintf("...%sSet", modelData.Name),
ReturnString: returnString,
})
}
// sortedMethodHandler returns the specific methodData for the Sorted method.
func sortedMethodHandler(modelData *modelData, depsMap *map[string]bool) {
name := "Sorted"
returnString := fmt.Sprintf("%sSet", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ParamsTypes: fmt.Sprintf("func(%sSet, %sSet) bool", modelData.Name, modelData.Name),
ReturnString: returnString,
})
}
// filteredMethodHandler returns the specific methodData for the Sorted method.
func filteredMethodHandler(modelData *modelData, depsMap *map[string]bool) {
name := "Filtered"
returnString := fmt.Sprintf("%sSet", modelData.Name)
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: name,
ParamsTypes: fmt.Sprintf("func(%sSet) bool", modelData.Name),
ReturnString: returnString,
})
}
// createTypeIdent creates a string from the given type that
// can be used inside an identifier.
func createTypeIdent(typStr string) string {
res := strings.Replace(typStr, ".", "", -1)
res = strings.Replace(res, "[", "Slice", -1)
res = strings.Replace(res, "map[", "Map", -1)
res = strings.Replace(res, "]", "", -1)
res = strings.Title(res)
return res
}
// CreatePool generates the pool package by parsing the source code AST
// of the given program.
// The generated package will be put in the given dir.
func CreatePool(program *loader.Program, dir string) {
modelsASTData := GetModelsASTData(program)
for modelName, modelASTData := range modelsASTData {
depsMap := map[string]bool{ModelsPath: true}
mData := modelData{
Name: modelName,
SnakeName: strutils.SnakeCaseString(modelName),
ModelsPackageName: PoolModelPackage,
QueryPackageName: PoolQueryPackage,
ModelType: modelASTData.ModelType,
IsModelMixin: modelASTData.IsModelMixin,
ConditionFuncs: []string{"And", "AndNot", "Or", "OrNot"},
}
// Add fields
addFieldsToModelData(modelASTData, &mData, &depsMap)
// Add field types
addFieldTypesToModelData(&mData)
// Add methods
addMethodsToModelData(modelsASTData, &mData, &depsMap)
// Setting imports
var deps []string
for dep := range depsMap {
if dep == "" {
continue
}
deps = append(deps, dep)
}
mData.Deps = deps
// Writing to file
createPoolFiles(dir, &mData)
}
}
// addMethodsToModelData extracts data from modelsASTData to populate methods in modelData
func addMethodsToModelData(modelsASTData map[string]ModelASTData, modelData *modelData, depsMap *map[string]bool) {
modelASTData := modelsASTData[modelData.Name]
for methodName, methodASTData := range modelASTData.Methods {
if handler, exists := specificMethodsHandlers[methodName]; exists {
handler(modelData, depsMap)
continue
}
var params, paramsWithType, paramsType, call, returns, returnAsserts, returnString string
for _, astParam := range methodASTData.Params {
paramType := astParam.Type.Type
p := fmt.Sprintf("%s,", astParam.Name)
if isRS, isRC := isRecordSetType(astParam.Type.Type, modelsASTData); isRS {
if isRC {
paramType = fmt.Sprintf("%sSet", modelData.Name)
}
}
if astParam.Variadic {
paramType = fmt.Sprintf("...%s", paramType)
}
params += p
paramsWithType += fmt.Sprintf("%s %s,", astParam.Name, paramType)
paramsType += fmt.Sprintf("%s,", paramType)
(*depsMap)[astParam.Type.ImportPath] = true
}
if len(methodASTData.Returns) == 1 {
call = "Call"
(*depsMap)[methodASTData.Returns[0].ImportPath] = true
typ := methodASTData.Returns[0].Type
returnAsserts = fmt.Sprintf("resTyped, _ := res.(%s)", typ)
returns = "resTyped"
if isRS, isRC := isRecordSetType(typ, modelsASTData); isRS {
if isRC {
typ = fmt.Sprintf("%sSet", modelData.Name)
}
returnAsserts = "resTyped := res.(models.RecordSet).Collection()"
returns = fmt.Sprintf("%s{RecordCollection: resTyped}", typ)
}
returnString = typ
} else if len(methodASTData.Returns) > 1 {
for i, ret := range methodASTData.Returns {
call = "CallMulti"
(*depsMap)[ret.ImportPath] = true
if isRS, isRC := isRecordSetType(ret.Type, modelsASTData); isRS {
retType := ret.Type
if isRC {
retType = fmt.Sprintf("%sSet", modelData.Name)
}
returnAsserts += fmt.Sprintf("resTyped%d := res[%d].(models.RecordSet).Collection()\n", i, i)
returns += fmt.Sprintf("%s{RecordCollection: resTyped%d},", retType, i)
returnString += fmt.Sprintf("%s,", retType)
} else {
returnAsserts += fmt.Sprintf("resTyped%d, _ := res[%d].(%s)\n", i, i, ret.Type)
returns += fmt.Sprintf("resTyped%d,", i)
returnString += fmt.Sprintf("%s,", ret.Type)
}
}
}
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: methodName,
ParamsTypes: strings.TrimRight(paramsType, ","),
ReturnString: strings.TrimSuffix(returnString, ","),
})
modelData.Methods = append(modelData.Methods, methodData{
Name: methodName,
Doc: methodASTData.Doc,
ToDeclare: methodASTData.ToDeclare,
Params: strings.TrimRight(params, ","),
ParamsWithType: strings.TrimRight(paramsWithType, ","),
ReturnAsserts: strings.TrimSuffix(returnAsserts, "\n"),
Returns: strings.TrimSuffix(returns, ","),
ReturnString: strings.TrimSuffix(returnString, ","),
Call: call,
})
}
}
// addFieldsToModelData extracts data from modelASTData to populate fields in modelData
func addFieldsToModelData(modelASTData ModelASTData, modelData *modelData, depsMap *map[string]bool) {
relModels := make(map[string]bool)
for fieldName, fieldASTData := range modelASTData.Fields {
typStr := fieldASTData.Type.Type
if fieldASTData.RelModel != "" {
relModels[fieldASTData.RelModel] = true
typStr = fmt.Sprintf("%sSet", fieldASTData.RelModel)
}
jsonName := strutils.GetDefaultString(fieldASTData.JSON, strutils.SnakeCaseString(fieldName))
modelData.Fields = append(modelData.Fields, fieldData{
Name: fieldName,
JSON: jsonName,
Type: typStr,
IsRS: fieldASTData.IsRS,
RelModel: fieldASTData.RelModel,
SanType: createTypeIdent(typStr),
MixinField: fieldASTData.MixinField,
EmbedField: fieldASTData.EmbedField,
})
(*depsMap)[fieldASTData.Type.ImportPath] = true
}
for rm := range relModels {
modelData.RelModels = append(modelData.RelModels, rm)
}
}
// addFieldsToModelData extracts field types from mData.Fields
// and add them to mData.Types
func addFieldTypesToModelData(mData *modelData) {
fTypes := make(map[string]bool)
for _, f := range mData.Fields {
if fTypes[f.Type] {
continue
}
fTypes[f.Type] = true
mData.Types = append(mData.Types, fieldType{
Type: f.Type,
SanType: f.SanType,
IsRS: f.IsRS,
Operators: []operatorDef{
{Name: "Equals"}, {Name: "NotEquals"}, {Name: "Greater"}, {Name: "GreaterOrEqual"}, {Name: "Lower"},
{Name: "LowerOrEqual"}, {Name: "Like"}, {Name: "Contains"}, {Name: "NotContains"}, {Name: "IContains"},
{Name: "NotIContains"}, {Name: "ILike"}, {Name: "In", Multi: true}, {Name: "NotIn", Multi: true},
{Name: "ChildOf"},
},
})
}
}
// createPoolFiles creates all pool files for the given model data
func createPoolFiles(dir string, mData *modelData) {
mData.sort()
// create the model's file in models directory
fileName := filepath.Join(dir, PoolModelPackage, fmt.Sprintf("%s.go", mData.SnakeName))
CreateFileFromTemplate(fileName, poolModelsTemplate, mData)
// create the model's query directory
if _, err := os.Stat(filepath.Join(dir, PoolQueryPackage, mData.SnakeName)); err != nil {
if err = os.MkdirAll(filepath.Join(dir, PoolQueryPackage, mData.SnakeName), 0755); err != nil {
panic(err)
}
}
// create the model's query file in query dir
fileName = filepath.Join(dir, PoolQueryPackage, fmt.Sprintf("%s.go", mData.SnakeName))
CreateFileFromTemplate(fileName, poolQueryTemplate, mData)
// create the model's query file in model's query dir
fileName = filepath.Join(dir, PoolQueryPackage, mData.SnakeName, fmt.Sprintf("%s.go", mData.SnakeName))
CreateFileFromTemplate(fileName, poolModelsQueryTemplate, mData)
}
// isRecordSetType returns true if the given typ is a RecordSet according
// to the AST data stored in models.
// The second returned value is true if typ is models.RecordCollection or models.RecordSet
// and false if it is a specific RecordSet type
func isRecordSetType(typ string, models map[string]ModelASTData) (bool, bool) {
if typ == "*RecordCollection" || typ == "*models.RecordCollection" {
return true, true
}
if typ == "RecordSet" || typ == "models.RecordSet" {
return true, true
}
if _, exists := models[strings.TrimSuffix(typ, "Set")]; exists {
return true, false
}
return false, false
}
// CreateFileFromTemplate generates a new file from the given template and data
func CreateFileFromTemplate(fileName string, template *template.Template, data interface{}) {
var srcBuffer bytes.Buffer
template.Execute(&srcBuffer, data)
srcData, err := imports.Process(fileName, srcBuffer.Bytes(), nil)
if err != nil {
log.Panic("Error while formatting generated source file", "error", err, "fileName",
fileName, "mData", fmt.Sprintf("%#v", data), "src", srcBuffer.String())
}
// Write to file
err = ioutil.WriteFile(fileName, srcData, 0644)
if err != nil {
log.Panic("Error while saving generated source file", "error", err, "fileName", fileName)
}
}
var poolModelsTemplate = template.Must(template.New("").Parse(`
// This file is autogenerated by doxa-generate
// DO NOT MODIFY THIS FILE - ANY CHANGES WILL BE OVERWRITTEN
package {{ .ModelsPackageName }}
import (
"github.com/labneco/doxa/doxa/tools/typesutils"
"github.com/labneco/doxa/pool/{{ .QueryPackageName }}"
{{ range .Deps }} "{{ . }}"
{{ end }}
)
// ------- MODEL ---------
// {{ .Name }}Model is a strongly typed model definition that is used
// to extend the {{ .Name }} model or to get a {{ .Name }}Set through
// its NewSet() function.
//
// To get the unique instance of this type, call {{ .Name }}().
type {{ .Name }}Model struct {
*models.Model
}
{{ if eq .ModelType "Mixin" }}
// NewSet returns a new {{ .Name }}Set instance wrapping the given model in the given Environment
func (m {{ .Name }}Model) NewSet(env models.Environment, modelName string) {{ .Name }}Set {
return {{ .Name }}Set{
RecordCollection: env.Pool(modelName),
}
}
{{ else }}
// NewSet returns a new {{ .Name }}Set instance in the given Environment
func (m {{ .Name }}Model) NewSet(env models.Environment) {{ .Name }}Set {
return {{ .Name }}Set{
RecordCollection: env.Pool("{{ .Name }}"),
}
}
// Create creates a new {{ .Name }} record and returns the newly created
// {{ .Name }}Set instance.
func (m {{ .Name }}Model) Create(env models.Environment, data *{{ .Name }}Data) {{ .Name }}Set {
return {{ .Name }}Set{
RecordCollection: m.Model.Create(env, data),
}
}
// Search searches the database and returns a new {{ .Name }}Set instance
// with the records found.
func (m {{ .Name }}Model) Search(env models.Environment, cond {{ $.QueryPackageName }}.{{ .Name }}Condition) {{ .Name }}Set {
return {{ .Name }}Set{
RecordCollection: m.Model.Search(env, cond),
}
}
// Browse returns a new RecordSet with the records with the given ids.
// Note that this function is just a shorcut for Search on a list of ids.
func (m {{ .Name }}Model) Browse(env models.Environment, ids []int64) {{ .Name }}Set {
return {{ .Name }}Set{
RecordCollection: m.Model.Browse(env, ids),
}
}
{{ end }}
// Fields returns the Field Collection of the {{ .Name }} Model
func (m {{ .Name }}Model) Fields() p{{ .Name }}FieldsCollection {
return p{{ .Name }}FieldsCollection {
FieldsCollection: m.Model.Fields(),
}
}
// Methods returns the Method Collection of the {{ .Name }} Model
func (m {{ .Name }}Model) Methods() p{{ .Name }}MethodsCollection {
return p{{ .Name }}MethodsCollection {
MethodsCollection: m.Model.Methods(),
}
}
// Underlying returns the underlying models.Model instance
func (m {{ .Name }}Model) Underlying() *models.Model {
return m.Model
}
var _ models.Modeler = {{ .Name }}Model{}
// Declare{{ .ModelType }}Model is a dummy method used for code generation
// It just returns m.
func (m {{ .Name }}Model) Declare{{ .ModelType }}Model() {{ .Name }}Model {
return m
}
// {{ .Name }} returns the unique instance of the {{ .Name }}Model type
// which is used to extend the {{ .Name }} model or to get a {{ .Name }}Set through
// its NewSet() function.
func {{ .Name }}() {{ .Name }}Model {
return {{ .Name }}Model{
Model: models.Registry.MustGet("{{ .Name }}"),
}
}
{{ range .Fields }}
// {{ .Name }} returns a FieldNamer for the "{{ .Name }}" field
func (m {{ $.Name }}Model) {{ .Name }}() models.FieldName {
return models.FieldName("{{ .Name }}")
}
{{ end }}
// ------- FIELD COLLECTION ----------
// A p{{ .Name }}FieldsCollection is the collection of fields
// of the {{ .Name }} model.
type p{{ .Name }}FieldsCollection struct {
*models.FieldsCollection
}
{{ range .Fields }}
// {{ .Name }} returns a pointer to the {{ .Name }} Field.
func (c p{{ $.Name }}FieldsCollection) {{ .Name }}() *models.Field {
return c.MustGet("{{ .Name }}")
}
{{ end }}
// ------- METHOD COLLECTION ----------
// A p{{ .Name }}MethodsCollection is the collection of methods
// of the {{ .Name }} model.
type p{{ .Name }}MethodsCollection struct {
*models.MethodsCollection
}
{{ range .AllMethods }}
// p{{ $.Name }}_{{ .Name }} holds the metadata of the {{ $.Name }}.{{ .Name }}() method
type p{{ $.Name }}_{{ .Name }} struct {
*models.Method
}
// Extend adds the given fnct function as a new layer on this method.
func (m p{{ $.Name }}_{{ .Name }}) Extend(doc string, fnct func({{ $.Name }}Set{{ if ne .ParamsTypes "" }}, {{ .ParamsTypes }}{{ end }}) ({{ .ReturnString }})) p{{ $.Name }}_{{ .Name }} {
return p{{ $.Name }}_{{ .Name }} {
Method: m.Method.Extend(doc, fnct),
}
}
// DeclareMethod declares this method to the framework with the given function as the first layer.
func (m p{{ $.Name }}_{{ .Name }}) DeclareMethod(doc string, fnct interface{}) p{{ $.Name }}_{{ .Name }} {
return p{{ $.Name }}_{{ .Name }} {
Method: m.Method.DeclareMethod(doc, fnct),
}
}
// Underlying returns a pointer to the underlying Method data object.
func (m p{{ $.Name }}_{{ .Name }}) Underlying() *models.Method {
return m.Method
}
var _ models.Methoder = p{{ $.Name }}_{{ .Name }}{}
// {{ .Name }} returns a pointer to the {{ .Name }} Method.
func (c p{{ $.Name }}MethodsCollection) {{ .Name }}() p{{ $.Name }}_{{ .Name }} {
return p{{ $.Name }}_{{ .Name }} {
Method: c.MustGet("{{ .Name }}"),
}
}
{{ end }}
// ------- DATA STRUCT ---------
// {{ .Name }}Data is an autogenerated struct type to handle {{ .Name }} data.
type {{ .Name }}Data struct {
{{ range .Fields }} {{ .Name }} {{ .Type }} ` + "`json:\"{{ .JSON }}\"`" + `
{{ end }}
}
// FieldMap returns this {{ .Name }}Data as a FieldMap with JSON names as keys.
// Only {{ .Name }}Data with non zero values will be set in the FieldMap.
// To add fields with zero values to the map, give them as fields in parameters.
func (d {{ .Name }}Data) FieldMap(fields ...models.FieldNamer) models.FieldMap {
res := make(models.FieldMap)
fieldsMap := make(map[string]bool)
var fJSON string
for _, field := range fields {
fJSON = {{ .Name }}().JSONizeFieldName(field.String())
fieldsMap[fJSON] = true
}
{{ range .Fields -}}
fJSON = {{ $.Name }}().JSONizeFieldName("{{ .Name }}")
if fieldsMap[fJSON] || !typesutils.IsZero(d.{{ .Name }}) {
res[fJSON] = d.{{ .Name }}
}
{{ end }}
return res
}
// Get returns the value of this {{ .Name }}Data for the given field.
// If the field equals its go zero value, then it returns nil, except if the
// field is given in fieldsToReset, in which case the zero value is returned.
//
// The second argument is true if the field has a non-zero go value or is in the fieldsToReset.
func (d {{ .Name }}Data) Get(field models.FieldNamer, fieldsToReset ...models.FieldNamer) (interface{}, bool) {
val, exists := d.FieldMap(fieldsToReset...).Get(field.String(), {{ .Name }}().Model)
if exists {
return val, true
}
return nil, false
}
// FieldsSet returns the list of fields set for update, taking into account the fieldsToReset.
func (d {{ .Name }}Data) FieldsSet(fieldsToReset ...models.FieldNamer) []models.FieldNamer {
return d.FieldMap(fieldsToReset...).FieldNames()
}
// Remove returns a new {{ .Name }}Data and []FieldNamer with the given field set to its
// zero value and removed from the returned FieldNamer slice.
func (d {{ .Name }}Data) Remove(rs {{ .Name }}Set, field models.FieldNamer, fieldsToReset ...models.FieldNamer) (*{{ .Name }}Data, []models.FieldNamer) {
fMap := d.FieldMap(fieldsToReset...)
fMap.Delete(field.String(), {{ .Name }}().Model)
return rs.DataStruct(fMap)
}
var _ models.FieldMapper = {{ .Name }}Data{}
var _ models.FieldMapper = new({{ .Name }}Data)
// ------- RECORD SET ---------
// {{ .Name }}Set is an autogenerated type to handle {{ .Name }} objects.
type {{ .Name }}Set struct {
*models.RecordCollection
}
var _ models.RecordSet = {{ .Name }}Set{}
// {{ .Name }}SetDoxaFunc is a dummy function to uniquely match interfaces.
func (s {{ .Name }}Set) {{ .Name }}SetDoxaFunc() {}
// First returns a copy of the first Record of this RecordSet.
// It returns an empty {{ .Name }} if the RecordSet is empty.
func (s {{ .Name }}Set) First() {{ .Name }}Data {
var res {{ .Name }}Data
s.RecordCollection.First(&res)
return res
}
// All Returns a copy of all records of the RecordCollection.
// It returns an empty slice if the RecordSet is empty.
func (s {{ .Name }}Set) All() []{{ .Name }}Data {
var ptrSlice []*{{ .Name }}Data
s.RecordCollection.All(&ptrSlice)
res := make([]{{ .Name }}Data, len(ptrSlice))
for i, ps := range ptrSlice {
res[i] = *ps
}
return res
}
// Records returns a slice with all the records of this RecordSet, as singleton
// RecordSets
func (s {{ .Name }}Set) Records() []{{ .Name }}Set {
recs := s.RecordCollection.Records()
res := make([]{{ .Name }}Set, len(recs))
for i, rec := range recs {
res[i] = {{ .Name }}Set{
RecordCollection: rec,
}
}
return res
}
// CartesianProduct returns the cartesian product of this {{ .Name }}Set with others.
func (s {{ .Name }}Set) CartesianProduct(others ...{{ .Name }}Set) []{{ .Name }}Set {
otherSet := make([]models.RecordSet, len(others))
for i, o := range others {
otherSet[i] = o
}
recs := s.RecordCollection.CartesianProduct(otherSet...)
res := make([]{{ .Name }}Set, len(recs))
for i, rec := range recs {
res[i] = {{ .Name }}Set{
RecordCollection: rec,
}
}
return res
}
// Sorted returns a new {{ .Name}}Set sorted according to the given less function.
//
// The less function should return true if rs1 < rs2
func (s {{ .Name}}Set) Sorted(less func(rs1, rs2 {{ .Name}}Set) bool) {{ .Name}}Set {
res := s.RecordCollection.Sorted(func(rc1 models.RecordSet, rc2 models.RecordSet) bool {
return less({{ .Name }}Set{RecordCollection: rc1.Collection()}, {{ .Name }}Set{RecordCollection: rc2.Collection()})
})
return {{ .Name }}Set{
RecordCollection: res,
}
}
// Filtered returns a new {{ .Name }}Set with only the elements of this record set
// for which test is true.
//
// Note that if this {{ .Name }}Set is not fully loaded, this function will call the database
// to load the fields before doing the filtering. In this case, it might be more efficient
// to search the database directly with the filter condition.
func (s {{ .Name}}Set) Filtered(test func(rs {{ .Name}}Set) bool) {{ .Name}}Set {
res := s.RecordCollection.Filtered(func(rc models.RecordSet) bool {
return test({{ .Name }}Set{RecordCollection: rc.Collection()})
})
return {{ .Name }}Set{
RecordCollection: res,
}
}
// Model returns an instance of {{ .Name }}Model
func (s {{ .Name }}Set) Model() {{ .Name }}Model {
return {{ .Name }}Model{
Model: s.RecordCollection.Model(),
}
}
{{ range .Fields }}
// {{ .Name }} is a getter for the value of the "{{ .Name }}" field of the first
// record in this RecordSet. It returns the Go zero value if the RecordSet is empty.
func (s {{ $.Name }}Set) {{ .Name }}() {{ .Type }} {
{{ if .IsRS }} return {{ .Type }}{
RecordCollection: s.RecordCollection.Get("{{ .Name }}").(models.RecordSet).Collection(),
}{{ else -}}
res, _ := s.RecordCollection.Get("{{ .Name }}").({{ .Type }})
return res {{ end }}
}
// Set{{ .Name }} is a setter for the value of the "{{ .Name }}" field of this
// RecordSet. All Records of this RecordSet will be updated. Each call to this
// method makes an update query in the database.
//
// Set{{ .Name }} panics if the RecordSet is empty.
func (s {{ $.Name }}Set) Set{{ .Name }}(value {{ .Type }}) {
s.RecordCollection.Set("{{ .Name }}", value)
}
{{ end }}
// Super returns a RecordSet with a modified callstack so that call to the current
// method will execute the next method layer.
//
// This method is meant to be used inside a method layer function to call its parent,
// such as:
//
// func (rs h.MyRecordSet) MyMethod() string {
// res := rs.Super().MyMethod()
// res += " ok!"
// return res
// }
//
// Calls to a different method than the current method will call its next layer only
// if the current method has been called from a layer of the other method. Otherwise,
// it will be the same as calling the other method directly.
func (s {{ .Name }}Set) Super() {{ .Name }}Set {
return {{ .Name }}Set{
RecordCollection: s.RecordCollection.Super(),
}
}
// DataStruct returns a new {{ .Name }}Data object populated with the values
// of the given FieldMap. It returns as a second argument the list of keys of the
// given FieldMap.
func (s {{ .Name }}Set) DataStruct(fMap models.FieldMap) (*{{ .Name }}Data, []models.FieldNamer) {
var res {{ .Name }}Data
models.MapToStruct(s.Collection(), &res, fMap)
return &res, fMap.FieldNames()
}
{{ range .Methods }}
{{ .Doc }}
func (s {{ $.Name }}Set) {{ .Name }}({{ .ParamsWithType }}) ({{ .ReturnString }}) {
{{- if eq .Returns "" }}
{{ $.Name }}().Methods().{{ .Name }}().Call(s.Collection(), {{ .Params}})
{{- else }}
res := {{ $.Name }}().Methods().{{ .Name }}().{{ .Call }}(s.Collection(), {{ .Params}})
{{ .ReturnAsserts }}
return {{ .Returns }}
{{- end }}
}
{{ end }}
func init() {
{{- if not .IsModelMixin }}
models.New{{ .ModelType }}Model("{{ .Name }}")
{{ end }}
{{ range .Methods -}}
{{ if .ToDeclare }} {{ $.Name }}().AddEmptyMethod("{{ .Name }}")
{{ end -}}
{{- end }}
{{ $.Name }}().AddFields(map[string]models.FieldDefinition{
{{- range .Fields -}}
{{ if or .MixinField .EmbedField}} "{{ .Name }}": models.DummyField{},{{ end }}
{{ end -}}
})
}
`))
var poolQueryTemplate = template.Must(template.New("").Parse(`
package {{ .QueryPackageName }}
import (
"github.com/labneco/doxa/doxa/tools/typesutils"
"github.com/labneco/doxa/pool/{{ .QueryPackageName }}/{{ .SnakeName }}"
{{ range .Deps }} "{{ . }}"
{{ end }}
)
type {{ .Name }}Condition = {{ .SnakeName }}.Condition
// {{ .Name }} returns a {{ .SnakeName }}.ConditionStart for {{ .Name }}Model
func {{ .Name }}() {{ .SnakeName }}.ConditionStart {
return {{ .SnakeName }}.ConditionStart{
ConditionStart: &models.ConditionStart{},
}
}
`))
var poolModelsQueryTemplate = template.Must(template.New("").Parse(`
// This file is autogenerated by doxa-generate
// DO NOT MODIFY THIS FILE - ANY CHANGES WILL BE OVERWRITTEN
package {{ .SnakeName }}
import (
"github.com/labneco/doxa/doxa/tools/typesutils"
{{ range .Deps }} "{{ . }}"
{{ end }}
)
// ------- INTERFACES --------
{{ range .RelModels }}
type {{ . }}Condition interface {
models.Conditioner
{{ . }}ConditionDoxaFunc()
}
type {{ . }}Set interface {
models.RecordSet
{{ . }}SetDoxaFunc()
}
{{ end }}
// ------- CONDITION ---------
// A Condition is a type safe WHERE clause in an SQL query
type Condition struct {
*models.Condition
}
{{ range .ConditionFuncs }}
// {{ . }} completes the current condition with a simple {{ . }} clause : c.{{ . }}().nextCond => c {{ . }} nextCond
//
// No brackets are added so AND precedence over OR applies.
func (c Condition) {{ . }}() ConditionStart {
return ConditionStart{
ConditionStart: c.Condition.{{ . }}(),
}
}
// {{ . }}Cond completes the current condition with the given cond as an {{ . }} clause
// between brackets : c.{{ . }}(cond) => c {{ . }} (cond)
func (c Condition) {{ . }}Cond(cond Condition) Condition {
return Condition{
Condition: c.Condition.{{ . }}Cond(cond.Condition),
}
}
{{ end }}
// Underlying returns the underlying models.Condition instance
func (c Condition) Underlying() *models.Condition {
return c.Condition
}
// {{ $.Name }}ConditionDoxaFunc is a dummy function to uniquely match interfaces.
func (c Condition) {{ $.Name }}ConditionDoxaFunc() {}
var _ models.Conditioner = Condition{}
// ------- CONDITION START ---------
// A ConditionStart is an object representing a Condition when
// we just added a logical operator (AND, OR, ...) and we are
// about to add a predicate.