/
column.go
102 lines (92 loc) · 2.8 KB
/
column.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package parser
import (
"fmt"
"strings"
"github.com/iancoleman/strcase"
)
type col struct {
ColumnName string
//DataType string
//ColumnType string
ColumnComment string
ModelProp string
ModelType string
ModelTag string
//ColumnKey string
//ColumnType string
SwaggerType string
SwaggerFormat string
IsAuthColumn bool
}
func newCol(cName, dType, cComment, cKey, cType, authColumn string, isAuthTable bool) col {
cComment = strings.Replace(cComment, `"`, "", -1)
cLowerName := strings.ToLower(cName)
modelProperty := strcase.ToCamel(cLowerName)
modelType, swgType, swgFormat := transType(dType, cName, cType, cKey)
//Content string `form:"content" json:"content,omitempty" comment:""`
pt := fmt.Sprintf("%s %s", modelProperty, modelType)
sql := ""
if cType != "" {
sql = fmt.Sprintf("%s%s", sql, cType)
}
if cKey != "" {
sql = fmt.Sprintf("%s,%s", sql, cKey)
if cKey == "PRI" {
modelProperty = "Id"
}
}
if dType == "json" {
cComment = fmt.Sprintf("must a string can unmarsh to an Object. %s", cComment)
}
formatTag := `gorm:"column:%s" form:"%s" json:"%s" comment:"%s" sql:"%s"`
if modelType == "*time.Time" {
formatTag = `gorm:"column:%s" form:"%s" json:"%s,omitempty" comment:"%s" sql:"%s"`
}
tag := fmt.Sprintf(formatTag, cName, cLowerName, cLowerName, cComment, sql)
isAuthColumn := isAuthTable && cName == authColumn
modelTag := fmt.Sprintf("%s `%s`", pt, tag)
return col{cName, cComment, modelProperty, modelType, modelTag, swgType, swgFormat, isAuthColumn}
}
func transType(dType, cName, cType, cKey string) (string, string, string) {
modelType := "NoneType"
swgType, swgFormat := "", ""
switch dType {
case "varchar", "longtext", "char", "enum", "set", "mediumtext", "json", "text":
modelType = "string"
swgFormat, swgType = "string", "string"
case "bigint":
modelType = "int"
swgFormat, swgType = "int64", "integer"
if strings.Contains(cType, "unsigned") {
modelType = "uint64"
swgFormat, swgType = "int64", "integer"
}
case "int", "tinyint", "smallint", "mediumint":
modelType = "int"
swgFormat, swgType = "int64", "integer"
if strings.Contains(cType, "unsigned") {
modelType = "uint"
swgFormat, swgType = "int32", "integer"
}
case "decimal", "float":
swgFormat, swgType = "float", "number"
modelType = "float32"
case "double":
swgFormat, swgType = "float", "number"
modelType = "float64"
case "blob":
swgFormat, swgType = "binary", "string"
modelType = "*[]byte"
case "time", "datetime", "date", "timestamp":
swgFormat, swgType = "date-time", "string"
modelType = "*time.Time"
}
if cKey == "PRI" {
modelType = "uint"
swgFormat, swgType = "int64", "integer"
}
if cName == "ID" || cName == "Id" || cName == "iD" || cName == "id" {
modelType = "uint"
}
return modelType, swgType, swgFormat
}