Skip to content

Commit 269ab26

Browse files
Merge pull request #156 from intelops/add-gorm-framework
feat: Add GORM integration for go-grpc-server and go-gin-server
2 parents 6ba17a6 + c7d65e3 commit 269ab26

File tree

6 files changed

+858
-82
lines changed

6 files changed

+858
-82
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[submodule "core/templates/compage-template-go"]
22
path = core/templates/compage-template-go
33
url = https://github.com/intelops/compage-template-go.git
4-
branch = add-mongodb-integration
4+
branch = template-v8
55
[submodule "core/templates/compage-template-java"]
66
path = core/templates/compage-template-java
77
url = https://github.com/intelops/compage-template-java.git

core/internal/languages/golang/frameworks/go-gin-server/copier.go

Lines changed: 113 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package goginserver
22

33
import (
44
"errors"
5+
"fmt"
56
"text/template"
67

78
"github.com/gertd/go-pluralize"
@@ -21,27 +22,35 @@ import (
2122
const RestServerPath = "/pkg/rest/server"
2223
const RestClientPath = "/pkg/rest/client"
2324

24-
const DaosPath = RestServerPath + "/daos"
25-
const SQLDBClientsPath = DaosPath + "/clients/sqls"
26-
const NoSQLDBClientsPath = DaosPath + "/clients/nosqls"
27-
28-
const ServicesPath = RestServerPath + "/services"
2925
const ControllersPath = RestServerPath + "/controllers"
26+
const ServicesPath = RestServerPath + "/services"
27+
const DaosPath = RestServerPath + "/daos"
3028
const ModelsPath = RestServerPath + "/models"
3129

32-
const SQLControllerFile = "sqls-controller.go.tmpl"
33-
const SQLServiceFile = "sqls-service.go.tmpl"
30+
const NoSQLDBClientsPath = DaosPath + "/clients/nosqls"
3431
const NoSQLControllerFile = "nosqls-controller.go.tmpl"
3532
const NoSQLServiceFile = "nosqls-service.go.tmpl"
36-
const DaoFile = "dao.go.tmpl"
3733
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"
3840
const MySQLDaoFile = "mysql-dao.go.tmpl"
41+
const SQLModelFile = "sqls-model.go.tmpl"
42+
43+
const DaoFile = "dao.go.tmpl"
3944
const SQLiteDaoFile = "sqlite-dao.go.tmpl"
40-
const MongoDBConfigFile = "mongodb.go.tmpl"
4145
const MySQLDBConfigFile = "mysql.go.tmpl"
4246
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"
4554

4655
const ClientFile = "client.go.tmpl"
4756

@@ -53,6 +62,9 @@ const SQLite = "SQLite"
5362
const MySQL = "MySQL"
5463
const InMemory = "InMemory"
5564

65+
const SQLiteGORM = "SQLite-GORM"
66+
const MySQLGORM = "MySQL-GORM"
67+
5668
// Copier Language specific *Copier
5769
type Copier struct {
5870
NodeDirectoryName string
@@ -231,6 +243,13 @@ func (c *Copier) getFuncMap(resource *corenode.Resource) template.FuncMap {
231243
}
232244
return fieldMetaData.Type
233245
},
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+
},
234253
"GetCompositeFields": func(key string) string {
235254
fieldMetaData, ok := resource.Fields[key]
236255
if ok && fieldMetaData.IsComposite {
@@ -408,10 +427,12 @@ func (c *Copier) addResourceSpecificTemplateData(resource *corenode.Resource) er
408427
c.Data["Fields"] = fields
409428
// db fields
410429
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+
}
415436
}
416437
}
417438

@@ -479,15 +500,6 @@ func (c *Copier) copySQLDBResourceFiles(resourceName string, filePaths []*string
479500
}
480501
filePaths = append(filePaths, &targetResourceControllerFileName)
481502

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-
491503
// copy service files to generated project
492504
targetResourceServiceFileName := c.NodeDirectoryName + ServicesPath + "/" + resourceName + "-" + strings.Replace(SQLServiceFile, "sqls-", "", 1)
493505
_, err = utils.CopyFile(targetResourceServiceFileName, c.TemplatesRootPath+ServicesPath+"/"+SQLServiceFile)
@@ -499,6 +511,16 @@ func (c *Copier) copySQLDBResourceFiles(resourceName string, filePaths []*string
499511

500512
var targetResourceDaoFileName string
501513
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+
502524
// dao files
503525
targetResourceDaoFileName = c.NodeDirectoryName + DaosPath + "/" + resourceName + "-" + SQLiteDaoFile
504526
_, err := utils.CopyFile(targetResourceDaoFileName, c.TemplatesRootPath+DaosPath+"/"+SQLiteDaoFile)
@@ -508,6 +530,15 @@ func (c *Copier) copySQLDBResourceFiles(resourceName string, filePaths []*string
508530
}
509531
filePaths = append(filePaths, &targetResourceDaoFileName)
510532
} 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+
511542
// dao files
512543
targetResourceDaoFileName = c.NodeDirectoryName + DaosPath + "/" + resourceName + "-" + MySQLDaoFile
513544
_, err := utils.CopyFile(targetResourceDaoFileName, c.TemplatesRootPath+DaosPath+"/"+MySQLDaoFile)
@@ -524,6 +555,42 @@ func (c *Copier) copySQLDBResourceFiles(resourceName string, filePaths []*string
524555
return nil, err
525556
}
526557
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)
527594
}
528595
return filePaths, nil
529596
}
@@ -582,6 +649,28 @@ func (c *Copier) CreateRestServer() error {
582649
}
583650
filePaths = append(filePaths, targetMySQLConfigFileName)
584651
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)
585674
}
586675
} else if c.IsNoSQLDB {
587676
// create nosql db config file (common to all resources for specific database)

0 commit comments

Comments
 (0)