2023-11-20 17:08:58 +03:00
|
|
|
package protobuf
|
2023-10-26 14:18:38 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/config"
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/create_files"
|
2023-11-20 17:08:58 +03:00
|
|
|
"github.com/ManyakRus/crud_generator/internal/folders"
|
2023-10-26 14:18:38 +03:00
|
|
|
"github.com/ManyakRus/crud_generator/internal/types"
|
2024-05-13 18:07:32 +03:00
|
|
|
"github.com/ManyakRus/crud_generator/pkg/dbmeta"
|
2023-10-26 14:18:38 +03:00
|
|
|
"github.com/ManyakRus/starter/log"
|
|
|
|
"github.com/ManyakRus/starter/micro"
|
|
|
|
"os"
|
|
|
|
"sort"
|
2024-06-04 14:39:37 +03:00
|
|
|
"strconv"
|
2023-10-26 14:18:38 +03:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-11-15 11:43:15 +03:00
|
|
|
// CreateAllFiles - создаёт все файлы в папке grpc proto
|
2023-10-26 14:18:38 +03:00
|
|
|
func CreateAllFiles(MapAll map[string]*types.Table) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
err = CreateFileProto(MapAll)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("CreateFileProto() error: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFileProto - создаёт 1 файл в папке grpc
|
|
|
|
func CreateFileProto(MapAll map[string]*types.Table) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
//чтение файлов
|
|
|
|
DirBin := micro.ProgramDir_bin()
|
2023-11-01 17:34:57 +03:00
|
|
|
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
|
2023-11-15 13:58:31 +03:00
|
|
|
DirTemplatesProto := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_PROTO + micro.SeparatorFile()
|
2024-11-21 16:04:41 +03:00
|
|
|
DirReadyProto := DirReady + config.Settings.FOLDERNAME_API + micro.SeparatorFile()
|
2023-10-26 14:18:38 +03:00
|
|
|
FilenameReadyProto := DirReadyProto + config.Settings.SERVICE_NAME + ".proto"
|
|
|
|
|
2023-11-20 18:02:26 +03:00
|
|
|
//создадим папку готовых файлов
|
|
|
|
folders.CreateFolder(DirReadyProto)
|
|
|
|
|
2024-11-21 16:04:41 +03:00
|
|
|
//создадим папку grpc_proto
|
|
|
|
folders.CreateFolder(DirReady + config.Settings.FOLDERNAME_GRPC_PROTO)
|
|
|
|
|
|
|
|
//
|
2023-11-15 17:59:21 +03:00
|
|
|
FilenameTemplateProto := DirTemplatesProto + "service.proto_"
|
2024-01-17 14:13:10 +03:00
|
|
|
if config.Settings.TEMPLATE_EXTERNAL_PROTO_FILENAME != "" {
|
|
|
|
FilenameTemplateProto = config.Settings.TEMPLATE_EXTERNAL_PROTO_FILENAME
|
|
|
|
}
|
2023-10-26 14:18:38 +03:00
|
|
|
bytes, err := os.ReadFile(FilenameTemplateProto)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("ReadFile() ", FilenameTemplateProto, " error: ", err)
|
|
|
|
}
|
|
|
|
TextProto := string(bytes)
|
|
|
|
|
|
|
|
//заменим название сервиса
|
2023-11-07 17:58:02 +03:00
|
|
|
ServiceName := config.Settings.SERVICE_NAME
|
2023-11-20 17:08:58 +03:00
|
|
|
ServiceNameProto := micro.StringFromUpperCase(ServiceName)
|
|
|
|
TEMPLATE_SERVICE_NAME := config.Settings.TEMPLATE_SERVICE_NAME
|
|
|
|
TextProto = strings.ReplaceAll(TextProto, TEMPLATE_SERVICE_NAME, ServiceNameProto)
|
|
|
|
//заменим ещё раз с большой буквы
|
|
|
|
TEMPLATE_SERVICE_NAME = micro.StringFromUpperCase(TEMPLATE_SERVICE_NAME)
|
|
|
|
TextProto = strings.ReplaceAll(TextProto, TEMPLATE_SERVICE_NAME, ServiceNameProto)
|
2024-11-21 16:04:41 +03:00
|
|
|
TextGrpcProto := create_files.TextProto()
|
|
|
|
TextProto = strings.ReplaceAll(TextProto, "/grpc_proto", "/"+TextGrpcProto)
|
2023-10-26 14:18:38 +03:00
|
|
|
|
2024-11-29 09:47:04 +03:00
|
|
|
////сортировка по названию таблиц
|
|
|
|
//keys := make([]string, 0, len(MapAll))
|
|
|
|
//for k := range MapAll {
|
|
|
|
// keys = append(keys, k)
|
|
|
|
//}
|
|
|
|
//sort.Strings(keys)
|
2023-10-26 14:18:38 +03:00
|
|
|
|
2024-11-18 15:17:44 +03:00
|
|
|
//найдём куда вставить текст
|
|
|
|
sFind := "\nservice "
|
|
|
|
PosProtoService := strings.Index(TextProto, sFind)
|
|
|
|
if PosProtoService < 0 {
|
|
|
|
log.Panic("Not found text ", sFind)
|
|
|
|
}
|
|
|
|
|
|
|
|
s2 := TextProto[PosProtoService+1:]
|
|
|
|
sFind = "\n"
|
|
|
|
posEnd := strings.Index(s2, sFind)
|
|
|
|
if posEnd < 0 {
|
|
|
|
log.Panic("Not found text ", sFind)
|
|
|
|
}
|
|
|
|
PosProtoService = PosProtoService + posEnd + 1
|
|
|
|
|
2024-11-29 09:47:04 +03:00
|
|
|
//сортировка по названию таблиц, обратная
|
|
|
|
MassAll := micro.MassFrom_Map_DESC(MapAll)
|
|
|
|
|
2023-10-26 14:18:38 +03:00
|
|
|
//найдём новый текст для каждой таблицы
|
2024-11-29 09:47:04 +03:00
|
|
|
for _, Table1 := range MassAll {
|
|
|
|
//Table1, ok := MapAll[key1]
|
|
|
|
//if ok == false {
|
|
|
|
// log.Panic("MapAll[key1] not found")
|
|
|
|
//}
|
2023-11-03 14:21:35 +03:00
|
|
|
|
2024-06-04 14:39:37 +03:00
|
|
|
//проверка что таблица нормальная
|
2024-09-06 15:10:00 +03:00
|
|
|
err1 := create_files.IsGood_Table(Table1)
|
2024-06-04 18:03:27 +03:00
|
|
|
if err1 != nil {
|
|
|
|
log.Warn(err1)
|
2024-06-04 14:39:37 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-11-18 15:17:44 +03:00
|
|
|
//Найдём место куда добавить текст
|
|
|
|
TextProtoNew := ""
|
|
|
|
Text1 := ""
|
|
|
|
|
|
|
|
//
|
|
|
|
Text1 = Find_CommentModel(TextProto, Table1)
|
|
|
|
TextProtoNew = TextProtoNew + Text1
|
|
|
|
//TextProto = micro.InsertTextFrom(TextProto, Text1, PosProtoService)
|
|
|
|
|
|
|
|
//
|
2024-09-17 14:41:37 +03:00
|
|
|
TextProtoNew = TextProtoNew + FindText_ProtoTable1(TextProto, Table1)
|
2024-11-18 15:17:44 +03:00
|
|
|
|
|
|
|
//
|
2024-09-17 14:41:37 +03:00
|
|
|
TextProtoNew = TextProtoNew + FindText_ProtoTable1_UpdateManyFields(TextProto, Table1)
|
2024-11-18 15:17:44 +03:00
|
|
|
|
|
|
|
//
|
2024-09-17 14:41:37 +03:00
|
|
|
TextProtoNew = TextProtoNew + FindText_ProtoTable1_UpdateEveryColumn(TextProto, Table1)
|
2024-03-06 17:45:55 +03:00
|
|
|
|
2024-08-16 12:44:58 +03:00
|
|
|
//добавим текст FindBy
|
2024-09-26 14:27:25 +03:00
|
|
|
TextProtoNew1 := ""
|
|
|
|
TextProto, TextProtoNew1 = FindText_FindBy(TextProto, Table1)
|
2024-08-21 13:25:41 +03:00
|
|
|
TextProtoNew = TextProtoNew + TextProtoNew1
|
2024-08-16 12:44:58 +03:00
|
|
|
|
|
|
|
//добавим текст FindMassBy
|
2024-08-21 13:25:41 +03:00
|
|
|
TextProto, TextProtoNew1 = FindText_FindMassBy(TextProto, Table1)
|
2024-11-18 15:17:44 +03:00
|
|
|
Text1 = TextProtoNew1
|
|
|
|
TextProtoNew = TextProtoNew + Text1
|
2024-08-16 12:44:58 +03:00
|
|
|
|
2024-09-17 16:50:01 +03:00
|
|
|
//добавим текст ReadAll
|
|
|
|
TextProto, TextProtoNew1 = FindText_ReadAll(TextProto, Table1)
|
2024-11-18 15:17:44 +03:00
|
|
|
Text1 = TextProtoNew1
|
|
|
|
TextProtoNew = TextProtoNew + Text1
|
2024-09-17 16:50:01 +03:00
|
|
|
|
2024-10-15 16:39:32 +03:00
|
|
|
//добавим текст FindModelBy
|
2024-10-17 14:14:00 +03:00
|
|
|
TextProto, TextProtoNew1 = FindText_FindModelBy(MapAll, TextProto, Table1)
|
2024-11-18 15:17:44 +03:00
|
|
|
Text1 = TextProtoNew1
|
|
|
|
TextProtoNew = TextProtoNew + Text1
|
2024-10-15 16:39:32 +03:00
|
|
|
|
2024-11-06 13:33:26 +03:00
|
|
|
//cache
|
2024-03-06 17:45:55 +03:00
|
|
|
if config.Settings.NEED_CREATE_CACHE_API == true {
|
2024-11-18 15:17:44 +03:00
|
|
|
Text1 = FindText_ProtoTable1_Cache(TextProto, Table1)
|
|
|
|
TextProtoNew = TextProtoNew + Text1
|
2024-03-06 17:45:55 +03:00
|
|
|
}
|
2024-11-06 13:33:26 +03:00
|
|
|
|
|
|
|
//ReadObject
|
|
|
|
if config.Settings.NEED_CREATE_READOBJECT == true {
|
|
|
|
TextProto, TextProtoNew1 = FindText_ReadObject(TextProto, Table1)
|
2024-11-18 15:17:44 +03:00
|
|
|
TextProtoNew = TextProtoNew + TextProtoNew1
|
2024-11-06 13:33:26 +03:00
|
|
|
}
|
2024-11-18 15:17:44 +03:00
|
|
|
|
|
|
|
//TextProtoNew = TextProtoNew + TextProtoNew1
|
2024-11-06 13:33:26 +03:00
|
|
|
|
|
|
|
//
|
2024-05-13 18:07:32 +03:00
|
|
|
TextProto = AddTextMessageRequestID(TextProto, Table1)
|
2023-10-26 14:18:38 +03:00
|
|
|
|
2024-11-18 15:17:44 +03:00
|
|
|
//
|
|
|
|
PosProto_ForInsert := FindPosProto_ForInsert(TextProto, Table1, PosProtoService)
|
|
|
|
TextProto = micro.InsertTextFrom(TextProto, TextProtoNew, PosProto_ForInsert)
|
|
|
|
//TextProto = TextProto[:PosProto_ForInsert] + TextProtoNew + TextProto[PosProtoService:]
|
2023-10-26 14:18:38 +03:00
|
|
|
}
|
2024-02-08 12:28:21 +03:00
|
|
|
|
|
|
|
//
|
2023-10-26 14:18:38 +03:00
|
|
|
|
2024-01-15 13:19:27 +03:00
|
|
|
//
|
2024-09-06 15:10:00 +03:00
|
|
|
TextProto = create_files.Delete_EmptyLines(TextProto)
|
2024-01-15 13:19:27 +03:00
|
|
|
|
2023-10-26 14:18:38 +03:00
|
|
|
//запись файла
|
2024-09-23 13:33:40 +03:00
|
|
|
err = os.WriteFile(FilenameReadyProto, []byte(TextProto), config.Settings.FILE_PERMISSIONS)
|
2023-10-26 14:18:38 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//func FillTableProto1(TextProto string, Table1 *types.Table) string {
|
|
|
|
// Otvet := TextProto
|
|
|
|
//
|
|
|
|
// //ModelName := Table1.NameGo
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// //создание текста
|
2024-09-17 14:41:37 +03:00
|
|
|
// TextProtoTable := FindText_ProtoTable1(Table1)
|
2023-10-26 14:18:38 +03:00
|
|
|
//
|
|
|
|
// return Otvet
|
|
|
|
//}
|
|
|
|
|
2024-06-04 14:39:37 +03:00
|
|
|
// AddTextMessageRequestID1 - в текст .proto добавляет message с RequestID
|
2024-05-13 18:07:32 +03:00
|
|
|
func AddTextMessageRequestID1(Text string, Table1 *types.Table) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
|
|
|
//найдём текст RequestID
|
2024-09-06 15:10:00 +03:00
|
|
|
PrimaryKeyColumn := create_files.Find_PrimaryKeyColumn(Table1)
|
2024-05-13 18:07:32 +03:00
|
|
|
if PrimaryKeyColumn == nil {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
2024-09-06 15:10:00 +03:00
|
|
|
TextRequest, TextFieldName := create_files.FindText_ProtobufRequest(Table1)
|
2024-05-13 18:07:32 +03:00
|
|
|
|
|
|
|
//найдём уже есть message
|
|
|
|
TextFind := "message " + TextRequest + " {"
|
|
|
|
pos1 := strings.Index(Otvet, TextFind)
|
|
|
|
if pos1 >= 0 {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
//найдём ProtobufTypePK
|
|
|
|
MappingPK, ok := dbmeta.GetMappings()[PrimaryKeyColumn.Type]
|
|
|
|
if ok == false {
|
|
|
|
log.Error("Неизвестный тип столбца " + PrimaryKeyColumn.Type)
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
ProtobufTypePK := MappingPK.ProtobufType
|
|
|
|
|
|
|
|
//добавим message
|
|
|
|
TextMessage := `
|
|
|
|
// ` + TextRequest + ` - параметры запроса на сервер
|
|
|
|
message ` + TextRequest + ` {
|
2024-08-21 13:25:41 +03:00
|
|
|
uint32 VersionModel = 1; //версия структуры модели
|
|
|
|
` + ProtobufTypePK + ` ` + TextFieldName + ` = 2; // id записи в БД
|
2024-05-13 18:07:32 +03:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
Otvet = Otvet + TextMessage
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
2024-06-04 14:39:37 +03:00
|
|
|
// AddTextMessageRequestID_ManyPK - в текст .proto добавляет message с RequestID
|
|
|
|
func AddTextMessageRequestID_ManyPK(Text string, Table1 *types.Table) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
|
|
|
//найдём текст RequestID
|
2024-09-06 15:10:00 +03:00
|
|
|
PrimaryKeyColumns := create_files.Find_PrimaryKeyColumns(Table1)
|
2024-06-04 14:39:37 +03:00
|
|
|
if len(PrimaryKeyColumns) == 0 {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
Otvet = AddTextMessageRequestID_ColumnType_ManyPK(Otvet, Table1, PrimaryKeyColumns[0])
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
2024-05-13 18:07:32 +03:00
|
|
|
// AddTextMessageRequestID_ColumnType - в текст .proto добавляет message с RequestID_Int64
|
|
|
|
func AddTextMessageRequestID_ColumnType(Text string, Table1 *types.Table, Column1 *types.Column) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
2024-06-04 14:39:37 +03:00
|
|
|
if Table1.PrimaryKeyColumnsCount == 1 {
|
|
|
|
Otvet = AddTextMessageRequestID_ColumnType1(Otvet, Table1, Column1)
|
|
|
|
} else {
|
|
|
|
Otvet = AddTextMessageRequestID_ColumnType_ManyPK(Otvet, Table1, Column1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTextMessageRequestID_ColumnType1 - в текст .proto добавляет message с RequestID_Int64, 1PK
|
|
|
|
func AddTextMessageRequestID_ColumnType1(Text string, Table1 *types.Table, Column1 *types.Column) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
2024-05-13 18:07:32 +03:00
|
|
|
//найдём текст RequestID
|
2024-09-06 15:10:00 +03:00
|
|
|
PrimaryKeyColumn := create_files.Find_PrimaryKeyColumn(Table1)
|
2024-05-13 18:07:32 +03:00
|
|
|
if PrimaryKeyColumn == nil {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
//
|
2024-09-06 15:10:00 +03:00
|
|
|
_, FieldNamePK := create_files.FindText_ProtobufRequest(Table1)
|
2024-05-13 18:07:32 +03:00
|
|
|
|
2024-09-06 15:10:00 +03:00
|
|
|
TextRequest, FieldName, _, _ := create_files.FindText_ProtobufRequest_ID_Type(Table1, Column1, "_")
|
2024-05-13 18:07:32 +03:00
|
|
|
|
|
|
|
//найдём уже есть message
|
|
|
|
TextFind := "message " + TextRequest + " {"
|
|
|
|
pos1 := strings.Index(Otvet, TextFind)
|
|
|
|
if pos1 >= 0 {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
//найдём ProtobufTypePK
|
|
|
|
MappingPK, ok := dbmeta.GetMappings()[PrimaryKeyColumn.Type]
|
|
|
|
if ok == false {
|
|
|
|
log.Error("Неизвестный тип столбца " + PrimaryKeyColumn.Type)
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
ProtobufTypePK := MappingPK.ProtobufType
|
|
|
|
|
|
|
|
//найдём ProtobufTypeColumn
|
|
|
|
Mapping1, ok := dbmeta.GetMappings()[Column1.Type]
|
|
|
|
if ok == false {
|
|
|
|
log.Error("Неизвестный тип столбца " + Column1.Type)
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
ProtobufTypeColumn := Mapping1.ProtobufType
|
|
|
|
|
|
|
|
//добавим message
|
|
|
|
TextMessage := `
|
|
|
|
// ` + TextRequest + ` - параметры запроса на сервер
|
|
|
|
message ` + TextRequest + ` {
|
2024-08-21 13:25:41 +03:00
|
|
|
uint32 VersionModel = 1; //версия структуры модели
|
|
|
|
` + ProtobufTypePK + ` ` + FieldNamePK + ` = 2; // id записи в БД
|
2024-05-13 18:07:32 +03:00
|
|
|
` + ProtobufTypeColumn + ` ` + FieldName + ` = 3; // значение поиска
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
Otvet = Otvet + TextMessage
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
2024-06-04 14:39:37 +03:00
|
|
|
// AddTextMessageRequestID_ColumnType_ManyPK - в текст .proto добавляет message с RequestID_Int64, много PK
|
|
|
|
func AddTextMessageRequestID_ColumnType_ManyPK(Text string, Table1 *types.Table, Column1 *types.Column) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
|
|
|
//найдём текст RequestID
|
2024-09-06 15:10:00 +03:00
|
|
|
PrimaryKeyColumns := create_files.Find_PrimaryKeyColumns(Table1)
|
2024-06-04 14:39:37 +03:00
|
|
|
if len(PrimaryKeyColumns) == 0 {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
//
|
|
|
|
|
2024-09-06 15:10:00 +03:00
|
|
|
TextRequest := create_files.FindText_ProtobufRequest_Column_ManyPK(Table1, Column1)
|
2024-06-04 14:39:37 +03:00
|
|
|
|
|
|
|
//найдём уже есть message
|
|
|
|
TextFind := "message " + TextRequest + " {"
|
|
|
|
pos1 := strings.Index(Otvet, TextFind)
|
|
|
|
if pos1 >= 0 {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
TextMessage := `
|
|
|
|
// ` + TextRequest + ` - параметры запроса на сервер
|
|
|
|
message ` + TextRequest + ` {
|
2024-08-21 13:25:41 +03:00
|
|
|
uint32 VersionModel = 1; //версия структуры модели`
|
2024-06-04 14:39:37 +03:00
|
|
|
|
|
|
|
//заполним строки про PrimaryKey
|
|
|
|
isPrimaryKey := false
|
|
|
|
Number := 1
|
|
|
|
for _, ColumnPK1 := range PrimaryKeyColumns {
|
|
|
|
Number = Number + 1
|
|
|
|
sNumber := strconv.Itoa(Number)
|
|
|
|
|
|
|
|
if Column1 == ColumnPK1 {
|
|
|
|
isPrimaryKey = true
|
|
|
|
}
|
|
|
|
|
|
|
|
//найдём ProtobufTypePK
|
|
|
|
MappingPK, ok := dbmeta.GetMappings()[ColumnPK1.Type]
|
|
|
|
if ok == false {
|
|
|
|
log.Error("Неизвестный тип столбца " + ColumnPK1.Type)
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
ProtobufTypePK := MappingPK.ProtobufType
|
2024-09-06 15:10:00 +03:00
|
|
|
_, FieldNamePK, _, _ := create_files.FindText_ProtobufRequest_ID_Type(Table1, ColumnPK1, "")
|
2024-06-04 14:39:37 +03:00
|
|
|
|
|
|
|
//добавим message
|
|
|
|
TextMessage = TextMessage + `
|
2024-08-21 13:25:41 +03:00
|
|
|
` + ProtobufTypePK + ` ` + FieldNamePK + ` = ` + sNumber + `; //id записи в БД`
|
2024-06-04 14:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//заполним строку про Column1
|
|
|
|
if isPrimaryKey == false {
|
|
|
|
|
|
|
|
//найдём ProtobufTypeColumn
|
|
|
|
Mapping1, ok := dbmeta.GetMappings()[Column1.Type]
|
|
|
|
if ok == false {
|
|
|
|
log.Error("Неизвестный тип столбца " + Column1.Type)
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
ProtobufTypeColumn := Mapping1.ProtobufType
|
2024-09-06 15:10:00 +03:00
|
|
|
_, FieldName, _, _ := create_files.FindText_ProtobufRequest_ID_Type(Table1, Column1, "")
|
2024-06-04 14:39:37 +03:00
|
|
|
Number = Number + 1
|
|
|
|
sNumber := strconv.Itoa(Number)
|
|
|
|
|
|
|
|
TextMessage = TextMessage + `
|
|
|
|
` + ProtobufTypeColumn + ` ` + FieldName + ` = ` + sNumber + `; //значение поиска`
|
|
|
|
}
|
|
|
|
|
|
|
|
TextMessage = TextMessage + `
|
|
|
|
}`
|
|
|
|
Otvet = Otvet + "\n" + TextMessage
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
2024-08-16 12:44:58 +03:00
|
|
|
// AddTextMessageRequestID_Columns - в текст .proto добавляет message из присланных колонок
|
|
|
|
func AddTextMessageRequestID_Columns(Text string, Columns []*types.Column) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
2024-09-06 15:10:00 +03:00
|
|
|
TextRequest := "Request_" + create_files.Find_RequestFieldNames_FromMass(Columns)
|
2024-08-16 12:44:58 +03:00
|
|
|
|
|
|
|
//найдём уже есть message
|
|
|
|
TextFind := "message " + TextRequest + " {"
|
|
|
|
pos1 := strings.Index(Otvet, TextFind)
|
|
|
|
if pos1 >= 0 {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
TextMessage := `
|
|
|
|
// ` + TextRequest + ` - параметры запроса на сервер
|
|
|
|
message ` + TextRequest + ` {
|
2024-08-21 13:25:41 +03:00
|
|
|
uint32 VersionModel = 1; //версия структуры модели`
|
2024-08-16 12:44:58 +03:00
|
|
|
|
2024-08-21 13:25:41 +03:00
|
|
|
//
|
|
|
|
for i, Column1 := range Columns {
|
2024-09-26 14:27:25 +03:00
|
|
|
ProtoType := create_files.Convert_GolangTypeNameToProtobufTypeName(Column1.TypeGo)
|
2024-09-06 15:10:00 +03:00
|
|
|
ProtoName := create_files.Find_RequestFieldName_FromMass(Column1, Columns)
|
2024-08-21 13:25:41 +03:00
|
|
|
TextMessage = TextMessage + `
|
2024-09-26 14:27:25 +03:00
|
|
|
` + ProtoType + ` ` + ProtoName + ` = ` + strconv.Itoa(i+2) + `; //значение поиска`
|
2024-08-21 13:25:41 +03:00
|
|
|
}
|
2024-08-16 12:44:58 +03:00
|
|
|
|
2024-10-15 16:39:32 +03:00
|
|
|
TextMessage = TextMessage + `
|
|
|
|
}`
|
|
|
|
Otvet = TextMessage
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTextMessageRequestModel_Column - в текст .proto добавляет message из присланных колонок
|
|
|
|
func AddTextMessageRequestModel_Column(Text string, Column1 *types.Column) string {
|
|
|
|
Otvet := Text
|
|
|
|
|
|
|
|
ProtoName := create_files.Convert_GolangTypeNameToProtobufFieldName(Column1.TypeGo)
|
|
|
|
TextRequest := "Request_Model_" + ProtoName
|
|
|
|
|
|
|
|
//найдём уже есть message
|
|
|
|
TextFind := "message " + TextRequest + " {"
|
|
|
|
pos1 := strings.Index(Otvet, TextFind)
|
|
|
|
if pos1 >= 0 {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
TextMessage := `
|
|
|
|
// ` + TextRequest + ` - параметры запроса на сервер
|
|
|
|
message ` + TextRequest + ` {
|
|
|
|
uint32 VersionModel = 1; //версия структуры модели
|
|
|
|
string ModelString = 2; //объект-модель в формате json
|
|
|
|
`
|
|
|
|
|
|
|
|
//
|
|
|
|
//for i, Column1 := range Columns {
|
|
|
|
ProtoType := create_files.Convert_GolangTypeNameToProtobufTypeName(Column1.TypeGo)
|
|
|
|
//ProtoName := create_files.Find_RequestFieldName(Table1, Column1)
|
|
|
|
TextMessage = TextMessage + "\t" + ProtoType + ` ` + ProtoName + ` = 3; //значение поиска`
|
|
|
|
//}
|
|
|
|
|
2024-08-16 12:44:58 +03:00
|
|
|
TextMessage = TextMessage + `
|
|
|
|
}`
|
|
|
|
Otvet = Otvet + "\n" + TextMessage
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
2024-06-04 14:39:37 +03:00
|
|
|
// AddTextMessageRequestID - возвращает текст в .proto для таблицы
|
2024-05-13 18:07:32 +03:00
|
|
|
func AddTextMessageRequestID(TextProto string, Table1 *types.Table) string {
|
|
|
|
Otvet := TextProto //"\n\t//\n"
|
|
|
|
|
2024-06-04 14:39:37 +03:00
|
|
|
Otvet = AddTextMessageRequestID_ManyPK(Otvet, Table1)
|
2024-05-13 18:07:32 +03:00
|
|
|
|
|
|
|
//сортировка по названию колонок
|
|
|
|
keys := make([]string, 0, len(Table1.MapColumns))
|
|
|
|
for k := range Table1.MapColumns {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
//для каждой колонки добавим добавим message RequestId_Int64
|
|
|
|
for _, key1 := range keys {
|
|
|
|
Column1, ok := Table1.MapColumns[key1]
|
|
|
|
if ok == false {
|
2024-09-17 14:41:37 +03:00
|
|
|
log.Panic("FindText_ProtoTable1_UpdateEveryColumn() Table1.MapColumns[key1] = false")
|
2024-05-13 18:07:32 +03:00
|
|
|
}
|
|
|
|
|
2024-06-04 14:39:37 +03:00
|
|
|
Otvet = AddTextMessageRequestID_ColumnType_ManyPK(Otvet, Table1, Column1)
|
2024-05-13 18:07:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
2024-11-18 15:17:44 +03:00
|
|
|
|
|
|
|
// Find_CommentModel - возвращает комментарий //ИмяМодели
|
|
|
|
func Find_CommentModel(TextProto string, Table1 *types.Table) string {
|
|
|
|
Otvet := ""
|
|
|
|
|
|
|
|
TextComment := "\n\t//" + Table1.NameGo + "\n"
|
|
|
|
pos1 := strings.Index(TextProto, TextComment)
|
|
|
|
if pos1 >= 0 {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
Otvet = TextComment
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindPosAfterCommentModel - возвращает позицию комментария //ИмяМодели
|
|
|
|
func FindPosAfterCommentModel(TextProto string, Table1 *types.Table) int {
|
|
|
|
//найдём начало где комментарий
|
|
|
|
TextComment := "\t//" + Table1.NameGo + "\n"
|
|
|
|
pos1 := strings.Index(TextProto, TextComment)
|
|
|
|
if pos1 < 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
pos1 = pos1 + len(TextComment)
|
|
|
|
|
|
|
|
//найдём конец "\n\n"
|
|
|
|
s2 := TextProto[pos1:]
|
|
|
|
TextFind := "\n\n"
|
|
|
|
pos2 := strings.Index(s2, TextFind)
|
|
|
|
if pos2 < 0 {
|
|
|
|
return pos1
|
|
|
|
}
|
|
|
|
pos2 = pos1 + pos2 + len(TextFind) - 2
|
|
|
|
|
|
|
|
return pos2
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindPosProto_ForInsert - возвращает позицию .proto для вставки, = ищет "//ИмяМодели\т" или "service"
|
|
|
|
func FindPosProto_ForInsert(TextProto string, Table1 *types.Table, PosProtoService int) int {
|
|
|
|
Otvet := PosProtoService
|
|
|
|
|
|
|
|
PosProtoTable := FindPosAfterCommentModel(TextProto, Table1)
|
|
|
|
Otvet = micro.Max(PosProtoTable, PosProtoService)
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|