1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2024-11-21 05:05:52 +02:00

сделал objects

This commit is contained in:
Nikitin Aleksandr 2024-11-06 13:33:26 +03:00
parent 85e267e65c
commit 55a7eff328
7 changed files with 369 additions and 18 deletions

View File

@ -581,31 +581,30 @@ TEMPLATES_CRUD_TABLE_READOBJECT_FUNCTION_FILENAME="crud_tables_object_function.g
TEMPLATES_CRUD_TABLE_READOBJECT_FUNCTION_TEST_FILENAME="crud_tables_object_function_test.go_"
#TEMPLATES_GRPC_SERVER_READOBJECT_FILENAME - short filename of "server_grpc_table_object.go_" file
TEMPLATES_GRPC_SERVER_READOBJECT_FILENAME="server_grpc_table_object.go_"
#TEMPLATES_GRPC_SERVER_READOBJECT_FILENAME - short filename of "server_grpc_table_readobject.go_" file
TEMPLATES_GRPC_SERVER_READOBJECT_FILENAME="server_grpc_table_readobject.go_"
#TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_FILENAME - short filename of "server_grpc_table_object_test.go_" file
TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_FILENAME="server_grpc_table_object_function.go_"
#TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_FILENAME - short filename of "server_grpc_table_readobject_test.go_" file
TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_FILENAME="server_grpc_table_readobject_function.go_"
#TEMPLATES_GRPC_SERVER_READOBJECT_TEST_FILENAME - short filename of "server_grpc_table_object_test.go_" file
TEMPLATES_GRPC_SERVER_READOBJECT_TEST_FILENAME="server_grpc_table_object_test.go_"
#TEMPLATES_GRPC_SERVER_READOBJECT_TEST_FILENAME - short filename of "server_grpc_table_readobject_test.go_" file
TEMPLATES_GRPC_SERVER_READOBJECT_TEST_FILENAME="server_grpc_table_readobject_test.go_"
#TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_TEST_FILENAME - short filename of "server_grpc_table_object_test.go_" file
TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_TEST_FILENAME="server_grpc_table_object_function_test.go_"
#TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_TEST_FILENAME - short filename of "server_grpc_table_readobject_test.go_" file
TEMPLATES_GRPC_SERVER_READOBJECT_FUNCTION_TEST_FILENAME="server_grpc_table_readobject_function_test.go_"
#TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FILENAME - short filename of "grpc_client_table_object.go_" file
TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FILENAME="grpc_client_table_object.go_"
#TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FILENAME - short filename of "grpc_client_table_readobject.go_" file
TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FILENAME="grpc_client_table_readobject.go_"
#TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_TEST_FILENAME - short filename of "grpc_client_table_object_test.go_" file
TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_TEST_FILENAME="grpc_client_table_object_test.go_"
#TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_TEST_FILENAME - short filename of "grpc_client_table_readobject_test.go_" file
TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_TEST_FILENAME="grpc_client_table_readobject_test.go_"
#TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_FILENAME - short filename of "grpc_client_table_object_function.go_" file
TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_FILENAME="grpc_client_table_object_function.go_"
#TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_TEST_FILENAME - short filename of "grpc_client_table_object_function_test.go_" file
TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_TEST_FILENAME="grpc_client_table_object_function_test.go_"
#TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_FILENAME - short filename of "grpc_client_table_readobject_function.go_" file
TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_FILENAME="grpc_client_table_readobject_function.go_"
#TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_TEST_FILENAME - short filename of "grpc_client_table_readobject_function_test.go_" file
TEMPLATES_GRPC_CLIENT_TABLES_READOBJECT_FUNCTION_TEST_FILENAME="grpc_client_table_readobject_function_test.go_"
#-------------------------------------------TEXT DELETED--------------------------------------------

View File

@ -0,0 +1,49 @@
package server_grpc
import (
"context"
"github.com/ManyakRus/starter/postgres_gorm"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/api/grpc_proto"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/objects/object_lawsuit_status_types"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/crud_objects/crud_object_lawsuit_status_types"
)
// LawsuitStatusType_ReadObject - читает и возвращает модель из БД
func (s *ServerGRPC) LawsuitStatusType_ReadObject(ctx context.Context, Request *grpc_proto.RequestId) (*grpc_proto.Response, error) {
var Otvet grpc_proto.Response
var err error
//проверим контекст уже отменён
if ctx.Err() != nil {
err = ctx.Err()
return &Otvet, err
}
//проверим совпадения версии модели
VersionServer := object_lawsuit_status_types.ObjectLawsuitStatusType{}.GetStructVersion()
VersionClient := Request.VersionModel
if VersionServer != VersionClient {
err = ErrorModelVersion(object_lawsuit_status_types.ObjectLawsuitStatusType{})
return &Otvet, err
}
//запрос в БД
db := postgres_gorm.GetConnection()
ID := Request.ID
m := &object_lawsuit_status_types.ObjectLawsuitStatusType{}
m.ID = AliasFromInt(ID)
err = crud_object_lawsuit_status_types.ReadObject_ctx(ctx, db, m)
if err != nil {
return &Otvet, err
}
//заполяем ответ
ModelString, err := m.GetJSON()
if err != nil {
return &Otvet, err
}
Otvet.ModelString = ModelString
return &Otvet, err
}

View File

@ -0,0 +1,34 @@
package server_grpc
import (
"context"
"github.com/ManyakRus/starter/config_main"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/api/grpc_proto"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/crud_starter"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/constants"
"github.com/ManyakRus/starter/postgres_gorm"
"testing"
)
func Test_server_LawsuitStatusType_ReadObject(t *testing.T) {
config_main.LoadEnv()
crud_starter.InitCrudTransport_DB()
postgres_gorm.Connect_WithApplicationName(constants.SERVICE_NAME + "_test")
defer postgres_gorm.CloseConnection()
ctx := context.Background()
Request := grpc_proto.RequestId{}
Request.ID = LawsuitStatusType_ID_Test
Request.VersionModel = object_lawsuit_status_types.ObjectLawsuitStatusType{}.GetStructVersion()
server1 := &ServerGRPC{}
Otvet, err := server1.LawsuitStatusType_ReadObject(ctx, &Request)
if err != nil {
t.Error("Test_server_LawsuitStatusType_ReadObject() error: ", err)
}
if Otvet.ModelString == "" {
t.Error("Test_server_LawsuitStatusType_ReadObject() error: ModelString=''")
}
}

View File

@ -104,10 +104,18 @@ func CreateFileProto(MapAll map[string]*types.Table) error {
TextProto, TextProtoNew1 = FindText_FindModelBy(MapAll, TextProto, Table1)
TextProtoNew = TextProtoNew + TextProtoNew1
//
//cache
if config.Settings.NEED_CREATE_CACHE_API == true {
TextProtoNew = TextProtoNew + FindText_ProtoTable1_Cache(TextProto, Table1)
}
//ReadObject
if config.Settings.NEED_CREATE_READOBJECT == true {
TextProto, TextProtoNew1 = FindText_ReadObject(TextProto, Table1)
}
TextProtoNew = TextProtoNew + TextProtoNew1
//
TextProto = AddTextMessageRequestID(TextProto, Table1)
}

View File

@ -0,0 +1,44 @@
package protobuf
import (
"github.com/ManyakRus/crud_generator/internal/create_files"
"github.com/ManyakRus/crud_generator/internal/types"
"strings"
)
// FindText_ReadObject - добавляет текст FindText_ReadObject
func FindText_ReadObject(TextProto string, Table1 *types.Table) (TextProtoNew string, TextAdd string) {
TextProtoNew = TextProto
TextAdd = ""
Text1 := FindText_ReadObject1(Table1)
//проверим такой текст уже есть
pos1 := strings.Index(TextProto, Text1)
if pos1 >= 0 {
return
}
//
TextAdd = TextAdd + Text1
return TextProtoNew, TextAdd
}
// FindText_ReadObject1 - находит текст FindBy
func FindText_ReadObject1(Table1 *types.Table) string {
Otvet := ""
ModelName := Table1.NameGo_translit
//PrimaryKeyColumn := create_files.Find_PrimaryKeyColumn(Table1)
//if PrimaryKeyColumn == nil {
// return Otvet
//}
TextRequest, _ := create_files.FindText_ProtobufRequest(Table1)
Otvet = "\trpc " + ModelName + "_ReadObject(" + TextRequest + ") returns (Response) {}"
return Otvet
return Otvet
}

View File

@ -135,6 +135,21 @@ func CreateAllFiles(MapAll map[string]*types.Table) error {
return err
}
//
if config.Settings.NEED_CREATE_READOBJECT == true {
err = CreateFiles_ReadObject(Table1)
if err != nil {
log.Error("CreateFiles_ReadObject() table: ", Table1.Name, " error: ", err)
return err
}
err = CreateFiles_ReadObject_Test(Table1)
if err != nil {
log.Error("CreateFiles_ReadObject_Test() table: ", Table1.Name, " error: ", err)
return err
}
}
}
return err
}

View File

@ -0,0 +1,202 @@
package server_grpc_tables
import (
"github.com/ManyakRus/crud_generator/internal/config"
"github.com/ManyakRus/crud_generator/internal/create_files"
"github.com/ManyakRus/crud_generator/internal/folders"
"github.com/ManyakRus/crud_generator/internal/types"
"github.com/ManyakRus/starter/log"
"github.com/ManyakRus/starter/micro"
"os"
"strings"
)
// CreateFiles_ReadObject - создаёт 1 файл в папке grpc_server
func CreateFiles_ReadObject(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()
DirTemplatesGRPCServer := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_SERVER + micro.SeparatorFile()
DirReadyGRPCServer := DirReady + config.Settings.TEMPLATE_FOLDERNAME_GRPC_SERVER + micro.SeparatorFile()
FilenameTemplateGRPCServer := DirTemplatesGRPCServer + config.Settings.TEMPLATES_GRPC_SERVER_READOBJECT_FILENAME
TableName := strings.ToLower(Table1.Name)
DirReadyTable := DirReadyGRPCServer
FilenameReadyGRPCServer := DirReadyTable + config.Settings.PREFIX_SERVER_GRPC + TableName + "_readobject" + ".go"
//создадим папку готовых файлов
folders.CreateFolder(DirReadyTable)
bytes, err := os.ReadFile(FilenameTemplateGRPCServer)
if err != nil {
log.Panic("ReadFile() ", FilenameTemplateGRPCServer, " error: ", err)
}
TextGRPCServer := string(bytes)
//заменим имя пакета на новое
TextGRPCServer = create_files.Replace_PackageName(TextGRPCServer, DirReadyTable)
//заменим импорты
if config.Settings.USE_DEFAULT_TEMPLATE == true {
TextGRPCServer = create_files.Delete_TemplateRepositoryImports(TextGRPCServer)
//
ObjectTableURL := create_files.Find_ObjectTableURL(TableName)
TextGRPCServer = create_files.AddImport(TextGRPCServer, ObjectTableURL)
ProtoURL := create_files.Find_ProtoURL()
TextGRPCServer = create_files.AddImport(TextGRPCServer, ProtoURL)
CrudObjectTableURL := create_files.Find_CrudObjectTableURL(TableName)
TextGRPCServer = create_files.AddImport(TextGRPCServer, CrudObjectTableURL)
if Table1.PrimaryKeyColumnsCount == 1 {
TextGRPCServer = Replace_IDRequestID_1PK(TextGRPCServer, Table1)
}
//замена "m.ID = AliasFromInt(ID)"
TextGRPCServer = Replace_PrimaryKeyM_ID(TextGRPCServer, Table1)
//замена "ID := Request.ID"
//TextGRPCServer = create_files.Replace_PrimaryKeyOtvetID(TextGRPCServer, Table1)
//замена RequestId{}
TextGRPCServer = ReplaceText_RequestID_PrimaryKey(TextGRPCServer, Table1)
//замена int64(ID) на ID
//TextGRPCServer = create_files.Replace_IDtoID(TextGRPCServer, Table1)
//добавим импорт uuid
TextGRPCServer = create_files.CheckAndAdd_ImportUUID_FromText(TextGRPCServer)
//удалим лишние функции
TextGRPCServer = DeleteFunc_Delete(TextGRPCServer, Table1)
TextGRPCServer = DeleteFunc_Restore(TextGRPCServer, Table1)
TextGRPCServer = DeleteFunc_Find_byExtID(TextGRPCServer, Table1)
}
//создание текста
TextGRPCServer = create_files.Replace_TemplateModel_to_Model(TextGRPCServer, Table1.NameGo)
TextGRPCServer = create_files.Replace_TemplateTableName_to_TableName(TextGRPCServer, Table1.Name)
TextGRPCServer = create_files.AddText_ModuleGenerated(TextGRPCServer)
//ModelName := Table1.NameGo
//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
if config.Settings.USE_DEFAULT_TEMPLATE == true {
TextGRPCServer = Convert_RequestIdToAlias(TextGRPCServer, Table1)
}
//удаление пустого импорта
TextGRPCServer = create_files.Delete_EmptyImport(TextGRPCServer)
//запись файла
err = os.WriteFile(FilenameReadyGRPCServer, []byte(TextGRPCServer), config.Settings.FILE_PERMISSIONS)
return err
}
// CreateFiles_ReadObject_Test - создаёт 1 файл в папке grpc_server
func CreateFiles_ReadObject_Test(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()
DirTemplatesGRPCServer := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_SERVER + micro.SeparatorFile()
DirReadyGRPCServer := DirReady + config.Settings.TEMPLATE_FOLDERNAME_GRPC_SERVER + micro.SeparatorFile()
FilenameTemplateGRPCServer := DirTemplatesGRPCServer + config.Settings.TEMPLATES_GRPC_SERVER_READOBJECT_TEST_FILENAME
TableName := strings.ToLower(Table1.Name)
DirReadyTable := DirReadyGRPCServer
FilenameReadyGRPCServer := DirReadyTable + config.Settings.PREFIX_SERVER_GRPC + TableName + "_readobject" + "_test.go"
//создадим папку готовых файлов
folders.CreateFolder(DirReadyTable)
bytes, err := os.ReadFile(FilenameTemplateGRPCServer)
if err != nil {
log.Panic("ReadFile() ", FilenameTemplateGRPCServer, " error: ", err)
}
TextGRPCServer := string(bytes)
//заменим имя пакета на новое
TextGRPCServer = create_files.Replace_PackageName(TextGRPCServer, DirReadyTable)
//заменим импорты
if config.Settings.USE_DEFAULT_TEMPLATE == true {
//удалим лишние функции
TextGRPCServer = DeleteFunc_Test_Delete(TextGRPCServer, Table1)
TextGRPCServer = DeleteFunc_Test_Restore(TextGRPCServer, Table1)
TextGRPCServer = DeleteFunc_Test_Find_byExtID(TextGRPCServer, Table1)
//добавим импорты
TextGRPCServer = create_files.Delete_TemplateRepositoryImports(TextGRPCServer)
//object
ObjectTableURL := create_files.Find_ObjectTableURL(TableName)
TextGRPCServer = create_files.AddImport(TextGRPCServer, ObjectTableURL)
//
ProtoURL := create_files.Find_ProtoURL()
TextGRPCServer = create_files.AddImport(TextGRPCServer, ProtoURL)
CrudStarterURL := create_files.Find_CrudStarterURL()
TextGRPCServer = create_files.AddImport(TextGRPCServer, CrudStarterURL)
ConstantsURL := create_files.Find_ConstantsURL()
TextGRPCServer = create_files.AddImport(TextGRPCServer, ConstantsURL)
TextGRPCServer = create_files.CheckAndAdd_Import(TextGRPCServer, "encoding/json")
//замена "postgres_gorm.Connect_WithApplicationName("
TextGRPCServer = create_files.Replace_Connect_WithApplicationName(TextGRPCServer)
if Table1.PrimaryKeyColumnsCount > 1 {
}
//Postgres_ID_Test = ID Minimum
TextGRPCServer = Replace_Model_ID_Test(TextGRPCServer, Table1)
//замена RequestId{}
TextGRPCServer = ReplaceText_RequestID_PrimaryKey(TextGRPCServer, Table1)
//замена Otvet.ID = -1
TextGRPCServer = Replace_ModelIDEqual1(TextGRPCServer, Table1)
//добавим импорт uuid
TextGRPCServer = create_files.CheckAndAdd_ImportUUID_FromText(TextGRPCServer)
//
TextGRPCServer = Replace_OtvetIDEqual0(TextGRPCServer, Table1)
}
//создание текста
TextGRPCServer = create_files.Replace_TemplateModel_to_Model(TextGRPCServer, Table1.NameGo)
TextGRPCServer = create_files.Replace_TemplateTableName_to_TableName(TextGRPCServer, Table1.Name)
TextGRPCServer = create_files.AddText_ModuleGenerated(TextGRPCServer)
// замена ID на PrimaryKey
//TextGRPCServer = create_files.Replace_PrimaryKeyOtvetID(TextGRPCServer, Table1)
//TextGRPCServer = create_files.Replace_PrimaryKeyM_ID(TextGRPCServer, Table1)
//SkipNow()
TextGRPCServer = create_files.AddSkipNow(TextGRPCServer, Table1)
//замена импортов на новые URL
//TextGRPCServer = create_files.Replace_RepositoryImportsURL(TextGRPCServer)
//удаление пустого импорта
TextGRPCServer = create_files.Delete_EmptyImport(TextGRPCServer)
//запись файла
err = os.WriteFile(FilenameReadyGRPCServer, []byte(TextGRPCServer), config.Settings.FILE_PERMISSIONS)
return err
}