import logic for model by asta

This commit is contained in:
ZhengYang 2014-08-19 17:11:38 +08:00
parent 98b3d4ee37
commit c487e9d6f7
1 changed files with 27 additions and 19 deletions

View File

@ -15,7 +15,7 @@ func generateModel(mname, fields, crupath string) {
i := strings.LastIndex(p[:len(p)-1], "/")
packageName = p[i+1 : len(p)-1]
}
modelStruct, err := getStruct(modelName, fields)
modelStruct, err, hastime := getStruct(modelName, fields)
if err != nil {
ColorLog("[ERRO] Could not genrate models struct: %s\n", err)
os.Exit(2)
@ -36,6 +36,11 @@ func generateModel(mname, fields, crupath string) {
content := strings.Replace(modelTpl, "{{packageName}}", packageName, -1)
content = strings.Replace(content, "{{modelName}}", modelName, -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)
// gofmt generated source code
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 == "" {
return "", errors.New("fields can't empty")
return "", errors.New("fields can't empty"), false
}
hastime := false
structStr := "type " + structname + " struct{\n"
fds := strings.Split(fields, ",")
for i, v := range fds {
kv := strings.SplitN(v, ":", 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 == "" {
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" {
structStr = structStr + "Id int64 `orm:\"auto\"`\n"
}
if hastimeinner {
hastime = true
}
structStr = structStr + camelString(kv[0]) + " " + typ + " " + tag + "\n"
}
structStr += "}\n"
return structStr, nil
return structStr, nil, hastime
}
// fields support type
// 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)
switch kv[0] {
case "string":
if len(kv) == 2 {
return "string", "`orm:\"size(" + kv[1] + ")\"`"
return "string", "`orm:\"size(" + kv[1] + ")\"`", false
} else {
return "string", "`orm:\"size(128)\"`"
return "string", "`orm:\"size(128)\"`", false
}
case "text":
return "string", "`orm:\"type(longtext)\"`"
return "string", "`orm:\"type(longtext)\"`", false
case "auto":
return "int64", "`orm:\"auto\"`"
return "int64", "`orm:\"auto\"`", false
case "pk":
return "int64", "`orm:\"pk\"`"
return "int64", "`orm:\"pk\"`", false
case "datetime":
return "time.Time", "`orm:\"type(datetime)\"`"
return "time.Time", "`orm:\"type(datetime)\"`", true
case "int", "int8", "int16", "int32", "int64":
fallthrough
case "uint", "uint8", "uint16", "uint32", "uint64":
@ -97,11 +106,11 @@ func getType(ktype string) (kt, tag string) {
case "bool":
fallthrough
case "float32", "float64":
return kv[0], ""
return kv[0], "", false
case "float":
return "float64", ""
return "float64", "", false
}
return "", ""
return "", "", false
}
var modelTpl = `package {{packageName}}
@ -111,8 +120,7 @@ import (
"fmt"
"reflect"
"strings"
"time"
{{timePkg}}
"github.com/astaxie/beego/orm"
)