mirror of
https://github.com/casbin/bee.git
synced 2025-07-18 05:04:20 +08:00
New tweaks
Added enhanced logging while using bee to scaffold code Fixed some typos and errors Added more error handling Added more docs
This commit is contained in:
47
g_model.go
47
g_model.go
@ -1,3 +1,17 @@
|
||||
// Copyright 2013 bee authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
@ -8,7 +22,9 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func generateModel(mname, fields, crupath string) {
|
||||
func generateModel(mname, fields, currpath string) {
|
||||
w := NewColorWriter(os.Stdout)
|
||||
|
||||
p, f := path.Split(mname)
|
||||
modelName := strings.Title(f)
|
||||
packageName := "models"
|
||||
@ -16,24 +32,28 @@ func generateModel(mname, fields, crupath string) {
|
||||
i := strings.LastIndex(p[:len(p)-1], "/")
|
||||
packageName = p[i+1 : len(p)-1]
|
||||
}
|
||||
|
||||
modelStruct, hastime, err := getStruct(modelName, fields)
|
||||
if err != nil {
|
||||
ColorLog("[ERRO] Could not genrate models struct: %s\n", err)
|
||||
ColorLog("[ERRO] Could not generate the model struct: %s\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
ColorLog("[INFO] Using '%s' as model name\n", modelName)
|
||||
ColorLog("[INFO] Using '%s' as package name\n", packageName)
|
||||
fp := path.Join(crupath, "models", p)
|
||||
|
||||
fp := path.Join(currpath, "models", p)
|
||||
if _, err := os.Stat(fp); os.IsNotExist(err) {
|
||||
// create controller directory
|
||||
// Create the model's directory
|
||||
if err := os.MkdirAll(fp, 0777); err != nil {
|
||||
ColorLog("[ERRO] Could not create models directory: %s\n", err)
|
||||
ColorLog("[ERRO] Could not create the model directory: %s\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
|
||||
fpath := path.Join(fp, strings.ToLower(modelName)+".go")
|
||||
if f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil {
|
||||
defer f.Close()
|
||||
defer CloseFile(f)
|
||||
content := strings.Replace(modelTpl, "{{packageName}}", packageName, -1)
|
||||
content = strings.Replace(content, "{{modelName}}", modelName, -1)
|
||||
content = strings.Replace(content, "{{modelStruct}}", modelStruct, -1)
|
||||
@ -43,11 +63,10 @@ func generateModel(mname, fields, crupath string) {
|
||||
content = strings.Replace(content, "{{timePkg}}", "", -1)
|
||||
}
|
||||
f.WriteString(content)
|
||||
// gofmt generated source code
|
||||
// Run 'gofmt' on the generated source code
|
||||
formatSourceCode(fpath)
|
||||
fmt.Println("\tcreate\t", fpath)
|
||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", fpath, "\x1b[0m")
|
||||
} else {
|
||||
// error creating file
|
||||
ColorLog("[ERRO] Could not create model file: %s\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
@ -55,23 +74,27 @@ func generateModel(mname, fields, crupath string) {
|
||||
|
||||
func getStruct(structname, fields string) (string, bool, error) {
|
||||
if fields == "" {
|
||||
return "", false, errors.New("fields can't empty")
|
||||
return "", false, errors.New("fields cannot be empty")
|
||||
}
|
||||
|
||||
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 "", false, errors.New("the filds format is wrong. should key:type,key:type " + v)
|
||||
return "", false, errors.New("the fields format is wrong. Should be key:type,key:type " + v)
|
||||
}
|
||||
|
||||
typ, tag, hastimeinner := getType(kv[1])
|
||||
if typ == "" {
|
||||
return "", false, errors.New("the filds format is wrong. should key:type,key:type " + v)
|
||||
return "", false, errors.New("the fields format is wrong. Should be key:type,key:type " + v)
|
||||
}
|
||||
|
||||
if i == 0 && strings.ToLower(kv[0]) != "id" {
|
||||
structStr = structStr + "Id int64 `orm:\"auto\"`\n"
|
||||
}
|
||||
|
||||
if hastimeinner {
|
||||
hastime = true
|
||||
}
|
||||
|
Reference in New Issue
Block a user