From e8a16a50f026717e49a9d40c756e6917ee4a91f0 Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Wed, 8 Nov 2023 13:32:47 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20convert?= =?UTF-8?q?=5Fid.json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/templates/configs/convert_id.json | 2 +- internal/create_files/create_files.go | 46 ++++++++++ .../create_files/grpc_server/grpc_server.go | 3 +- internal/create_files/model/model.go | 91 ++++++++----------- 4 files changed, 89 insertions(+), 53 deletions(-) diff --git a/bin/templates/configs/convert_id.json b/bin/templates/configs/convert_id.json index 739d041..f2f244d 100644 --- a/bin/templates/configs/convert_id.json +++ b/bin/templates/configs/convert_id.json @@ -1,5 +1,5 @@ { - "lawsuit_payments": "alias.PaymentId", + "lawsuit_payments.ID": "alias.PaymentId", "invoice_debt_type_stats.InvoiceID": "alias.InvoiceId", "invoice_debt_type_stats.LawsuitID": "alias.LawsuitId", "contract_black_items.ContractNumber": "alias.ContractNumber", diff --git a/internal/create_files/create_files.go b/internal/create_files/create_files.go index bd1ce11..a3fea8a 100644 --- a/internal/create_files/create_files.go +++ b/internal/create_files/create_files.go @@ -6,6 +6,7 @@ import ( "github.com/ManyakRus/crud_generator/internal/config" "github.com/ManyakRus/crud_generator/internal/mini_func" "github.com/ManyakRus/crud_generator/internal/types" + "github.com/ManyakRus/crud_generator/pkg/dbmeta" "github.com/ManyakRus/starter/log" "github.com/ManyakRus/starter/micro" "github.com/iancoleman/strcase" @@ -334,3 +335,48 @@ func AddImport(Text, URL string) string { return Otvet } + +// FindHasTimeColumn - возвращает true если есть колонка с типом время +func FindHasTimeColumn(Table1 *types.Table) bool { + Otvet := false + + for _, Column1 := range Table1.MapColumns { + SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type] + if ok == false { + log.Panic("GetMappings() ", Column1.Type, " error: not found") + } + if SQLMapping1.GoType == "time.Time" { + Otvet = true + break + } + } + + return Otvet +} + +// AddImportTime - добавляет покет в секцию Import, если его там нет +func AddImportTime(TextModel string, Table1 *types.Table) string { + Otvet := TextModel + + //если уже есть импорт + pos1 := strings.Index(Otvet, `"time"`) + if pos1 >= 0 { + return Otvet + } + + HasTimeColumn := FindHasTimeColumn(Table1) + if HasTimeColumn == false { + return Otvet + } + + // + pos1 = strings.Index(Otvet, "import (") + if pos1 < 0 { + log.Error("not found word: import (") + return TextModel + } + + Otvet = Otvet[:pos1+8] + "\n\t" + `"time"` + Otvet[pos1+8:] + + return Otvet +} diff --git a/internal/create_files/grpc_server/grpc_server.go b/internal/create_files/grpc_server/grpc_server.go index a40c497..8879c67 100644 --- a/internal/create_files/grpc_server/grpc_server.go +++ b/internal/create_files/grpc_server/grpc_server.go @@ -253,7 +253,8 @@ func ConvertID(Text string, Table1 *types.Table) string { Otvet := Text TableName := Table1.Name - TextConvert, ok := types.MapConvertID[TableName] + IDName, _ := create_files.FindPrimaryKeyNameTypeGo(Table1) + TextConvert, ok := types.MapConvertID[TableName+"."+IDName] if ok == false { return Otvet } diff --git a/internal/create_files/model/model.go b/internal/create_files/model/model.go index a3d5b8e..cf5e016 100644 --- a/internal/create_files/model/model.go +++ b/internal/create_files/model/model.go @@ -5,7 +5,6 @@ import ( "github.com/ManyakRus/crud_generator/internal/constants" "github.com/ManyakRus/crud_generator/internal/create_files" "github.com/ManyakRus/crud_generator/internal/types" - "github.com/ManyakRus/crud_generator/pkg/dbmeta" "github.com/ManyakRus/starter/log" "github.com/ManyakRus/starter/micro" "os" @@ -13,6 +12,7 @@ import ( "strings" ) +// CreateAllFiles - создаёт все файлы в папке model func CreateAllFiles(MapAll map[string]*types.Table) error { var err error @@ -27,6 +27,7 @@ func CreateAllFiles(MapAll map[string]*types.Table) error { return err } +// CreateFiles - создаёт 1 файл в папке model func CreateFiles(Table1 *types.Table) error { var err error @@ -58,7 +59,7 @@ func CreateFiles(Table1 *types.Table) error { TextModel := string(bytes) //создание текста - TextModelStruct, ModelName, err := FindTextModelStruct(Table1) + TextModel, TextModelStruct, ModelName, err := FindTextModelStruct(TextModel, Table1) TextModel = ReplaceModelStruct(TextModel, TextModelStruct) // @@ -72,7 +73,7 @@ func CreateFiles(Table1 *types.Table) error { } TextModel = DeleteFuncFind_byExtID(TextModel, ModelName, Table1) - TextModel = AddImportTime(TextModel, Table1) + TextModel = create_files.AddImportTime(TextModel, Table1) TextModel = create_files.DeleteImportModel(TextModel) //запись файла @@ -81,7 +82,8 @@ func CreateFiles(Table1 *types.Table) error { return err } -func FindTextModelStruct(Table1 *types.Table) (string, string, error) { +// FindTextModelStruct - возвращает текст структуры и тегов gorm +func FindTextModelStruct(TextModel string, Table1 *types.Table) (string, string, string, error) { var Otvet string var ModelName string var err error @@ -105,16 +107,18 @@ type ` + ModelName + ` struct { //цикл по всем колонкам for _, key1 := range keys { Column1, _ := Table1.MapColumns[key1] - TextColumn := FindTextColumn(&Column1) + var TextColumn string + TextModel, TextColumn = FindTextColumn(TextModel, Table1, &Column1) Otvet = Otvet + TextColumn + "\n" Table1.MapColumns[key1] = Column1 } Otvet = Otvet + "\n}" - return Otvet, ModelName, err + return TextModel, Otvet, ModelName, err } -func FindTextColumn(Column1 *types.Column) string { +// FindTextColumn - возвращает текст gorm +func FindTextColumn(TextModel string, Table1 *types.Table, Column1 *types.Column) (string, string) { Otvet := "" // Code string `json:"code" gorm:"column:code;default:0"` @@ -128,6 +132,7 @@ func FindTextColumn(Column1 *types.Column) string { //} //Type_go := SQLMapping1.GoType Type_go := Column1.TypeGo + TextModel, Type_go = FindColumnTypeGo(TextModel, Table1, Column1) Column1.TypeGo = Type_go TextDefaultValue := create_files.FindTextDefaultValue(Type_go) TextPrimaryKey := FindTextPrimaryKey(Column1.IsIdentity) @@ -153,9 +158,10 @@ func FindTextColumn(Column1 *types.Column) string { Otvet = Otvet + "`" Otvet = Otvet + "\t//" + Description - return Otvet + return TextModel, Otvet } +// FindTextPrimaryKey - возвращает строку gorm для primaryKey func FindTextPrimaryKey(Is_identity bool) string { Otvet := "" @@ -166,6 +172,7 @@ func FindTextPrimaryKey(Is_identity bool) string { return Otvet } +// ReplaceModelStruct - заменяет структуру модели на новую func ReplaceModelStruct(TextTemplateModel, TextModelStruct string) string { Otvet := "" @@ -256,49 +263,6 @@ func DeleteFuncRestore(TextModel, Modelname string, Table1 *types.Table) string return Otvet } -func AddImportTime(TextModel string, Table1 *types.Table) string { - Otvet := TextModel - - //если уже есть импорт - pos1 := strings.Index(Otvet, `"time"`) - if pos1 >= 0 { - return Otvet - } - - HasTimeColumn := FindHasTimeColumn(Table1) - if HasTimeColumn == false { - return Otvet - } - - // - pos1 = strings.Index(Otvet, "import (") - if pos1 < 0 { - log.Error("not found word: import (") - return TextModel - } - - Otvet = Otvet[:pos1+8] + "\n\t" + `"time"` + Otvet[pos1+8:] - - return Otvet -} - -func FindHasTimeColumn(Table1 *types.Table) bool { - Otvet := false - - for _, Column1 := range Table1.MapColumns { - SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type] - if ok == false { - log.Panic("GetMappings() ", Column1.Type, " error: not found") - } - if SQLMapping1.GoType == "time.Time" { - Otvet = true - break - } - } - - return Otvet -} - // DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID() func DeleteFuncFind_byExtID(TextModel, Modelname string, Table1 *types.Table) string { Otvet := TextModel @@ -332,3 +296,28 @@ func DeleteFuncFind_byExtID(TextModel, Modelname string, Table1 *types.Table) st return Otvet } + +// FindColumnTypeGo - заменяет ID на Alias +func FindColumnTypeGo(TextModel string, Table1 *types.Table, Column1 *types.Column) (string, string) { + Otvet := Column1.TypeGo + + //тип колонки из БД или из convert_id.json + TableName := Table1.Name + IDName := Column1.NameGo + TextConvert, ok := types.MapConvertID[TableName+"."+IDName] + if ok == false { + return TextModel, Otvet + } + + Otvet = TextConvert + + //добавим импорт + URL := create_files.FindURL_Alias() + if URL == "" { + return TextModel, Otvet + } + + TextModel = create_files.AddImport(TextModel, URL) + + return TextModel, Otvet +}