mirror of
https://github.com/ManyakRus/crud_generator.git
synced 2024-12-22 00:36:41 +02:00
новый
This commit is contained in:
parent
a6ea64ee3e
commit
55bdf2adee
@ -1,6 +1,7 @@
|
|||||||
package create_files
|
package create_files
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/iancoleman/strcase"
|
||||||
"github.com/jinzhu/inflection"
|
"github.com/jinzhu/inflection"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,3 +17,12 @@ func FindSingularName(s string) string {
|
|||||||
|
|
||||||
return Otvet
|
return Otvet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FormatName - возвращает наименование в формате CamelCase
|
||||||
|
func FormatName(Name string) string {
|
||||||
|
Otvet := Name
|
||||||
|
|
||||||
|
Otvet = strcase.ToCamel(Otvet)
|
||||||
|
|
||||||
|
return Otvet
|
||||||
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/ManyakRus/crud_generator/internal/constants"
|
"github.com/ManyakRus/crud_generator/internal/constants"
|
||||||
"github.com/ManyakRus/crud_generator/internal/create_files"
|
"github.com/ManyakRus/crud_generator/internal/create_files"
|
||||||
"github.com/ManyakRus/crud_generator/internal/types"
|
"github.com/ManyakRus/crud_generator/internal/types"
|
||||||
|
"github.com/ManyakRus/crud_generator/pkg/dbmeta"
|
||||||
"github.com/ManyakRus/starter/micro"
|
"github.com/ManyakRus/starter/micro"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -42,22 +43,98 @@ func CreateModelFiles1(Table1 *types.Table) error {
|
|||||||
}
|
}
|
||||||
TextTemplateModel := string(bytes)
|
TextTemplateModel := string(bytes)
|
||||||
|
|
||||||
TextModelStruct, err := FindTextModelStruct(Table1)
|
TextModelStruct, ModelName, err := FindTextModelStruct(Table1)
|
||||||
|
TextTemplateModel = ReplaceModelStruct(TextTemplateModel, TextModelStruct)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindTextModelStruct(Table1 *types.Table) (string, error) {
|
func FindTextModelStruct(Table1 *types.Table) (string, string, error) {
|
||||||
var Otvet string
|
var Otvet string
|
||||||
|
var ModelName string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
TableName := Table1.Name
|
TableName := Table1.Name
|
||||||
ModelName := create_files.FindSingularName(TableName)
|
ModelName = create_files.FindSingularName(TableName)
|
||||||
|
ModelName = create_files.FormatName(ModelName)
|
||||||
|
|
||||||
Otvet = `// ` + ModelName + ` - model from table ` + TableName + `
|
Otvet = `// ` + ModelName + ` - model from table ` + TableName + `
|
||||||
type ` + ModelName + ` struct {
|
type ` + ModelName + ` struct {
|
||||||
`
|
`
|
||||||
|
for _, Column1 := range Table1.MapColumns {
|
||||||
|
TextColumn := FindTextColumn(Column1)
|
||||||
|
Otvet = Otvet + TextColumn + "\n"
|
||||||
|
}
|
||||||
|
|
||||||
Otvet = Otvet + "}"
|
Otvet = Otvet + "\n}"
|
||||||
return Otvet, err
|
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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user