From 4ebf7eab65bf47b12beae5958175364b1adf3c6a Mon Sep 17 00:00:00 2001 From: Sergey Lanzman Date: Sun, 25 Sep 2016 01:34:26 +0300 Subject: [PATCH 1/7] add support multi package with gaoling way myPackage.struct --- g_docs.go | 127 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 82 insertions(+), 45 deletions(-) diff --git a/g_docs.go b/g_docs.go index a92c917..c74abdc 100644 --- a/g_docs.go +++ b/g_docs.go @@ -33,6 +33,8 @@ import ( "gopkg.in/yaml.v2" + "io/ioutil" + "github.com/astaxie/beego/swagger" "github.com/astaxie/beego/utils" ) @@ -50,6 +52,30 @@ var importlist map[string]string var controllerList map[string]map[string]*swagger.Item //controllername Paths items var modelsList map[string]map[string]swagger.Schema var rootapi swagger.Swagger +var astPkgs map[string]*ast.Package + +// refer to builtin.go +var basicTypes = map[string]string{ + "bool": "boolean:", + "uint": "integer:int32", + "uint8": "integer:int32", + "uint16": "integer:int32", + "uint32": "integer:int32", + "uint64": "integer:int64", + "int": "integer:int64", + "int8": "integer:int32", + "int16:int32": "integer:int32", + "int32": "integer:int32", + "int64": "integer:int64", + "uintptr": "integer:int64", + "float32": "number:float", + "float64": "number:double", + "string": "string:", + "complex64": "number:float", + "complex128": "number:double", + "byte": "string:byte", + "rune": "string:byte", +} func init() { pkgCache = make(map[string]struct{}) @@ -57,6 +83,35 @@ func init() { importlist = make(map[string]string) controllerList = make(map[string]map[string]*swagger.Item) modelsList = make(map[string]map[string]swagger.Schema) + curPath, _ := os.Getwd() + astPkgs = map[string]*ast.Package{} + parsePackagesFromDir(curPath) +} + +func parsePackagesFromDir(path string) { + parsePackageFromDir(path) + list, _ := ioutil.ReadDir(path) + for _, item := range list { + if item.IsDir() && item.Name() != "vendor" { + parsePackagesFromDir(path + "/" + item.Name()) + } + } +} + +func parsePackageFromDir(path string) { + fileSet := token.NewFileSet() + folderPkgs, err := parser.ParseDir(fileSet, path, 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", path) + os.Exit(1) + } + for k, v := range folderPkgs { + astPkgs[k] = v + } } func generateDocs(curpath string) { @@ -414,13 +469,13 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat schema.Type = typeFormat[0] schema.Format = typeFormat[1] } else { - cmpath, m, mod, realTypes := getModel(schemaName) + m, mod, realTypes := getModel(schemaName) schema.Ref = "#/definitions/" + m if _, ok := modelsList[pkgpath+controllerName]; !ok { modelsList[pkgpath+controllerName] = make(map[string]swagger.Schema, 0) } modelsList[pkgpath+controllerName][schemaName] = mod - appendModels(cmpath, pkgpath, controllerName, realTypes) + appendModels(pkgpath, controllerName, realTypes) } if isArray { rs.Schema = &swagger.Schema{ @@ -460,7 +515,7 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat pp := strings.Split(p[2], ".") typ := pp[len(pp)-1] if len(pp) >= 2 { - cmpath, m, mod, realTypes := getModel(p[2]) + m, mod, realTypes := getModel(p[2]) para.Schema = &swagger.Schema{ Ref: "#/definitions/" + m, } @@ -468,7 +523,7 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat modelsList[pkgpath+controllerName] = make(map[string]swagger.Schema, 0) } modelsList[pkgpath+controllerName][typ] = mod - appendModels(cmpath, pkgpath, controllerName, realTypes) + appendModels(pkgpath, controllerName, realTypes) } else { isArray := false paraType := "" @@ -610,22 +665,10 @@ func getparams(str string) []string { return r } -func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTypes []string) { +func getModel(str string) (objectname string, m swagger.Schema, realTypes []string) { 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) - } + packageName := "" m.Type = "object" for _, pkg := range astPkgs { for _, fl := range pkg.Files { @@ -634,7 +677,8 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp if k != objectname { continue } - parseObject(d, k, &m, &realTypes, astPkgs) + packageName = pkg.Name + parseObject(d, k, &m, &realTypes, astPkgs, pkg.Name) } } } @@ -647,11 +691,12 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp if len(rootapi.Definitions) == 0 { rootapi.Definitions = make(map[string]swagger.Schema) } + objectname = packageName + "." + objectname rootapi.Definitions[objectname] = m return } -func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string, astPkgs map[string]*ast.Package) { +func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string, astPkgs map[string]*ast.Package, packageName string) { ts, ok := d.Decl.(*ast.TypeSpec) if !ok { ColorLog("Unknown type without TypeSec: %v\n", d) @@ -666,7 +711,18 @@ func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string if st.Fields.List != nil { m.Properties = make(map[string]swagger.Propertie) for _, field := range st.Fields.List { + realType := "" isSlice, realType, sType := typeAnalyser(field) + if (isSlice && isBasicType(realType)) || sType == "object" { + if len(strings.Split(realType, " ")) > 1 { + realType = strings.Replace(realType, " ", ".", -1) + realType = strings.Replace(realType, "&", "", -1) + realType = strings.Replace(realType, "{", "", -1) + realType = strings.Replace(realType, "}", "", -1) + } else { + realType = packageName + "." + realType + } + } *realTypes = append(*realTypes, realType) mp := swagger.Propertie{} if isSlice { @@ -747,7 +803,7 @@ func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string 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) + parseObject(obj, nameOfObj, m, realTypes, astPkgs, pkg.Name) } } } @@ -793,18 +849,6 @@ func isBasicType(Type string) bool { return false } -// refer to builtin.go -var basicTypes = map[string]string{ - "bool": "boolean:", - "uint": "integer:int32", "uint8": "integer:int32", "uint16": "integer:int32", "uint32": "integer:int32", "uint64": "integer:int64", - "int": "integer:int64", "int8": "integer:int32", "int16:int32": "integer:int32", "int32": "integer:int32", "int64": "integer:int64", - "uintptr": "integer:int64", - "float32": "number:float", "float64": "number:double", - "string": "string:", - "complex64": "number:float", "complex128": "number:double", - "byte": "string:byte", "rune": "string:byte", -} - // regexp get json tag func grepJSONTag(tag string) string { r, _ := regexp.Compile(`json:"([^"]*)"`) @@ -816,23 +860,16 @@ func grepJSONTag(tag string) string { } // append models -func appendModels(cmpath, pkgpath, controllerName string, realTypes []string) { - var p string - if cmpath != "" { - p = strings.Join(strings.Split(cmpath, "/"), ".") + "." - } else { - p = "" - } +func appendModels(pkgpath, controllerName string, realTypes []string) { for _, realType := range realTypes { if realType != "" && !isBasicType(strings.TrimLeft(realType, "[]")) && !strings.HasPrefix(realType, "map") && !strings.HasPrefix(realType, "&") { - if _, ok := modelsList[pkgpath+controllerName][p+realType]; ok { + if _, ok := modelsList[pkgpath+controllerName][realType]; ok { continue } - //fmt.Printf(pkgpath + ":" + controllerName + ":" + cmpath + ":" + realType + "\n") - _, _, mod, newRealTypes := getModel(p + realType) - modelsList[pkgpath+controllerName][p+realType] = mod - appendModels(cmpath, pkgpath, controllerName, newRealTypes) + _, mod, newRealTypes := getModel(realType) + modelsList[pkgpath+controllerName][realType] = mod + appendModels(pkgpath, controllerName, newRealTypes) } } } From 287af20d1a3e4c0a263f7848009f7b8aa20fc1b6 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Mon, 26 Sep 2016 20:25:15 +0200 Subject: [PATCH 2/7] fix: This fixes #247 Moved isBeegoProject(app) method from pack.go to util.go. Rewrite it to walk the application folder tree looking for main files. --- pack.go | 19 ------------------- util.go | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/pack.go b/pack.go index bdf3581..d6d84e4 100644 --- a/pack.go +++ b/pack.go @@ -21,7 +21,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "os" "os/exec" path "path/filepath" @@ -454,24 +453,6 @@ func packDirectory(excludePrefix []string, excludeSuffix []string, return } -func isBeegoProject(thePath string) bool { - fh, _ := os.Open(thePath) - fis, _ := fh.Readdir(-1) - regex := regexp.MustCompile(`(?s)package main.*?import.*?\(.*?github.com/astaxie/beego".*?\).*func main()`) - for _, fi := range fis { - if fi.IsDir() == false && strings.HasSuffix(fi.Name(), ".go") { - data, err := ioutil.ReadFile(path.Join(thePath, fi.Name())) - if err != nil { - continue - } - if len(regex.Find(data)) > 0 { - return true - } - } - } - return false -} - func packApp(cmd *Command, args []string) int { ShowShortVersionBanner() diff --git a/util.go b/util.go index fd2e1ce..32db2f5 100644 --- a/util.go +++ b/util.go @@ -15,14 +15,16 @@ package main import ( + "fmt" + "io/ioutil" "log" "os" + "path" "path/filepath" + "regexp" "runtime" "strings" "time" - "path" - "fmt" ) // Go is a basic promise implementation: it wraps calls a function in a goroutine @@ -35,7 +37,7 @@ func Go(f func() error) chan error { return ch } -// if os.env DEBUG set, debug is on +// Debugf outputs a formtted debug message, when os.env DEBUG is set. func Debugf(format string, a ...interface{}) { if os.Getenv("DEBUG") != "" { _, file, line, ok := runtime.Caller(1) @@ -174,6 +176,37 @@ func GetGOPATHs() []string { return paths } +func isBeegoProject(thePath string) bool { + mainFiles := []string{} + hasBeegoRegex := regexp.MustCompile(`(?s)package main.*?import.*?\(.*?github.com/astaxie/beego".*?\).*func main()`) + // Walk the application path tree to look for main files. + // Main files must satisfy the 'hasBeegoRegex' regular expression. + err := filepath.Walk(thePath, func(fpath string, f os.FileInfo, err error) error { + if !f.IsDir() { // Skip sub-directories + data, _err := ioutil.ReadFile(fpath) + if _err != nil { + return _err + } + if len(hasBeegoRegex.Find(data)) > 0 { + mainFiles = append(mainFiles, fpath) + } + } + return nil + }) + + if err != nil { + log.Fatalf("Unable to walk '%s' tree: %v", thePath, err) + return false + } + + if len(mainFiles) > 0 { + return true + } + return false +} + +// SearchGOPATHs searchs the user GOPATH(s) for the specified application name. +// It returns a boolean, the application's GOPATH and its full path. func SearchGOPATHs(app string) (bool, string, string) { gps := GetGOPATHs() if len(gps) == 0 { From 56568f4d2c60703d10b3bfb4e7326a9a6244a639 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Mon, 26 Sep 2016 20:32:14 +0200 Subject: [PATCH 3/7] tweak: Exit with message instead of a panic In case no application is found in GOPATH(s), exit with a friendly message instead of a panic. Also removed useless else block in watch.go. --- run.go | 8 ++++---- watch.go | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/run.go b/run.go index 8aab6b5..b84e199 100644 --- a/run.go +++ b/run.go @@ -87,7 +87,7 @@ func runApp(cmd *Command, args []string) int { currentGoPath = _gopath appname = path.Base(currpath) } else { - panic(fmt.Sprintf("No Beego application '%s' found in your GOPATH", args[0])) + exitPrint(fmt.Sprintf("No Beego application '%s' found in your GOPATH", args[0])) } ColorLog("[INFO] Using '%s' as 'appname'\n", appname) @@ -103,13 +103,13 @@ func runApp(cmd *Command, args []string) int { Debugf("current path:%s\n", currpath) - if runmode == "prod" || runmode == "dev"{ + if runmode == "prod" || runmode == "dev" { os.Setenv("BEEGO_RUNMODE", runmode) ColorLog("[INFO] Using '%s' as 'runmode'\n", os.Getenv("BEEGO_RUNMODE")) - }else if runmode != ""{ + } else if runmode != "" { os.Setenv("BEEGO_RUNMODE", runmode) ColorLog("[WARN] Using '%s' as 'runmode'\n", os.Getenv("BEEGO_RUNMODE")) - }else if os.Getenv("BEEGO_RUNMODE") != ""{ + } else if os.Getenv("BEEGO_RUNMODE") != "" { ColorLog("[WARN] Using '%s' as 'runmode'\n", os.Getenv("BEEGO_RUNMODE")) } diff --git a/watch.go b/watch.go index efde48a..1c8f593 100644 --- a/watch.go +++ b/watch.go @@ -240,9 +240,8 @@ func shouldIgnoreFile(filename string) bool { } if r.MatchString(filename) { return true - } else { - continue } + continue } return false } From d93e9a4271a69a3f0e7957e72e37b7cb1dd1d82d Mon Sep 17 00:00:00 2001 From: Sergey Lanzman Date: Tue, 27 Sep 2016 18:18:03 +0300 Subject: [PATCH 4/7] Update g_docs.go --- g_docs.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/g_docs.go b/g_docs.go index c74abdc..9d6ffe9 100644 --- a/g_docs.go +++ b/g_docs.go @@ -90,7 +90,11 @@ func init() { func parsePackagesFromDir(path string) { parsePackageFromDir(path) - list, _ := ioutil.ReadDir(path) + list, err := ioutil.ReadDir(path) + if err != nil { + ColorLog("[ERRO] Can't read directory %s : %s\n", path, err) + os.Exit(1) + } for _, item := range list { if item.IsDir() && item.Name() != "vendor" { parsePackagesFromDir(path + "/" + item.Name()) From 31406ebe5a48da20cc37209da700e4c4f1bd13dc Mon Sep 17 00:00:00 2001 From: Konstantin Date: Sat, 8 Oct 2016 11:35:26 +0300 Subject: [PATCH 5/7] Default values in structure Swagger default value for golang stucture. format: doc:"default(my_defaul_value)" type MyStruct struct { Template string `json:"template" doc:"default(

Hello, {{ name }}!

)"` Name string `json:"name"` MyNumber int64 `json:"temp" doc:"default(10)"` MyBool bool `json:"bl" doc:"default(true)"` MyFloat float64 `json:"fl" doc:"default(12.1234)"` } --- g_docs.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/g_docs.go b/g_docs.go index 9d6ffe9..4d9f279 100644 --- a/g_docs.go +++ b/g_docs.go @@ -770,6 +770,30 @@ func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string var tagValues []string stag := reflect.StructTag(strings.Trim(field.Tag.Value, "`")) + + defaultValue := stag.Get("doc") + if defaultValue != ""{ + r, _ := regexp.Compile(`default\((.*)\)`) + if r.MatchString(defaultValue) { + res := r.FindStringSubmatch(defaultValue) + mp.Default = res[1] + switch realType{ + case "int","int64", "int32", "int16", "int8": + mp.Default, _ = strconv.Atoi(res[1]) + case "bool": + mp.Default, _ = strconv.ParseBool(res[1]) + case "float64": + mp.Default, _ = strconv.ParseFloat(res[1], 64) + case "float32": + mp.Default, _ = strconv.ParseFloat(res[1], 32) + default: + mp.Default = res[1] + } + }else{ + ColorLog("[WARN] Invalid default value: %s\n", defaultValue) + } + } + tag := stag.Get("json") if tag != "" { From e8f85a57c27ecbdaecfd147099cbc42945a0c41c Mon Sep 17 00:00:00 2001 From: Konstantin Date: Mon, 10 Oct 2016 17:36:51 +0300 Subject: [PATCH 6/7] Add types error handler (log) the usual warning not panic --- g_docs.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/g_docs.go b/g_docs.go index 4d9f279..a0ae1f6 100644 --- a/g_docs.go +++ b/g_docs.go @@ -779,15 +779,24 @@ func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string mp.Default = res[1] switch realType{ case "int","int64", "int32", "int16", "int8": - mp.Default, _ = strconv.Atoi(res[1]) + if mp.Default, err = strconv.Atoi(res[1]); err != nil{ + ColorLog("[WARN] Invalid default value type(%s): %s\n",realType, res[1]) + } + case "bool": - mp.Default, _ = strconv.ParseBool(res[1]) + if mp.Default, err = strconv.ParseBool(res[1]); err != nil{ + ColorLog("[WARN] Invalid default value type(%s): %s\n",realType, res[1]) + } case "float64": - mp.Default, _ = strconv.ParseFloat(res[1], 64) + if mp.Default, err = strconv.ParseFloat(res[1], 64); err != nil{ + ColorLog("[WARN] Invalid default value type(%s): %s\n",realType, res[1]) + } case "float32": - mp.Default, _ = strconv.ParseFloat(res[1], 32) - default: - mp.Default = res[1] + if mp.Default, err = strconv.ParseFloat(res[1], 32); err != nil{ + ColorLog("[WARN] Invalid default value type(%s): %s\n",realType, res[1]) + } + default: + mp.Default = res[1] } }else{ ColorLog("[WARN] Invalid default value: %s\n", defaultValue) From 7c7e75435c3968c10b836d18cd23f5ca877fdd69 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Mon, 10 Oct 2016 19:49:33 +0300 Subject: [PATCH 7/7] Update g_docs.go --- g_docs.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/g_docs.go b/g_docs.go index a0ae1f6..6f78329 100644 --- a/g_docs.go +++ b/g_docs.go @@ -769,6 +769,8 @@ func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string } var tagValues []string + var err error + stag := reflect.StructTag(strings.Trim(field.Tag.Value, "`")) defaultValue := stag.Get("doc")