mirror of
https://github.com/beego/bee.git
synced 2024-11-22 15:10:54 +00:00
import logic for model by asta
This commit is contained in:
parent
98b3d4ee37
commit
c487e9d6f7
46
g_model.go
46
g_model.go
@ -15,7 +15,7 @@ func generateModel(mname, fields, crupath string) {
|
|||||||
i := strings.LastIndex(p[:len(p)-1], "/")
|
i := strings.LastIndex(p[:len(p)-1], "/")
|
||||||
packageName = p[i+1 : len(p)-1]
|
packageName = p[i+1 : len(p)-1]
|
||||||
}
|
}
|
||||||
modelStruct, err := getStruct(modelName, fields)
|
modelStruct, err, hastime := getStruct(modelName, fields)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ColorLog("[ERRO] Could not genrate models struct: %s\n", err)
|
ColorLog("[ERRO] Could not genrate models struct: %s\n", err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
@ -36,6 +36,11 @@ func generateModel(mname, fields, crupath string) {
|
|||||||
content := strings.Replace(modelTpl, "{{packageName}}", packageName, -1)
|
content := strings.Replace(modelTpl, "{{packageName}}", packageName, -1)
|
||||||
content = strings.Replace(content, "{{modelName}}", modelName, -1)
|
content = strings.Replace(content, "{{modelName}}", modelName, -1)
|
||||||
content = strings.Replace(content, "{{modelStruct}}", modelStruct, -1)
|
content = strings.Replace(content, "{{modelStruct}}", modelStruct, -1)
|
||||||
|
if hastime {
|
||||||
|
content = strings.Replace(content, "{{timePkg}}", `"time"`, -1)
|
||||||
|
} else {
|
||||||
|
content = strings.Replace(content, "{{timePkg}}", "", -1)
|
||||||
|
}
|
||||||
f.WriteString(content)
|
f.WriteString(content)
|
||||||
// gofmt generated source code
|
// gofmt generated source code
|
||||||
formatSourceCode(fpath)
|
formatSourceCode(fpath)
|
||||||
@ -47,49 +52,53 @@ func generateModel(mname, fields, crupath string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getStruct(structname, fields string) (string, error) {
|
func getStruct(structname, fields string) (string, error, bool) {
|
||||||
if fields == "" {
|
if fields == "" {
|
||||||
return "", errors.New("fields can't empty")
|
return "", errors.New("fields can't empty"), false
|
||||||
}
|
}
|
||||||
|
hastime := false
|
||||||
structStr := "type " + structname + " struct{\n"
|
structStr := "type " + structname + " struct{\n"
|
||||||
fds := strings.Split(fields, ",")
|
fds := strings.Split(fields, ",")
|
||||||
for i, v := range fds {
|
for i, v := range fds {
|
||||||
kv := strings.SplitN(v, ":", 2)
|
kv := strings.SplitN(v, ":", 2)
|
||||||
if len(kv) != 2 {
|
if len(kv) != 2 {
|
||||||
return "", errors.New("the filds format is wrong. should key:type,key:type " + v)
|
return "", errors.New("the filds format is wrong. should key:type,key:type " + v), false
|
||||||
}
|
}
|
||||||
typ, tag := getType(kv[1])
|
typ, tag, hastimeinner := getType(kv[1])
|
||||||
if typ == "" {
|
if typ == "" {
|
||||||
return "", errors.New("the filds format is wrong. should key:type,key:type " + v)
|
return "", errors.New("the filds format is wrong. should key:type,key:type " + v), false
|
||||||
}
|
}
|
||||||
if i == 0 && strings.ToLower(kv[0]) != "id" {
|
if i == 0 && strings.ToLower(kv[0]) != "id" {
|
||||||
structStr = structStr + "Id int64 `orm:\"auto\"`\n"
|
structStr = structStr + "Id int64 `orm:\"auto\"`\n"
|
||||||
}
|
}
|
||||||
|
if hastimeinner {
|
||||||
|
hastime = true
|
||||||
|
}
|
||||||
structStr = structStr + camelString(kv[0]) + " " + typ + " " + tag + "\n"
|
structStr = structStr + camelString(kv[0]) + " " + typ + " " + tag + "\n"
|
||||||
}
|
}
|
||||||
structStr += "}\n"
|
structStr += "}\n"
|
||||||
return structStr, nil
|
return structStr, nil, hastime
|
||||||
}
|
}
|
||||||
|
|
||||||
// fields support type
|
// fields support type
|
||||||
// http://beego.me/docs/mvc/model/models.md#mysql
|
// http://beego.me/docs/mvc/model/models.md#mysql
|
||||||
func getType(ktype string) (kt, tag string) {
|
func getType(ktype string) (kt, tag string, hasTime bool) {
|
||||||
kv := strings.SplitN(ktype, ":", 2)
|
kv := strings.SplitN(ktype, ":", 2)
|
||||||
switch kv[0] {
|
switch kv[0] {
|
||||||
case "string":
|
case "string":
|
||||||
if len(kv) == 2 {
|
if len(kv) == 2 {
|
||||||
return "string", "`orm:\"size(" + kv[1] + ")\"`"
|
return "string", "`orm:\"size(" + kv[1] + ")\"`", false
|
||||||
} else {
|
} else {
|
||||||
return "string", "`orm:\"size(128)\"`"
|
return "string", "`orm:\"size(128)\"`", false
|
||||||
}
|
}
|
||||||
case "text":
|
case "text":
|
||||||
return "string", "`orm:\"type(longtext)\"`"
|
return "string", "`orm:\"type(longtext)\"`", false
|
||||||
case "auto":
|
case "auto":
|
||||||
return "int64", "`orm:\"auto\"`"
|
return "int64", "`orm:\"auto\"`", false
|
||||||
case "pk":
|
case "pk":
|
||||||
return "int64", "`orm:\"pk\"`"
|
return "int64", "`orm:\"pk\"`", false
|
||||||
case "datetime":
|
case "datetime":
|
||||||
return "time.Time", "`orm:\"type(datetime)\"`"
|
return "time.Time", "`orm:\"type(datetime)\"`", true
|
||||||
case "int", "int8", "int16", "int32", "int64":
|
case "int", "int8", "int16", "int32", "int64":
|
||||||
fallthrough
|
fallthrough
|
||||||
case "uint", "uint8", "uint16", "uint32", "uint64":
|
case "uint", "uint8", "uint16", "uint32", "uint64":
|
||||||
@ -97,11 +106,11 @@ func getType(ktype string) (kt, tag string) {
|
|||||||
case "bool":
|
case "bool":
|
||||||
fallthrough
|
fallthrough
|
||||||
case "float32", "float64":
|
case "float32", "float64":
|
||||||
return kv[0], ""
|
return kv[0], "", false
|
||||||
case "float":
|
case "float":
|
||||||
return "float64", ""
|
return "float64", "", false
|
||||||
}
|
}
|
||||||
return "", ""
|
return "", "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
var modelTpl = `package {{packageName}}
|
var modelTpl = `package {{packageName}}
|
||||||
@ -111,8 +120,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
{{timePkg}}
|
||||||
|
|
||||||
"github.com/astaxie/beego/orm"
|
"github.com/astaxie/beego/orm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user