1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2024-11-27 08:40:53 +02:00

сделал uuid

This commit is contained in:
Nikitin Aleksandr 2024-04-24 17:07:09 +03:00
parent 10df6325f7
commit 6572707faf
6 changed files with 129 additions and 27 deletions

View File

@ -30,8 +30,9 @@ func (s *ServerGRPC) LawsuitStatusType_Read(ctx context.Context, Request *grpc_p
//запрос в БД
db := postgres_gorm.GetConnection()
ID := Request.ID
m := &lawsuit_status_types.LawsuitStatusType{}
m.ID = Request.ID
m.ID = ID
err = crud_lawsuit_status_types.Read_ctx(ctx, db, m)
if err != nil {
return &Otvet, err
@ -68,8 +69,9 @@ func (s *ServerGRPC) LawsuitStatusType_Delete(ctx context.Context, Request *grpc
//запрос в БД
db := postgres_gorm.GetConnection()
ID := Request.ID
m := &lawsuit_status_types.LawsuitStatusType{}
m.ID = Request.ID
m.ID = ID
err = crud_lawsuit_status_types.Delete_ctx(ctx, db, m)
if err != nil {
return &Otvet, err
@ -106,8 +108,9 @@ func (s *ServerGRPC) LawsuitStatusType_Restore(ctx context.Context, Request *grp
//запрос в БД
db := postgres_gorm.GetConnection()
ID := Request.ID
m := &lawsuit_status_types.LawsuitStatusType{}
m.ID = Request.ID
m.ID = ID
err = crud_lawsuit_status_types.Restore_ctx(ctx, db, m)
if err != nil {
return &Otvet, err

View File

@ -35,8 +35,9 @@ func (s *ServerGRPC) LawsuitStatusType_ReadFromCache(ctx context.Context, Reques
//запрос в БД
db := postgres_gorm.GetConnection()
ID := Request.ID
Model := lawsuit_status_types.LawsuitStatusType{}
Model, err = crud_lawsuit_status_types.ReadFromCache_ctx(ctx, db, Request.ID)
Model, err = crud_lawsuit_status_types.ReadFromCache_ctx(ctx, db, ID)
if err != nil {
return &Otvet, err
}

View File

@ -24,8 +24,9 @@ func (s *ServerGRPC) LawsuitStatusType_Update(ctx context.Context, Request *grpc
//запрос в БД
value := Request.FieldName
db := postgres_gorm.GetConnection()
ID := Request.ID
Model := &lawsuit_status_types.LawsuitStatusType{}
Model.ID = Request.ID
Model.ID = ID
Model.ColumnName = value
err = crud_lawsuit_status_types.Update_ColumnName_ctx(ctx, db, Model)
if err != nil {

View File

@ -104,7 +104,7 @@ func Test_server_LawsuitStatusType_Create(t *testing.T) {
var ModelString string
Model := lawsuit_status_types.LawsuitStatusType{}
Model.ID = 1
Model.ID = -1
ModelString, err := Model.GetJSON()
if err != nil {
t.Error("Test_server_LawsuitStatusType_Create() error: ", err)

View File

@ -1509,8 +1509,9 @@ func FindTextConvertGolangTypeToProtobufType(Table1 *types.Table, Column1 *types
}
// FindTextConvertProtobufTypeToGolangType - возвращает имя переменной + имя колонки, преобразованное в тип golang из protobuf
func FindTextConvertProtobufTypeToGolangType(Table1 *types.Table, Column1 *types.Column, VariableName string) string {
func FindTextConvertProtobufTypeToGolangType(Table1 *types.Table, Column1 *types.Column, VariableName string) (string, string) {
Otvet := VariableName + Column1.NameGo
GolangCode := ""
TableName := Table1.Name
IDName := Column1.Name
@ -1522,7 +1523,7 @@ func FindTextConvertProtobufTypeToGolangType(Table1 *types.Table, Column1 *types
TextConvert, ok := types.MapConvertID[TableName+"."+IDName]
if ok == true {
Otvet = TextConvert + "(" + VariableName + Column1.NameGo + ")"
return Otvet
return Otvet, GolangCode
}
//time.Time в timestamppb
@ -1530,12 +1531,17 @@ func FindTextConvertProtobufTypeToGolangType(Table1 *types.Table, Column1 *types
case "time.Time":
{
Otvet = VariableName + Column1.NameGo + ".AsTime()"
return Otvet
return Otvet, GolangCode
}
case "uuid.UUID":
{
Otvet = "uuid.FromBytes([]byte(" + VariableName + RequestColumnName + "))"
return Otvet
GolangCode = `ID, err := uuid.FromBytes([]byte(Request.` + RequestColumnName + `))
if err != nil {
return &Otvet, err
}
`
return Otvet, GolangCode
}
}
//if Column1.TypeGo == "time.Time" {
@ -1561,7 +1567,7 @@ func FindTextConvertProtobufTypeToGolangType(Table1 *types.Table, Column1 *types
// Otvet = "float64(" + VariableName + Column1.NameGo + ")"
//}
return Otvet
return Otvet, GolangCode
}
// FindTextEqualEmpty - находит текст сравнение с пустым значением
@ -1682,6 +1688,29 @@ func Replace_Postgres_ID_Test(Text string, Table1 *types.Table) string {
return Otvet
}
// Replace_Model_ID_Test - заменяет текст "const LawsuitStatusType_ID_Test = 0" на нужный ИД
func Replace_Model_ID_Test(Text string, Table1 *types.Table) string {
Otvet := Text
TEXT_TEMPLATE_MODEL := config.Settings.TEXT_TEMPLATE_MODEL
ModelName := Table1.NameGo
TextFind := "const " + TEXT_TEMPLATE_MODEL + "_ID_Test = 0"
ColumnPrimary := FindPrimaryKeyColumn(Table1)
IDMinimum := Table1.IDMinimum
if ColumnPrimary.TypeGo == "uuid.UUID" {
if Table1.IDMinimum == "" {
Otvet = strings.ReplaceAll(Otvet, TextFind, `var `+ModelName+`_ID_Test = ""`)
} else {
Otvet = strings.ReplaceAll(Otvet, TextFind, `var `+ModelName+`_ID_Test = "`+IDMinimum+`"`)
}
} else {
Otvet = strings.ReplaceAll(Otvet, TextFind, TextFind+IDMinimum)
}
return Otvet
}
// ReplaceTextRequestID - заменяет RequestId{} на RequestString{}
func ReplaceTextRequestID(Text string, Table1 *types.Table) string {
Otvet := Text
@ -1700,17 +1729,36 @@ func ReplaceTextRequestID(Text string, Table1 *types.Table) string {
func ReplaceTextRequestID_PrimaryKey(Text string, Table1 *types.Table) string {
Otvet := Text
TextRequest := "Request"
Otvet = ReplaceTextRequestID_PrimaryKey1(Otvet, Table1, TextRequest)
TextRequest = "Request2"
Otvet = ReplaceTextRequestID_PrimaryKey1(Otvet, Table1, TextRequest)
return Otvet
}
// ReplaceTextRequestID_PrimaryKey1 - заменяет RequestId{} на RequestString{}
func ReplaceTextRequestID_PrimaryKey1(Text string, Table1 *types.Table, TextRequest string) string {
Otvet := Text
PrimaryKeyColumn := FindPrimaryKeyColumn(Table1)
TypeGo := PrimaryKeyColumn.TypeGo
TextRequestID, TextID := FindTextProtobufRequestPrimaryKey(Table1, TypeGo)
TextConvertID, GolangCode := FindTextConvertProtobufTypeToGolangType(Table1, PrimaryKeyColumn, "Request.")
if GolangCode != "" {
Otvet = strings.ReplaceAll(Otvet, "ID := "+TextRequest+".ID", GolangCode)
Otvet = strings.ReplaceAll(Otvet, TextRequest+".ID = ", TextRequest+"."+TextID+" = ")
} else {
Otvet = strings.ReplaceAll(Otvet, TextRequest+".ID", TextConvertID)
}
Otvet = strings.ReplaceAll(Otvet, "RequestId{}", TextRequestID+"{}")
Otvet = strings.ReplaceAll(Otvet, "*grpc_proto.RequestId", "*grpc_proto."+TextRequestID)
//Otvet = strings.ReplaceAll(Otvet, "Request.ID", "Request."+TextID)
TextID = FindTextConvertProtobufTypeToGolangType(Table1, PrimaryKeyColumn, "Request.")
Otvet = strings.ReplaceAll(Otvet, "Request.ID", TextID)
return Otvet
}
@ -1739,6 +1787,18 @@ func ReplaceOtvetIDEqual1(Text string, Table1 *types.Table) string {
return Otvet
}
// ReplaceModelIDEqual1 - заменяет Otvet.ID = -1
func ReplaceModelIDEqual1(Text string, Table1 *types.Table) string {
Otvet := Text
PrimaryKeyColumn := FindPrimaryKeyColumn(Table1)
Value := FindNegativeValue(PrimaryKeyColumn.TypeGo)
Otvet = strings.ReplaceAll(Otvet, "Model.ID = -1", "Model.ID = "+Value)
return Otvet
}
// FindNegativeValue - возвращает -1 для числовых типов
func FindNegativeValue(TypeGo string) string {
Otvet := ""
@ -1755,7 +1815,7 @@ func FindNegativeValue(TypeGo string) string {
func FindRequestColumnName(Table1 *types.Table, Column1 *types.Column) string {
Otvet := ""
_, Otvet := FindTextProtobufRequest(Table1, Column1.TypeGo)
_, Otvet = FindTextProtobufRequest(Table1, Column1.TypeGo)
return Otvet
}

View File

@ -34,9 +34,9 @@ func CreateAllFiles(MapAll map[string]*types.Table) error {
//тестовые файлы grpc_server
if config.Settings.NEED_CREATE_GRPC_SERVER_TEST == true {
err = CreateTestFiles(Table1)
err = CreateFilesTest(Table1)
if err != nil {
log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err)
log.Error("CreateFilesTest() table: ", Table1.Name, " error: ", err)
return err
}
}
@ -52,9 +52,9 @@ func CreateAllFiles(MapAll map[string]*types.Table) error {
//тестовые файлы grpc_server update
if config.Settings.NEED_CREATE_GRPC_SERVER_TEST == true {
err = CreateTestFilesUpdateEveryColumn(Table1)
err = CreateFilesUpdateEveryColumnTest(Table1)
if err != nil {
log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err)
log.Error("CreateFilesTest() table: ", Table1.Name, " error: ", err)
return err
}
}
@ -76,7 +76,7 @@ func CreateAllFiles(MapAll map[string]*types.Table) error {
if config.Settings.NEED_CREATE_CACHE_TEST_FILES == true {
err = CreateFilesCacheTest(Table1)
if err != nil {
log.Error("CreateTestFiles() table: ", Table1.Name, " error: ", err)
log.Error("CreateFilesTest() table: ", Table1.Name, " error: ", err)
return err
}
}
@ -164,8 +164,8 @@ func CreateFiles(Table1 *types.Table) error {
return err
}
// CreateTestFiles - создаёт 1 файл в папке grpc_server
func CreateTestFiles(Table1 *types.Table) error {
// CreateFilesTest - создаёт 1 файл в папке grpc_server
func CreateFilesTest(Table1 *types.Table) error {
var err error
//чтение файлов
@ -212,6 +212,19 @@ func CreateTestFiles(Table1 *types.Table) error {
TextGRPCServer = create_files.AddImport(TextGRPCServer, CrudStarterURL)
TextGRPCServer = create_files.CheckAndAddImport(TextGRPCServer, "encoding/json")
//Postgres_ID_Test = ID Minimum
TextGRPCServer = create_files.Replace_Model_ID_Test(TextGRPCServer, Table1)
//замена RequestId{}
TextGRPCServer = create_files.ReplaceTextRequestID_PrimaryKey(TextGRPCServer, Table1)
//замена Otvet.ID = -1
TextGRPCServer = create_files.ReplaceModelIDEqual1(TextGRPCServer, Table1)
//добавим импорт uuid
TextGRPCServer = create_files.CheckAndAddImportUUID_FromText(TextGRPCServer)
}
//создание текста
@ -437,11 +450,15 @@ func CreateFilesUpdateEveryColumn(Table1 *types.Table) error {
TextGRPCServer = create_files.ConvertRequestIdToAlias(TextGRPCServer, Table1)
TextGRPCServer = create_files.CheckAndAddImportAlias(TextGRPCServer)
TextGRPCServer = config.Settings.TEXT_MODULE_GENERATED + TextGRPCServer
}
//import uuid
//замена RequestId{}
TextGRPCServer = create_files.ReplaceTextRequestID_PrimaryKey(TextGRPCServer, Table1)
//добавим импорт uuid
TextGRPCServer = create_files.CheckAndAddImportUUID_FromText(TextGRPCServer)
}
//удаление пустого импорта
TextGRPCServer = create_files.DeleteEmptyImport(TextGRPCServer)
@ -509,8 +526,8 @@ func FindTextUpdateEveryColumn1(TextGRPCServerUpdateFunc string, Table1 *types.T
return Otvet
}
// CreateTestFilesUpdateEveryColumn - создаёт 1 файл в папке grpc_server
func CreateTestFilesUpdateEveryColumn(Table1 *types.Table) error {
// CreateFilesUpdateEveryColumnTest - создаёт 1 файл в папке grpc_server
func CreateFilesUpdateEveryColumnTest(Table1 *types.Table) error {
var err error
//чтение файлов
@ -579,6 +596,12 @@ func CreateTestFilesUpdateEveryColumn(Table1 *types.Table) error {
TextGRPCServer = config.Settings.TEXT_MODULE_GENERATED + TextGRPCServer
//замена RequestId{}
TextGRPCServer = create_files.ReplaceTextRequestID_PrimaryKey(TextGRPCServer, Table1)
//добавим импорт uuid
TextGRPCServer = create_files.CheckAndAddImportUUID_FromText(TextGRPCServer)
//Import Timestamp
TextGRPCServer = create_files.CheckAndAddImportTimestamp_FromText(TextGRPCServer)
@ -694,6 +717,13 @@ func CreateFilesCache(Table1 *types.Table) error {
CrudTableURL := create_files.FindCrudTableURL(TableName)
TextGRPCServer = create_files.AddImport(TextGRPCServer, CrudTableURL)
//замена RequestId{}
TextGRPCServer = create_files.ReplaceTextRequestID_PrimaryKey(TextGRPCServer, Table1)
//добавим импорт uuid
TextGRPCServer = create_files.CheckAndAddImportUUID_FromText(TextGRPCServer)
}
//создание текста
@ -755,6 +785,13 @@ func CreateFilesCacheTest(Table1 *types.Table) error {
CrudStarterURL := create_files.FindCrudStarterURL()
TextGRPCServer = create_files.AddImport(TextGRPCServer, CrudStarterURL)
//замена RequestId{}
TextGRPCServer = create_files.ReplaceTextRequestID_PrimaryKey(TextGRPCServer, Table1)
//добавим импорт uuid
TextGRPCServer = create_files.CheckAndAddImportUUID_FromText(TextGRPCServer)
}
//создание текста