1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 10:10:53 +00:00

Merge pull request #4 from beego/develop

Develop update
This commit is contained in:
Sergey Lanzman 2016-09-14 22:59:52 +03:00 committed by GitHub
commit 6189e101c9
5 changed files with 137 additions and 105 deletions

View File

@ -650,7 +650,8 @@ func checkEnv(appname string) (apppath, packpath string, err error) {
for _, gpath := range gps { for _, gpath := range gps {
gsrcpath := path.Join(gpath, "src") gsrcpath := path.Join(gpath, "src")
if strings.HasPrefix(currpath, gsrcpath) { if strings.HasPrefix(currpath, gsrcpath) {
return currpath, currpath[len(gsrcpath)+1:], nil packpath = strings.Replace(currpath[len(gsrcpath)+1:], string(path.Separator), "/", -1)
return currpath, packpath, nil
} }
} }

2
bee.go
View File

@ -25,7 +25,7 @@ import (
"strings" "strings"
) )
const version = "1.5.0" const version = "1.5.1"
type Command struct { type Command struct {
// Run runs the command. // Run runs the command.

View File

@ -1168,11 +1168,12 @@ import (
"github.com/astaxie/beego" "github.com/astaxie/beego"
) )
// oprations for {{ctrlName}} // {{ctrlName}}Controller oprations for {{ctrlName}}
type {{ctrlName}}Controller struct { type {{ctrlName}}Controller struct {
beego.Controller beego.Controller
} }
// URLMapping ...
func (c *{{ctrlName}}Controller) URLMapping() { func (c *{{ctrlName}}Controller) URLMapping() {
c.Mapping("Post", c.Post) c.Mapping("Post", c.Post)
c.Mapping("GetOne", c.GetOne) c.Mapping("GetOne", c.GetOne)
@ -1181,6 +1182,7 @@ func (c *{{ctrlName}}Controller) URLMapping() {
c.Mapping("Delete", c.Delete) c.Mapping("Delete", c.Delete)
} }
// Post ...
// @Title Post // @Title Post
// @Description create {{ctrlName}} // @Description create {{ctrlName}}
// @Param body body models.{{ctrlName}} true "body for {{ctrlName}} content" // @Param body body models.{{ctrlName}} true "body for {{ctrlName}} content"
@ -1202,7 +1204,8 @@ func (c *{{ctrlName}}Controller) Post() {
c.ServeJSON() c.ServeJSON()
} }
// @Title Get // GetOne ...
// @Title Get One
// @Description get {{ctrlName}} by id // @Description get {{ctrlName}} by id
// @Param id path string true "The key for staticblock" // @Param id path string true "The key for staticblock"
// @Success 200 {object} models.{{ctrlName}} // @Success 200 {object} models.{{ctrlName}}
@ -1220,6 +1223,7 @@ func (c *{{ctrlName}}Controller) GetOne() {
c.ServeJSON() c.ServeJSON()
} }
// GetAll ...
// @Title Get All // @Title Get All
// @Description get {{ctrlName}} // @Description get {{ctrlName}}
// @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..." // @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..."
@ -1235,9 +1239,9 @@ func (c *{{ctrlName}}Controller) GetAll() {
var fields []string var fields []string
var sortby []string var sortby []string
var order []string var order []string
var query map[string]string = make(map[string]string) var query = make(map[string]string)
var limit int64 = 10 var limit int64 = 10
var offset int64 = 0 var offset int64
// fields: col1,col2,entity.col3 // fields: col1,col2,entity.col3
if v := c.GetString("fields"); v != "" { if v := c.GetString("fields"); v != "" {
@ -1282,7 +1286,8 @@ func (c *{{ctrlName}}Controller) GetAll() {
c.ServeJSON() c.ServeJSON()
} }
// @Title Update // Put ...
// @Title Put
// @Description update the {{ctrlName}} // @Description update the {{ctrlName}}
// @Param id path string true "The id you want to update" // @Param id path string true "The id you want to update"
// @Param body body models.{{ctrlName}} true "body for {{ctrlName}} content" // @Param body body models.{{ctrlName}} true "body for {{ctrlName}} content"
@ -1305,6 +1310,7 @@ func (c *{{ctrlName}}Controller) Put() {
c.ServeJSON() c.ServeJSON()
} }
// Delete ...
// @Title Delete // @Title Delete
// @Description delete the {{ctrlName}} // @Description delete the {{ctrlName}}
// @Param id path string true "The id you want to delete" // @Param id path string true "The id you want to delete"

View File

@ -82,11 +82,12 @@ import (
"github.com/astaxie/beego" "github.com/astaxie/beego"
) )
// operations for {{controllerName}} // {{controllerName}}Controller operations for {{controllerName}}
type {{controllerName}}Controller struct { type {{controllerName}}Controller struct {
beego.Controller beego.Controller
} }
// URLMapping ...
func (c *{{controllerName}}Controller) URLMapping() { func (c *{{controllerName}}Controller) URLMapping() {
c.Mapping("Post", c.Post) c.Mapping("Post", c.Post)
c.Mapping("GetOne", c.GetOne) c.Mapping("GetOne", c.GetOne)
@ -95,6 +96,7 @@ func (c *{{controllerName}}Controller) URLMapping() {
c.Mapping("Delete", c.Delete) c.Mapping("Delete", c.Delete)
} }
// Post ...
// @Title Create // @Title Create
// @Description create {{controllerName}} // @Description create {{controllerName}}
// @Param body body models.{{controllerName}} true "body for {{controllerName}} content" // @Param body body models.{{controllerName}} true "body for {{controllerName}} content"
@ -105,6 +107,7 @@ func (c *{{controllerName}}Controller) Post() {
} }
// GetOne ...
// @Title GetOne // @Title GetOne
// @Description get {{controllerName}} by id // @Description get {{controllerName}} by id
// @Param id path string true "The key for staticblock" // @Param id path string true "The key for staticblock"
@ -115,6 +118,7 @@ func (c *{{controllerName}}Controller) GetOne() {
} }
// GetAll ...
// @Title GetAll // @Title GetAll
// @Description get {{controllerName}} // @Description get {{controllerName}}
// @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..." // @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..."
@ -130,7 +134,8 @@ func (c *{{controllerName}}Controller) GetAll() {
} }
// @Title Update // Put ...
// @Title Put
// @Description update the {{controllerName}} // @Description update the {{controllerName}}
// @Param id path string true "The id you want to update" // @Param id path string true "The id you want to update"
// @Param body body models.{{controllerName}} true "body for {{controllerName}} content" // @Param body body models.{{controllerName}} true "body for {{controllerName}} content"
@ -141,6 +146,7 @@ func (c *{{controllerName}}Controller) Put() {
} }
// Delete ...
// @Title Delete // @Title Delete
// @Description delete the {{controllerName}} // @Description delete the {{controllerName}}
// @Param id path string true "The id you want to delete" // @Param id path string true "The id you want to delete"
@ -164,11 +170,12 @@ import (
"github.com/astaxie/beego" "github.com/astaxie/beego"
) )
// oprations for {{controllerName}} // {{controllerName}}Controller oprations for {{controllerName}}
type {{controllerName}}Controller struct { type {{controllerName}}Controller struct {
beego.Controller beego.Controller
} }
// URLMapping ...
func (c *{{controllerName}}Controller) URLMapping() { func (c *{{controllerName}}Controller) URLMapping() {
c.Mapping("Post", c.Post) c.Mapping("Post", c.Post)
c.Mapping("GetOne", c.GetOne) c.Mapping("GetOne", c.GetOne)
@ -177,6 +184,7 @@ func (c *{{controllerName}}Controller) URLMapping() {
c.Mapping("Delete", c.Delete) c.Mapping("Delete", c.Delete)
} }
// Post ...
// @Title Post // @Title Post
// @Description create {{controllerName}} // @Description create {{controllerName}}
// @Param body body models.{{controllerName}} true "body for {{controllerName}} content" // @Param body body models.{{controllerName}} true "body for {{controllerName}} content"
@ -195,7 +203,8 @@ func (c *{{controllerName}}Controller) Post() {
c.ServeJSON() c.ServeJSON()
} }
// @Title Get // GetOne ...
// @Title Get One
// @Description get {{controllerName}} by id // @Description get {{controllerName}} by id
// @Param id path string true "The key for staticblock" // @Param id path string true "The key for staticblock"
// @Success 200 {object} models.{{controllerName}} // @Success 200 {object} models.{{controllerName}}
@ -213,6 +222,7 @@ func (c *{{controllerName}}Controller) GetOne() {
c.ServeJSON() c.ServeJSON()
} }
// GetAll ...
// @Title Get All // @Title Get All
// @Description get {{controllerName}} // @Description get {{controllerName}}
// @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..." // @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..."
@ -228,9 +238,9 @@ func (c *{{controllerName}}Controller) GetAll() {
var fields []string var fields []string
var sortby []string var sortby []string
var order []string var order []string
var query map[string]string = make(map[string]string) var query = make(map[string]string)
var limit int64 = 10 var limit int64 = 10
var offset int64 = 0 var offset int64
// fields: col1,col2,entity.col3 // fields: col1,col2,entity.col3
if v := c.GetString("fields"); v != "" { if v := c.GetString("fields"); v != "" {
@ -275,7 +285,8 @@ func (c *{{controllerName}}Controller) GetAll() {
c.ServeJSON() c.ServeJSON()
} }
// @Title Update // Put ...
// @Title Put
// @Description update the {{controllerName}} // @Description update the {{controllerName}}
// @Param id path string true "The id you want to update" // @Param id path string true "The id you want to update"
// @Param body body models.{{controllerName}} true "body for {{controllerName}} content" // @Param body body models.{{controllerName}} true "body for {{controllerName}} content"
@ -295,6 +306,7 @@ func (c *{{controllerName}}Controller) Put() {
c.ServeJSON() c.ServeJSON()
} }
// Delete ...
// @Title Delete // @Title Delete
// @Description delete the {{controllerName}} // @Description delete the {{controllerName}}
// @Param id path string true "The id you want to delete" // @Param id path string true "The id you want to delete"

195
g_docs.go
View File

@ -365,7 +365,9 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat
} else if strings.HasPrefix(t, "@Title") { } else if strings.HasPrefix(t, "@Title") {
opts.OperationID = controllerName + "." + strings.TrimSpace(t[len("@Title"):]) opts.OperationID = controllerName + "." + strings.TrimSpace(t[len("@Title"):])
} else if strings.HasPrefix(t, "@Description") { } else if strings.HasPrefix(t, "@Description") {
opts.Summary = strings.TrimSpace(t[len("@Description"):]) opts.Description = strings.TrimSpace(t[len("@Description"):])
} else if strings.HasPrefix(t, "@Summary") {
opts.Summary = strings.TrimSpace(t[len("@Summary"):])
} else if strings.HasPrefix(t, "@Success") { } else if strings.HasPrefix(t, "@Success") {
ss := strings.TrimSpace(t[len("@Success"):]) ss := strings.TrimSpace(t[len("@Success"):])
rs := swagger.Response{} rs := swagger.Response{}
@ -610,96 +612,7 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
if k != objectname { if k != objectname {
continue continue
} }
ts, ok := d.Decl.(*ast.TypeSpec) parseObject(d, k, &m, &realTypes, astPkgs)
if !ok {
ColorLog("Unknown type without TypeSec: %v\n", d)
os.Exit(1)
}
// TODO support other types, such as `ArrayType`, `MapType`, `InterfaceType` etc...
st, ok := ts.Type.(*ast.StructType)
if !ok {
continue
}
m.Title = k
if st.Fields.List != nil {
m.Properties = make(map[string]swagger.Propertie)
for _, field := range st.Fields.List {
isSlice, realType, sType := typeAnalyser(field)
realTypes = append(realTypes, realType)
mp := swagger.Propertie{}
// add type slice
if isSlice {
mp.Type = "array"
if isBasicType(realType) {
typeFormat := strings.Split(sType, ":")
mp.Items = &swagger.Propertie{
Type: typeFormat[0],
Format: typeFormat[1],
}
} else {
mp.Items = &swagger.Propertie{
Ref: "#/definitions/" + realType,
}
}
} else {
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
if field.Names != nil {
// set property name as field name
var name = field.Names[0].Name
// if no tag skip tag processing
if field.Tag == nil {
m.Properties[name] = mp
continue
}
var tagValues []string
stag := reflect.StructTag(strings.Trim(field.Tag.Value, "`"))
tag := stag.Get("json")
if tag != "" {
tagValues = strings.Split(tag, ",")
}
// dont add property if json tag first value is "-"
if len(tagValues) == 0 || tagValues[0] != "-" {
// set property name to the left most json tag value only if is not omitempty
if len(tagValues) > 0 && tagValues[0] != "omitempty" {
name = tagValues[0]
}
if thrifttag := stag.Get("thrift"); thrifttag != "" {
ts := strings.Split(thrifttag, ",")
if ts[0] != "" {
name = ts[0]
}
}
if required := stag.Get("required"); required != "" {
m.Required = append(m.Required, name)
}
if desc := stag.Get("description"); desc != "" {
mp.Description = desc
}
m.Properties[name] = mp
}
if ignore := stag.Get("ignore"); ignore != "" {
continue
}
}
}
}
} }
} }
} }
@ -716,6 +629,106 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
return return
} }
func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string, astPkgs map[string]*ast.Package) {
ts, ok := d.Decl.(*ast.TypeSpec)
if !ok {
ColorLog("Unknown type without TypeSec: %v\n", d)
os.Exit(1)
}
// TODO support other types, such as `ArrayType`, `MapType`, `InterfaceType` etc...
st, ok := ts.Type.(*ast.StructType)
if !ok {
return
}
m.Title = k
if st.Fields.List != nil {
m.Properties = make(map[string]swagger.Propertie)
for _, field := range st.Fields.List {
isSlice, realType, sType := typeAnalyser(field)
*realTypes = append(*realTypes, realType)
mp := swagger.Propertie{}
if isSlice {
mp.Type = "array"
if isBasicType(realType) {
typeFormat := strings.Split(sType, ":")
mp.Items = &swagger.Propertie{
Type: typeFormat[0],
Format: typeFormat[1],
}
} else {
mp.Items = &swagger.Propertie{
Ref: "#/definitions/" + realType,
}
}
} else {
if isBasicType(realType) {
typeFormat := strings.Split(sType, ":")
mp.Type = typeFormat[0]
mp.Format = typeFormat[1]
} else if sType == "object" {
mp.Ref = "#/definitions/" + realType
}
}
if field.Names != nil {
// set property name as field name
var name = field.Names[0].Name
// if no tag skip tag processing
if field.Tag == nil {
m.Properties[name] = mp
continue
}
var tagValues []string
stag := reflect.StructTag(strings.Trim(field.Tag.Value, "`"))
tag := stag.Get("json")
if tag != "" {
tagValues = strings.Split(tag, ",")
}
// dont add property if json tag first value is "-"
if len(tagValues) == 0 || tagValues[0] != "-" {
// set property name to the left most json tag value only if is not omitempty
if len(tagValues) > 0 && tagValues[0] != "omitempty" {
name = tagValues[0]
}
if thrifttag := stag.Get("thrift"); thrifttag != "" {
ts := strings.Split(thrifttag, ",")
if ts[0] != "" {
name = ts[0]
}
}
if required := stag.Get("required"); required != "" {
m.Required = append(m.Required, name)
}
if desc := stag.Get("description"); desc != "" {
mp.Description = desc
}
m.Properties[name] = mp
}
if ignore := stag.Get("ignore"); ignore != "" {
continue
}
} else {
for _, pkg := range astPkgs {
for _, fl := range pkg.Files {
for nameOfObj, obj := range fl.Scope.Objects {
if obj.Name == fmt.Sprint(field.Type) {
parseObject(obj, nameOfObj, m, realTypes, astPkgs)
}
}
}
}
}
}
}
}
func typeAnalyser(f *ast.Field) (isSlice bool, realType, swaggerType string) { func typeAnalyser(f *ast.Field) (isSlice bool, realType, swaggerType string) {
if arr, ok := f.Type.(*ast.ArrayType); ok { if arr, ok := f.Type.(*ast.ArrayType); ok {
if isBasicType(fmt.Sprint(arr.Elt)) { if isBasicType(fmt.Sprint(arr.Elt)) {