From 95a94e1bfd04680770716ba56dde06f20390f5c8 Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Tue, 20 Feb 2024 16:33:21 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20TEMPLATE?= =?UTF-8?q?S=5FCRUD=5FTABLE=5FUPDATE=5FFILENAME?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/templates/configs_/settings.txt | 9 ++- .../pkg/db/crud/crud_table_update.go_ | 14 ++++ .../pkg/db/crud/crud_table_update_func.go_ | 4 +- .../pkg/db/crud/crud_table_update_test.go_ | 8 ++ internal/config/config.go | 13 +++ .../db_crud_tables/db_crud_tables.go | 80 ++++++++++++------- 6 files changed, 98 insertions(+), 30 deletions(-) create mode 100644 bin/templates/pkg/db/crud/crud_table_update.go_ create mode 100644 bin/templates/pkg/db/crud/crud_table_update_test.go_ diff --git a/bin/templates/configs_/settings.txt b/bin/templates/configs_/settings.txt index 45f1899..2c8114b 100644 --- a/bin/templates/configs_/settings.txt +++ b/bin/templates/configs_/settings.txt @@ -194,4 +194,11 @@ TEMPLATES_MODEL_CRUD_DELETE_FUNCTIONS_FILENAME="model_crud_delete_functions.json TEMPLATES_NAME_REPLACE_FILENAME="name_replace.json" #TEMPLATES_NULLABLE_FILENAME - short filename of "nullable.json" file -TEMPLATES_NULLABLE_FILENAME="nullable.json" \ No newline at end of file +TEMPLATES_NULLABLE_FILENAME="nullable.json" + +#TEMPLATES_CRUD_TABLE_UPDATE_FILENAME - short filename of "crud_table_update.go_" file +TEMPLATES_CRUD_TABLE_UPDATE_FILENAME="crud_table_update.go_" + +#TEMPLATES_CRUD_TABLE_UPDATE_TEST_FILENAME - short filename of "crud_table_update_test.go_" file +TEMPLATES_CRUD_TABLE_UPDATE_TEST_FILENAME="crud_table_update_test.go_" + diff --git a/bin/templates/pkg/db/crud/crud_table_update.go_ b/bin/templates/pkg/db/crud/crud_table_update.go_ new file mode 100644 index 0000000..d8b50cf --- /dev/null +++ b/bin/templates/pkg/db/crud/crud_table_update.go_ @@ -0,0 +1,14 @@ +package crud_lawsuit_status_types + +import ( + "context" + "errors" + "fmt" + "github.com/ManyakRus/starter/contextmain" + "github.com/ManyakRus/starter/micro" + "github.com/ManyakRus/starter/postgres_gorm" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/constants" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" + "gorm.io/gorm" + "time" +) 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 6d24a86..b278dbc 100644 --- a/bin/templates/pkg/db/crud/crud_table_update_func.go_ +++ b/bin/templates/pkg/db/crud/crud_table_update_func.go_ @@ -1,4 +1,4 @@ -// Read - изменяет 1 поле Name в базе данных +// Read - изменяет 1 поле ColumnName в базе данных func (crud Crud_DB) Read(m *lawsuit_status_types.LawsuitStatusType) error { var err error @@ -10,7 +10,7 @@ func (crud Crud_DB) Read(m *lawsuit_status_types.LawsuitStatusType) error { return err } -// Read_ctx - изменяет 1 поле Name в базе данных +// Read_ctx - изменяет 1 поле ColumnName в базе данных func (crud Crud_DB) Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error diff --git a/bin/templates/pkg/db/crud/crud_table_update_test.go_ b/bin/templates/pkg/db/crud/crud_table_update_test.go_ new file mode 100644 index 0000000..8395884 --- /dev/null +++ b/bin/templates/pkg/db/crud/crud_table_update_test.go_ @@ -0,0 +1,8 @@ +package crud_lawsuit_status_types + +import ( + "github.com/ManyakRus/starter/config_main" + "github.com/ManyakRus/starter/postgres_gorm" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" + "testing" +) diff --git a/internal/config/config.go b/internal/config/config.go index a033e9f..d10433c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -46,6 +46,8 @@ type SettingsINI struct { TEMPLATES_MODEL_CRUD_DELETE_FUNCTIONS_FILENAME string TEMPLATES_NAME_REPLACE_FILENAME string TEMPLATES_NULLABLE_FILENAME string + TEMPLATES_CRUD_TABLE_UPDATE_FILENAME string + TEMPLATES_CRUD_TABLE_UPDATE_TEST_FILENAME string NEED_CREATE_MODEL_STRUCT bool NEED_CREATE_MODEL_CRUD bool NEED_CREATE_DB bool @@ -413,6 +415,17 @@ func FillSettings() { Name = "TEMPLATES_NULLABLE_FILENAME" s = Getenv(Name, true) Settings.TEMPLATES_NULLABLE_FILENAME = s + + // + Name = "TEMPLATES_CRUD_TABLE_UPDATE_FILENAME" + s = Getenv(Name, true) + Settings.TEMPLATES_CRUD_TABLE_UPDATE_FILENAME = s + + // + Name = "TEMPLATES_CRUD_TABLE_UPDATE_TEST_FILENAME" + s = Getenv(Name, true) + Settings.TEMPLATES_CRUD_TABLE_UPDATE_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 f11e323..3159e29 100644 --- a/internal/create_files/db_crud_tables/db_crud_tables.go +++ b/internal/create_files/db_crud_tables/db_crud_tables.go @@ -474,32 +474,45 @@ func CreateFilesUpdateEveryColumn(Table1 *types.Table) error { DirTemplatesCrud := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile() DirReadyCrud := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile() + config.Settings.PREFIX_CRUD + TableName + micro.SeparatorFile() - FilenameTemplateCrud := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_UPDATE_FUNC_FILENAME + FilenameTemplateCrudFunc := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_UPDATE_FUNC_FILENAME DirReadyTable := DirReadyCrud - FilenameReadyCrudUpdate := DirReadyTable + config.Settings.PREFIX_CRUD + TableName + "_update.go" + FilenameReadyCrudUpdateFunc := DirReadyTable + config.Settings.PREFIX_CRUD + TableName + "_update.go" //создадим папку готовых файлов folders.CreateFolder(DirReadyTable) - bytes, err := os.ReadFile(FilenameTemplateCrud) + //читаем шаблон файла, только функции + bytes, err := os.ReadFile(FilenameTemplateCrudFunc) if err != nil { - log.Panic("ReadFile() ", FilenameTemplateCrud, " error: ", err) + log.Panic("ReadFile() ", FilenameTemplateCrudFunc, " error: ", err) } TextCrudUpdateFunc := string(bytes) - TextCrud := "package " + config.Settings.PREFIX_CRUD + TableName + "\n\n" - TextCrud = TextCrud + `import ( - "errors" - "context" - "fmt" - "time" - "gorm.io/gorm" - "github.com/ManyakRus/starter/contextmain" - "github.com/ManyakRus/starter/micro" - "github.com/ManyakRus/starter/postgres_gorm" -) + //читаем шаблон файла, без функций + FilenameTemplateCrud := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_UPDATE_FILENAME + bytes, err = os.ReadFile(FilenameTemplateCrud) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateCrud, " error: ", err) + } + TextCrud := string(bytes) + TextCrud = TextCrud + "\n" -` + //заменим имя пакета на новое + TextCrud = create_files.ReplacePackageName(TextCrud, DirReadyTable) + + // TextCrud := "package " + config.Settings.PREFIX_CRUD + TableName + "\n\n" + // TextCrud = TextCrud + `import ( + // "errors" + // "context" + // "fmt" + // "time" + // "gorm.io/gorm" + // "github.com/ManyakRus/starter/contextmain" + // "github.com/ManyakRus/starter/micro" + // "github.com/ManyakRus/starter/postgres_gorm" + //) + // + //` //заменим импорты if config.Settings.USE_DEFAULT_TEMPLATE == true { @@ -533,7 +546,7 @@ func CreateFilesUpdateEveryColumn(Table1 *types.Table) error { TextCrud = create_files.DeleteEmptyLines(TextCrud) //запись файла - err = os.WriteFile(FilenameReadyCrudUpdate, []byte(TextCrud), constants.FILE_PERMISSIONS) + err = os.WriteFile(FilenameReadyCrudUpdateFunc, []byte(TextCrud), constants.FILE_PERMISSIONS) return err } @@ -618,27 +631,40 @@ func CreateTestFilesUpdateEveryColumn(Table1 *types.Table) error { DirTemplatesCrud := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile() DirReadyCrud := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile() + config.Settings.PREFIX_CRUD + TableName + micro.SeparatorFile() - FilenameTemplateCrud := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_UPDATE_FUNC_TEST_FILENAME + FilenameTemplateCrudFunc := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_UPDATE_FUNC_TEST_FILENAME DirReadyTable := DirReadyCrud FilenameReadyCrudUpdate := DirReadyTable + config.Settings.PREFIX_CRUD + TableName + "_update_test.go" //создадим папку готовых файлов folders.CreateFolder(DirReadyTable) - bytes, err := os.ReadFile(FilenameTemplateCrud) + //читаем шаблон файла, только функции + bytes, err := os.ReadFile(FilenameTemplateCrudFunc) if err != nil { - log.Panic("ReadFile() ", FilenameTemplateCrud, " error: ", err) + log.Panic("ReadFile() ", FilenameTemplateCrudFunc, " error: ", err) } TextCrudUpdateFunc := string(bytes) - TextCrud := "package " + config.Settings.PREFIX_CRUD + TableName + "\n\n" - TextCrud = TextCrud + `import ( - "testing" - "github.com/ManyakRus/starter/config_main" - "github.com/ManyakRus/starter/postgres_gorm" -) + //читаем шаблон файла, без функций + FilenameTemplateCrud := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_UPDATE_TEST_FILENAME + bytes, err = os.ReadFile(FilenameTemplateCrud) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateCrud, " error: ", err) + } + TextCrud := string(bytes) + TextCrud = TextCrud + "\n" -` + //заменим имя пакета на новое + TextCrud = create_files.ReplacePackageName(TextCrud, DirReadyTable) + + // TextCrud := "package " + config.Settings.PREFIX_CRUD + TableName + "\n\n" + // TextCrud = TextCrud + `import ( + // "testing" + // "github.com/ManyakRus/starter/config_main" + // "github.com/ManyakRus/starter/postgres_gorm" + //) + // + //` //заменим импорты //if config.Settings.USE_DEFAULT_TEMPLATE == true {