From 55bdf2adee52353377df0c8659ab0d99885eb4f9 Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Wed, 25 Oct 2023 11:41:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/create_files/create_files.go | 10 +++ internal/create_files/model/model.go | 87 +++++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 5 deletions(-) diff --git a/internal/create_files/create_files.go b/internal/create_files/create_files.go index a4070af..9a553cb 100644 --- a/internal/create_files/create_files.go +++ b/internal/create_files/create_files.go @@ -1,6 +1,7 @@ package create_files import ( + "github.com/iancoleman/strcase" "github.com/jinzhu/inflection" ) @@ -16,3 +17,12 @@ func FindSingularName(s string) string { return Otvet } + +// FormatName - возвращает наименование в формате CamelCase +func FormatName(Name string) string { + Otvet := Name + + Otvet = strcase.ToCamel(Otvet) + + return Otvet +} diff --git a/internal/create_files/model/model.go b/internal/create_files/model/model.go index f544a87..e7170aa 100644 --- a/internal/create_files/model/model.go +++ b/internal/create_files/model/model.go @@ -5,6 +5,7 @@ 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/micro" "log" "os" @@ -42,22 +43,98 @@ func CreateModelFiles1(Table1 *types.Table) error { } TextTemplateModel := string(bytes) - TextModelStruct, err := FindTextModelStruct(Table1) + TextModelStruct, ModelName, err := FindTextModelStruct(Table1) + TextTemplateModel = ReplaceModelStruct(TextTemplateModel, TextModelStruct) return err } -func FindTextModelStruct(Table1 *types.Table) (string, error) { +func FindTextModelStruct(Table1 *types.Table) (string, string, error) { var Otvet string + var ModelName string var err error TableName := Table1.Name - ModelName := create_files.FindSingularName(TableName) + ModelName = create_files.FindSingularName(TableName) + ModelName = create_files.FormatName(ModelName) Otvet = `// ` + ModelName + ` - model from table ` + TableName + ` type ` + ModelName + ` struct { ` + for _, Column1 := range Table1.MapColumns { + TextColumn := FindTextColumn(Column1) + Otvet = Otvet + TextColumn + "\n" + } - Otvet = Otvet + "}" - return Otvet, err + Otvet = Otvet + "\n}" + return Otvet, ModelName, err +} + +func FindTextColumn(Column1 types.Column) string { + Otvet := "" + // Code string `json:"code" gorm:"column:code;default:0"` + + ColumnName := Column1.Name + ColumnModelName := create_files.FormatName(Column1.Name) + SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type] + if ok == false { + log.Panic("GetMappings() ", Column1.Type, " error: not found") + } + Type_go := SQLMapping1.GoType + TextDefaultValue := FindTextDefaultValue(Type_go) + TextPrimaryKey := FindTextPrimaryKey(Column1.Is_identity) + + Otvet = Otvet + "\t" + ColumnModelName + " " + Type_go + " `json:\"" + ColumnName + "\"" + "gorm:\"column:" + ColumnName + TextPrimaryKey + TextDefaultValue + Otvet = Otvet + "`" + + return Otvet +} + +func FindTextDefaultValue(Type_go string) string { + var Otvet string + + sValue := "" + switch Type_go { + case "string": + sValue = "\"\"" + case "int", "int32", "int64", "float32", "float64", "uint", "uint32", "uint64": + sValue = "0" + case "time.Time": + sValue = "null" + } + + if sValue != "" { + Otvet = ";default:" + sValue + } + + return Otvet +} + +func FindTextPrimaryKey(Is_identity bool) string { + Otvet := "" + + if Is_identity == true { + Otvet = ";primaryKey;autoIncrement:true" + } + + return Otvet +} + +func ReplaceModelStruct(TextTemplateModel, TextModelStruct string) string { + Otvet := "" + + ModelName := config.Settings.TEXT_TEMPLATE_MODEL + + TextFind1 := "// " + ModelName + pos1 := strings.Index(TextTemplateModel, TextFind1) + if pos1 == 0 { + TextFind1 := "type " + ModelName + " struct {" + pos1 = strings.Index(TextTemplateModel, TextFind1) + } + + if pos1 == 0 { + log.Panic("ReplaceModelStruct() error: in model.go_ not found text: ", TextFind1) + } + + return Otvet }