From 37adeda7c18466e3f94581c4c732aa7e8d4ff5be Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Mon, 13 Nov 2023 16:42:54 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20initCrud?= =?UTF-8?q?Transport=5Fmanual=5FNRPC()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/templates/pkg/db/db.go_ | 66 +++++++++++++++---- .../pkg/object_model/entities/model_crud.go_ | 1 + {internal => cmd}/main.go | 0 {internal => cmd}/main_test.go | 0 internal/config/config.go | 26 ++++++++ .../create_files/crud_starter/crud_starter.go | 18 +++++ internal/create_files/db/db.go | 10 +-- .../create_files/grpc_client/grpc_client.go | 10 +-- .../create_files/grpc_server/grpc_server.go | 11 ++-- internal/create_files/model/model.go | 2 +- .../create_files/nrpc_client/nrpc_client.go | 11 ++-- 11 files changed, 125 insertions(+), 30 deletions(-) rename {internal => cmd}/main.go (100%) rename {internal => cmd}/main_test.go (100%) diff --git a/bin/templates/pkg/db/db.go_ b/bin/templates/pkg/db/db.go_ index 59dca2c..52e2c1b 100644 --- a/bin/templates/pkg/db/db.go_ +++ b/bin/templates/pkg/db/db.go_ @@ -10,6 +10,7 @@ import ( "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" "gorm.io/gorm" "time" + "github.com/ManyakRus/starter/contextmain" ) // TableName - имя таблицы в БД Postgres @@ -23,7 +24,7 @@ type Crud_DB struct { func (crud Crud_DB) Read(m *lawsuit_status_types.LawsuitStatusType) error { var err error - ctxMain := context.Background() + ctxMain := contextmain.GetContext() ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() @@ -35,6 +36,11 @@ func (crud Crud_DB) Read(m *lawsuit_status_types.LawsuitStatusType) error { func (crud Crud_DB) Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error + if micro.ContextDone(ctx) == true { + err = context.Canceled + return err + } + id := int64(m.ID) db := postgres_gorm.GetConnection() @@ -48,7 +54,7 @@ func (crud Crud_DB) Read_ctx(ctx context.Context, m *lawsuit_status_types.Lawsui // Save - записывает новый или существующий объект в базу данных func (crud Crud_DB) Save(m *lawsuit_status_types.LawsuitStatusType) error { - ctxMain := context.Background() + ctxMain := contextmain.GetContext() ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() @@ -58,14 +64,20 @@ func (crud Crud_DB) Save(m *lawsuit_status_types.LawsuitStatusType) error { // Save_ctx - записывает новый или существующий объект в базу данных func (crud Crud_DB) Save_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { + var err error + if micro.ContextDone(ctx) == true { + err = context.Canceled + return err + } + is_create := !micro.BoolFromInt64(int64(m.ID)) - err := crud.create_update_ctx(ctx, m, is_create) + err = crud.create_update_ctx(ctx, m, is_create) return err } // Update - записывает существующий объект в базу данных func (crud Crud_DB) Update(m *lawsuit_status_types.LawsuitStatusType) error { - ctxMain := context.Background() + ctxMain := contextmain.GetContext() ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() @@ -75,13 +87,19 @@ func (crud Crud_DB) Update(m *lawsuit_status_types.LawsuitStatusType) error { // Update_ctx - записывает существующий объект в базу данных func (crud Crud_DB) Update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { - err := crud.create_update_ctx(ctx, m, false) + var err error + if micro.ContextDone(ctx) == true { + err = context.Canceled + return err + } + + err = crud.create_update_ctx(ctx, m, false) return err } // Create - записывает новый объект в базу данных func (crud Crud_DB) Create(m *lawsuit_status_types.LawsuitStatusType) error { - ctxMain := context.Background() + ctxMain := contextmain.GetContext() ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() @@ -91,7 +109,13 @@ func (crud Crud_DB) Create(m *lawsuit_status_types.LawsuitStatusType) error { // Create_ctx - записывает новый объект в базу данных func (crud Crud_DB) Create_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { - err := crud.create_update_ctx(ctx, m, true) + var err error + if micro.ContextDone(ctx) == true { + err = context.Canceled + return err + } + + err = crud.create_update_ctx(ctx, m, true) return err } @@ -99,7 +123,7 @@ func (crud Crud_DB) Create_ctx(ctx context.Context, m *lawsuit_status_types.Laws func (crud Crud_DB) create_update(m *lawsuit_status_types.LawsuitStatusType, is_create bool) error { var err error - ctxMain := context.Background() + ctxMain := contextmain.GetContext() ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() @@ -113,6 +137,11 @@ func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_typ // log.Trace("start Save() ", TableName, " id: ", int64(m.ID)) + if micro.ContextDone(ctx) == true { + err = context.Canceled + return err + } + // проверка ID if is_create == true { if int64(m.ID) != 0 { @@ -177,7 +206,7 @@ func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_typ func (crud Crud_DB) Delete(m *lawsuit_status_types.LawsuitStatusType) error { var err error - ctxMain := context.Background() + ctxMain := contextmain.GetContext() ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() @@ -189,6 +218,11 @@ func (crud Crud_DB) Delete(m *lawsuit_status_types.LawsuitStatusType) error { func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error + if micro.ContextDone(ctx) == true { + err = context.Canceled + return err + } + m2 := lawsuit_status_types.LawsuitStatusType{} m2.ID = m.ID err = crud.Read_ctx(ctx, &m2) @@ -208,7 +242,7 @@ func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.Laws func (crud Crud_DB) Restore(m *lawsuit_status_types.LawsuitStatusType) error { var err error - ctxMain := context.Background() + ctxMain := contextmain.GetContext() ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() @@ -220,6 +254,11 @@ func (crud Crud_DB) Restore(m *lawsuit_status_types.LawsuitStatusType) error { func (crud Crud_DB) Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error + if micro.ContextDone(ctx) == true { + err = context.Canceled + return err + } + m2 := lawsuit_status_types.LawsuitStatusType{} m2.ID = m.ID err = crud.Read_ctx(ctx, &m2) @@ -245,7 +284,7 @@ func (crud Crud_DB) Find_ByExtID(m *lawsuit_status_types.LawsuitStatusType) erro } // - ctxMain := context.Background() + ctxMain := contextmain.GetContext() ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() @@ -258,6 +297,11 @@ func (crud Crud_DB) Find_ByExtID(m *lawsuit_status_types.LawsuitStatusType) erro func (crud Crud_DB) Find_ByExtID_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error + if micro.ContextDone(ctx) == true { + err = context.Canceled + return err + } + if m.ExtID == 0 { err = errors.New("Error: ExtID=0") return err diff --git a/bin/templates/pkg/object_model/entities/model_crud.go_ b/bin/templates/pkg/object_model/entities/model_crud.go_ index 9b9f172..a8008fb 100644 --- a/bin/templates/pkg/object_model/entities/model_crud.go_ +++ b/bin/templates/pkg/object_model/entities/model_crud.go_ @@ -13,6 +13,7 @@ var versionLawsuitStatusType uint32 // crud_LawsuitStatusType - объект контроллер crud операций var crud_LawsuitStatusType ICrud_LawsuitStatusType +// интерфейс стандартных CRUD операций, для использования в DB или GRPC или NRPC type ICrud_LawsuitStatusType interface { Read(*LawsuitStatusType) error Save(*LawsuitStatusType) error diff --git a/internal/main.go b/cmd/main.go similarity index 100% rename from internal/main.go rename to cmd/main.go diff --git a/internal/main_test.go b/cmd/main_test.go similarity index 100% rename from internal/main_test.go rename to cmd/main_test.go diff --git a/internal/config/config.go b/internal/config/config.go index 0fd12bd..5a08f8c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -42,6 +42,11 @@ type SettingsINI struct { PREFIX_SERVER_GRPC string COMMENT_MODEL_STRUCT string TEXT_MODULE_GENERATED string + NEED_CREATE_DB_TEST bool + NEED_CREATE_GRPC_SERVER_TEST bool + NEED_CREATE_GRPC_CLIENT_TEST bool + NEED_CREATE_NRPC_SERVER_TEST bool + NEED_CREATE_NRPC_CLIENT_TEST bool } // FillSettings загружает переменные окружения в структуру из переменных окружения @@ -108,6 +113,27 @@ func FillSettings() { Settings.COMMENT_MODEL_STRUCT = os.Getenv("COMMENT_MODEL_STRUCT") Settings.TEXT_MODULE_GENERATED = os.Getenv("TEXT_MODULE_GENERATED") + + sNEED_CREATE_DB_TEST := os.Getenv("NEED_CREATE_DB_TEST") + NEED_CREATE_DB_TEST := BoolFromString(sNEED_CREATE_DB_TEST) + Settings.NEED_CREATE_DB_TEST = NEED_CREATE_DB_TEST + + sNEED_CREATE_GRPC_SERVER_TEST := os.Getenv("NEED_CREATE_GRPC_SERVER_TEST") + NEED_CREATE_GRPC_SERVER_TEST := BoolFromString(sNEED_CREATE_GRPC_SERVER_TEST) + Settings.NEED_CREATE_GRPC_SERVER_TEST = NEED_CREATE_GRPC_SERVER_TEST + + sNEED_CREATE_GRPC_CLIENT_TEST := os.Getenv("NEED_CREATE_GRPC_CLIENT_TEST") + NEED_CREATE_GRPC_CLIENT_TEST := BoolFromString(sNEED_CREATE_GRPC_CLIENT_TEST) + Settings.NEED_CREATE_GRPC_CLIENT_TEST = NEED_CREATE_GRPC_CLIENT_TEST + + sNEED_CREATE_NRPC_SERVER_TEST := os.Getenv("NEED_CREATE_NRPC_SERVER_TEST") + NEED_CREATE_NRPC_TEST := BoolFromString(sNEED_CREATE_NRPC_SERVER_TEST) + Settings.NEED_CREATE_NRPC_SERVER_TEST = NEED_CREATE_NRPC_TEST + + sNEED_CREATE_NRPC_CLIENT_TEST := os.Getenv("NEED_CREATE_NRPC_CLIENT_TEST") + NEED_CREATE_NRPC_CLIENT_TEST := BoolFromString(sNEED_CREATE_NRPC_CLIENT_TEST) + Settings.NEED_CREATE_NRPC_CLIENT_TEST = NEED_CREATE_NRPC_CLIENT_TEST + } // CurrentDirectory - возвращает текущую директорию ОС diff --git a/internal/create_files/crud_starter/crud_starter.go b/internal/create_files/crud_starter/crud_starter.go index 25f70bb..622057d 100644 --- a/internal/create_files/crud_starter/crud_starter.go +++ b/internal/create_files/crud_starter/crud_starter.go @@ -167,6 +167,11 @@ func FindText_InitCrudTransport_DB(MapAll map[string]*types.Table, ModelURL stri Otvet := ` // InitCrudTransport_DB - заполняет объекты crud для работы с БД напрямую func InitCrudTransport_DB() {` + if config.Settings.USE_DEFAULT_TEMPLATE == true { + Otvet = Otvet + ` + initCrudTransport_manual_DB() +` + } //сортировка по названию таблиц keys := make([]string, 0, len(MapAll)) for k := range MapAll { @@ -209,6 +214,13 @@ func FindTextGRPC(MapAll map[string]*types.Table, ModelURL string) string { Otvet := ` // InitCrudTransport_GRPC - заполняет объекты crud для работы с БД напрямую func InitCrudTransport_GRPC() {` + + if config.Settings.USE_DEFAULT_TEMPLATE == true { + Otvet = Otvet + ` + initCrudTransport_manual_GRPC() +` + } + //сортировка по названию таблиц keys := make([]string, 0, len(MapAll)) for k := range MapAll { @@ -251,6 +263,12 @@ func FindTextNRPC(MapAll map[string]*types.Table, ModelURL string) string { Otvet := ` // InitCrudTransport_NRPC - заполняет объекты crud для работы с БД напрямую func InitCrudTransport_NRPC() {` + + if config.Settings.USE_DEFAULT_TEMPLATE == true { + Otvet = Otvet + ` + initCrudTransport_manual_NRPC() +` + } //сортировка по названию таблиц keys := make([]string, 0, len(MapAll)) for k := range MapAll { diff --git a/internal/create_files/db/db.go b/internal/create_files/db/db.go index 9a4df4d..1d88354 100644 --- a/internal/create_files/db/db.go +++ b/internal/create_files/db/db.go @@ -32,10 +32,12 @@ func CreateAllFiles(MapAll map[string]*types.Table) error { } //тестовые файлы db - err = CreateTestFiles(Table1) - if err != nil { - log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err) - return err + if config.Settings.NEED_CREATE_DB_TEST == true { + err = CreateTestFiles(Table1) + if err != nil { + log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err) + return err + } } } diff --git a/internal/create_files/grpc_client/grpc_client.go b/internal/create_files/grpc_client/grpc_client.go index cfb3937..d42740d 100644 --- a/internal/create_files/grpc_client/grpc_client.go +++ b/internal/create_files/grpc_client/grpc_client.go @@ -31,10 +31,12 @@ func CreateAllFiles(MapAll map[string]*types.Table) error { } //тестовые файлы grpc_client - err = CreateTestFiles(Table1) - if err != nil { - log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err) - return err + 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 + } } } diff --git a/internal/create_files/grpc_server/grpc_server.go b/internal/create_files/grpc_server/grpc_server.go index e9f6beb..1e18e78 100644 --- a/internal/create_files/grpc_server/grpc_server.go +++ b/internal/create_files/grpc_server/grpc_server.go @@ -31,13 +31,14 @@ func CreateAllFiles(MapAll map[string]*types.Table) error { } //тестовые файлы grpc_server - err = CreateTestFiles(Table1) - if err != nil { - log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err) - return err + 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 + } } } - return err } diff --git a/internal/create_files/model/model.go b/internal/create_files/model/model.go index 0d82dee..4b4ac41 100644 --- a/internal/create_files/model/model.go +++ b/internal/create_files/model/model.go @@ -93,7 +93,7 @@ func CreateFilesModel_struct(Table1 *types.Table, DirTemplatesModel, DirReadyMod // TextModel = strings.ReplaceAll(TextModel, config.Settings.TEXT_TEMPLATE_MODEL, ModelName) TextModel = strings.ReplaceAll(TextModel, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name) - TextModel = config.Settings.TEXT_MODULE_GENERATED + TextModel + //TextModel = config.Settings.TEXT_MODULE_GENERATED + TextModel if config.Settings.HAS_IS_DELETED == true { TextModel = DeleteFuncDelete(TextModel, ModelName, Table1) diff --git a/internal/create_files/nrpc_client/nrpc_client.go b/internal/create_files/nrpc_client/nrpc_client.go index a25624c..17c01a5 100644 --- a/internal/create_files/nrpc_client/nrpc_client.go +++ b/internal/create_files/nrpc_client/nrpc_client.go @@ -31,13 +31,14 @@ func CreateAllFiles(MapAll map[string]*types.Table) error { } //тестовые файлы nrpc_client - err = CreateTestFiles(Table1) - if err != nil { - log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err) - return err + if config.Settings.NEED_CREATE_NRPC_CLIENT_TEST == true { + err = CreateTestFiles(Table1) + if err != nil { + log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err) + return err + } } } - return err }