2014-06-19 13:09:17 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-06-18 04:19:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego/swagger"
|
|
|
|
"github.com/astaxie/beego/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var globalDocsTemplate = `package docs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
"github.com/astaxie/beego/swagger"
|
|
|
|
)
|
|
|
|
|
|
|
|
var rootinfo string = {{.rootinfo}}
|
|
|
|
var subapi string = {{.subapi}}
|
|
|
|
var rootapi swagger.ResourceListing
|
|
|
|
|
|
|
|
var apilist map[string]*swagger.ApiDeclaration
|
|
|
|
|
|
|
|
func init() {
|
2014-06-20 04:01:58 +00:00
|
|
|
basepath := "{{.version}}"
|
2014-06-18 04:19:03 +00:00
|
|
|
err := json.Unmarshal([]byte(rootinfo), &rootapi)
|
|
|
|
if err != nil {
|
|
|
|
beego.Error(err)
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(subapi), &apilist)
|
|
|
|
if err != nil {
|
|
|
|
beego.Error(err)
|
|
|
|
}
|
|
|
|
beego.GlobalDocApi["Root"] = rootapi
|
|
|
|
for k, v := range apilist {
|
|
|
|
for i, a := range v.Apis {
|
|
|
|
a.Path = urlReplace(k + a.Path)
|
|
|
|
v.Apis[i] = a
|
|
|
|
}
|
|
|
|
if beego.HttpAddr != "" {
|
|
|
|
v.BasePath = beego.HttpAddr + ":" + strconv.Itoa(beego.HttpPort) + basepath
|
|
|
|
} else {
|
|
|
|
v.BasePath = "http://127.0.0.1:" + strconv.Itoa(beego.HttpPort) + basepath
|
|
|
|
}
|
|
|
|
beego.GlobalDocApi[strings.Trim(k, "/")] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:] + "}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(pt, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
var pkgCache map[string]bool //pkg:controller:function:comments comments: key:value
|
|
|
|
var controllerComments map[string]string
|
|
|
|
var importlist map[string]string
|
|
|
|
var apilist map[string]*swagger.ApiDeclaration
|
|
|
|
var controllerList map[string][]swagger.Api
|
2014-06-19 08:40:55 +00:00
|
|
|
var modelsList map[string][]swagger.Model
|
2014-06-18 04:19:03 +00:00
|
|
|
var rootapi swagger.ResourceListing
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdGenerate.Run = generateCode
|
|
|
|
pkgCache = make(map[string]bool)
|
|
|
|
controllerComments = make(map[string]string)
|
|
|
|
importlist = make(map[string]string)
|
|
|
|
apilist = make(map[string]*swagger.ApiDeclaration)
|
|
|
|
controllerList = make(map[string][]swagger.Api)
|
2014-06-19 08:40:55 +00:00
|
|
|
modelsList = make(map[string][]swagger.Model)
|
2014-06-18 04:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateDocs(curpath string) {
|
|
|
|
fset := token.NewFileSet()
|
|
|
|
|
|
|
|
f, err := parser.ParseFile(fset, path.Join(curpath, "routers", "router.go"), nil, parser.ParseComments)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ColorLog("[ERRO] parse router.go error\n")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
rootapi.Infos = swagger.Infomation{}
|
|
|
|
rootapi.SwaggerVersion = swagger.SwaggerVersion
|
|
|
|
//analysis API comments
|
|
|
|
if f.Comments != nil {
|
|
|
|
for _, c := range f.Comments {
|
|
|
|
for _, s := range strings.Split(c.Text(), "\n") {
|
|
|
|
if strings.HasPrefix(s, "@APIVersion") {
|
2014-06-18 10:50:08 +00:00
|
|
|
rootapi.ApiVersion = s[len("@APIVersion "):]
|
2014-06-18 04:19:03 +00:00
|
|
|
} else if strings.HasPrefix(s, "@Title") {
|
2014-06-18 10:50:08 +00:00
|
|
|
rootapi.Infos.Title = s[len("@Title "):]
|
2014-06-18 04:19:03 +00:00
|
|
|
} else if strings.HasPrefix(s, "@Description") {
|
2014-06-18 10:50:08 +00:00
|
|
|
rootapi.Infos.Description = s[len("@Description "):]
|
2014-06-18 04:19:03 +00:00
|
|
|
} else if strings.HasPrefix(s, "@TermsOfServiceUrl") {
|
2014-06-18 10:50:08 +00:00
|
|
|
rootapi.Infos.TermsOfServiceUrl = s[len("@TermsOfServiceUrl "):]
|
2014-06-18 04:19:03 +00:00
|
|
|
} else if strings.HasPrefix(s, "@Contact") {
|
2014-06-18 10:50:08 +00:00
|
|
|
rootapi.Infos.Contact = s[len("@Contact "):]
|
2014-06-18 04:19:03 +00:00
|
|
|
} else if strings.HasPrefix(s, "@License") {
|
2014-06-18 10:50:08 +00:00
|
|
|
rootapi.Infos.License = s[len("@License "):]
|
2014-06-18 04:19:03 +00:00
|
|
|
} else if strings.HasPrefix(s, "@LicenseUrl") {
|
2014-06-18 10:50:08 +00:00
|
|
|
rootapi.Infos.LicenseUrl = s[len("@LicenseUrl "):]
|
2014-06-18 04:19:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, im := range f.Imports {
|
|
|
|
analisyscontrollerPkg(im.Path.Value)
|
|
|
|
}
|
|
|
|
for _, d := range f.Decls {
|
|
|
|
switch specDecl := d.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
for _, l := range specDecl.Body.List {
|
|
|
|
switch smtp := l.(type) {
|
|
|
|
case *ast.AssignStmt:
|
|
|
|
for _, l := range smtp.Rhs {
|
|
|
|
f, params := analisysNewNamespace(l.(*ast.CallExpr))
|
2014-06-20 04:01:58 +00:00
|
|
|
globalDocsTemplate = strings.Replace(globalDocsTemplate, "{{.version}}", f, -1)
|
2014-06-18 04:19:03 +00:00
|
|
|
for _, p := range params {
|
|
|
|
switch pp := p.(type) {
|
|
|
|
case *ast.CallExpr:
|
|
|
|
if selname := pp.Fun.(*ast.SelectorExpr).Sel.String(); selname == "NSNamespace" {
|
|
|
|
s, params := analisysNewNamespace(pp)
|
|
|
|
subapi := swagger.ApiRef{Path: s}
|
|
|
|
controllerName := ""
|
|
|
|
for _, sp := range params {
|
|
|
|
switch pp := sp.(type) {
|
|
|
|
case *ast.CallExpr:
|
|
|
|
if pp.Fun.(*ast.SelectorExpr).Sel.String() == "NSInclude" {
|
|
|
|
controllerName = analisysNSInclude(s, pp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := controllerComments[controllerName]; ok {
|
|
|
|
subapi.Description = v
|
|
|
|
}
|
|
|
|
rootapi.Apis = append(rootapi.Apis, subapi)
|
|
|
|
} else if selname == "NSInclude" {
|
|
|
|
analisysNSInclude(f, pp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
apiinfo, err := json.Marshal(rootapi)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
subapi, err := json.Marshal(apilist)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
os.Mkdir(path.Join(curpath, "docs"), 0755)
|
|
|
|
fd, err := os.Create(path.Join(curpath, "docs", "docs.go"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
a := strings.Replace(globalDocsTemplate, "{{.rootinfo}}", "`"+string(apiinfo)+"`", -1)
|
|
|
|
a = strings.Replace(a, "{{.subapi}}", "`"+string(subapi)+"`", -1)
|
|
|
|
fd.WriteString(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func analisysNewNamespace(ce *ast.CallExpr) (first string, others []ast.Expr) {
|
|
|
|
for i, p := range ce.Args {
|
|
|
|
if i == 0 {
|
|
|
|
switch pp := p.(type) {
|
|
|
|
case *ast.BasicLit:
|
|
|
|
first = strings.Trim(pp.Value, `"`)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
others = append(others, p)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func analisysNSInclude(baseurl string, ce *ast.CallExpr) string {
|
|
|
|
cname := ""
|
|
|
|
a := &swagger.ApiDeclaration{}
|
|
|
|
a.ApiVersion = rootapi.ApiVersion
|
|
|
|
a.SwaggerVersion = swagger.SwaggerVersion
|
|
|
|
a.ResourcePath = baseurl
|
|
|
|
a.Produces = []string{"application/json", "application/xml", "text/plain", "text/html"}
|
|
|
|
a.Apis = make([]swagger.Api, 0)
|
2014-06-19 08:40:55 +00:00
|
|
|
a.Models = make(map[string]swagger.Model)
|
2014-06-18 04:19:03 +00:00
|
|
|
for _, p := range ce.Args {
|
|
|
|
x := p.(*ast.UnaryExpr).X.(*ast.CompositeLit).Type.(*ast.SelectorExpr)
|
|
|
|
if v, ok := importlist[fmt.Sprint(x.X)]; ok {
|
|
|
|
cname = v + x.Sel.Name
|
|
|
|
}
|
|
|
|
if apis, ok := controllerList[cname]; ok {
|
|
|
|
if len(a.Apis) > 0 {
|
|
|
|
a.Apis = append(a.Apis, apis...)
|
|
|
|
} else {
|
|
|
|
a.Apis = apis
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 08:40:55 +00:00
|
|
|
if models, ok := modelsList[cname]; ok {
|
|
|
|
for _, m := range models {
|
|
|
|
a.Models[m.Id] = m
|
|
|
|
}
|
|
|
|
}
|
2014-06-18 04:19:03 +00:00
|
|
|
}
|
|
|
|
apilist[baseurl] = a
|
|
|
|
return cname
|
|
|
|
}
|
|
|
|
|
|
|
|
func analisyscontrollerPkg(pkgpath string) {
|
|
|
|
pkgpath = strings.Trim(pkgpath, "\"")
|
|
|
|
pps := strings.Split(pkgpath, "/")
|
|
|
|
importlist[pps[len(pps)-1]] = pkgpath
|
|
|
|
if pkgpath == "github.com/astaxie/beego" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gopath := os.Getenv("GOPATH")
|
|
|
|
if gopath == "" {
|
|
|
|
panic("please set gopath")
|
|
|
|
}
|
|
|
|
pkgRealpath := ""
|
|
|
|
|
|
|
|
wgopath := filepath.SplitList(gopath)
|
|
|
|
for _, wg := range wgopath {
|
|
|
|
wg, _ = filepath.EvalSymlinks(filepath.Join(wg, "src", pkgpath))
|
|
|
|
if utils.FileExists(wg) {
|
|
|
|
pkgRealpath = wg
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pkgRealpath != "" {
|
|
|
|
if _, ok := pkgCache[pkgpath]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ColorLog("[ERRO] the %s pkg not exist in gopath\n", pkgpath)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fileSet := token.NewFileSet()
|
|
|
|
astPkgs, err := parser.ParseDir(fileSet, pkgRealpath, func(info os.FileInfo) bool {
|
|
|
|
name := info.Name()
|
|
|
|
return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
|
|
|
|
}, parser.ParseComments)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ColorLog("[ERRO] the %s pkg parser.ParseDir error\n", pkgpath)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
for _, pkg := range astPkgs {
|
|
|
|
for _, fl := range pkg.Files {
|
|
|
|
for _, d := range fl.Decls {
|
|
|
|
switch specDecl := d.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
if specDecl.Recv != nil && len(specDecl.Recv.List) > 0 {
|
|
|
|
if t, ok := specDecl.Recv.List[0].Type.(*ast.StarExpr); ok {
|
|
|
|
parserComments(specDecl.Doc, specDecl.Name.String(), fmt.Sprint(t.X), pkgpath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *ast.GenDecl:
|
|
|
|
if specDecl.Tok.String() == "type" {
|
|
|
|
for _, s := range specDecl.Specs {
|
|
|
|
switch tp := s.(*ast.TypeSpec).Type.(type) {
|
|
|
|
case *ast.StructType:
|
|
|
|
_ = tp.Struct
|
|
|
|
controllerComments[pkgpath+s.(*ast.TypeSpec).Name.String()] = specDecl.Doc.Text()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the func comments
|
|
|
|
func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpath string) error {
|
|
|
|
innerapi := swagger.Api{}
|
|
|
|
opts := swagger.Operation{}
|
|
|
|
if comments != nil && comments.List != nil {
|
|
|
|
for _, c := range comments.List {
|
|
|
|
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
|
|
|
|
if strings.HasPrefix(t, "@router") {
|
2014-06-18 10:50:08 +00:00
|
|
|
elements := t[len("@router "):]
|
2014-06-18 04:19:03 +00:00
|
|
|
e1 := strings.SplitN(elements, " ", 2)
|
|
|
|
if len(e1) < 1 {
|
|
|
|
return errors.New("you should has router infomation")
|
|
|
|
}
|
|
|
|
innerapi.Path = e1[0]
|
|
|
|
if len(e1) == 2 && e1[1] != "" {
|
|
|
|
e1 = strings.SplitN(e1[1], " ", 2)
|
|
|
|
opts.HttpMethod = strings.ToUpper(strings.Trim(e1[0], "[]"))
|
|
|
|
} else {
|
|
|
|
opts.HttpMethod = "GET"
|
|
|
|
}
|
|
|
|
} else if strings.HasPrefix(t, "@Title") {
|
2014-06-18 10:50:08 +00:00
|
|
|
opts.Nickname = t[len("@Title "):]
|
2014-06-18 04:19:03 +00:00
|
|
|
} else if strings.HasPrefix(t, "@Description") {
|
2014-06-18 10:50:08 +00:00
|
|
|
opts.Summary = t[len("@Description "):]
|
2014-06-18 04:19:03 +00:00
|
|
|
} else if strings.HasPrefix(t, "@Success") {
|
|
|
|
rs := swagger.ResponseMessage{}
|
2014-06-18 10:50:08 +00:00
|
|
|
st := strings.Split(t[len("@Success "):], " ")
|
2014-06-19 08:40:55 +00:00
|
|
|
rs.Message = st[2]
|
|
|
|
if st[1] == "{object}" {
|
|
|
|
m, mod := getModel(st[2])
|
|
|
|
rs.ResponseModel = m
|
|
|
|
if _, ok := modelsList[pkgpath+controllerName]; ok {
|
|
|
|
modelsList[pkgpath+controllerName] = append(modelsList[pkgpath+controllerName], mod)
|
|
|
|
} else {
|
|
|
|
modelsList[pkgpath+controllerName] = make([]swagger.Model, 1)
|
|
|
|
modelsList[pkgpath+controllerName][0] = mod
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 04:19:03 +00:00
|
|
|
rs.Code, _ = strconv.Atoi(st[0])
|
|
|
|
opts.ResponseMessages = append(opts.ResponseMessages, rs)
|
|
|
|
} else if strings.HasPrefix(t, "@Param") {
|
|
|
|
para := swagger.Parameter{}
|
2014-06-18 10:50:08 +00:00
|
|
|
p := getparams(strings.TrimSpace(t[len("@Param "):]))
|
2014-06-18 04:19:03 +00:00
|
|
|
para.Name = p[0]
|
|
|
|
para.ParamType = p[1]
|
|
|
|
para.DataType = p[2]
|
|
|
|
if len(p) > 4 {
|
|
|
|
para.Required, _ = strconv.ParseBool(p[3])
|
|
|
|
para.Description = p[4]
|
|
|
|
} else {
|
|
|
|
para.Description = p[3]
|
|
|
|
}
|
|
|
|
opts.Parameters = append(opts.Parameters, para)
|
|
|
|
} else if strings.HasPrefix(t, "@Failure") {
|
|
|
|
rs := swagger.ResponseMessage{}
|
2014-06-18 10:50:08 +00:00
|
|
|
st := t[len("@Failure "):]
|
2014-06-18 04:19:03 +00:00
|
|
|
var cd []rune
|
|
|
|
for i, s := range st {
|
|
|
|
if s == ' ' {
|
|
|
|
rs.Message = st[i+1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cd = append(cd, s)
|
|
|
|
}
|
|
|
|
rs.Code, _ = strconv.Atoi(string(cd))
|
|
|
|
opts.ResponseMessages = append(opts.ResponseMessages, rs)
|
|
|
|
} else if strings.HasPrefix(t, "@Type") {
|
2014-06-18 10:50:08 +00:00
|
|
|
opts.Type = t[len("@Type "):]
|
2014-06-18 04:19:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
innerapi.Operations = append(innerapi.Operations, opts)
|
|
|
|
if innerapi.Path != "" {
|
|
|
|
if _, ok := controllerList[pkgpath+controllerName]; ok {
|
|
|
|
controllerList[pkgpath+controllerName] = append(controllerList[pkgpath+controllerName], innerapi)
|
|
|
|
} else {
|
|
|
|
controllerList[pkgpath+controllerName] = make([]swagger.Api, 1)
|
|
|
|
controllerList[pkgpath+controllerName][0] = innerapi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// analisys params return []string
|
|
|
|
// @Param query form string true "The email for login"
|
|
|
|
// [query form string true "The email for login"]
|
|
|
|
func getparams(str string) []string {
|
|
|
|
var s []byte
|
|
|
|
var j int
|
|
|
|
var start bool
|
|
|
|
var r []string
|
|
|
|
for i, c := range []byte(str) {
|
|
|
|
if c == ' ' || c == '\t' || c == '\n' {
|
|
|
|
if !start {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if j == 3 {
|
|
|
|
r = append(r, string(s))
|
|
|
|
r = append(r, strings.Trim((str[i+1:]), " \"\t\n"))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
start = false
|
|
|
|
j++
|
|
|
|
r = append(r, string(s))
|
|
|
|
s = make([]byte, 0)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
start = true
|
|
|
|
s = append(s, c)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
2014-06-19 08:40:55 +00:00
|
|
|
|
|
|
|
func getModel(str string) (objectname string, m swagger.Model) {
|
|
|
|
strs := strings.Split(str, ".")
|
|
|
|
objectname = strs[len(strs)-1]
|
|
|
|
pkgpath := strings.Join(strs[:len(strs)-1], "/")
|
|
|
|
curpath, _ := os.Getwd()
|
|
|
|
pkgRealpath := path.Join(curpath, pkgpath)
|
|
|
|
fileSet := token.NewFileSet()
|
|
|
|
astPkgs, err := parser.ParseDir(fileSet, pkgRealpath, func(info os.FileInfo) bool {
|
|
|
|
name := info.Name()
|
|
|
|
return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
|
|
|
|
}, parser.ParseComments)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ColorLog("[ERRO] the model %s parser.ParseDir error\n", str)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range astPkgs {
|
|
|
|
for _, fl := range pkg.Files {
|
|
|
|
for k, d := range fl.Scope.Objects {
|
|
|
|
if d.Kind == ast.Typ {
|
|
|
|
if k != objectname {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ts, ok := d.Decl.(*ast.TypeSpec)
|
|
|
|
if !ok {
|
|
|
|
ColorLog("Unknown type without TypeSec: %v", d)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
st, ok := ts.Type.(*ast.StructType)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m.Id = k
|
|
|
|
if st.Fields.List != nil {
|
|
|
|
m.Properties = make(map[string]swagger.ModelProperty)
|
|
|
|
for _, field := range st.Fields.List {
|
|
|
|
mp := swagger.ModelProperty{}
|
|
|
|
mp.Type = fmt.Sprint(field.Type)
|
|
|
|
mp.Description = strings.Trim(field.Tag.Value, "`")
|
|
|
|
m.Properties[field.Names[0].Name] = mp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.Id == "" {
|
|
|
|
ColorLog("can't find the object: %v", str)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|