2023-10-26 10:33:12 +02:00
|
|
|
package grpc_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/config"
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/constants"
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/create_files"
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/types"
|
|
|
|
"github.com/ManyakRus/starter/log"
|
|
|
|
"github.com/ManyakRus/starter/micro"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateAllFiles - создаёт все файлы в папке grpc_server
|
|
|
|
func CreateAllFiles(MapAll map[string]*types.Table) error {
|
|
|
|
var err error
|
|
|
|
|
2023-11-02 14:57:13 +02:00
|
|
|
for _, Table1 := range MapAll {
|
|
|
|
//проверка что таблица нормальная
|
|
|
|
err1 := create_files.CheckGoodTable(Table1)
|
|
|
|
if err1 != nil {
|
|
|
|
log.Warn(err1)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-26 10:33:12 +02:00
|
|
|
//файлы grpc_server
|
2023-11-02 14:57:13 +02:00
|
|
|
err = CreateFiles(Table1)
|
2023-10-26 10:33:12 +02:00
|
|
|
if err != nil {
|
2023-11-02 14:57:13 +02:00
|
|
|
log.Error("CreateFiles() table: ", Table1.Name, " error: ", err)
|
2023-10-26 10:33:12 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//тестовые файлы grpc_server
|
2023-11-13 15:42:54 +02:00
|
|
|
if config.Settings.NEED_CREATE_GRPC_SERVER_TEST == true {
|
|
|
|
err = CreateTestFiles(Table1)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err)
|
|
|
|
return err
|
|
|
|
}
|
2023-10-26 10:33:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFiles - создаёт 1 файл в папке grpc_server
|
|
|
|
func CreateFiles(Table1 *types.Table) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
//чтение файлов
|
|
|
|
DirBin := micro.ProgramDir_bin()
|
2023-11-01 16:34:57 +02:00
|
|
|
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
|
2023-10-26 10:33:12 +02:00
|
|
|
DirTemplatesGRPCServer := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_SERVER + micro.SeparatorFile()
|
2023-11-07 16:58:02 +02:00
|
|
|
DirReadyGRPCServer := DirReady + config.Settings.TEMPLATE_FOLDERNAME_GRPC_SERVER + micro.SeparatorFile()
|
2023-10-26 10:33:12 +02:00
|
|
|
|
2023-11-07 16:58:02 +02:00
|
|
|
FilenameTemplateGRPCServer := DirTemplatesGRPCServer + "server_grpc.go_"
|
2023-10-26 10:33:12 +02:00
|
|
|
TableName := strings.ToLower(Table1.Name)
|
|
|
|
DirTable := DirReadyGRPCServer
|
2023-11-07 16:58:02 +02:00
|
|
|
FilenameReadyGRPCServer := DirTable + config.Settings.PREFIX_SERVER_GRPC + TableName + ".go"
|
2023-10-26 10:33:12 +02:00
|
|
|
|
|
|
|
bytes, err := os.ReadFile(FilenameTemplateGRPCServer)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("ReadFile() ", FilenameTemplateGRPCServer, " error: ", err)
|
|
|
|
}
|
2023-11-15 16:59:21 +02:00
|
|
|
TextGRPCServer := string(bytes)
|
2023-10-26 10:33:12 +02:00
|
|
|
|
|
|
|
//создание текста
|
|
|
|
ModelName := Table1.NameGo
|
2023-11-15 16:59:21 +02:00
|
|
|
TextGRPCServer = strings.ReplaceAll(TextGRPCServer, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
|
|
|
|
TextGRPCServer = strings.ReplaceAll(TextGRPCServer, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
|
|
|
TextGRPCServer = config.Settings.TEXT_MODULE_GENERATED + TextGRPCServer
|
2023-10-26 10:33:12 +02:00
|
|
|
|
2023-11-07 16:58:02 +02:00
|
|
|
if config.Settings.USE_DEFAULT_TEMPLATE == true {
|
|
|
|
if config.Settings.HAS_IS_DELETED == true {
|
2023-11-15 16:59:21 +02:00
|
|
|
TextGRPCServer = DeleteFuncDelete(TextGRPCServer, ModelName, Table1)
|
|
|
|
TextGRPCServer = DeleteFuncDeleteCtx(TextGRPCServer, ModelName, Table1)
|
|
|
|
TextGRPCServer = DeleteFuncRestore(TextGRPCServer, ModelName, Table1)
|
|
|
|
TextGRPCServer = DeleteFuncRestoreCtx(TextGRPCServer, ModelName, Table1)
|
2023-11-07 16:58:02 +02:00
|
|
|
}
|
2023-11-15 16:59:21 +02:00
|
|
|
TextGRPCServer = DeleteFuncFind_byExtID(TextGRPCServer, ModelName, Table1)
|
|
|
|
TextGRPCServer = ConvertID(TextGRPCServer, Table1)
|
|
|
|
|
|
|
|
//замена импортов на новые URL
|
|
|
|
//TextGRPCServer = create_files.ReplaceServiceURLImports(TextGRPCServer)
|
|
|
|
TextGRPCServer = create_files.DeleteTemplateRepositoryImports(TextGRPCServer)
|
|
|
|
|
|
|
|
//proto
|
|
|
|
RepositoryGRPCProtoURL := create_files.FindGRPCProtoURL()
|
|
|
|
TextGRPCServer = create_files.AddImport(TextGRPCServer, RepositoryGRPCProtoURL)
|
|
|
|
|
|
|
|
//model
|
|
|
|
RepositoryModelURL := create_files.FindModelTableURL(TableName)
|
|
|
|
TextGRPCServer = create_files.AddImport(TextGRPCServer, RepositoryModelURL)
|
2023-10-26 10:33:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//запись файла
|
2023-11-15 16:59:21 +02:00
|
|
|
err = os.WriteFile(FilenameReadyGRPCServer, []byte(TextGRPCServer), constants.FILE_PERMISSIONS)
|
2023-10-26 10:33:12 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTestFiles - создаёт 1 файл в папке grpc_server
|
|
|
|
func CreateTestFiles(Table1 *types.Table) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
//чтение файлов
|
|
|
|
DirBin := micro.ProgramDir_bin()
|
2023-11-01 16:34:57 +02:00
|
|
|
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
|
2023-10-26 10:33:12 +02:00
|
|
|
DirTemplatesGRPCServer := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_SERVER + micro.SeparatorFile()
|
2023-11-07 16:58:02 +02:00
|
|
|
DirReadyGRPCServer := DirReady + config.Settings.TEMPLATE_FOLDERNAME_GRPC_SERVER + micro.SeparatorFile()
|
2023-10-26 10:33:12 +02:00
|
|
|
|
2023-11-07 16:58:02 +02:00
|
|
|
FilenameTemplateGRPCServer := DirTemplatesGRPCServer + "server_grpc_test.go_"
|
2023-10-26 10:33:12 +02:00
|
|
|
TableName := strings.ToLower(Table1.Name)
|
|
|
|
DirTable := DirReadyGRPCServer
|
2023-11-07 16:58:02 +02:00
|
|
|
FilenameReadyGRPCServer := DirTable + config.Settings.PREFIX_SERVER_GRPC + TableName + "_test.go"
|
2023-10-26 10:33:12 +02:00
|
|
|
|
|
|
|
bytes, err := os.ReadFile(FilenameTemplateGRPCServer)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("ReadFile() ", FilenameTemplateGRPCServer, " error: ", err)
|
|
|
|
}
|
2023-11-15 16:59:21 +02:00
|
|
|
TextGRPCServer := string(bytes)
|
2023-10-26 10:33:12 +02:00
|
|
|
|
|
|
|
//создание текста
|
|
|
|
ModelName := Table1.NameGo
|
2023-11-15 16:59:21 +02:00
|
|
|
TextGRPCServer = strings.ReplaceAll(TextGRPCServer, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
|
|
|
|
TextGRPCServer = strings.ReplaceAll(TextGRPCServer, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
|
|
|
TextGRPCServer = config.Settings.TEXT_MODULE_GENERATED + TextGRPCServer
|
2023-10-26 10:33:12 +02:00
|
|
|
|
|
|
|
if config.Settings.HAS_IS_DELETED == true {
|
2023-11-15 16:59:21 +02:00
|
|
|
TextGRPCServer = DeleteFuncTestDelete(TextGRPCServer, ModelName, Table1)
|
|
|
|
TextGRPCServer = DeleteFuncTestRestore(TextGRPCServer, ModelName, Table1)
|
2023-10-26 10:33:12 +02:00
|
|
|
}
|
2023-11-15 16:59:21 +02:00
|
|
|
TextGRPCServer = DeleteFuncTestFind_byExtID(TextGRPCServer, ModelName, Table1)
|
2023-10-26 10:33:12 +02:00
|
|
|
|
2023-11-08 17:12:04 +02:00
|
|
|
//Postgres_ID_Test = ID Minimum
|
|
|
|
if Table1.IDMinimum != "" {
|
|
|
|
TextFind := "const " + ModelName + "_ID_Test = "
|
2023-11-15 16:59:21 +02:00
|
|
|
TextGRPCServer = strings.ReplaceAll(TextGRPCServer, TextFind+"1", TextFind+Table1.IDMinimum)
|
2023-11-08 17:12:04 +02:00
|
|
|
}
|
|
|
|
|
2023-11-02 14:57:13 +02:00
|
|
|
// замена ID на PrimaryKey
|
2023-11-15 16:59:21 +02:00
|
|
|
TextGRPCServer = create_files.ReplacePrimaryKeyID(TextGRPCServer, Table1)
|
2023-11-02 14:57:13 +02:00
|
|
|
|
|
|
|
//SkipNow()
|
2023-11-15 16:59:21 +02:00
|
|
|
TextGRPCServer = create_files.AddSkipNow(TextGRPCServer, Table1)
|
|
|
|
|
|
|
|
//замена импортов на новые URL
|
|
|
|
TextGRPCServer = create_files.ReplaceServiceURLImports(TextGRPCServer)
|
2023-11-02 14:57:13 +02:00
|
|
|
|
2023-10-26 10:33:12 +02:00
|
|
|
//запись файла
|
2023-11-15 16:59:21 +02:00
|
|
|
err = os.WriteFile(FilenameReadyGRPCServer, []byte(TextGRPCServer), constants.FILE_PERMISSIONS)
|
2023-10-26 10:33:12 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFuncDelete - удаляет функцию Delete()
|
|
|
|
func DeleteFuncDelete(Text, ModelName string, Table1 *types.Table) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
|
|
|
_, ok := Table1.MapColumns["is_deleted"]
|
|
|
|
if ok == true {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
2023-11-07 16:58:02 +02:00
|
|
|
Otvet = create_files.DeleteFuncFromComment(Otvet, "\n// "+ModelName+"_Delete ")
|
2023-10-26 10:33:12 +02:00
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFuncRestore - удаляет функцию Restore()
|
|
|
|
func DeleteFuncRestore(Text, ModelName string, Table1 *types.Table) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
|
|
|
_, ok := Table1.MapColumns["is_deleted"]
|
|
|
|
if ok == true {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
Otvet = create_files.DeleteFuncFromComment(Text, "\n// "+ModelName+"_Restore ")
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFuncDeleteCtx - удаляет функцию Delete_ctx()
|
|
|
|
func DeleteFuncDeleteCtx(Text, ModelName string, Table1 *types.Table) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
|
|
|
_, ok := Table1.MapColumns["is_deleted"]
|
|
|
|
if ok == true {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
Otvet = create_files.DeleteFuncFromComment(Text, "\n// "+ModelName+"_Delete_ctx ")
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFuncRestoreCtx - удаляет функцию Restore_ctx()
|
|
|
|
func DeleteFuncRestoreCtx(Text, ModelName string, Table1 *types.Table) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
|
|
|
_, ok := Table1.MapColumns["is_deleted"]
|
|
|
|
if ok == true {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
Otvet = create_files.DeleteFuncFromComment(Text, "\n// "+ModelName+"_Restore_ctx ")
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
|
|
|
|
func DeleteFuncFind_byExtID(Text, ModelName string, Table1 *types.Table) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
2023-10-26 14:23:47 +02:00
|
|
|
//если есть обе колонки - ничего не делаем
|
|
|
|
ok := create_files.Has_Column_ExtID_ConnectionID(Table1)
|
2023-10-26 10:33:12 +02:00
|
|
|
if ok == true {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2023-10-26 14:23:47 +02:00
|
|
|
Otvet = create_files.DeleteFuncFromComment(Text, "\n// "+ModelName+"_FindByExtID ")
|
2023-10-26 10:33:12 +02:00
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFuncTestDelete - удаляет функцию Delete()
|
|
|
|
func DeleteFuncTestDelete(Text, ModelName string, Table1 *types.Table) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
|
|
|
_, ok := Table1.MapColumns["is_deleted"]
|
|
|
|
if ok == true {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
2023-11-07 16:58:02 +02:00
|
|
|
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "Test_server_"+ModelName+"_Delete")
|
2023-10-26 10:33:12 +02:00
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFuncTestRestore - удаляет функцию Restore()
|
|
|
|
func DeleteFuncTestRestore(Text, ModelName string, Table1 *types.Table) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
|
|
|
_, ok := Table1.MapColumns["is_deleted"]
|
|
|
|
if ok == true {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "Test_server_"+ModelName+"Restore")
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
|
|
|
|
func DeleteFuncTestFind_byExtID(Text, ModelName string, Table1 *types.Table) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
2023-10-26 14:23:47 +02:00
|
|
|
//если есть обе колонки - ничего не делаем
|
|
|
|
ok := create_files.Has_Column_ExtID_ConnectionID(Table1)
|
2023-10-26 10:33:12 +02:00
|
|
|
if ok == true {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2023-10-26 14:23:47 +02:00
|
|
|
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "Test_server_"+ModelName+"_FindByExtID")
|
2023-10-26 10:33:12 +02:00
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
2023-11-07 16:58:02 +02:00
|
|
|
|
|
|
|
// ConvertID - заменяет ID на Alias
|
|
|
|
func ConvertID(Text string, Table1 *types.Table) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
|
|
|
TableName := Table1.Name
|
2023-11-15 10:43:15 +02:00
|
|
|
IDName, _ := create_files.FindPrimaryKeyNameType(Table1)
|
2023-11-08 12:32:47 +02:00
|
|
|
TextConvert, ok := types.MapConvertID[TableName+"."+IDName]
|
2023-11-07 16:58:02 +02:00
|
|
|
if ok == false {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, "Request.Id", TextConvert+"(Request.Id)")
|
|
|
|
if TextConvert[:6] != "alias." {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
URL := create_files.FindURL_Alias()
|
|
|
|
if URL == "" {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
Otvet = create_files.AddImport(Otvet, URL)
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|