1
0
mirror of https://github.com/beego/bee.git synced 2024-11-23 11:50:55 +00:00

Merge pull request #1 from kyle-wang/develop

Develop
This commit is contained in:
kyle 2016-01-07 11:37:54 +08:00
commit 63a8205a26
7 changed files with 63 additions and 63 deletions

View File

@ -309,7 +309,7 @@ func (o *ObjectController) Post() {
json.Unmarshal(o.Ctx.Input.RequestBody, &ob) json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
objectid := models.AddOne(ob) objectid := models.AddOne(ob)
o.Data["json"] = map[string]string{"ObjectId": objectid} o.Data["json"] = map[string]string{"ObjectId": objectid}
o.ServeJson() o.ServeJSON()
} }
// @Title Get // @Title Get
@ -323,12 +323,12 @@ func (o *ObjectController) Get() {
if objectId != "" { if objectId != "" {
ob, err := models.GetOne(objectId) ob, err := models.GetOne(objectId)
if err != nil { if err != nil {
o.Data["json"] = err o.Data["json"] = err.Error()
} else { } else {
o.Data["json"] = ob o.Data["json"] = ob
} }
} }
o.ServeJson() o.ServeJSON()
} }
// @Title GetAll // @Title GetAll
@ -339,7 +339,7 @@ func (o *ObjectController) Get() {
func (o *ObjectController) GetAll() { func (o *ObjectController) GetAll() {
obs := models.GetAll() obs := models.GetAll()
o.Data["json"] = obs o.Data["json"] = obs
o.ServeJson() o.ServeJSON()
} }
// @Title update // @Title update
@ -356,11 +356,11 @@ func (o *ObjectController) Put() {
err := models.Update(objectId, ob.Score) err := models.Update(objectId, ob.Score)
if err != nil { if err != nil {
o.Data["json"] = err o.Data["json"] = err.Error()
} else { } else {
o.Data["json"] = "update success!" o.Data["json"] = "update success!"
} }
o.ServeJson() o.ServeJSON()
} }
// @Title delete // @Title delete
@ -373,7 +373,7 @@ func (o *ObjectController) Delete() {
objectId := o.Ctx.Input.Params[":objectId"] objectId := o.Ctx.Input.Params[":objectId"]
models.Delete(objectId) models.Delete(objectId)
o.Data["json"] = "delete success!" o.Data["json"] = "delete success!"
o.ServeJson() o.ServeJSON()
} }
` `
@ -402,7 +402,7 @@ func (u *UserController) Post() {
json.Unmarshal(u.Ctx.Input.RequestBody, &user) json.Unmarshal(u.Ctx.Input.RequestBody, &user)
uid := models.AddUser(user) uid := models.AddUser(user)
u.Data["json"] = map[string]string{"uid": uid} u.Data["json"] = map[string]string{"uid": uid}
u.ServeJson() u.ServeJSON()
} }
// @Title Get // @Title Get
@ -412,7 +412,7 @@ func (u *UserController) Post() {
func (u *UserController) GetAll() { func (u *UserController) GetAll() {
users := models.GetAllUsers() users := models.GetAllUsers()
u.Data["json"] = users u.Data["json"] = users
u.ServeJson() u.ServeJSON()
} }
// @Title Get // @Title Get
@ -426,12 +426,12 @@ func (u *UserController) Get() {
if uid != "" { if uid != "" {
user, err := models.GetUser(uid) user, err := models.GetUser(uid)
if err != nil { if err != nil {
u.Data["json"] = err u.Data["json"] = err.Error()
} else { } else {
u.Data["json"] = user u.Data["json"] = user
} }
} }
u.ServeJson() u.ServeJSON()
} }
// @Title update // @Title update
@ -448,12 +448,12 @@ func (u *UserController) Put() {
json.Unmarshal(u.Ctx.Input.RequestBody, &user) json.Unmarshal(u.Ctx.Input.RequestBody, &user)
uu, err := models.UpdateUser(uid, &user) uu, err := models.UpdateUser(uid, &user)
if err != nil { if err != nil {
u.Data["json"] = err u.Data["json"] = err.Error()
} else { } else {
u.Data["json"] = uu u.Data["json"] = uu
} }
} }
u.ServeJson() u.ServeJSON()
} }
// @Title delete // @Title delete
@ -466,7 +466,7 @@ func (u *UserController) Delete() {
uid := u.GetString(":uid") uid := u.GetString(":uid")
models.DeleteUser(uid) models.DeleteUser(uid)
u.Data["json"] = "delete success!" u.Data["json"] = "delete success!"
u.ServeJson() u.ServeJSON()
} }
// @Title login // @Title login
@ -484,7 +484,7 @@ func (u *UserController) Login() {
} else { } else {
u.Data["json"] = "user not exist" u.Data["json"] = "user not exist"
} }
u.ServeJson() u.ServeJSON()
} }
// @Title logout // @Title logout
@ -493,7 +493,7 @@ func (u *UserController) Login() {
// @router /logout [get] // @router /logout [get]
func (u *UserController) Logout() { func (u *UserController) Logout() {
u.Data["json"] = "logout success" u.Data["json"] = "logout success"
u.ServeJson() u.ServeJSON()
} }
` `

4
g.go
View File

@ -166,8 +166,8 @@ func generateCode(cmd *Command, args []string) int {
upsql := "" upsql := ""
downsql := "" downsql := ""
if fields != "" { if fields != "" {
upsql = `m.Sql("CREATE TABLE ` + mname + "(" + generateSQLFromFields(fields.String()) + `)");` upsql = `m.SQL("CREATE TABLE ` + mname + "(" + generateSQLFromFields(fields.String()) + `)");`
downsql = `m.Sql("DROP TABLE ` + "`" + mname + "`" + `")` downsql = `m.SQL("DROP TABLE ` + "`" + mname + "`" + `")`
} }
generateMigration(mname, upsql, downsql, curpath) generateMigration(mname, upsql, downsql, curpath)
case "controller": case "controller":

View File

@ -1188,7 +1188,7 @@ func (c *{{ctrlName}}Controller) Post() {
} else { } else {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
} }
c.ServeJson() c.ServeJSON()
} }
// @Title Get // @Title Get
@ -1206,7 +1206,7 @@ func (c *{{ctrlName}}Controller) GetOne() {
} else { } else {
c.Data["json"] = v c.Data["json"] = v
} }
c.ServeJson() c.ServeJSON()
} }
// @Title Get All // @Title Get All
@ -1254,7 +1254,7 @@ func (c *{{ctrlName}}Controller) GetAll() {
kv := strings.Split(cond, ":") kv := strings.Split(cond, ":")
if len(kv) != 2 { if len(kv) != 2 {
c.Data["json"] = errors.New("Error: invalid query key/value pair") c.Data["json"] = errors.New("Error: invalid query key/value pair")
c.ServeJson() c.ServeJSON()
return return
} }
k, v := kv[0], kv[1] k, v := kv[0], kv[1]
@ -1268,7 +1268,7 @@ func (c *{{ctrlName}}Controller) GetAll() {
} else { } else {
c.Data["json"] = l c.Data["json"] = l
} }
c.ServeJson() c.ServeJSON()
} }
// @Title Update // @Title Update
@ -1291,7 +1291,7 @@ func (c *{{ctrlName}}Controller) Put() {
} else { } else {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
} }
c.ServeJson() c.ServeJSON()
} }
// @Title Delete // @Title Delete
@ -1308,7 +1308,7 @@ func (c *{{ctrlName}}Controller) Delete() {
} else { } else {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
} }
c.ServeJson() c.ServeJSON()
} }
` `
ROUTER_TPL = `// @APIVersion 1.0.0 ROUTER_TPL = `// @APIVersion 1.0.0

View File

@ -182,7 +182,7 @@ func (c *{{controllerName}}Controller) Post() {
} else { } else {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
} }
c.ServeJson() c.ServeJSON()
} }
// @Title Get // @Title Get
@ -200,7 +200,7 @@ func (c *{{controllerName}}Controller) GetOne() {
} else { } else {
c.Data["json"] = v c.Data["json"] = v
} }
c.ServeJson() c.ServeJSON()
} }
// @Title Get All // @Title Get All
@ -248,7 +248,7 @@ func (c *{{controllerName}}Controller) GetAll() {
kv := strings.Split(cond, ":") kv := strings.Split(cond, ":")
if len(kv) != 2 { if len(kv) != 2 {
c.Data["json"] = errors.New("Error: invalid query key/value pair") c.Data["json"] = errors.New("Error: invalid query key/value pair")
c.ServeJson() c.ServeJSON()
return return
} }
k, v := kv[0], kv[1] k, v := kv[0], kv[1]
@ -262,7 +262,7 @@ func (c *{{controllerName}}Controller) GetAll() {
} else { } else {
c.Data["json"] = l c.Data["json"] = l
} }
c.ServeJson() c.ServeJSON()
} }
// @Title Update // @Title Update
@ -282,7 +282,7 @@ func (c *{{controllerName}}Controller) Put() {
} else { } else {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
} }
c.ServeJson() c.ServeJSON()
} }
// @Title Delete // @Title Delete
@ -299,6 +299,6 @@ func (c *{{controllerName}}Controller) Delete() {
} else { } else {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
} }
c.ServeJson() c.ServeJSON()
} }
` `

View File

@ -52,7 +52,7 @@ const (
) )
var rootapi swagger.ResourceListing var rootapi swagger.ResourceListing
var apilist map[string]*swagger.ApiDeclaration var apilist map[string]*swagger.APIDeclaration
func init() { func init() {
if beego.EnableDocs { if beego.EnableDocs {
@ -64,14 +64,14 @@ func init() {
if err != nil { if err != nil {
beego.Error(err) beego.Error(err)
} }
beego.GlobalDocApi["Root"] = rootapi beego.GlobalDocAPI["Root"] = rootapi
for k, v := range apilist { for k, v := range apilist {
for i, a := range v.Apis { for i, a := range v.APIs {
a.Path = urlReplace(k + a.Path) a.Path = urlReplace(k + a.Path)
v.Apis[i] = a v.APIs[i] = a
} }
v.BasePath = BasePath v.BasePath = BasePath
beego.GlobalDocApi[strings.Trim(k, "/")] = v beego.GlobalDocAPI[strings.Trim(k, "/")] = v
} }
} }
} }
@ -102,8 +102,8 @@ const (
var pkgCache map[string]bool //pkg:controller:function:comments comments: key:value var pkgCache map[string]bool //pkg:controller:function:comments comments: key:value
var controllerComments map[string]string var controllerComments map[string]string
var importlist map[string]string var importlist map[string]string
var apilist map[string]*swagger.ApiDeclaration var apilist map[string]*swagger.APIDeclaration
var controllerList map[string][]swagger.Api var controllerList map[string][]swagger.API
var modelsList map[string]map[string]swagger.Model var modelsList map[string]map[string]swagger.Model
var rootapi swagger.ResourceListing var rootapi swagger.ResourceListing
@ -111,8 +111,8 @@ func init() {
pkgCache = make(map[string]bool) pkgCache = make(map[string]bool)
controllerComments = make(map[string]string) controllerComments = make(map[string]string)
importlist = make(map[string]string) importlist = make(map[string]string)
apilist = make(map[string]*swagger.ApiDeclaration) apilist = make(map[string]*swagger.APIDeclaration)
controllerList = make(map[string][]swagger.Api) controllerList = make(map[string][]swagger.API)
modelsList = make(map[string]map[string]swagger.Model) modelsList = make(map[string]map[string]swagger.Model)
} }
@ -133,19 +133,19 @@ func generateDocs(curpath string) {
for _, c := range f.Comments { for _, c := range f.Comments {
for _, s := range strings.Split(c.Text(), "\n") { for _, s := range strings.Split(c.Text(), "\n") {
if strings.HasPrefix(s, "@APIVersion") { if strings.HasPrefix(s, "@APIVersion") {
rootapi.ApiVersion = strings.TrimSpace(s[len("@APIVersion"):]) rootapi.APIVersion = strings.TrimSpace(s[len("@APIVersion"):])
} else if strings.HasPrefix(s, "@Title") { } else if strings.HasPrefix(s, "@Title") {
rootapi.Infos.Title = strings.TrimSpace(s[len("@Title"):]) rootapi.Infos.Title = strings.TrimSpace(s[len("@Title"):])
} else if strings.HasPrefix(s, "@Description") { } else if strings.HasPrefix(s, "@Description") {
rootapi.Infos.Description = strings.TrimSpace(s[len("@Description"):]) rootapi.Infos.Description = strings.TrimSpace(s[len("@Description"):])
} else if strings.HasPrefix(s, "@TermsOfServiceUrl") { } else if strings.HasPrefix(s, "@TermsOfServiceUrl") {
rootapi.Infos.TermsOfServiceUrl = strings.TrimSpace(s[len("@TermsOfServiceUrl"):]) rootapi.Infos.TermsOfServiceURL = strings.TrimSpace(s[len("@TermsOfServiceUrl"):])
} else if strings.HasPrefix(s, "@Contact") { } else if strings.HasPrefix(s, "@Contact") {
rootapi.Infos.Contact = strings.TrimSpace(s[len("@Contact"):]) rootapi.Infos.Contact = strings.TrimSpace(s[len("@Contact"):])
} else if strings.HasPrefix(s, "@License") { } else if strings.HasPrefix(s, "@License") {
rootapi.Infos.License = strings.TrimSpace(s[len("@License"):]) rootapi.Infos.License = strings.TrimSpace(s[len("@License"):])
} else if strings.HasPrefix(s, "@LicenseUrl") { } else if strings.HasPrefix(s, "@LicenseUrl") {
rootapi.Infos.LicenseUrl = strings.TrimSpace(s[len("@LicenseUrl"):]) rootapi.Infos.LicenseURL = strings.TrimSpace(s[len("@LicenseUrl"):])
} }
} }
} }
@ -172,7 +172,7 @@ func generateDocs(curpath string) {
case *ast.CallExpr: case *ast.CallExpr:
if selname := pp.Fun.(*ast.SelectorExpr).Sel.String(); selname == "NSNamespace" { if selname := pp.Fun.(*ast.SelectorExpr).Sel.String(); selname == "NSNamespace" {
s, params := analisysNewNamespace(pp) s, params := analisysNewNamespace(pp)
subapi := swagger.ApiRef{Path: s} subapi := swagger.APIRef{Path: s}
controllerName := "" controllerName := ""
for _, sp := range params { for _, sp := range params {
switch pp := sp.(type) { switch pp := sp.(type) {
@ -185,7 +185,7 @@ func generateDocs(curpath string) {
if v, ok := controllerComments[controllerName]; ok { if v, ok := controllerComments[controllerName]; ok {
subapi.Description = v subapi.Description = v
} }
rootapi.Apis = append(rootapi.Apis, subapi) rootapi.APIs = append(rootapi.APIs, subapi)
} else if selname == "NSInclude" { } else if selname == "NSInclude" {
analisysNSInclude(f, pp) analisysNSInclude(f, pp)
} }
@ -233,12 +233,12 @@ func analisysNewNamespace(ce *ast.CallExpr) (first string, others []ast.Expr) {
func analisysNSInclude(baseurl string, ce *ast.CallExpr) string { func analisysNSInclude(baseurl string, ce *ast.CallExpr) string {
cname := "" cname := ""
a := &swagger.ApiDeclaration{} a := &swagger.APIDeclaration{}
a.ApiVersion = rootapi.ApiVersion a.APIVersion = rootapi.APIVersion
a.SwaggerVersion = swagger.SwaggerVersion a.SwaggerVersion = swagger.SwaggerVersion
a.ResourcePath = baseurl a.ResourcePath = baseurl
a.Produces = []string{"application/json", "application/xml", "text/plain", "text/html"} a.Produces = []string{"application/json", "application/xml", "text/plain", "text/html"}
a.Apis = make([]swagger.Api, 0) a.APIs = make([]swagger.API, 0)
a.Models = make(map[string]swagger.Model) a.Models = make(map[string]swagger.Model)
for _, p := range ce.Args { for _, p := range ce.Args {
x := p.(*ast.UnaryExpr).X.(*ast.CompositeLit).Type.(*ast.SelectorExpr) x := p.(*ast.UnaryExpr).X.(*ast.CompositeLit).Type.(*ast.SelectorExpr)
@ -246,15 +246,15 @@ func analisysNSInclude(baseurl string, ce *ast.CallExpr) string {
cname = v + x.Sel.Name cname = v + x.Sel.Name
} }
if apis, ok := controllerList[cname]; ok { if apis, ok := controllerList[cname]; ok {
if len(a.Apis) > 0 { if len(a.APIs) > 0 {
a.Apis = append(a.Apis, apis...) a.APIs = append(a.APIs, apis...)
} else { } else {
a.Apis = apis a.APIs = apis
} }
} }
if models, ok := modelsList[cname]; ok { if models, ok := modelsList[cname]; ok {
for _, m := range models { for _, m := range models {
a.Models[m.Id] = m a.Models[m.ID] = m
} }
} }
} }
@ -355,7 +355,7 @@ func isSystemPackage(pkgpath string) bool {
// parse the func comments // parse the func comments
func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpath string) error { func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpath string) error {
innerapi := swagger.Api{} innerapi := swagger.API{}
opts := swagger.Operation{} opts := swagger.Operation{}
if comments != nil && comments.List != nil { if comments != nil && comments.List != nil {
for _, c := range comments.List { for _, c := range comments.List {
@ -369,9 +369,9 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat
innerapi.Path = e1[0] innerapi.Path = e1[0]
if len(e1) == 2 && e1[1] != "" { if len(e1) == 2 && e1[1] != "" {
e1 = strings.SplitN(e1[1], " ", 2) e1 = strings.SplitN(e1[1], " ", 2)
opts.HttpMethod = strings.ToUpper(strings.Trim(e1[0], "[]")) opts.HTTPMethod = strings.ToUpper(strings.Trim(e1[0], "[]"))
} else { } else {
opts.HttpMethod = "GET" opts.HTTPMethod = "GET"
} }
} else if strings.HasPrefix(t, "@Title") { } else if strings.HasPrefix(t, "@Title") {
opts.Nickname = strings.TrimSpace(t[len("@Title"):]) opts.Nickname = strings.TrimSpace(t[len("@Title"):])
@ -490,7 +490,7 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat
if _, ok := controllerList[pkgpath+controllerName]; ok { if _, ok := controllerList[pkgpath+controllerName]; ok {
controllerList[pkgpath+controllerName] = append(controllerList[pkgpath+controllerName], innerapi) controllerList[pkgpath+controllerName] = append(controllerList[pkgpath+controllerName], innerapi)
} else { } else {
controllerList[pkgpath+controllerName] = make([]swagger.Api, 1) controllerList[pkgpath+controllerName] = make([]swagger.API, 1)
controllerList[pkgpath+controllerName][0] = innerapi controllerList[pkgpath+controllerName][0] = innerapi
} }
} }
@ -561,7 +561,7 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Model, realType
if !ok { if !ok {
continue continue
} }
m.Id = k m.ID = k
if st.Fields.List != nil { if st.Fields.List != nil {
m.Properties = make(map[string]swagger.ModelProperty) m.Properties = make(map[string]swagger.ModelProperty)
for _, field := range st.Fields.List { for _, field := range st.Fields.List {
@ -635,7 +635,7 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Model, realType
} }
} }
} }
if m.Id == "" { if m.ID == "" {
ColorLog("can't find the object: %v", str) ColorLog("can't find the object: %v", str)
os.Exit(1) os.Exit(1)
} }

View File

@ -79,13 +79,13 @@ func init() {
// Run the migrations // Run the migrations
func (m *{{StructName}}) Up() { func (m *{{StructName}}) Up() {
// use m.Sql("CREATE TABLE ...") to make schema update // use m.SQL("CREATE TABLE ...") to make schema update
{{UpSQL}} {{UpSQL}}
} }
// Reverse the migrations // Reverse the migrations
func (m *{{StructName}}) Down() { func (m *{{StructName}}) Down() {
// use m.Sql("DROP TABLE ...") to reverse schema update // use m.SQL("DROP TABLE ...") to reverse schema update
{{DownSQL}} {{DownSQL}}
} }
` `

View File

@ -28,8 +28,8 @@ func generateScaffold(sname, fields, crupath, driver, conn string) {
upsql := "" upsql := ""
downsql := "" downsql := ""
if fields != "" { if fields != "" {
upsql = `m.Sql("CREATE TABLE ` + sname + "(" + generateSQLFromFields(fields) + `)");` upsql = `m.SQL("CREATE TABLE ` + sname + "(" + generateSQLFromFields(fields) + `)");`
downsql = `m.Sql("DROP TABLE ` + "`" + sname + "`" + `")` downsql = `m.SQL("DROP TABLE ` + "`" + sname + "`" + `")`
} }
generateMigration(sname, upsql, downsql, crupath) generateMigration(sname, upsql, downsql, crupath)
} }
@ -48,12 +48,12 @@ func generateSQLFromFields(fields string) string {
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 {
ColorLog("[ERRO] the filds format is wrong. should key:type,key:type " + v) ColorLog("[ERRO] the fields format is wrong. should key:type,key:type " + v + "\n")
return "" return ""
} }
typ, tag := getSqlType(kv[1]) typ, tag := getSqlType(kv[1])
if typ == "" { if typ == "" {
ColorLog("[ERRO] the filds format is wrong. should key:type,key:type " + v) ColorLog("[ERRO] the fields format is wrong. should key:type,key:type " + v + "\n")
return "" return ""
} }
if i == 0 && strings.ToLower(kv[0]) != "id" { if i == 0 && strings.ToLower(kv[0]) != "id" {