1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2025-01-30 22:31:02 +02:00

сделал rapira

This commit is contained in:
Nikitin Aleksandr 2023-11-16 12:59:02 +03:00
parent e672005b55
commit 553b9fe517
6 changed files with 302 additions and 302 deletions

View File

@ -1,299 +1 @@
package grpc_client
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_client
func CreateAllFiles(MapAll map[string]*types.Table) error {
var err error
for _, Table1 := range MapAll {
//проверка что таблица нормальная
err1 := create_files.CheckGoodTable(Table1)
if err1 != nil {
log.Warn(err1)
continue
}
//файлы grpc_client
err = CreateFiles(Table1)
if err != nil {
log.Error("CreateFiles() table: ", Table1.Name, " error: ", err)
return err
}
//тестовые файлы grpc_client
if config.Settings.NEED_CREATE_GRPC_CLIENT_TEST == true {
err = CreateTestFiles(Table1)
if err != nil {
log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err)
return err
}
}
}
return err
}
// CreateFiles - создаёт 1 файл в папке grpc_client
func CreateFiles(Table1 *types.Table) error {
var err error
//чтение файлов
DirBin := micro.ProgramDir_bin()
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
DirTemplatesGRPCClient := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile()
DirReadyGRPCClient := DirReady + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile()
FilenameTemplateGRPCClient := DirTemplatesGRPCClient + "grpc_client.go_"
TableName := strings.ToLower(Table1.Name)
DirTable := DirReadyGRPCClient + "grpc_" + TableName + micro.SeparatorFile()
FilenameReadyGRPCClient := DirTable + "grpc_" + TableName + ".go"
//создадим каталог
ok, err := micro.FileExists(DirTable)
if ok == false {
err = os.MkdirAll(DirTable, 0777)
if err != nil {
log.Panic("Mkdir() ", DirTable, " error: ", err)
}
}
bytes, err := os.ReadFile(FilenameTemplateGRPCClient)
if err != nil {
log.Panic("ReadFile() ", FilenameTemplateGRPCClient, " error: ", err)
}
TextGRPCClient := string(bytes)
//создание текста
ModelName := Table1.NameGo
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
TextGRPCClient = config.Settings.TEXT_MODULE_GENERATED + TextGRPCClient
if config.Settings.HAS_IS_DELETED == true {
TextGRPCClient = DeleteFuncDelete(TextGRPCClient, ModelName, Table1)
//TextGRPCClient = DeleteFuncDeleteCtx(TextGRPCClient, ModelName, Table1)
TextGRPCClient = DeleteFuncRestore(TextGRPCClient, ModelName, Table1)
//TextGRPCClient = DeleteFuncRestoreCtx(TextGRPCClient, ModelName, Table1)
}
TextGRPCClient = DeleteFuncFind_byExtID(TextGRPCClient, ModelName, Table1)
//замена импортов на новые URL
//TextGRPCClient = create_files.ReplaceServiceURLImports(TextGRPCClient)
if config.Settings.USE_DEFAULT_TEMPLATE == true {
//TextGRPCServer = create_files.ReplaceServiceURLImports(TextGRPCServer)
TextGRPCClient = create_files.DeleteTemplateRepositoryImports(TextGRPCClient)
//proto
RepositoryGRPCProtoURL := create_files.FindGRPCProtoURL()
TextGRPCClient = create_files.AddImport(TextGRPCClient, RepositoryGRPCProtoURL)
//model
RepositoryModelURL := create_files.FindModelTableURL(TableName)
TextGRPCClient = create_files.AddImport(TextGRPCClient, RepositoryModelURL)
//grpc client
RepositoryGRPCClientlURL := create_files.FindGRPClientURL()
TextGRPCClient = create_files.AddImport(TextGRPCClient, RepositoryGRPCClientlURL)
//constants
RepositoryGRPCConstantsURL := create_files.FindGRPCConstantsURL()
TextGRPCClient = create_files.AddImport(TextGRPCClient, RepositoryGRPCConstantsURL)
}
//запись файла
err = os.WriteFile(FilenameReadyGRPCClient, []byte(TextGRPCClient), constants.FILE_PERMISSIONS)
return err
}
// CreateTestFiles - создаёт 1 файл в папке grpc_client
func CreateTestFiles(Table1 *types.Table) error {
var err error
//чтение файлов
DirBin := micro.ProgramDir_bin()
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
DirTemplatesGRPCClient := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile()
DirReadyGRPCClient := DirReady + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile()
FilenameTemplateGRPCClient := DirTemplatesGRPCClient + "grpc_client_test.go_"
TableName := strings.ToLower(Table1.Name)
DirTable := DirReadyGRPCClient + "grpc_" + TableName + micro.SeparatorFile()
FilenameReadyGRPCClient := DirTable + "grpc_" + TableName + "_test.go"
//создадим каталог
ok, err := micro.FileExists(DirTable)
if ok == false {
err = os.Mkdir(DirTable, 0777)
if err != nil {
log.Panic("Mkdir() ", DirTable, " error: ", err)
}
}
bytes, err := os.ReadFile(FilenameTemplateGRPCClient)
if err != nil {
log.Panic("ReadFile() ", FilenameTemplateGRPCClient, " error: ", err)
}
TextGRPCClient := string(bytes)
//создание текста
ModelName := Table1.NameGo
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
TextGRPCClient = config.Settings.TEXT_MODULE_GENERATED + TextGRPCClient
if config.Settings.HAS_IS_DELETED == true {
TextGRPCClient = DeleteFuncTestDelete(TextGRPCClient, ModelName, Table1)
TextGRPCClient = DeleteFuncTestRestore(TextGRPCClient, ModelName, Table1)
}
TextGRPCClient = DeleteFuncTestFind_byExtID(TextGRPCClient, ModelName, Table1)
//Postgres_ID_Test = ID Minimum
if Table1.IDMinimum != "" {
TextFind := "const Postgres_ID_Test = "
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, TextFind+"1", TextFind+Table1.IDMinimum)
}
// замена ID на PrimaryKey
TextGRPCClient = create_files.ReplacePrimaryKeyID(TextGRPCClient, Table1)
//SkipNow()
TextGRPCClient = create_files.AddSkipNow(TextGRPCClient, Table1)
//замена импортов на новые URL
TextGRPCClient = create_files.ReplaceServiceURLImports(TextGRPCClient)
//запись файла
err = os.WriteFile(FilenameReadyGRPCClient, []byte(TextGRPCClient), constants.FILE_PERMISSIONS)
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
}
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Delete ")
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// 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// 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// Restore_ctx ")
return Otvet
}
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
func DeleteFuncFind_byExtID(Text, ModelName string, Table1 *types.Table) string {
Otvet := Text
//если есть обе колонки - ничего не делаем
ok := create_files.Has_Column_ExtID_ConnectionID(Table1)
if ok == true {
return Otvet
}
//
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Find_ByExtID ")
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
}
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "TestDelete")
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, "TestRestore")
return Otvet
}
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
func DeleteFuncTestFind_byExtID(Text, ModelName string, Table1 *types.Table) string {
Otvet := Text
//если есть обе колонки - ничего не делаем
ok := create_files.Has_Column_ExtID_ConnectionID(Table1)
if ok == true {
return Otvet
}
//
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "TestFindByExtID")
return Otvet
}

View File

@ -0,0 +1,299 @@
package grpc_client_tables
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_client
func CreateAllFiles(MapAll map[string]*types.Table) error {
var err error
for _, Table1 := range MapAll {
//проверка что таблица нормальная
err1 := create_files.CheckGoodTable(Table1)
if err1 != nil {
log.Warn(err1)
continue
}
//файлы grpc_client
err = CreateFiles(Table1)
if err != nil {
log.Error("CreateFiles() table: ", Table1.Name, " error: ", err)
return err
}
//тестовые файлы grpc_client
if config.Settings.NEED_CREATE_GRPC_CLIENT_TEST == true {
err = CreateTestFiles(Table1)
if err != nil {
log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err)
return err
}
}
}
return err
}
// CreateFiles - создаёт 1 файл в папке grpc_client
func CreateFiles(Table1 *types.Table) error {
var err error
//чтение файлов
DirBin := micro.ProgramDir_bin()
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
DirTemplatesGRPCClient := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile()
DirReadyGRPCClient := DirReady + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile()
FilenameTemplateGRPCClient := DirTemplatesGRPCClient + "grpc_client.go_"
TableName := strings.ToLower(Table1.Name)
DirTable := DirReadyGRPCClient + "grpc_" + TableName + micro.SeparatorFile()
FilenameReadyGRPCClient := DirTable + "grpc_" + TableName + ".go"
//создадим каталог
ok, err := micro.FileExists(DirTable)
if ok == false {
err = os.MkdirAll(DirTable, 0777)
if err != nil {
log.Panic("Mkdir() ", DirTable, " error: ", err)
}
}
bytes, err := os.ReadFile(FilenameTemplateGRPCClient)
if err != nil {
log.Panic("ReadFile() ", FilenameTemplateGRPCClient, " error: ", err)
}
TextGRPCClient := string(bytes)
//создание текста
ModelName := Table1.NameGo
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
TextGRPCClient = config.Settings.TEXT_MODULE_GENERATED + TextGRPCClient
if config.Settings.HAS_IS_DELETED == true {
TextGRPCClient = DeleteFuncDelete(TextGRPCClient, ModelName, Table1)
//TextGRPCClient = DeleteFuncDeleteCtx(TextGRPCClient, ModelName, Table1)
TextGRPCClient = DeleteFuncRestore(TextGRPCClient, ModelName, Table1)
//TextGRPCClient = DeleteFuncRestoreCtx(TextGRPCClient, ModelName, Table1)
}
TextGRPCClient = DeleteFuncFind_byExtID(TextGRPCClient, ModelName, Table1)
//замена импортов на новые URL
//TextGRPCClient = create_files.ReplaceServiceURLImports(TextGRPCClient)
if config.Settings.USE_DEFAULT_TEMPLATE == true {
//TextGRPCServer = create_files.ReplaceServiceURLImports(TextGRPCServer)
TextGRPCClient = create_files.DeleteTemplateRepositoryImports(TextGRPCClient)
//proto
RepositoryGRPCProtoURL := create_files.FindGRPCProtoURL()
TextGRPCClient = create_files.AddImport(TextGRPCClient, RepositoryGRPCProtoURL)
//model
RepositoryModelURL := create_files.FindModelTableURL(TableName)
TextGRPCClient = create_files.AddImport(TextGRPCClient, RepositoryModelURL)
//grpc client
RepositoryGRPCClientlURL := create_files.FindGRPClientURL()
TextGRPCClient = create_files.AddImport(TextGRPCClient, RepositoryGRPCClientlURL)
//constants
RepositoryGRPCConstantsURL := create_files.FindGRPCConstantsURL()
TextGRPCClient = create_files.AddImport(TextGRPCClient, RepositoryGRPCConstantsURL)
}
//запись файла
err = os.WriteFile(FilenameReadyGRPCClient, []byte(TextGRPCClient), constants.FILE_PERMISSIONS)
return err
}
// CreateTestFiles - создаёт 1 файл в папке grpc_client
func CreateTestFiles(Table1 *types.Table) error {
var err error
//чтение файлов
DirBin := micro.ProgramDir_bin()
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
DirTemplatesGRPCClient := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile()
DirReadyGRPCClient := DirReady + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile()
FilenameTemplateGRPCClient := DirTemplatesGRPCClient + "grpc_client_test.go_"
TableName := strings.ToLower(Table1.Name)
DirTable := DirReadyGRPCClient + "grpc_" + TableName + micro.SeparatorFile()
FilenameReadyGRPCClient := DirTable + "grpc_" + TableName + "_test.go"
//создадим каталог
ok, err := micro.FileExists(DirTable)
if ok == false {
err = os.Mkdir(DirTable, 0777)
if err != nil {
log.Panic("Mkdir() ", DirTable, " error: ", err)
}
}
bytes, err := os.ReadFile(FilenameTemplateGRPCClient)
if err != nil {
log.Panic("ReadFile() ", FilenameTemplateGRPCClient, " error: ", err)
}
TextGRPCClient := string(bytes)
//создание текста
ModelName := Table1.NameGo
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
TextGRPCClient = config.Settings.TEXT_MODULE_GENERATED + TextGRPCClient
if config.Settings.HAS_IS_DELETED == true {
TextGRPCClient = DeleteFuncTestDelete(TextGRPCClient, ModelName, Table1)
TextGRPCClient = DeleteFuncTestRestore(TextGRPCClient, ModelName, Table1)
}
TextGRPCClient = DeleteFuncTestFind_byExtID(TextGRPCClient, ModelName, Table1)
//Postgres_ID_Test = ID Minimum
if Table1.IDMinimum != "" {
TextFind := "const Postgres_ID_Test = "
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, TextFind+"1", TextFind+Table1.IDMinimum)
}
// замена ID на PrimaryKey
TextGRPCClient = create_files.ReplacePrimaryKeyID(TextGRPCClient, Table1)
//SkipNow()
TextGRPCClient = create_files.AddSkipNow(TextGRPCClient, Table1)
//замена импортов на новые URL
TextGRPCClient = create_files.ReplaceServiceURLImports(TextGRPCClient)
//запись файла
err = os.WriteFile(FilenameReadyGRPCClient, []byte(TextGRPCClient), constants.FILE_PERMISSIONS)
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
}
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Delete ")
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// 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// 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// Restore_ctx ")
return Otvet
}
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
func DeleteFuncFind_byExtID(Text, ModelName string, Table1 *types.Table) string {
Otvet := Text
//если есть обе колонки - ничего не делаем
ok := create_files.Has_Column_ExtID_ConnectionID(Table1)
if ok == true {
return Otvet
}
//
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Find_ByExtID ")
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
}
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "TestDelete")
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, "TestRestore")
return Otvet
}
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
func DeleteFuncTestFind_byExtID(Text, ModelName string, Table1 *types.Table) string {
Otvet := Text
//если есть обе колонки - ничего не делаем
ok := create_files.Has_Column_ExtID_ConnectionID(Table1)
if ok == true {
return Otvet
}
//
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "TestFindByExtID")
return Otvet
}

View File

@ -0,0 +1 @@
package grpc_client_tables

View File

@ -1 +0,0 @@
package service_client

View File

@ -1 +0,0 @@
package service_client

View File

@ -5,7 +5,7 @@ import (
"github.com/ManyakRus/crud_generator/internal/create_files/crud_starter"
"github.com/ManyakRus/crud_generator/internal/create_files/db"
"github.com/ManyakRus/crud_generator/internal/create_files/generation_code_sh"
"github.com/ManyakRus/crud_generator/internal/create_files/grpc_client"
"github.com/ManyakRus/crud_generator/internal/create_files/grpc_client_tables"
"github.com/ManyakRus/crud_generator/internal/create_files/grpc_proto"
"github.com/ManyakRus/crud_generator/internal/create_files/grpc_server"
"github.com/ManyakRus/crud_generator/internal/create_files/main_file"
@ -62,7 +62,7 @@ func StartFillAll() error {
}
//grpc_client
err = grpc_client.CreateAllFiles(MapAll)
err = grpc_client_tables.CreateAllFiles(MapAll)
if err != nil {
return err
}