@@ -2,6 +2,7 @@ package goginserver
2
2
3
3
import (
4
4
"errors"
5
+ "fmt"
5
6
"text/template"
6
7
7
8
"github.com/gertd/go-pluralize"
@@ -21,27 +22,35 @@ import (
21
22
const RestServerPath = "/pkg/rest/server"
22
23
const RestClientPath = "/pkg/rest/client"
23
24
24
- const DaosPath = RestServerPath + "/daos"
25
- const SQLDBClientsPath = DaosPath + "/clients/sqls"
26
- const NoSQLDBClientsPath = DaosPath + "/clients/nosqls"
27
-
28
- const ServicesPath = RestServerPath + "/services"
29
25
const ControllersPath = RestServerPath + "/controllers"
26
+ const ServicesPath = RestServerPath + "/services"
27
+ const DaosPath = RestServerPath + "/daos"
30
28
const ModelsPath = RestServerPath + "/models"
31
29
32
- const SQLControllerFile = "sqls-controller.go.tmpl"
33
- const SQLServiceFile = "sqls-service.go.tmpl"
30
+ const NoSQLDBClientsPath = DaosPath + "/clients/nosqls"
34
31
const NoSQLControllerFile = "nosqls-controller.go.tmpl"
35
32
const NoSQLServiceFile = "nosqls-service.go.tmpl"
36
- const DaoFile = "dao.go.tmpl"
37
33
const MongoDBDaoFile = "mongodb-dao.go.tmpl"
34
+ const MongoDBConfigFile = "mongodb.go.tmpl"
35
+ const NoSQLModelFile = "nosqls-model.go.tmpl"
36
+
37
+ const SQLDBClientsPath = DaosPath + "/clients/sqls"
38
+ const SQLControllerFile = "sqls-controller.go.tmpl"
39
+ const SQLServiceFile = "sqls-service.go.tmpl"
38
40
const MySQLDaoFile = "mysql-dao.go.tmpl"
41
+ const SQLModelFile = "sqls-model.go.tmpl"
42
+
43
+ const DaoFile = "dao.go.tmpl"
39
44
const SQLiteDaoFile = "sqlite-dao.go.tmpl"
40
- const MongoDBConfigFile = "mongodb.go.tmpl"
41
45
const MySQLDBConfigFile = "mysql.go.tmpl"
42
46
const SQLiteDBConfigFile = "sqlite.go.tmpl"
43
- const SQLModelFile = "sqls-model.go.tmpl"
44
- const NoSQLModelFile = "nosqls-model.go.tmpl"
47
+
48
+ // SQLGORMModelFile GORM integration
49
+ const SQLGORMModelFile = "sqls-gorm-model.go.tmpl"
50
+ const MySQLGORMDaoFile = "mysql-gorm-dao.go.tmpl"
51
+ const SQLiteGORMDaoFile = "sqlite-gorm-dao.go.tmpl"
52
+ const MySQLGORMDBConfigFile = "mysql-gorm.go.tmpl"
53
+ const SQLiteGORMDBConfigFile = "sqlite-gorm.go.tmpl"
45
54
46
55
const ClientFile = "client.go.tmpl"
47
56
@@ -53,6 +62,9 @@ const SQLite = "SQLite"
53
62
const MySQL = "MySQL"
54
63
const InMemory = "InMemory"
55
64
65
+ const SQLiteGORM = "SQLite-GORM"
66
+ const MySQLGORM = "MySQL-GORM"
67
+
56
68
// Copier Language specific *Copier
57
69
type Copier struct {
58
70
NodeDirectoryName string
@@ -231,6 +243,13 @@ func (c *Copier) getFuncMap(resource *corenode.Resource) template.FuncMap {
231
243
}
232
244
return fieldMetaData .Type
233
245
},
246
+ // This function helps the template to add a foreignKey based on composite field
247
+ "AddForeignKeyIfCompositeField" : func (key , value string ) string {
248
+ if fieldMetaData , ok := resource .Fields [key ]; ok && fieldMetaData .IsComposite {
249
+ return fmt .Sprintf ("%s %s `gorm:\" foreignKey:ID\" json:\" %s,omitempty\" `" , key , value , strcase .ToLowerCamel (key ))
250
+ }
251
+ return fmt .Sprintf ("%s %s `json:\" %s,omitempty\" `" , key , value , strcase .ToLowerCamel (key ))
252
+ },
234
253
"GetCompositeFields" : func (key string ) string {
235
254
fieldMetaData , ok := resource .Fields [key ]
236
255
if ok && fieldMetaData .IsComposite {
@@ -408,10 +427,12 @@ func (c *Copier) addResourceSpecificTemplateData(resource *corenode.Resource) er
408
427
c .Data ["Fields" ] = fields
409
428
// db fields
410
429
if c .IsSQLDB {
411
- err := c .addSQLDetails (resource )
412
- if err != nil {
413
- log .Debug ("error while adding sql details to resource specific template data" , err )
414
- return err
430
+ if c .SQLDB == SQLite || c .SQLDB == MySQL {
431
+ err := c .addSQLDetails (resource )
432
+ if err != nil {
433
+ log .Debug ("error while adding sql details to resource specific template data" , err )
434
+ return err
435
+ }
415
436
}
416
437
}
417
438
@@ -479,15 +500,6 @@ func (c *Copier) copySQLDBResourceFiles(resourceName string, filePaths []*string
479
500
}
480
501
filePaths = append (filePaths , & targetResourceControllerFileName )
481
502
482
- // copy model files to generated project
483
- targetResourceModelFileName := c .NodeDirectoryName + ModelsPath + "/" + resourceName + "-" + strings .Replace (SQLModelFile , "sqls-" , "" , 1 )
484
- _ , err = utils .CopyFile (targetResourceModelFileName , c .TemplatesRootPath + ModelsPath + "/" + SQLModelFile )
485
- if err != nil {
486
- log .Debugf ("error copying model file: %v" , err )
487
- return nil , err
488
- }
489
- filePaths = append (filePaths , & targetResourceModelFileName )
490
-
491
503
// copy service files to generated project
492
504
targetResourceServiceFileName := c .NodeDirectoryName + ServicesPath + "/" + resourceName + "-" + strings .Replace (SQLServiceFile , "sqls-" , "" , 1 )
493
505
_ , err = utils .CopyFile (targetResourceServiceFileName , c .TemplatesRootPath + ServicesPath + "/" + SQLServiceFile )
@@ -499,6 +511,16 @@ func (c *Copier) copySQLDBResourceFiles(resourceName string, filePaths []*string
499
511
500
512
var targetResourceDaoFileName string
501
513
if c .SQLDB == SQLite {
514
+ // model files
515
+ // copy model files to generated project
516
+ targetResourceModelFileName := c .NodeDirectoryName + ModelsPath + "/" + resourceName + "-" + strings .Replace (SQLModelFile , "sqls-" , "" , 1 )
517
+ _ , err = utils .CopyFile (targetResourceModelFileName , c .TemplatesRootPath + ModelsPath + "/" + SQLModelFile )
518
+ if err != nil {
519
+ log .Debugf ("error copying model file: %v" , err )
520
+ return nil , err
521
+ }
522
+ filePaths = append (filePaths , & targetResourceModelFileName )
523
+
502
524
// dao files
503
525
targetResourceDaoFileName = c .NodeDirectoryName + DaosPath + "/" + resourceName + "-" + SQLiteDaoFile
504
526
_ , err := utils .CopyFile (targetResourceDaoFileName , c .TemplatesRootPath + DaosPath + "/" + SQLiteDaoFile )
@@ -508,6 +530,15 @@ func (c *Copier) copySQLDBResourceFiles(resourceName string, filePaths []*string
508
530
}
509
531
filePaths = append (filePaths , & targetResourceDaoFileName )
510
532
} else if c .SQLDB == MySQL {
533
+ // model files
534
+ targetResourceModelFileName := c .NodeDirectoryName + ModelsPath + "/" + resourceName + "-" + strings .Replace (SQLModelFile , "sqls-" , "" , 1 )
535
+ _ , err = utils .CopyFile (targetResourceModelFileName , c .TemplatesRootPath + ModelsPath + "/" + SQLModelFile )
536
+ if err != nil {
537
+ log .Debugf ("error copying model file: %v" , err )
538
+ return nil , err
539
+ }
540
+ filePaths = append (filePaths , & targetResourceModelFileName )
541
+
511
542
// dao files
512
543
targetResourceDaoFileName = c .NodeDirectoryName + DaosPath + "/" + resourceName + "-" + MySQLDaoFile
513
544
_ , err := utils .CopyFile (targetResourceDaoFileName , c .TemplatesRootPath + DaosPath + "/" + MySQLDaoFile )
@@ -524,6 +555,42 @@ func (c *Copier) copySQLDBResourceFiles(resourceName string, filePaths []*string
524
555
return nil , err
525
556
}
526
557
filePaths = append (filePaths , & targetResourceDaoFileName )
558
+ } else if c .SQLDB == SQLiteGORM {
559
+ // model files
560
+ targetResourceModelFileName := c .NodeDirectoryName + ModelsPath + "/" + resourceName + "-" + strings .Replace (SQLGORMModelFile , "sqls-gorm-" , "" , 1 )
561
+ _ , err = utils .CopyFile (targetResourceModelFileName , c .TemplatesRootPath + ModelsPath + "/" + SQLGORMModelFile )
562
+ if err != nil {
563
+ log .Debugf ("error copying model file: %v" , err )
564
+ return nil , err
565
+ }
566
+ filePaths = append (filePaths , & targetResourceModelFileName )
567
+
568
+ // dao files
569
+ targetResourceDaoFileName = c .NodeDirectoryName + DaosPath + "/" + resourceName + "-" + strings .Replace (SQLiteGORMDaoFile , "sqlite-gorm-" , "" , 1 )
570
+ _ , err := utils .CopyFile (targetResourceDaoFileName , c .TemplatesRootPath + DaosPath + "/" + SQLiteGORMDaoFile )
571
+ if err != nil {
572
+ log .Debugf ("error copying sqlite gorm dao file: %v" , err )
573
+ return nil , err
574
+ }
575
+ filePaths = append (filePaths , & targetResourceDaoFileName )
576
+ } else if c .SQLDB == MySQLGORM {
577
+ // model files
578
+ targetResourceModelFileName := c .NodeDirectoryName + ModelsPath + "/" + resourceName + "-" + strings .Replace (SQLGORMModelFile , "sqls-gorm-" , "" , 1 )
579
+ _ , err = utils .CopyFile (targetResourceModelFileName , c .TemplatesRootPath + ModelsPath + "/" + SQLGORMModelFile )
580
+ if err != nil {
581
+ log .Debugf ("error copying model file: %v" , err )
582
+ return nil , err
583
+ }
584
+ filePaths = append (filePaths , & targetResourceModelFileName )
585
+
586
+ // dao files
587
+ targetResourceDaoFileName = c .NodeDirectoryName + DaosPath + "/" + resourceName + "-" + strings .Replace (MySQLGORMDaoFile , "mysql-gorm-" , "" , 1 )
588
+ _ , err := utils .CopyFile (targetResourceDaoFileName , c .TemplatesRootPath + DaosPath + "/" + MySQLGORMDaoFile )
589
+ if err != nil {
590
+ log .Debugf ("error copying mysql gorm dao file: %v" , err )
591
+ return nil , err
592
+ }
593
+ filePaths = append (filePaths , & targetResourceDaoFileName )
527
594
}
528
595
return filePaths , nil
529
596
}
@@ -582,6 +649,28 @@ func (c *Copier) CreateRestServer() error {
582
649
}
583
650
filePaths = append (filePaths , targetMySQLConfigFileName )
584
651
return executor .Execute (filePaths , c .Data )
652
+ } else if c .SQLDB == SQLiteGORM {
653
+ var filePaths []string
654
+ // client files
655
+ targetSQLiteConfigFileName := c .NodeDirectoryName + SQLDBClientsPath + "/" + SQLiteGORMDBConfigFile
656
+ _ , err := utils .CopyFile (targetSQLiteConfigFileName , c .TemplatesRootPath + SQLDBClientsPath + "/" + SQLiteGORMDBConfigFile )
657
+ if err != nil {
658
+ log .Debugf ("error copying sqlite gorm config file: %v" , err )
659
+ return err
660
+ }
661
+ filePaths = append (filePaths , targetSQLiteConfigFileName )
662
+ return executor .Execute (filePaths , c .Data )
663
+ } else if c .SQLDB == MySQLGORM {
664
+ var filePaths []string
665
+ // client files
666
+ targetMySQLConfigFileName := c .NodeDirectoryName + SQLDBClientsPath + "/" + MySQLGORMDBConfigFile
667
+ _ , err := utils .CopyFile (targetMySQLConfigFileName , c .TemplatesRootPath + SQLDBClientsPath + "/" + MySQLGORMDBConfigFile )
668
+ if err != nil {
669
+ log .Debugf ("error copying mysql gorm config file: %v" , err )
670
+ return err
671
+ }
672
+ filePaths = append (filePaths , targetMySQLConfigFileName )
673
+ return executor .Execute (filePaths , c .Data )
585
674
}
586
675
} else if c .IsNoSQLDB {
587
676
// create nosql db config file (common to all resources for specific database)
0 commit comments