diff --git a/bin/templates/internal/app/grpc/server_grpc/server_grpc_table_update_func.go_ b/bin/templates/internal/app/grpc/server_grpc/server_grpc_table_update_func.go_ new file mode 100644 index 0000000..c2545a8 --- /dev/null +++ b/bin/templates/internal/app/grpc/server_grpc/server_grpc_table_update_func.go_ @@ -0,0 +1,29 @@ +// LawsuitStatusType_Read - изменяет колонку ColumnName в базе данных +func (s *ServerGRPC) LawsuitStatusType_Read(ctx context.Context, Request *grpc_proto.RequestId) (*grpc_proto.ResponseEmpty, error) { + var Otvet grpc_proto.ResponseEmpty + var err error + + //проверим контекст уже отменён + if micro.ContextDone(ctx) == true { + err = context.Canceled + return &Otvet, err + } + + //проверим совпадения версии модели + VersionServer := lawsuit_status_types.LawsuitStatusType{}.GetStructVersion() + VersionClient := Request.VersionModel + if VersionServer != VersionClient { + err = ErrorModelVersion(lawsuit_status_types.LawsuitStatusType{}) + return &Otvet, err + } + + //запрос в БД + Model := &lawsuit_status_types.LawsuitStatusType{} + Model.ID = Request.ID + err = Model.Read() + if err != nil { + return &Otvet, err + } + + return &Otvet, err +} \ No newline at end of file diff --git a/bin/templates/internal/app/grpc/server_grpc/server_grpc_table_update_func_test.go_ b/bin/templates/internal/app/grpc/server_grpc/server_grpc_table_update_func_test.go_ new file mode 100644 index 0000000..3aa8193 --- /dev/null +++ b/bin/templates/internal/app/grpc/server_grpc/server_grpc_table_update_func_test.go_ @@ -0,0 +1,18 @@ +func Test_server_LawsuitStatusType_Read(t *testing.T) { + config_main.LoadEnv() + crud_starter.InitCrudTransport_DB() + + ctx := context.Background() + Request := grpc_proto.RequestId{} + Request.ID = LawsuitStatusType_ID_Test + Request.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion() + + server1 := &ServerGRPC{} + Otvet, err := server1.LawsuitStatusType_Read(ctx, &Request) + if err != nil { + t.Error("Test_server_LawsuitStatusType_Read() error: ", err) + } + if Otvet.ModelString == "" { + t.Error("Test_server_LawsuitStatusType_Read() error: ModelString=''") + } +} \ No newline at end of file diff --git a/internal/constants/constants.go b/internal/constants/constants.go index 851aa2b..ea4098b 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -47,3 +47,6 @@ const CRUD_TABLES_FREFIX = "crud_" const MODEL_TABLE_MANUAL_FILENAME = "model_table_manual.go_" const MODEL_TABLE_UPDATE_FILENAME = "model_table_update.go_" + +const SERVER_GRPC_TABLE_UPDATE_FUNC_FILENAME = "server_grpc_table_update_func.go_" +const SERVER_GRPC_TABLE_UPDATE_FUNC_TEST_FILENAME = "server_grpc_table_update_func_test.go_" diff --git a/internal/create_files/create_files.go b/internal/create_files/create_files.go index f2b9c8e..8692014 100644 --- a/internal/create_files/create_files.go +++ b/internal/create_files/create_files.go @@ -824,6 +824,7 @@ func DeleteEmptyLines(Text string) string { Otvet = strings.ReplaceAll(Otvet, "\n\t//\n\n", "\n\n") //Otvet = strings.ReplaceAll(Otvet, "\r\r", "\r") //Otvet = strings.ReplaceAll(Otvet, "\r\n", "\n") + Otvet = strings.ReplaceAll(Otvet, "}\n\n", "}\n") pos1 := strings.Index(Otvet, "\n\n\n") if pos1 >= 0 { Otvet = DeleteEmptyLines(Otvet) @@ -889,3 +890,50 @@ func AddInterfaceFunction(s, TextAdd string) string { return Otvet } + +// FindTextProtobufRequest - возвращает имя message из .proto, в зависимости от типа, а также название поля +func FindTextProtobufRequest(TypeGo string) (string, string) { + Otvet := "RequestID" + TextRequestFieldName := "ID" + + switch TypeGo { + case "int", "int64": + { + Otvet = "RequestId" + TextRequestFieldName = "ID" + } + + case "int32": + { + Otvet = "RequestInt32" + TextRequestFieldName = "Int32" + } + case "string": + { + Otvet = "RequestString" + TextRequestFieldName = "StringFind" + } + case "time.Time": + { + Otvet = "RequestDate" + TextRequestFieldName = "Date" + } + case "float32": + { + Otvet = "RequestFloat32" + TextRequestFieldName = "Float32" + } + case "float64": + { + Otvet = "RequestFloat64" + TextRequestFieldName = "Float64" + } + case "bool": + { + Otvet = "RequestBool" + TextRequestFieldName = "Bool" + } + } + + return Otvet, TextRequestFieldName +} 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..868f59f 100644 --- a/internal/create_files/grpc_server_tables/grpc_server_tables.go +++ b/internal/create_files/grpc_server_tables/grpc_server_tables.go @@ -9,6 +9,7 @@ import ( "github.com/ManyakRus/starter/log" "github.com/ManyakRus/starter/micro" "os" + "sort" "strings" ) @@ -39,6 +40,26 @@ func CreateAllFiles(MapAll map[string]*types.Table) error { return err } } + + // + if config.Settings.NEED_CREATE_UPDATE_EVERY_COLUMN == true { + //файлы grpc_server update + err = CreateFilesUpdateEveryColumn(Table1) + if err != nil { + log.Error("CreateFiles() table: ", Table1.Name, " error: ", err) + return err + } + + //тестовые файлы grpc_server update + if config.Settings.NEED_CREATE_GRPC_SERVER_TEST == true { + err = CreateFilesUpdateEveryColumnTest(Table1) + if err != nil { + log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err) + return err + } + } + + } } return err } @@ -342,3 +363,236 @@ func ConvertID(Text string, Table1 *types.Table) string { return Otvet } + +// CreateFilesUpdateEveryColumn - создаёт 1 файл в папке grpc_server +func CreateFilesUpdateEveryColumn(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 + constants.SERVER_GRPC_TABLE_UPDATE_FUNC_FILENAME + TableName := strings.ToLower(Table1.Name) + DirReadyTable := DirReadyGRPCServer + FilenameReadyGRPCServerUpdate := DirReadyTable + config.Settings.PREFIX_SERVER_GRPC + TableName + "_update.go" + + //создадим папку готовых файлов + folders.CreateFolder(DirReadyTable) + + bytes, err := os.ReadFile(FilenameTemplateGRPCServer) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateGRPCServer, " error: ", err) + } + TextGRPCServerUpdateFunc := string(bytes) + + TextGRPCServer := "package " + config.Settings.TEMPLATE_FOLDERNAME_GRPC + "\n\n" + TextGRPCServer = TextGRPCServer + `import ( + "context" + "github.com/ManyakRus/starter/micro" + ) + +` + + //заменим импорты + //if config.Settings.USE_DEFAULT_TEMPLATE == true { + ModelTableURL := create_files.FindModelTableURL(TableName) + TextGRPCServer = create_files.AddImport(TextGRPCServer, ModelTableURL) + + ProtoURL := create_files.FindProtoURL() + TextGRPCServer = create_files.AddImport(TextGRPCServer, ProtoURL) + + ModelURL := create_files.FindModelURL() + TextGRPCServer = create_files.AddImport(TextGRPCServer, ModelURL) + + TextGRPCServer = ConvertID(TextGRPCServer, Table1) + //} + + //создание текста + TextUpdateEveryColumn := FindTextUpdateEveryColumn(TextGRPCServerUpdateFunc, Table1) + //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 = TextGRPCServer + TextUpdateEveryColumn + + TextGRPCServer = config.Settings.TEXT_MODULE_GENERATED + TextGRPCServer + + //удаление пустого импорта + TextGRPCServer = create_files.DeleteEmptyImport(TextGRPCServer) + TextGRPCServer = create_files.DeleteEmptyLines(TextGRPCServer) + + //запись файла + err = os.WriteFile(FilenameReadyGRPCServerUpdate, []byte(TextGRPCServer), constants.FILE_PERMISSIONS) + + return err +} + +// FindTextUpdateEveryColumn - возвращает текст для всех таблиц +func FindTextUpdateEveryColumn(TextGRPCServerUpdateFunc string, Table1 *types.Table) string { + Otvet := "" + + //сортировка по названию таблиц + keys := make([]string, 0, len(Table1.MapColumns)) + for k := range Table1.MapColumns { + keys = append(keys, k) + } + sort.Strings(keys) + + //найдём новый текст для каждой таблицы + for _, key1 := range keys { + Column1, ok := Table1.MapColumns[key1] + if ok == false { + log.Panic("FindTextProtoTable1_UpdateEveryColumn() Table1.MapColumns[key1] = false") + } + if create_files.Is_Common_Сolumn(Column1) == true { + continue + } + + TextColumn1 := FindTextUpdateEveryColumn1(TextGRPCServerUpdateFunc, Table1, Column1) + Otvet = Otvet + TextColumn1 + "\n\n" + + } + + return Otvet +} + +// FindTextUpdateEveryColumn1 - возвращает текст для одной таблицы +func FindTextUpdateEveryColumn1(TextGRPCServerUpdateFunc string, Table1 *types.Table, Column1 *types.Column) string { + Otvet := TextGRPCServerUpdateFunc + + ModelName := Table1.NameGo + ColumnName := Column1.NameGo + FuncName := "Update_" + ColumnName + TextRequest, TextRequestFieldName := create_files.FindTextProtobufRequest(Column1.TypeGo) + + Otvet = strings.ReplaceAll(Otvet, config.Settings.TEXT_TEMPLATE_MODEL+"_Read", ModelName+"_"+FuncName) + Otvet = strings.ReplaceAll(Otvet, config.Settings.TEXT_TEMPLATE_MODEL, ModelName) + Otvet = strings.ReplaceAll(Otvet, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name) + Otvet = strings.ReplaceAll(Otvet, "grpc_proto.RequestId", "grpc_proto."+TextRequest) + Otvet = strings.ReplaceAll(Otvet, "ColumnName", ColumnName) + Otvet = strings.ReplaceAll(Otvet, "Model.ID", "Model."+ColumnName) + Otvet = strings.ReplaceAll(Otvet, "Request.ID", "Request."+TextRequestFieldName) + + return Otvet +} + +// CreateFilesUpdateEveryColumnTest - создаёт 1 файл в папке grpc_server +func CreateFilesUpdateEveryColumnTest(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 + constants.SERVER_GRPC_TABLE_UPDATE_FUNC_TEST_FILENAME + TableName := strings.ToLower(Table1.Name) + DirReadyTable := DirReadyGRPCServer + FilenameReadyGRPCServerUpdate := DirReadyTable + config.Settings.PREFIX_SERVER_GRPC + TableName + "_update_test.go" + + //создадим папку готовых файлов + folders.CreateFolder(DirReadyTable) + + bytes, err := os.ReadFile(FilenameTemplateGRPCServer) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateGRPCServer, " error: ", err) + } + TextGRPCServerUpdateFunc := string(bytes) + + TextGRPCServer := "package " + config.Settings.TEMPLATE_FOLDERNAME_GRPC + "\n\n" + TextGRPCServer = TextGRPCServer + `import ( + "context" + "github.com/ManyakRus/starter/config_main" + ) + +` + + //заменим импорты + //if config.Settings.USE_DEFAULT_TEMPLATE == true { + ModelTableURL := create_files.FindModelTableURL(TableName) + TextGRPCServer = create_files.AddImport(TextGRPCServer, ModelTableURL) + + ProtoURL := create_files.FindProtoURL() + TextGRPCServer = create_files.AddImport(TextGRPCServer, ProtoURL) + + ModelURL := create_files.FindModelURL() + TextGRPCServer = create_files.AddImport(TextGRPCServer, ModelURL) + + CrudStarterURL := create_files.FindCrudStarterURL() + TextGRPCServer = create_files.AddImport(TextGRPCServer, CrudStarterURL) + + TextGRPCServer = ConvertID(TextGRPCServer, Table1) + //} + + //создание текста + TextUpdateEveryColumn := FindTextUpdateEveryColumnTest(TextGRPCServerUpdateFunc, Table1) + //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 = TextGRPCServer + TextUpdateEveryColumn + + TextGRPCServer = config.Settings.TEXT_MODULE_GENERATED + TextGRPCServer + + //удаление пустого импорта + TextGRPCServer = create_files.DeleteEmptyImport(TextGRPCServer) + TextGRPCServer = create_files.DeleteEmptyLines(TextGRPCServer) + + //запись файла + err = os.WriteFile(FilenameReadyGRPCServerUpdate, []byte(TextGRPCServer), constants.FILE_PERMISSIONS) + + return err +} + +// FindTextUpdateEveryColumnTest - возвращает текст для всех таблиц +func FindTextUpdateEveryColumnTest(TextGRPCServerUpdateFunc string, Table1 *types.Table) string { + Otvet := "" + + //сортировка по названию таблиц + keys := make([]string, 0, len(Table1.MapColumns)) + for k := range Table1.MapColumns { + keys = append(keys, k) + } + sort.Strings(keys) + + //найдём новый текст для каждой таблицы + for _, key1 := range keys { + Column1, ok := Table1.MapColumns[key1] + if ok == false { + log.Panic("FindTextProtoTable1_UpdateEveryColumn() Table1.MapColumns[key1] = false") + } + if create_files.Is_Common_Сolumn(Column1) == true { + continue + } + + TextColumn1 := FindTextUpdateEveryColumnTest1(TextGRPCServerUpdateFunc, Table1, Column1) + Otvet = Otvet + TextColumn1 + "\n\n" + + } + + return Otvet +} + +// FindTextUpdateEveryColumnTest1 - возвращает текст для одной таблицы +func FindTextUpdateEveryColumnTest1(TextGRPCServerUpdateFunc string, Table1 *types.Table, Column1 *types.Column) string { + Otvet := TextGRPCServerUpdateFunc + + ModelName := Table1.NameGo + ColumnName := Column1.NameGo + FuncName := "Update_" + ColumnName + TextRequest, TextRequestFieldName := create_files.FindTextProtobufRequest(Column1.TypeGo) + + Otvet = strings.ReplaceAll(Otvet, config.Settings.TEXT_TEMPLATE_MODEL+"_Read", ModelName+"_"+FuncName) + Otvet = strings.ReplaceAll(Otvet, config.Settings.TEXT_TEMPLATE_MODEL, ModelName) + Otvet = strings.ReplaceAll(Otvet, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name) + Otvet = strings.ReplaceAll(Otvet, "grpc_proto.RequestId", "grpc_proto."+TextRequest) + Otvet = strings.ReplaceAll(Otvet, "ColumnName", ColumnName) + Otvet = strings.ReplaceAll(Otvet, "Model.ID", "Model."+ColumnName) + Otvet = strings.ReplaceAll(Otvet, "Request.ID", "Request."+TextRequestFieldName) + + return Otvet +} diff --git a/internal/create_files/protobuf/protobuf.go b/internal/create_files/protobuf/protobuf.go index 7889c1e..e4d969f 100644 --- a/internal/create_files/protobuf/protobuf.go +++ b/internal/create_files/protobuf/protobuf.go @@ -363,33 +363,6 @@ func FindTextUpdateEveryColumn(TextProto string, Table1 *types.Table, Column1 *t return Otvet } -// FindTextRequest - возвращает имя message из .proto, в зависимости от типа -func FindTextRequest(TypeGo string) string { - Otvet := "RequestID" - - switch TypeGo { - case "int", "int64": - - Otvet = "RequestId" - case "int32": - Otvet = "RequestInt32" - case "string": - - Otvet = "RequestString" - case "time.Time": - - Otvet = "RequestDate" - case "float32": - Otvet = "RequestFloat32" - case "float64": - Otvet = "RequestFloat64" - case "bool": - Otvet = "RequestBool" - } - - return Otvet -} - // TextUpdateEveryColumn - возвращает текст .proto функции Update_ColumnName() func TextUpdateEveryColumn(Table1 *types.Table, Column1 *types.Column) string { Otvet := "" @@ -398,7 +371,7 @@ func TextUpdateEveryColumn(Table1 *types.Table, Column1 *types.Column) string { TextRequest := "RequestID" TypeGo := Column1.TypeGo - TextRequest = FindTextRequest(TypeGo) + TextRequest, _ = create_files.FindTextProtobufRequest(TypeGo) ColumnName := Column1.NameGo Otvet = "rpc " + ModelName + "_Update_" + ColumnName + "(" + TextRequest + ") returns (ResponseEmpty) {}"