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

support object ref model

This commit is contained in:
astaxie 2016-08-16 23:32:13 +08:00
parent 0e240214ad
commit 525d2b0901
3 changed files with 54 additions and 29 deletions

View File

@ -298,7 +298,7 @@ type ObjectController struct {
beego.Controller
}
// @Title create
// @Title Create
// @Description create object
// @Param body body models.Object true "The object content"
// @Success 200 {string} models.Object.Id
@ -342,7 +342,7 @@ func (o *ObjectController) GetAll() {
o.ServeJSON()
}
// @Title update
// @Title Update
// @Description update the object
// @Param objectId path string true "The objectid you want to update"
// @Param body body models.Object true "The body"
@ -363,7 +363,7 @@ func (o *ObjectController) Put() {
o.ServeJSON()
}
// @Title delete
// @Title Delete
// @Description delete the object
// @Param objectId path string true "The objectId you want to delete"
// @Success 200 {string} delete success!
@ -391,7 +391,7 @@ type UserController struct {
beego.Controller
}
// @Title createUser
// @Title CreateUser
// @Description create users
// @Param body body models.User true "body for user content"
// @Success 200 {int} models.User.Id
@ -405,7 +405,7 @@ func (u *UserController) Post() {
u.ServeJSON()
}
// @Title Get
// @Title GetAll
// @Description get all Users
// @Success 200 {object} models.User
// @router / [get]
@ -434,7 +434,7 @@ func (u *UserController) Get() {
u.ServeJSON()
}
// @Title update
// @Title Update
// @Description update the user
// @Param uid path string true "The uid you want to update"
// @Param body body models.User true "body for user content"
@ -456,7 +456,7 @@ func (u *UserController) Put() {
u.ServeJSON()
}
// @Title delete
// @Title Delete
// @Description delete the user
// @Param uid path string true "The uid you want to delete"
// @Success 200 {string} delete success!
@ -469,7 +469,7 @@ func (u *UserController) Delete() {
u.ServeJSON()
}
// @Title login
// @Title Login
// @Description Logs user into the system
// @Param username query string true "The username for login"
// @Param password query string true "The password for login"

View File

@ -95,7 +95,7 @@ func (c *{{controllerName}}Controller) URLMapping() {
c.Mapping("Delete", c.Delete)
}
// @Title Post
// @Title Create
// @Description create {{controllerName}}
// @Param body body models.{{controllerName}} true "body for {{controllerName}} content"
// @Success 201 {object} models.{{controllerName}}
@ -105,7 +105,7 @@ func (c *{{controllerName}}Controller) Post() {
}
// @Title Get
// @Title GetOne
// @Description get {{controllerName}} by id
// @Param id path string true "The key for staticblock"
// @Success 200 {object} models.{{controllerName}}
@ -115,7 +115,7 @@ func (c *{{controllerName}}Controller) GetOne() {
}
// @Title Get All
// @Title GetAll
// @Description get {{controllerName}}
// @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..."
// @Param fields query string false "Fields returned. e.g. col1,col2 ..."

View File

@ -123,7 +123,7 @@ func generateDocs(curpath string) {
controllerName = analisysNSInclude(s, pp)
if v, ok := controllerComments[controllerName]; ok {
rootapi.Tags = append(rootapi.Tags, swagger.Tag{
Name: s,
Name: strings.Trim(s, "/"),
Description: v,
})
}
@ -191,7 +191,7 @@ func analisysNSInclude(baseurl string, ce *ast.CallExpr) string {
tag := ""
if baseurl != "" {
rt = baseurl + rt
tag = baseurl
tag = strings.Trim(baseurl, "/")
} else {
tag = cname
}
@ -338,7 +338,7 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat
if len(e1) < 1 {
return errors.New("you should has router infomation")
}
routerPath = e1[0]
routerPath = urlReplace(e1[0])
if len(e1) == 2 && e1[1] != "" {
e1 = strings.SplitN(e1[1], " ", 2)
HTTPMethod = strings.ToUpper(strings.Trim(e1[0], "[]"))
@ -570,7 +570,7 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
if st.Fields.List != nil {
m.Properties = make(map[string]swagger.Propertie)
for _, field := range st.Fields.List {
isSlice, realType := typeAnalyser(field)
isSlice, realType, sType := typeAnalyser(field)
realTypes = append(realTypes, realType)
mp := swagger.Propertie{}
// add type slice
@ -578,8 +578,10 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
mp.Type = "array"
mp.Properties = make(map[string]swagger.Propertie)
if isBasicType(realType) {
typeFormat := strings.Split(sType, ":")
mp.Properties["items"] = swagger.Propertie{
Type: realType,
Type: typeFormat[0],
Format: typeFormat[1],
}
} else {
mp.Properties["items"] = swagger.Propertie{
@ -587,7 +589,13 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
}
}
} else {
mp.Type = realType
if isBasicType(realType) {
typeFormat := strings.Split(sType, ":")
mp.Type = typeFormat[0]
mp.Format = typeFormat[1]
} else if sType == "object" {
mp.Ref = "#/definitions/" + realType
}
}
// dont add property if anonymous field
@ -644,7 +652,7 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
}
}
if m.Title == "" {
ColorLog("can't find the object: %v", str)
ColorLog("can't find the object: %s", str)
os.Exit(1)
}
if len(rootapi.Definitions) == 0 {
@ -654,31 +662,34 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
return
}
func typeAnalyser(f *ast.Field) (isSlice bool, realType string) {
func typeAnalyser(f *ast.Field) (isSlice bool, realType, swaggerType string) {
if arr, ok := f.Type.(*ast.ArrayType); ok {
if isBasicType(fmt.Sprint(arr.Elt)) {
return false, fmt.Sprintf("[]%v", arr.Elt)
return false, fmt.Sprintf("[]%v", arr.Elt), basicTypes[fmt.Sprint(arr.Elt)]
}
if mp, ok := arr.Elt.(*ast.MapType); ok {
return false, fmt.Sprintf("map[%v][%v]", mp.Key, mp.Value)
return false, fmt.Sprintf("map[%v][%v]", mp.Key, mp.Value), "object"
}
if star, ok := arr.Elt.(*ast.StarExpr); ok {
return true, fmt.Sprint(star.X)
return true, fmt.Sprint(star.X), "object"
}
return true, fmt.Sprint(arr.Elt)
return true, fmt.Sprint(arr.Elt), "object"
}
switch t := f.Type.(type) {
case *ast.StarExpr:
return false, fmt.Sprint(t.X)
return false, fmt.Sprint(t.X), "object"
case *ast.MapType:
return false, fmt.Sprint(t.Value), "object"
}
return false, fmt.Sprint(f.Type)
if k, ok := basicTypes[fmt.Sprint(f.Type)]; ok {
return false, fmt.Sprint(f.Type), k
}
return false, fmt.Sprint(f.Type), "object"
}
func isBasicType(Type string) bool {
for _, v := range basicTypes {
if v == Type {
return true
}
if _, ok := basicTypes[Type]; ok {
return true
}
return false
}
@ -726,3 +737,17 @@ func appendModels(cmpath, pkgpath, controllerName string, realTypes []string) {
}
}
}
func urlReplace(src string) string {
pt := strings.Split(src, "/")
for i, p := range pt {
if len(p) > 0 {
if p[0] == ':' {
pt[i] = "{" + p[1:] + "}"
} else if p[0] == '?' && p[1] == ':' {
pt[i] = "{" + p[2:] + "}"
}
}
}
return strings.Join(pt, "/")
}