From 5c7d4172d20b96197d8eb1d9d0081878937bbb5b Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Mon, 11 Mar 2024 18:03:30 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20TEMPLATE?= =?UTF-8?q?S=5FGRPC=5FCLIENT=5FTABLES=5FCACHE=5FFILENAME?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/templates/configs_/settings.txt | 6 + .../pkg/db/crud/crud_table_update_func.go_ | 2 +- .../grpc/grpc_client/grpc_client_table.go_ | 3 - .../grpc_client/grpc_client_table_cache.go_ | 56 ++++++ .../grpc_client_table_cache_test.go_ | 27 +++ internal/config/config.go | 12 ++ .../db_crud_tables/db_crud_tables.go | 4 +- .../grpc_client_tables/grpc_client_tables.go | 183 ++++++++++++++++-- .../grpc_server_tables/grpc_server_tables.go | 2 +- 9 files changed, 269 insertions(+), 26 deletions(-) create mode 100644 bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_cache.go_ create mode 100644 bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_cache_test.go_ diff --git a/bin/templates/configs_/settings.txt b/bin/templates/configs_/settings.txt index f253b79..8201f02 100644 --- a/bin/templates/configs_/settings.txt +++ b/bin/templates/configs_/settings.txt @@ -222,3 +222,9 @@ NEED_CREATE_CACHE_FILES=true #NEED_CREATE_CACHE_TEST_FILES - need create "*_cache_test.go" files NEED_CREATE_CACHE_TEST_FILES=true + +#TEMPLATES_GRPC_CLIENT_TABLES_CACHE_FILENAME - short filename of "grpc_client_table_cache.go_" file +TEMPLATES_GRPC_CLIENT_TABLES_CACHE_FILENAME="grpc_client_table_cache.go_" + +#TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME - short filename of "grpc_client_table_cache.go_" file +TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME="grpc_client_table_cache.go_" \ No newline at end of file diff --git a/bin/templates/pkg/db/crud/crud_table_update_func.go_ b/bin/templates/pkg/db/crud/crud_table_update_func.go_ index e8daede..7f98116 100644 --- a/bin/templates/pkg/db/crud/crud_table_update_func.go_ +++ b/bin/templates/pkg/db/crud/crud_table_update_func.go_ @@ -45,7 +45,7 @@ func (crud Crud_DB) Read_ctx(ctx context.Context, m *lawsuit_status_types.Lawsui } //удалим из кэша - //cache.Remove(m.ID) + //cache.Remove(int64(m.ID)) return err } diff --git a/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table.go_ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table.go_ index 404a789..18c06ef 100644 --- a/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table.go_ +++ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table.go_ @@ -1,6 +1,3 @@ -//Файл создан автоматически кодогенератором crud_generator -//Не изменяйте ничего здесь. - package grpc_lawsuit_status_types import ( diff --git a/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_cache.go_ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_cache.go_ new file mode 100644 index 0000000..86d425b --- /dev/null +++ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_cache.go_ @@ -0,0 +1,56 @@ +package grpc_lawsuit_status_types + +import ( + "context" + "encoding/json" + "github.com/ManyakRus/starter/log" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/api/grpc_proto" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc/grpc_client" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc/grpc_constants" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc_nrpc" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/nrpc/nrpc_client" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" + "time" +) + +// ReadFromCache - возвращает модель из БД +func (crud Crud_GRPC) ReadFromCache(m *lawsuit_status_types.LawsuitStatusType) error { + var err error + + // подключение + grpc_client.Connect_GRPC_NRPC() + + // подготовка запроса + var versionModel = crud.GetVersionModel() + + Request := &grpc_proto.RequestId{} + Request.ID = int64(m.ID) + Request.VersionModel = versionModel + + ctxMain := context.Background() + ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(grpc_constants.GetTimeoutSeconds())) + defer ctxCancelFunc() + + // запрос + var Response *grpc_proto.Response + if grpc_nrpc.NeedNRPC == true { + Response, err = nrpc_client.Client.LawsuitStatusType_ReadFromCache(Request) + } else { + Response, err = grpc_client.Client.LawsuitStatusType_ReadFromCache(ctx, Request) + } + if err != nil { + if grpc_client.IsErrorModelVersion(err) == true { + log.Panic(err) + } + return err + } + + // ответ + sModel := Response.ModelString + err = json.Unmarshal([]byte(sModel), m) + if err != nil { + return err + } + + return err +} diff --git a/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_cache_test.go_ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_cache_test.go_ new file mode 100644 index 0000000..f3fdd47 --- /dev/null +++ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_cache_test.go_ @@ -0,0 +1,27 @@ +package grpc_lawsuit_status_types + +import ( + "github.com/ManyakRus/starter/config_main" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc/grpc_client" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" + "testing" +) + +func TestReadFromCache(t *testing.T) { + config_main.LoadEnv() + grpc_client.Connect() + defer grpc_client.CloseConnection() + + crud := Crud_GRPC{} + Otvet := lawsuit_status_types.LawsuitStatusType{} + Otvet.ID = Postgres_ID_Test + err := crud.ReadFromCache(&Otvet) + + if err != nil { + t.Error("ReadFromCache() error: ", err) + } + + if Otvet.ID == 0 { + t.Error("ReadFromCache() error: ID =0") + } +} diff --git a/internal/config/config.go b/internal/config/config.go index 59cd337..7ba2d6f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -52,6 +52,8 @@ type SettingsINI struct { TEMPLATES_GRPC_CLIENT_TABLE_UPDATE_TEST_FILENAME string TEMPLATES_GRPC_SERVER_TABLE_UPDATE_FILENAME string TEMPLATES_GRPC_SERVER_TABLE_UPDATE_TEST_FILENAME string + TEMPLATES_GRPC_CLIENT_TABLES_CACHE_FILENAME string + TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME string NEED_CREATE_MODEL_STRUCT bool NEED_CREATE_MODEL_CRUD bool NEED_CREATE_DB bool @@ -468,6 +470,16 @@ func FillSettings() { s = Getenv(Name, true) Settings.NEED_CREATE_CACHE_TEST_FILES = BoolFromString(s) + // + Name = "TEMPLATES_GRPC_CLIENT_TABLES_CACHE_FILENAME" + s = Getenv(Name, true) + Settings.TEMPLATES_GRPC_CLIENT_TABLES_CACHE_FILENAME = s + + // + Name = "TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME" + s = Getenv(Name, true) + Settings.TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME = s + } // CurrentDirectory - возвращает текущую директорию ОС diff --git a/internal/create_files/db_crud_tables/db_crud_tables.go b/internal/create_files/db_crud_tables/db_crud_tables.go index 2fa3adf..59c7655 100644 --- a/internal/create_files/db_crud_tables/db_crud_tables.go +++ b/internal/create_files/db_crud_tables/db_crud_tables.go @@ -539,7 +539,7 @@ func CreateFilesUpdateEveryColumn(Table1 *types.Table) error { ModelTableURL := create_files.FindModelTableURL(TableName) TextCrud = create_files.AddImport(TextCrud, ModelTableURL) - //TextCrud = create_files.ConvertIdToAlias(TextCrud, Table1) + TextCrud = create_files.ConvertIdToAlias(TextCrud, Table1) } //создание текста @@ -692,7 +692,7 @@ func CreateTestFilesUpdateEveryColumn(Table1 *types.Table) error { ModelTableURL := create_files.FindModelTableURL(TableName) TextCrud = create_files.AddImport(TextCrud, ModelTableURL) - //TextCrud = create_files.ConvertIdToAlias(TextCrud, Table1) + TextCrud = create_files.ConvertIdToAlias(TextCrud, Table1) } //создание текста diff --git a/internal/create_files/grpc_client_tables/grpc_client_tables.go b/internal/create_files/grpc_client_tables/grpc_client_tables.go index 1908c10..918cd37 100644 --- a/internal/create_files/grpc_client_tables/grpc_client_tables.go +++ b/internal/create_files/grpc_client_tables/grpc_client_tables.go @@ -58,6 +58,27 @@ func CreateAllFiles(MapAll map[string]*types.Table) error { } } + //NEED_CREATE_CACHE_API + if config.Settings.NEED_CREATE_CACHE_API == true { + //файлы grpc_server cache + if config.Settings.NEED_CREATE_CACHE_FILES == true { + err = CreateFiles_GRPC_Client_Cache(Table1) + if err != nil { + log.Error("CreateFiles() table: ", Table1.Name, " error: ", err) + return err + } + } + + //тестовые файлы grpc_server cache + if config.Settings.NEED_CREATE_CACHE_TEST_FILES == true { + err = CreateFiles_GRPC_Client_Cache_Test(Table1) + if err != nil { + log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err) + return err + } + } + + } } return err @@ -91,31 +112,13 @@ func CreateFiles(Table1 *types.Table) error { //заменим имя пакета на новое TextGRPCClient = create_files.ReplacePackageName(TextGRPCClient, DirReadyTable) - ////заменим импорты - //if config.Settings.USE_DEFAULT_TEMPLATE == true { - // TextGRPCClient = create_files.DeleteTemplateRepositoryImports(TextGRPCClient) - // - // ConstantsURL := create_files.FindGRPCConstantsURL() - // TextGRPCClient = create_files.AddImport(TextGRPCClient, ConstantsURL) - // - // GRPC_NRPC_URL := create_files.Find_GRPC_NRPC_URL() - // TextGRPCClient = create_files.AddImport(TextGRPCClient, GRPC_NRPC_URL) - // - // DBConstantsURL := create_files.FindDBConstantsURL() - // TextGRPCClient = create_files.AddImport(TextGRPCClient, DBConstantsURL) - // - // //удалим лишние функции - // TextGRPCClient = create_files.DeleteFuncDelete(TextGRPCClient, Table1) - // TextGRPCClient = create_files.DeleteFuncRestore(TextGRPCClient, Table1) - // TextGRPCClient = create_files.DeleteFuncFind_byExtID(TextGRPCClient, Table1) - //} - //создание текста 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.USE_DEFAULT_TEMPLATE == true { //TextGRPCClient = create_files.ReplaceServiceURLImports(TextGRPCClient) TextGRPCClient = create_files.DeleteTemplateRepositoryImports(TextGRPCClient) @@ -658,3 +661,145 @@ func FindTextUpdateEveryColumnTest1(TextGRPC_ClientUpdateFunc string, Table1 *ty return Otvet } + +// CreateFiles_GRPC_Client_Cache - создаёт 1 файл в папке grpc_client +func CreateFiles_GRPC_Client_Cache(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() + DirTemplatesCache := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile() + DirReadyCache := DirReady + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile() + + FilenameTemplateGRPCClient := DirTemplatesCache + config.Settings.TEMPLATES_GRPC_CLIENT_TABLES_CACHE_FILENAME + TableName := strings.ToLower(Table1.Name) + DirReadyTable := DirReadyCache + "grpc_" + TableName + micro.SeparatorFile() + FilenameReadyGRPCClient := DirReadyTable + "grpc_" + TableName + "_cache.go" + + //создадим папку готовых файлов + folders.CreateFolder(DirReadyTable) + + bytes, err := os.ReadFile(FilenameTemplateGRPCClient) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateGRPCClient, " error: ", err) + } + TextGRPCClient := string(bytes) + + //заменим имя пакета на новое + TextGRPCClient = create_files.ReplacePackageName(TextGRPCClient, DirReadyTable) + + //создание текста + 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.USE_DEFAULT_TEMPLATE == true { + //TextGRPCClient = create_files.ReplaceServiceURLImports(TextGRPCClient) + TextGRPCClient = create_files.DeleteTemplateRepositoryImports(TextGRPCClient) + + //proto + RepositoryGRPCProtoURL := create_files.FindProtoURL() + 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) + + //nrpc client + RepositoryNRPCClientlURL := create_files.FindNRPClientURL() + TextGRPCClient = create_files.AddImport(TextGRPCClient, RepositoryNRPCClientlURL) + + //constants GRPC + RepositoryGRPCConstantsURL := create_files.FindGRPCConstantsURL() + TextGRPCClient = create_files.AddImport(TextGRPCClient, RepositoryGRPCConstantsURL) + + //DBConstantsURL := create_files.FindDBConstantsURL() + //TextGRPCClient = create_files.AddImport(TextGRPCClient, DBConstantsURL) + + //grpc_nrpc + GRPC_NRPC_URL := create_files.Find_GRPC_NRPC_URL() + TextGRPCClient = create_files.AddImport(TextGRPCClient, GRPC_NRPC_URL) + } + + //удаление пустого импорта + TextGRPCClient = create_files.DeleteEmptyImport(TextGRPCClient) + + //удаление пустых строк + TextGRPCClient = create_files.DeleteEmptyLines(TextGRPCClient) + + //запись файла + err = os.WriteFile(FilenameReadyGRPCClient, []byte(TextGRPCClient), constants.FILE_PERMISSIONS) + + return err +} + +// CreateFiles_GRPC_Client_Cache_Test - создаёт 1 файл в папке grpc_client +func CreateFiles_GRPC_Client_Cache_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() + DirTemplatesCache := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile() + DirReadyCache := DirReady + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile() + + FilenameTemplateCache := DirTemplatesCache + config.Settings.TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME + TableName := strings.ToLower(Table1.Name) + DirReadyTable := DirReadyCache + "grpc_" + TableName + micro.SeparatorFile() + FilenameReadyCache := DirReadyTable + "grpc_" + TableName + "_cache_test.go" + + //создадим папку готовых файлов + folders.CreateFolder(DirReadyTable) + + bytes, err := os.ReadFile(FilenameTemplateCache) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateCache, " error: ", err) + } + TextGRPCClient := string(bytes) + + //заменим имя пакета на новое + TextGRPCClient = create_files.ReplacePackageName(TextGRPCClient, DirReadyTable) + + //заменим импорты + if config.Settings.USE_DEFAULT_TEMPLATE == true { + TextGRPCClient = create_files.DeleteTemplateRepositoryImports(TextGRPCClient) + + GRPClientURL := create_files.FindGRPClientURL() + TextGRPCClient = create_files.AddImport(TextGRPCClient, GRPClientURL) + + ModelTableName := create_files.FindModelTableURL(TableName) + TextGRPCClient = create_files.AddImport(TextGRPCClient, ModelTableName) + } + + // замена ID на PrimaryKey + TextGRPCClient = create_files.ReplacePrimaryKeyID(TextGRPCClient, Table1) + + //SkipNow() + TextGRPCClient = create_files.AddSkipNow(TextGRPCClient, Table1) + + //замена импортов на новые URL + TextGRPCClient = create_files.ReplaceServiceURLImports(TextGRPCClient) + + //удаление пустого импорта + TextGRPCClient = create_files.DeleteEmptyImport(TextGRPCClient) + + //удаление пустых строк + TextGRPCClient = create_files.DeleteEmptyLines(TextGRPCClient) + + //добавим комментарий в начало файла + TextGRPCClient = config.Settings.TEXT_MODULE_GENERATED + TextGRPCClient + + //запись файла + err = os.WriteFile(FilenameReadyCache, []byte(TextGRPCClient), constants.FILE_PERMISSIONS) + + return err +} diff --git a/internal/create_files/grpc_server_tables/grpc_server_tables.go b/internal/create_files/grpc_server_tables/grpc_server_tables.go index e998100..a253357 100644 --- a/internal/create_files/grpc_server_tables/grpc_server_tables.go +++ b/internal/create_files/grpc_server_tables/grpc_server_tables.go @@ -61,7 +61,7 @@ func CreateAllFiles(MapAll map[string]*types.Table) error { } - //UPDATE_EVERY_COLUMN + //NEED_CREATE_CACHE_API if config.Settings.NEED_CREATE_CACHE_API == true { //файлы grpc_server cache if config.Settings.NEED_CREATE_CACHE_FILES == true {