From 493e329b4738d14a7164152686804c6f1a184b7e Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Wed, 7 Feb 2024 18:10:52 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20.proto?= =?UTF-8?q?=2070?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../network/grpc/grpc_client/grpc_client.go_ | 6 +- .../grpc/grpc_client/grpc_client_table.go_ | 28 +-- .../grpc/grpc_client/grpc_client_vars.go_ | 6 + .../network/nrpc/nrpc_client/nrpc_client.go_ | 6 +- .../nrpc/nrpc_client/nrpc_client_table.go_ | 28 +-- .../nrpc/nrpc_client/nrpc_client_vars.go_ | 7 + internal/constants/constants.go | 4 + internal/create_files/create_files.go | 14 +- .../create_files/grpc_client/grpc_client.go | 2 +- .../grpc_client_tables/grpc_client_tables.go | 2 +- .../grpc_client_vars/grpc_client_vars.go | 205 ++++++++++++++++++ .../grpc_client_vars/grpc_client_vars_test.go | 1 + .../grpc_server_tables/grpc_server_tables.go | 6 +- .../create_files/nrpc_client/nrpc_client.go | 2 +- .../nrpc_client_tables/nrpc_client_tables.go | 2 +- internal/create_files/protobuf/protobuf.go | 90 +++++--- .../server_grpc_starter.go | 2 +- .../server_nrpc_starter.go | 2 +- internal/logic/logic.go | 8 + 19 files changed, 341 insertions(+), 80 deletions(-) create mode 100644 bin/templates/pkg/network/grpc/grpc_client/grpc_client_vars.go_ create mode 100644 bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_vars.go_ create mode 100644 internal/create_files/grpc_client_vars/grpc_client_vars.go create mode 100644 internal/create_files/grpc_client_vars/grpc_client_vars_test.go diff --git a/bin/templates/pkg/network/grpc/grpc_client/grpc_client.go_ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client.go_ index b3f156a..eabb349 100644 --- a/bin/templates/pkg/network/grpc/grpc_client/grpc_client.go_ +++ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client.go_ @@ -30,9 +30,6 @@ var Settings SettingsINI // Conn - подключение к серверу GRPC var Conn *grpc.ClientConn -// Client - подключение к клиенту GRPC -var Client grpc_proto.SyncServiceClient - // mutex_Connect - защита от многопоточности Reconnect() var mutex_Connect = &sync.Mutex{} @@ -76,7 +73,8 @@ func Connect_err() error { return err } - Client = grpc_proto.NewSyncServiceClient(Conn) + FillClients() + 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 be4f07b..f58a157 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_ @@ -41,7 +41,7 @@ func (crud Crud_GRPC) Read(m *lawsuit_status_types.LawsuitStatusType) error { // var Otvet lawsuit_status_types.LawsuitStatusType // подключение - if grpc_client.Client == nil { + if grpc_client.LawsuitStatusTypeClient == nil { grpc_client.Connect() } @@ -57,7 +57,7 @@ func (crud Crud_GRPC) Read(m *lawsuit_status_types.LawsuitStatusType) error { defer ctxCancelFunc() // запрос - Response, err := grpc_client.Client.LawsuitStatusType_Read(ctx, Request) + Response, err := grpc_client.LawsuitStatusTypeClient.Read(ctx, Request) if err != nil { sError := err.Error() if len(sError) >= len(constants.TEXT_ERROR_MODEL_VERSION) && sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { @@ -81,7 +81,7 @@ func (crud Crud_GRPC) Create(m *lawsuit_status_types.LawsuitStatusType) error { // var Otvet lawsuit_status_types.LawsuitStatusType // подключение - if grpc_client.Client == nil { + if grpc_client.LawsuitStatusTypeClient == nil { grpc_client.Connect() } @@ -101,7 +101,7 @@ func (crud Crud_GRPC) Create(m *lawsuit_status_types.LawsuitStatusType) error { defer ctxCancelFunc() // запрос - Response, err := grpc_client.Client.LawsuitStatusType_Create(ctx, Request) + Response, err := grpc_client.LawsuitStatusTypeClient.Create(ctx, Request) if err != nil { sError := err.Error() if len(sError) >= len(constants.TEXT_ERROR_MODEL_VERSION) && sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { @@ -125,7 +125,7 @@ func (crud Crud_GRPC) Update(m *lawsuit_status_types.LawsuitStatusType) error { // var Otvet lawsuit_status_types.LawsuitStatusType // подключение - if grpc_client.Client == nil { + if grpc_client.LawsuitStatusTypeClient == nil { grpc_client.Connect() } @@ -145,7 +145,7 @@ func (crud Crud_GRPC) Update(m *lawsuit_status_types.LawsuitStatusType) error { defer ctxCancelFunc() // запрос - Response, err := grpc_client.Client.LawsuitStatusType_Update(ctx, Request) + Response, err := grpc_client.LawsuitStatusTypeClient.Update(ctx, Request) if err != nil { sError := err.Error() if len(sError) >= len(constants.TEXT_ERROR_MODEL_VERSION) && sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { @@ -169,7 +169,7 @@ func (crud Crud_GRPC) Save(m *lawsuit_status_types.LawsuitStatusType) error { // var Otvet lawsuit_status_types.LawsuitStatusType // подключение - if grpc_client.Client == nil { + if grpc_client.LawsuitStatusTypeClient == nil { grpc_client.Connect() } @@ -189,7 +189,7 @@ func (crud Crud_GRPC) Save(m *lawsuit_status_types.LawsuitStatusType) error { defer ctxCancelFunc() // запрос - Response, err := grpc_client.Client.LawsuitStatusType_Save(ctx, Request) + Response, err := grpc_client.LawsuitStatusTypeClient.Save(ctx, Request) if err != nil { sError := err.Error() if len(sError) >= len(constants.TEXT_ERROR_MODEL_VERSION) && sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { @@ -213,7 +213,7 @@ func (crud Crud_GRPC) Delete(m *lawsuit_status_types.LawsuitStatusType) error { // var Otvet lawsuit_status_types.LawsuitStatusType // подключение - if grpc_client.Client == nil { + if grpc_client.LawsuitStatusTypeClient == nil { grpc_client.Connect() } @@ -229,7 +229,7 @@ func (crud Crud_GRPC) Delete(m *lawsuit_status_types.LawsuitStatusType) error { defer ctxCancelFunc() // запрос - Response, err := grpc_client.Client.LawsuitStatusType_Delete(ctx, Request) + Response, err := grpc_client.LawsuitStatusTypeClient.Delete(ctx, Request) if err != nil { sError := err.Error() if len(sError) >= len(constants.TEXT_ERROR_MODEL_VERSION) && sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { @@ -253,7 +253,7 @@ func (crud Crud_GRPC) Restore(m *lawsuit_status_types.LawsuitStatusType) error { // var Otvet lawsuit_status_types.LawsuitStatusType // подключение - if grpc_client.Client == nil { + if grpc_client.LawsuitStatusTypeClient == nil { grpc_client.Connect() } @@ -269,7 +269,7 @@ func (crud Crud_GRPC) Restore(m *lawsuit_status_types.LawsuitStatusType) error { defer ctxCancelFunc() // запрос - Response, err := grpc_client.Client.LawsuitStatusType_Restore(ctx, Request) + Response, err := grpc_client.LawsuitStatusTypeClient.Restore(ctx, Request) if err != nil { sError := err.Error() if len(sError) >= len(constants.TEXT_ERROR_MODEL_VERSION) && sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { @@ -291,7 +291,7 @@ func (crud Crud_GRPC) Restore(m *lawsuit_status_types.LawsuitStatusType) error { // Find_ByExtID - находит модель в БД по ext_id и connection_id func (crud Crud_GRPC) Find_ByExtID(m *lawsuit_status_types.LawsuitStatusType) error { //подключение - if grpc_client.Client == nil { + if grpc_client.LawsuitStatusTypeClient == nil { grpc_client.Connect() } @@ -308,7 +308,7 @@ func (crud Crud_GRPC) Find_ByExtID(m *lawsuit_status_types.LawsuitStatusType) er defer ctxCancelFunc() //запрос - Response, err := grpc_client.Client.LawsuitStatusType_FindByExtID(ctx, Request) + Response, err := grpc_client.LawsuitStatusTypeClient.FindByExtID(ctx, Request) if err != nil { sError := err.Error() if sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { diff --git a/bin/templates/pkg/network/grpc/grpc_client/grpc_client_vars.go_ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client_vars.go_ new file mode 100644 index 0000000..ef2b3e6 --- /dev/null +++ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client_vars.go_ @@ -0,0 +1,6 @@ +package grpc_client + +import ( + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/api/grpc_proto" +) + diff --git a/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client.go_ b/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client.go_ index c6b03dd..b492a78 100644 --- a/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client.go_ +++ b/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client.go_ @@ -36,9 +36,6 @@ var Settings SettingsINI // Conn - подключение к NATS var Conn *nats.Conn -// Client - подключение к клиенту NRPC -var Client *grpc_proto.Sync_serviceClient - // mutex_Connect - защита от многопоточности Reconnect() var mutex_Connect = &sync.Mutex{} @@ -80,8 +77,7 @@ func Connect_err() error { return err } - // This is our generated client. - Client = grpc_proto.NewSync_serviceClient(Conn) + FillClients() return err } diff --git a/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_table.go_ b/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_table.go_ index 5f640a6..71898ee 100644 --- a/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_table.go_ +++ b/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_table.go_ @@ -39,7 +39,7 @@ func (crud Crud_NRPC) Read(m *lawsuit_status_types.LawsuitStatusType) error { // var Otvet lawsuit_status_types.LawsuitStatusType // подключение - if nrpc_client.Client == nil { + if nrpc_client.LawsuitStatusTypeClient == nil { nrpc_client.Connect() } @@ -51,7 +51,7 @@ func (crud Crud_NRPC) Read(m *lawsuit_status_types.LawsuitStatusType) error { Request.VersionModel = versionModel // запрос - Response, err := nrpc_client.Client.LawsuitStatusType_Read(Request) + Response, err := nrpc_client.LawsuitStatusTypeClient.Read(Request) if err != nil { sError := err.Error() if len(sError) >= len(constants.TEXT_ERROR_MODEL_VERSION) && sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { @@ -75,7 +75,7 @@ func (crud Crud_NRPC) Create(m *lawsuit_status_types.LawsuitStatusType) error { // var Otvet lawsuit_status_types.LawsuitStatusType // подключение - if nrpc_client.Client == nil { + if nrpc_client.LawsuitStatusTypeClient == nil { nrpc_client.Connect() } @@ -91,7 +91,7 @@ func (crud Crud_NRPC) Create(m *lawsuit_status_types.LawsuitStatusType) error { Request.VersionModel = VersionModel // запрос - Response, err := nrpc_client.Client.LawsuitStatusType_Create(Request) + Response, err := nrpc_client.LawsuitStatusTypeClient.Create(Request) if err != nil { sError := err.Error() if len(sError) >= len(constants.TEXT_ERROR_MODEL_VERSION) && sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { @@ -115,7 +115,7 @@ func (crud Crud_NRPC) Update(m *lawsuit_status_types.LawsuitStatusType) error { // var Otvet lawsuit_status_types.LawsuitStatusType // подключение - if nrpc_client.Client == nil { + if nrpc_client.LawsuitStatusTypeClient == nil { nrpc_client.Connect() } @@ -131,7 +131,7 @@ func (crud Crud_NRPC) Update(m *lawsuit_status_types.LawsuitStatusType) error { Request.VersionModel = VersionModel // запрос - Response, err := nrpc_client.Client.LawsuitStatusType_Update(Request) + Response, err := nrpc_client.LawsuitStatusTypeClient.Update(Request) if err != nil { sError := err.Error() if len(sError) >= len(constants.TEXT_ERROR_MODEL_VERSION) && sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { @@ -155,7 +155,7 @@ func (crud Crud_NRPC) Save(m *lawsuit_status_types.LawsuitStatusType) error { // var Otvet lawsuit_status_types.LawsuitStatusType // подключение - if nrpc_client.Client == nil { + if nrpc_client.LawsuitStatusTypeClient == nil { nrpc_client.Connect() } @@ -171,7 +171,7 @@ func (crud Crud_NRPC) Save(m *lawsuit_status_types.LawsuitStatusType) error { Request.VersionModel = VersionModel // запрос - Response, err := nrpc_client.Client.LawsuitStatusType_Save(Request) + Response, err := nrpc_client.LawsuitStatusTypeClient.Save(Request) if err != nil { sError := err.Error() if len(sError) >= len(constants.TEXT_ERROR_MODEL_VERSION) && sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { @@ -195,7 +195,7 @@ func (crud Crud_NRPC) Delete(m *lawsuit_status_types.LawsuitStatusType) error { // var Otvet lawsuit_status_types.LawsuitStatusType // подключение - if nrpc_client.Client == nil { + if nrpc_client.LawsuitStatusTypeClient == nil { nrpc_client.Connect() } @@ -207,7 +207,7 @@ func (crud Crud_NRPC) Delete(m *lawsuit_status_types.LawsuitStatusType) error { Request.VersionModel = VersionModel // запрос - Response, err := nrpc_client.Client.LawsuitStatusType_Delete(Request) + Response, err := nrpc_client.LawsuitStatusTypeClient.Delete(Request) if err != nil { sError := err.Error() if len(sError) >= len(constants.TEXT_ERROR_MODEL_VERSION) && sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { @@ -231,7 +231,7 @@ func (crud Crud_NRPC) Restore(m *lawsuit_status_types.LawsuitStatusType) error { // var Otvet lawsuit_status_types.LawsuitStatusType // подключение - if nrpc_client.Client == nil { + if nrpc_client.LawsuitStatusTypeClient == nil { nrpc_client.Connect() } @@ -243,7 +243,7 @@ func (crud Crud_NRPC) Restore(m *lawsuit_status_types.LawsuitStatusType) error { Request.VersionModel = VersionModel // запрос - Response, err := nrpc_client.Client.LawsuitStatusType_Restore(Request) + Response, err := nrpc_client.LawsuitStatusTypeClient.Restore(Request) if err != nil { sError := err.Error() if len(sError) >= len(constants.TEXT_ERROR_MODEL_VERSION) && sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { @@ -265,7 +265,7 @@ func (crud Crud_NRPC) Restore(m *lawsuit_status_types.LawsuitStatusType) error { // Find_ByExtID - находит модель в БД по ext_id и connection_id func (crud Crud_NRPC) Find_ByExtID(m *lawsuit_status_types.LawsuitStatusType) error { //подключение - if nrpc_client.Client == nil { + if nrpc_client.LawsuitStatusTypeClient == nil { nrpc_client.Connect() } @@ -278,7 +278,7 @@ func (crud Crud_NRPC) Find_ByExtID(m *lawsuit_status_types.LawsuitStatusType) er Request.VersionModel = VersionModel //запрос - Response, err := nrpc_client.Client.LawsuitStatusType_FindByExtID(Request) + Response, err := nrpc_client.LawsuitStatusTypeClient.FindByExtID(Request) if err != nil { sError := err.Error() if sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION { diff --git a/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_vars.go_ b/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_vars.go_ new file mode 100644 index 0000000..27adfe9 --- /dev/null +++ b/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_vars.go_ @@ -0,0 +1,7 @@ +package nrpc_client + +import ( + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/api/nrpc/grpc_proto" +) + + diff --git a/internal/constants/constants.go b/internal/constants/constants.go index 9f35b15..11a5d3f 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -42,3 +42,7 @@ const STARTER_TABLES_FILENAME = "starter_tables.go_" const STARTER_TABLES_FILENAME_TEST = "starter_tables_test.go_" const STARTER_TABLES_PREFIX = "crud_starter_" const CRUD_TABLES_FREFIX = "crud_" + +const GRPC_CLIENT_VARS_FILENAME = "grpc_client_vars.go" + +const NRPC_CLIENT_VARS_FILENAME = "nrpc_client_vars.go" diff --git a/internal/create_files/create_files.go b/internal/create_files/create_files.go index 8549a83..d890715 100644 --- a/internal/create_files/create_files.go +++ b/internal/create_files/create_files.go @@ -312,8 +312,8 @@ func FindTableNameURL(TableName string) string { return Otvet } -// FindProtoURL - возвращает URL репозитория с пакетом "grpc_proto" -func FindProtoURL() string { +// FindGRPCProtoURL - возвращает URL репозитория с пакетом "grpc_proto" +func FindGRPCProtoURL() string { Otvet := "" TEMPLATE_FOLDERNAME_GRPC_PROTO := strings.ToLower(config.Settings.TEMPLATE_FOLDERNAME_GRPC_PROTO) @@ -322,6 +322,16 @@ func FindProtoURL() string { return Otvet } +// FindNRPCProtoURL - возвращает URL репозитория с пакетом "grpc_proto" +func FindNRPCProtoURL() string { + Otvet := "" + + TEMPLATE_FOLDERNAME_NRPC_PROTO := strings.ToLower(config.Settings.TEMPLATE_FOLDERNAME_GRPC_PROTO) + Otvet = config.Settings.SERVICE_REPOSITORY_URL + "/" + TEMPLATE_FOLDERNAME_NRPC_PROTO + "/nrpc/" + "grpc_proto" + + return Otvet +} + // FindModelTableURL - возвращает URL репозитория model для таблицы TableName func FindModelTableURL(TableName string) string { Otvet := "" diff --git a/internal/create_files/grpc_client/grpc_client.go b/internal/create_files/grpc_client/grpc_client.go index df514e3..6a2490a 100644 --- a/internal/create_files/grpc_client/grpc_client.go +++ b/internal/create_files/grpc_client/grpc_client.go @@ -63,7 +63,7 @@ func CreateGRPCClient() error { if config.Settings.USE_DEFAULT_TEMPLATE == true { TextGRPCClient = create_files.DeleteTemplateRepositoryImports(TextGRPCClient) - ProtoURL := create_files.FindProtoURL() + ProtoURL := create_files.FindGRPCProtoURL() TextGRPCClient = create_files.AddImport(TextGRPCClient, ProtoURL) DBConstantsURL := create_files.FindDBConstantsURL() 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 e45dde9..97c3640 100644 --- a/internal/create_files/grpc_client_tables/grpc_client_tables.go +++ b/internal/create_files/grpc_client_tables/grpc_client_tables.go @@ -96,7 +96,7 @@ func CreateFiles(Table1 *types.Table) error { TextGRPCClient = create_files.DeleteTemplateRepositoryImports(TextGRPCClient) //proto - RepositoryGRPCProtoURL := create_files.FindProtoURL() + RepositoryGRPCProtoURL := create_files.FindGRPCProtoURL() TextGRPCClient = create_files.AddImport(TextGRPCClient, RepositoryGRPCProtoURL) //model diff --git a/internal/create_files/grpc_client_vars/grpc_client_vars.go b/internal/create_files/grpc_client_vars/grpc_client_vars.go new file mode 100644 index 0000000..994a2df --- /dev/null +++ b/internal/create_files/grpc_client_vars/grpc_client_vars.go @@ -0,0 +1,205 @@ +package grpc_client_vars + +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/folders" + "github.com/ManyakRus/crud_generator/internal/types" + "github.com/ManyakRus/starter/log" + "github.com/ManyakRus/starter/micro" + "os" + "sort" +) + +// CreateAllFiles - создаёт все файлы в папке grpc proto +func CreateAllFiles(MapAll map[string]*types.Table) error { + var err error + + err = CreateFileGRPCClientVars(MapAll) + if err != nil { + log.Error("CreateFileGRPCClientVars() error: ", err) + return err + } + + err = CreateFileNRPCClientVars(MapAll) + if err != nil { + log.Error("CreateFileNRPCClientVars() error: ", err) + return err + } + + return err +} + +// CreateFileGRPCClientVars - создаёт 1 файл в папке grpc +func CreateFileGRPCClientVars(MapAll map[string]*types.Table) error { + var err error + + if config.Settings.NEED_CREATE_GRPC == false { + return err + } + + //чтение файлов + 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() + FilenameReadyGRPCClientVars := DirReadyGRPCClient + constants.GRPC_CLIENT_VARS_FILENAME + + //создадим папку готовых файлов + folders.CreateFolder(DirReadyGRPCClient) + + FilenameTemplateVars := DirTemplatesGRPCClient + constants.GRPC_CLIENT_VARS_FILENAME + "_" + bytes, err := os.ReadFile(FilenameTemplateVars) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateVars, " error: ", err) + } + TextGRPCClientVars := string(bytes) + + //заменим импорты + if config.Settings.USE_DEFAULT_TEMPLATE == true { + TextGRPCClientVars = create_files.DeleteTemplateRepositoryImports(TextGRPCClientVars) + + GRPCProtoURL := create_files.FindGRPCProtoURL() + TextGRPCClientVars = create_files.AddImport(TextGRPCClientVars, GRPCProtoURL) + } + + // + TextVars := FindTextVars(MapAll, false) + TextGRPCClientVars = TextGRPCClientVars + TextVars + + // + TextFuncFillClients := FindTextFuncFillClients(MapAll) + TextGRPCClientVars = TextGRPCClientVars + ` +// FillClients - создание клиентов GRPC` + TextFuncFillClients + + // + TextGRPCClientVars = TextGRPCClientVars + "\n}\n" + + // + TextGRPCClientVars = create_files.DeleteEmptyLines(TextGRPCClientVars) + + //запись файла + err = os.WriteFile(FilenameReadyGRPCClientVars, []byte(TextGRPCClientVars), constants.FILE_PERMISSIONS) + + return err +} + +// CreateFileNRPCClientVars - создаёт 1 файл в папке grpc +func CreateFileNRPCClientVars(MapAll map[string]*types.Table) error { + var err error + + if config.Settings.NEED_CREATE_NRPC == false { + return err + } + + //чтение файлов + DirBin := micro.ProgramDir_bin() + DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile() + DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile() + DirTemplatesNRPCClient := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_NRPC_CLIENT + micro.SeparatorFile() + DirReadyNRPCClient := DirReady + config.Settings.TEMPLATE_FOLDERNAME_NRPC_CLIENT + micro.SeparatorFile() + FilenameReadyGRPCClientVars := DirReadyNRPCClient + constants.NRPC_CLIENT_VARS_FILENAME + + //создадим папку готовых файлов + folders.CreateFolder(DirReadyNRPCClient) + + FilenameTemplateVars := DirTemplatesNRPCClient + constants.NRPC_CLIENT_VARS_FILENAME + "_" + bytes, err := os.ReadFile(FilenameTemplateVars) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateVars, " error: ", err) + } + TextNRPCClientVars := string(bytes) + + //заменим импорты + if config.Settings.USE_DEFAULT_TEMPLATE == true { + TextNRPCClientVars = create_files.DeleteTemplateRepositoryImports(TextNRPCClientVars) + + NRPCProtoURL := create_files.FindNRPCProtoURL() + TextNRPCClientVars = create_files.AddImport(TextNRPCClientVars, NRPCProtoURL) + } + + // + TextVars := FindTextVars(MapAll, true) + TextNRPCClientVars = TextNRPCClientVars + TextVars + + // + TextFuncFillClients := FindTextFuncFillClients(MapAll) + TextNRPCClientVars = TextNRPCClientVars + ` +// FillClients - создание клиентов NRPC` + TextFuncFillClients + + // + TextNRPCClientVars = TextNRPCClientVars + "\n}\n" + + // + TextNRPCClientVars = create_files.DeleteEmptyLines(TextNRPCClientVars) + + //запись файла + err = os.WriteFile(FilenameReadyGRPCClientVars, []byte(TextNRPCClientVars), constants.FILE_PERMISSIONS) + + return err +} + +// FindTextVars - возвращает текст создания переменных +func FindTextVars(MapAll map[string]*types.Table, IsNRPC bool) string { + Otvet := "" + + //сортировка + keys := make([]string, 0, len(MapAll)) + for k := range MapAll { + keys = append(keys, k) + } + sort.Strings(keys) + + TextStar := "" + if IsNRPC == true { + TextStar = "*" + } + + //создание переменных + for _, key1 := range keys { + Table1 := MapAll[key1] + //проверка что таблица нормальная + err1 := create_files.CheckGoodTable(Table1) + if err1 != nil { + log.Warn(err1) + continue + } + Comment := create_files.FindModelComment(Table1) + Text1 := Comment + "\n" + "var " + Table1.NameGo + "Client " + TextStar + "grpc_proto." + Table1.NameGo + "Client" + Otvet = Otvet + "\n" + Text1 + "\n" + } + return Otvet +} + +// FindTextFuncFillClients - возвращает текст создания функции FillClients +func FindTextFuncFillClients(MapAll map[string]*types.Table) string { + Otvet := "" + + //сортировка + keys := make([]string, 0, len(MapAll)) + for k := range MapAll { + keys = append(keys, k) + } + sort.Strings(keys) + + //создание функции + Otvet = Otvet + ` +func FillClients() {` + + for _, key1 := range keys { + Table1 := MapAll[key1] + //проверка что таблица нормальная + err1 := create_files.CheckGoodTable(Table1) + if err1 != nil { + log.Warn(err1) + continue + } + Text1 := "\t" + Table1.NameGo + "Client = " + "grpc_proto.New" + Table1.NameGo + "Client(Conn)" + Otvet = Otvet + "\n" + Text1 + + } + + return Otvet +} diff --git a/internal/create_files/grpc_client_vars/grpc_client_vars_test.go b/internal/create_files/grpc_client_vars/grpc_client_vars_test.go new file mode 100644 index 0000000..fe7eb2b --- /dev/null +++ b/internal/create_files/grpc_client_vars/grpc_client_vars_test.go @@ -0,0 +1 @@ +package grpc_client_vars 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 b789fcb..a64090c 100644 --- a/internal/create_files/grpc_server_tables/grpc_server_tables.go +++ b/internal/create_files/grpc_server_tables/grpc_server_tables.go @@ -78,7 +78,7 @@ func CreateFiles(Table1 *types.Table) error { ModelTableURL := create_files.FindModelTableURL(TableName) TextGRPCServer = create_files.AddImport(TextGRPCServer, ModelTableURL) - ProtoURL := create_files.FindProtoURL() + ProtoURL := create_files.FindGRPCProtoURL() TextGRPCServer = create_files.AddImport(TextGRPCServer, ProtoURL) //удалим лишние функции @@ -101,7 +101,7 @@ func CreateFiles(Table1 *types.Table) error { //TextGRPCServer = create_files.DeleteTemplateRepositoryImports(TextGRPCServer) ////proto - //RepositoryGRPCProtoURL := create_files.FindProtoURL() + //RepositoryGRPCProtoURL := create_files.FindGRPCProtoURL() //TextGRPCServer = create_files.AddImport(TextGRPCServer, RepositoryGRPCProtoURL) // ////model @@ -153,7 +153,7 @@ func CreateTestFiles(Table1 *types.Table) error { ModelTableURL := create_files.FindModelTableURL(TableName) TextGRPCServer = create_files.AddImport(TextGRPCServer, ModelTableURL) - ProtoURL := create_files.FindProtoURL() + ProtoURL := create_files.FindGRPCProtoURL() TextGRPCServer = create_files.AddImport(TextGRPCServer, ProtoURL) CrudStarterURL := create_files.FindCrudStarterURL() diff --git a/internal/create_files/nrpc_client/nrpc_client.go b/internal/create_files/nrpc_client/nrpc_client.go index bd24ce5..8feab8b 100644 --- a/internal/create_files/nrpc_client/nrpc_client.go +++ b/internal/create_files/nrpc_client/nrpc_client.go @@ -66,7 +66,7 @@ func CreateNRPCClient() error { if config.Settings.USE_DEFAULT_TEMPLATE == true { TextNRPCClient = create_files.DeleteTemplateRepositoryImports(TextNRPCClient) - ProtoURL := create_files.FindProtoURL() + ProtoURL := create_files.FindGRPCProtoURL() TextNRPCClient = create_files.AddImport(TextNRPCClient, ProtoURL) DBConstantsURL := create_files.FindDBConstantsURL() diff --git a/internal/create_files/nrpc_client_tables/nrpc_client_tables.go b/internal/create_files/nrpc_client_tables/nrpc_client_tables.go index 5eaa67c..8a572ce 100644 --- a/internal/create_files/nrpc_client_tables/nrpc_client_tables.go +++ b/internal/create_files/nrpc_client_tables/nrpc_client_tables.go @@ -75,7 +75,7 @@ func CreateFiles(Table1 *types.Table) error { if config.Settings.USE_DEFAULT_TEMPLATE == true { TextNRPCClient = create_files.DeleteTemplateRepositoryImports(TextNRPCClient) - GRPCProtoURL := create_files.FindProtoURL() + GRPCProtoURL := create_files.FindGRPCProtoURL() TextNRPCClient = create_files.AddImport(TextNRPCClient, GRPCProtoURL) NRPCClientURL := create_files.FindNRPCClientURL() diff --git a/internal/create_files/protobuf/protobuf.go b/internal/create_files/protobuf/protobuf.go index 2dcb3d4..960d4be 100644 --- a/internal/create_files/protobuf/protobuf.go +++ b/internal/create_files/protobuf/protobuf.go @@ -68,8 +68,8 @@ func CreateFileProto(MapAll map[string]*types.Table) error { sort.Strings(keys) //найдём новый текст для каждой таблицы - TextProtoNew := "" for _, key1 := range keys { + TextProtoNew := "" Table1, ok := MapAll[key1] if ok == false { log.Panic("MapAll[key1] not found") @@ -82,26 +82,33 @@ func CreateFileProto(MapAll map[string]*types.Table) error { continue } - TextProtoNew = TextProtoNew + FindTextProtoTable1(TextProto, Table1) - } + Comments1 := create_files.FindModelComment(Table1) + TextFuncStart := ` +` + Comments1 + ` +service ` + Table1.NameGo + ` {` + TextFunctions := FindTextProtoTable1(TextProto, Table1) + TextProtoNew = TextProtoNew + TextFuncStart + TextFunctions + "}\n\n" - //найдём куда вставить текст - sFind := "\nservice " - pos1 := strings.Index(TextProto, sFind) - if pos1 < 0 { - log.Panic("Not found text ", sFind) - } + //найдём куда вставить текст + PosStart := 0 + sFind := "\nservice " + Table1.NameGo + " " + pos1 := strings.Index(TextProto, sFind) + if pos1 < 0 { + PosStart = len(TextProto) - 1 //в конец + } else { + s2 := TextProto[pos1+1:] + sFind = "\n" + posEnd := strings.Index(s2, sFind) + if posEnd < 0 { + log.Panic("Not found text ", sFind) + } + PosStart = pos1 + posEnd + 1 + } - s2 := TextProto[pos1+1:] - sFind = "\n" - posEnd := strings.Index(s2, sFind) - if posEnd < 0 { - log.Panic("Not found text ", sFind) - } - PosStart := pos1 + posEnd + 1 + // + TextProto = TextProto[:PosStart] + TextProtoNew + TextProto[PosStart:] - // - TextProto = TextProto[:PosStart] + TextProtoNew + TextProto[PosStart:] + } // TextProto = create_files.DeleteEmptyLines(TextProto) @@ -148,13 +155,26 @@ func FindTextProtoTable1(TextProto string, Table1 *types.Table) string { return Otvet } +// FindTextService - возвращает текст одного сервиса +func FindTextService(TextProto string, ModelName string) string { + Otvet := "" + + TextFind1 := "\nservice " + ModelName + " " + TextFind2 := "\n}" + + Otvet = micro.StringBetween(TextProto, TextFind1, TextFind2) + + return Otvet +} + // FindTextRead - возвращает текст .proto func FindTextRead(TextProto string, ModelName string) string { Otvet := "" Otvet2 := TextRead(ModelName) //проверка такой текст уже есть - pos1 := strings.Index(TextProto, Otvet2) + TextService := FindTextService(TextProto, ModelName) + pos1 := strings.Index(TextService, Otvet2) if pos1 >= 0 { return Otvet } @@ -170,7 +190,8 @@ func FindTextCreate(TextProto string, ModelName string) string { Otvet2 := TextCreate(ModelName) //проверка такой текст уже есть - pos1 := strings.Index(TextProto, Otvet2) + TextService := FindTextService(TextProto, ModelName) + pos1 := strings.Index(TextService, Otvet2) if pos1 >= 0 { return Otvet } @@ -186,7 +207,8 @@ func FindTextUpdate(TextProto string, ModelName string) string { Otvet2 := TextUpdate(ModelName) //проверка такой текст уже есть - pos1 := strings.Index(TextProto, Otvet2) + TextService := FindTextService(TextProto, ModelName) + pos1 := strings.Index(TextService, Otvet2) if pos1 >= 0 { return Otvet } @@ -202,7 +224,8 @@ func FindTextSave(TextProto string, ModelName string) string { Otvet2 := TextSave(ModelName) //проверка такой текст уже есть - pos1 := strings.Index(TextProto, Otvet2) + TextService := FindTextService(TextProto, ModelName) + pos1 := strings.Index(TextService, Otvet2) if pos1 >= 0 { return Otvet } @@ -218,7 +241,8 @@ func FindTextDelete(TextProto string, ModelName string) string { Otvet2 := TextDelete(ModelName) //проверка такой текст уже есть - pos1 := strings.Index(TextProto, Otvet2) + TextService := FindTextService(TextProto, ModelName) + pos1 := strings.Index(TextService, Otvet2) if pos1 >= 0 { return Otvet } @@ -234,7 +258,8 @@ func FindTextRestore(TextProto string, ModelName string) string { Otvet2 := TextRestore(ModelName) //проверка такой текст уже есть - pos1 := strings.Index(TextProto, Otvet2) + TextService := FindTextService(TextProto, ModelName) + pos1 := strings.Index(TextService, Otvet2) if pos1 >= 0 { return Otvet } @@ -250,7 +275,8 @@ func FindTextFindByExtId(TextProto string, ModelName string) string { Otvet2 := TextFindByExtId(ModelName) //проверка такой текст уже есть - pos1 := strings.Index(TextProto, Otvet2) + TextService := FindTextService(TextProto, ModelName) + pos1 := strings.Index(TextService, Otvet2) if pos1 >= 0 { return Otvet } @@ -262,49 +288,49 @@ func FindTextFindByExtId(TextProto string, ModelName string) string { // TextRead - возвращает текст .proto func TextRead(ModelName string) string { - Otvet := "rpc " + ModelName + "_Read(RequestId) returns (Response) {}" + Otvet := "rpc Read(RequestId) returns (Response) {}" return Otvet } // TextCreate - возвращает текст .proto func TextCreate(ModelName string) string { - Otvet := "rpc " + ModelName + "_Create(RequestModel) returns (Response) {}" + Otvet := "rpc Create(RequestModel) returns (Response) {}" return Otvet } // TextUpdate - возвращает текст .proto func TextUpdate(ModelName string) string { - Otvet := "rpc " + ModelName + "_Update(RequestModel) returns (Response) {}" + Otvet := "rpc Update(RequestModel) returns (Response) {}" return Otvet } // TextSave - возвращает текст .proto func TextSave(ModelName string) string { - Otvet := "rpc " + ModelName + "_Save(RequestModel) returns (Response) {}" + Otvet := "rpc Save(RequestModel) returns (Response) {}" return Otvet } // TextDelete - возвращает текст .proto func TextDelete(ModelName string) string { - Otvet := "rpc " + ModelName + "_Delete(RequestId) returns (Response) {}" + Otvet := "rpc Delete(RequestId) returns (Response) {}" return Otvet } // TextRestore - возвращает текст .proto func TextRestore(ModelName string) string { - Otvet := "rpc " + ModelName + "_Restore(RequestId) returns (Response) {}" + Otvet := "rpc Restore(RequestId) returns (Response) {}" return Otvet } // TextFindByExtId - возвращает текст .proto func TextFindByExtId(ModelName string) string { - Otvet := "rpc " + ModelName + "_FindByExtID(RequestExtID) returns (Response) {}" + Otvet := "rpc FindByExtID(RequestExtID) returns (Response) {}" return Otvet } diff --git a/internal/create_files/server_grpc_starter/server_grpc_starter.go b/internal/create_files/server_grpc_starter/server_grpc_starter.go index cf63e0a..844bfef 100644 --- a/internal/create_files/server_grpc_starter/server_grpc_starter.go +++ b/internal/create_files/server_grpc_starter/server_grpc_starter.go @@ -57,7 +57,7 @@ func CreateServerGRPCStarter() error { if config.Settings.USE_DEFAULT_TEMPLATE == true { TextGRPCStarter = create_files.DeleteTemplateRepositoryImports(TextGRPCStarter) - ProtoURL := create_files.FindProtoURL() + ProtoURL := create_files.FindGRPCProtoURL() TextGRPCStarter = create_files.AddImport(TextGRPCStarter, ProtoURL) } diff --git a/internal/create_files/server_nrpc_starter/server_nrpc_starter.go b/internal/create_files/server_nrpc_starter/server_nrpc_starter.go index 62534bf..a21be89 100644 --- a/internal/create_files/server_nrpc_starter/server_nrpc_starter.go +++ b/internal/create_files/server_nrpc_starter/server_nrpc_starter.go @@ -57,7 +57,7 @@ func CreateServerGRPCStarter() error { TextNRPCStarter = create_files.DeleteTemplateRepositoryImports(TextNRPCStarter) //Proto URL - RepositoryProtoURL := create_files.FindProtoURL() + RepositoryProtoURL := create_files.FindGRPCProtoURL() TextNRPCStarter = create_files.AddImport(TextNRPCStarter, RepositoryProtoURL) //server grpc URL diff --git a/internal/logic/logic.go b/internal/logic/logic.go index b620ac8..8f9a4bb 100644 --- a/internal/logic/logic.go +++ b/internal/logic/logic.go @@ -10,6 +10,7 @@ import ( "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_client_vars" "github.com/ManyakRus/crud_generator/internal/create_files/grpc_server_tables" "github.com/ManyakRus/crud_generator/internal/create_files/main_file" "github.com/ManyakRus/crud_generator/internal/create_files/makefile" @@ -177,5 +178,12 @@ func StartFillAll() error { return err } + //grpc_client_vars + err = grpc_client_vars.CreateAllFiles(MapAll) + if err != nil { + //log.Error("env_file.CreateAllFiles() error: ", err) + return err + } + return err }