From 7e605092312175f0c6946236ba72772d53bd9059 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Thu, 25 Aug 2016 19:32:39 +0300 Subject: [PATCH 1/8] Separate and add @Description with @Summary params @Description -> is "descriptions" in swagger @Summary -> is "summary" in swagger this different options. --- g_docs.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/g_docs.go b/g_docs.go index 6c1e870..66cd973 100644 --- a/g_docs.go +++ b/g_docs.go @@ -354,7 +354,9 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat } else if strings.HasPrefix(t, "@Title") { opts.OperationID = controllerName + "." + strings.TrimSpace(t[len("@Title"):]) } 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") { ss := strings.TrimSpace(t[len("@Success"):]) rs := swagger.Response{} From 004778c662d0445eb993488e8c8a55cde72cc103 Mon Sep 17 00:00:00 2001 From: Sergey Lanzman Date: Thu, 25 Aug 2016 01:42:23 +0300 Subject: [PATCH 2/8] swagger add embedded struct --- g_docs.go | 191 +++++++++++++++++++++++++++++------------------------- 1 file changed, 101 insertions(+), 90 deletions(-) diff --git a/g_docs.go b/g_docs.go index 25cf451..7cda8a7 100644 --- a/g_docs.go +++ b/g_docs.go @@ -610,96 +610,7 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp if k != objectname { continue } - 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 { - 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 - } - } - } - } + parseObject(d, k, &m, &realTypes, astPkgs) } } } @@ -716,6 +627,106 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp 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) { if arr, ok := f.Type.(*ast.ArrayType); ok { if isBasicType(fmt.Sprint(arr.Elt)) { From f45388586405a1241a3cdb324bff5d541531389e Mon Sep 17 00:00:00 2001 From: astaxie Date: Tue, 30 Aug 2016 00:18:17 +0800 Subject: [PATCH 3/8] update to 1.5.1 --- bee.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bee.go b/bee.go index 28ae437..76c5359 100644 --- a/bee.go +++ b/bee.go @@ -25,7 +25,7 @@ import ( "strings" ) -const version = "1.5.0" +const version = "1.5.1" type Command struct { // Run runs the command. From f6a219fb3a4f3c845bfddda7e1333141bba372d5 Mon Sep 17 00:00:00 2001 From: CodyGuo Date: Sun, 4 Sep 2016 13:53:45 +0800 Subject: [PATCH 4/8] Fix to issue #270 --- apiapp.go | 1 + 1 file changed, 1 insertion(+) diff --git a/apiapp.go b/apiapp.go index 4d4e58a..27e538d 100644 --- a/apiapp.go +++ b/apiapp.go @@ -650,6 +650,7 @@ func checkEnv(appname string) (apppath, packpath string, err error) { for _, gpath := range gps { gsrcpath := path.Join(gpath, "src") if strings.HasPrefix(currpath, gsrcpath) { + packpath = strings.Replace(currpath[len(gsrcpath)+1:], string(path.Separator), "/", -1) return currpath, currpath[len(gsrcpath)+1:], nil } } From ed67ef47e6825c5bfeb089559bf42fa44d83c394 Mon Sep 17 00:00:00 2001 From: CodyGuo Date: Sun, 4 Sep 2016 20:45:11 +0800 Subject: [PATCH 5/8] Fix to issue #270 --- apiapp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apiapp.go b/apiapp.go index 27e538d..892719b 100644 --- a/apiapp.go +++ b/apiapp.go @@ -651,7 +651,7 @@ func checkEnv(appname string) (apppath, packpath string, err error) { gsrcpath := path.Join(gpath, "src") if strings.HasPrefix(currpath, gsrcpath) { packpath = strings.Replace(currpath[len(gsrcpath)+1:], string(path.Separator), "/", -1) - return currpath, currpath[len(gsrcpath)+1:], nil + return currpath, packpath, nil } } From ad34b7be2a3dbbaa1ab6d113cea86ad022ba89bf Mon Sep 17 00:00:00 2001 From: ZetaChow Date: Fri, 9 Sep 2016 15:20:59 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=EF=BC=8C=E4=BB=A5=E7=AC=A6=E5=90=88Lint?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g_appcode.go | 8 +++++++- g_controllers.go | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/g_appcode.go b/g_appcode.go index 76d78ad..5d44663 100644 --- a/g_appcode.go +++ b/g_appcode.go @@ -1168,11 +1168,12 @@ import ( "github.com/astaxie/beego" ) -// oprations for {{ctrlName}} +// {{ctrlName}}Controller oprations for {{ctrlName}} type {{ctrlName}}Controller struct { beego.Controller } +// URLMapping ... func (c *{{ctrlName}}Controller) URLMapping() { c.Mapping("Post", c.Post) c.Mapping("GetOne", c.GetOne) @@ -1181,6 +1182,7 @@ func (c *{{ctrlName}}Controller) URLMapping() { c.Mapping("Delete", c.Delete) } +// Post ... // @Title Post // @Description create {{ctrlName}} // @Param body body models.{{ctrlName}} true "body for {{ctrlName}} content" @@ -1202,6 +1204,7 @@ func (c *{{ctrlName}}Controller) Post() { c.ServeJSON() } +// Get ... // @Title Get // @Description get {{ctrlName}} by id // @Param id path string true "The key for staticblock" @@ -1220,6 +1223,7 @@ func (c *{{ctrlName}}Controller) GetOne() { c.ServeJSON() } +// GetAll ... // @Title Get All // @Description get {{ctrlName}} // @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..." @@ -1282,6 +1286,7 @@ func (c *{{ctrlName}}Controller) GetAll() { c.ServeJSON() } +// Update ... // @Title Update // @Description update the {{ctrlName}} // @Param id path string true "The id you want to update" @@ -1305,6 +1310,7 @@ func (c *{{ctrlName}}Controller) Put() { c.ServeJSON() } +// Delete ... // @Title Delete // @Description delete the {{ctrlName}} // @Param id path string true "The id you want to delete" diff --git a/g_controllers.go b/g_controllers.go index 79ed554..15ab0bd 100644 --- a/g_controllers.go +++ b/g_controllers.go @@ -82,11 +82,12 @@ import ( "github.com/astaxie/beego" ) -// operations for {{controllerName}} +// {{controllerName}}Controller operations for {{controllerName}} type {{controllerName}}Controller struct { beego.Controller } +// URLMapping ... func (c *{{controllerName}}Controller) URLMapping() { c.Mapping("Post", c.Post) c.Mapping("GetOne", c.GetOne) @@ -95,6 +96,7 @@ func (c *{{controllerName}}Controller) URLMapping() { c.Mapping("Delete", c.Delete) } +// Post ... // @Title Create // @Description create {{controllerName}} // @Param body body models.{{controllerName}} true "body for {{controllerName}} content" @@ -105,6 +107,7 @@ func (c *{{controllerName}}Controller) Post() { } +// GetOne ... // @Title GetOne // @Description get {{controllerName}} by id // @Param id path string true "The key for staticblock" @@ -115,6 +118,7 @@ func (c *{{controllerName}}Controller) GetOne() { } +// GetAll ... // @Title GetAll // @Description get {{controllerName}} // @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..." @@ -130,6 +134,7 @@ func (c *{{controllerName}}Controller) GetAll() { } +// Update ... // @Title Update // @Description update the {{controllerName}} // @Param id path string true "The id you want to update" @@ -141,6 +146,7 @@ func (c *{{controllerName}}Controller) Put() { } +// Delete ... // @Title Delete // @Description delete the {{controllerName}} // @Param id path string true "The id you want to delete" @@ -164,11 +170,12 @@ import ( "github.com/astaxie/beego" ) -// oprations for {{controllerName}} +// {{controllerName}}Controller oprations for {{controllerName}} type {{controllerName}}Controller struct { beego.Controller } +// URLMapping ... func (c *{{controllerName}}Controller) URLMapping() { c.Mapping("Post", c.Post) c.Mapping("GetOne", c.GetOne) @@ -177,6 +184,7 @@ func (c *{{controllerName}}Controller) URLMapping() { c.Mapping("Delete", c.Delete) } +// Post ... // @Title Post // @Description create {{controllerName}} // @Param body body models.{{controllerName}} true "body for {{controllerName}} content" @@ -195,6 +203,7 @@ func (c *{{controllerName}}Controller) Post() { c.ServeJSON() } +// Get ... // @Title Get // @Description get {{controllerName}} by id // @Param id path string true "The key for staticblock" @@ -213,6 +222,7 @@ func (c *{{controllerName}}Controller) GetOne() { c.ServeJSON() } +// GetAll ... // @Title Get All // @Description get {{controllerName}} // @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..." @@ -275,6 +285,7 @@ func (c *{{controllerName}}Controller) GetAll() { c.ServeJSON() } +// Update ... // @Title Update // @Description update the {{controllerName}} // @Param id path string true "The id you want to update" @@ -295,6 +306,7 @@ func (c *{{controllerName}}Controller) Put() { c.ServeJSON() } +// Delete ... // @Title Delete // @Description delete the {{controllerName}} // @Param id path string true "The id you want to delete" From 411aa430805140f9b56b5adbf71a295e189399d6 Mon Sep 17 00:00:00 2001 From: ZetaChow Date: Fri, 9 Sep 2016 15:27:36 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9PUT=E5=92=8CGet=20One=20?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=9A=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g_appcode.go | 8 ++++---- g_controllers.go | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/g_appcode.go b/g_appcode.go index 5d44663..9276360 100644 --- a/g_appcode.go +++ b/g_appcode.go @@ -1204,8 +1204,8 @@ func (c *{{ctrlName}}Controller) Post() { c.ServeJSON() } -// Get ... -// @Title Get +// GetOne ... +// @Title Get One // @Description get {{ctrlName}} by id // @Param id path string true "The key for staticblock" // @Success 200 {object} models.{{ctrlName}} @@ -1286,8 +1286,8 @@ func (c *{{ctrlName}}Controller) GetAll() { c.ServeJSON() } -// Update ... -// @Title Update +// Put ... +// @Title Put // @Description update the {{ctrlName}} // @Param id path string true "The id you want to update" // @Param body body models.{{ctrlName}} true "body for {{ctrlName}} content" diff --git a/g_controllers.go b/g_controllers.go index 15ab0bd..7c782b6 100644 --- a/g_controllers.go +++ b/g_controllers.go @@ -134,8 +134,8 @@ func (c *{{controllerName}}Controller) GetAll() { } -// Update ... -// @Title Update +// Put ... +// @Title Put // @Description update the {{controllerName}} // @Param id path string true "The id you want to update" // @Param body body models.{{controllerName}} true "body for {{controllerName}} content" @@ -203,8 +203,8 @@ func (c *{{controllerName}}Controller) Post() { c.ServeJSON() } -// Get ... -// @Title Get +// GetOne ... +// @Title Get One // @Description get {{controllerName}} by id // @Param id path string true "The key for staticblock" // @Success 200 {object} models.{{controllerName}} @@ -285,8 +285,8 @@ func (c *{{controllerName}}Controller) GetAll() { c.ServeJSON() } -// Update ... -// @Title Update +// Put ... +// @Title Put // @Description update the {{controllerName}} // @Param id path string true "The id you want to update" // @Param body body models.{{controllerName}} true "body for {{controllerName}} content" From 0d0bea81bf67f667ec45441e07a1f94a74a881a3 Mon Sep 17 00:00:00 2001 From: ZetaChow Date: Fri, 9 Sep 2016 15:38:44 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E5=8E=BB=E6=8E=89offset(int64)=E8=B5=8B0?= =?UTF-8?q?=E5=80=BC,=E5=8E=BB=E6=8E=89query=20(map[...]...)=E5=AE=9A?= =?UTF-8?q?=E4=B9=89,=E4=BB=A5=E7=AC=A6=E5=90=88Lint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g_appcode.go | 4 ++-- g_controllers.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/g_appcode.go b/g_appcode.go index 9276360..4e90107 100644 --- a/g_appcode.go +++ b/g_appcode.go @@ -1239,9 +1239,9 @@ func (c *{{ctrlName}}Controller) GetAll() { var fields []string var sortby []string var order []string - var query map[string]string = make(map[string]string) + var query = make(map[string]string) var limit int64 = 10 - var offset int64 = 0 + var offset int64 // fields: col1,col2,entity.col3 if v := c.GetString("fields"); v != "" { diff --git a/g_controllers.go b/g_controllers.go index 7c782b6..9d0b0ae 100644 --- a/g_controllers.go +++ b/g_controllers.go @@ -238,9 +238,9 @@ func (c *{{controllerName}}Controller) GetAll() { var fields []string var sortby []string var order []string - var query map[string]string = make(map[string]string) + var query = make(map[string]string) var limit int64 = 10 - var offset int64 = 0 + var offset int64 // fields: col1,col2,entity.col3 if v := c.GetString("fields"); v != "" {