feat: support generating Swagger docs by api (#6)

This commit is contained in:
xiao-kong-long 2024-01-21 22:01:59 +08:00 committed by GitHub
parent 9d16ab1b8b
commit b05528d7e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 2 deletions

View File

@ -99,12 +99,30 @@ func GenerateCode(cmd *commands.Command, args []string) int {
tags = []string{}
}
// get apis value
apisIndex := -1
for i, arg := range args {
if arg == "--apis" && i+1 < len(args) {
apisIndex = i + 1
break
}
}
var apis []string
if apisIndex != -1 {
apis = strings.Split(args[apisIndex], ",")
for i, str := range apis {
apis[i] = "/" + strings.TrimSpace(str)
}
} else {
apis = []string{}
}
gcmd := args[0]
switch gcmd {
case "scaffold":
scaffold(cmd, args, currpath)
case "docs":
swaggergen.GenerateDocs(currpath, tags)
swaggergen.GenerateDocs(currpath, tags, apis)
case "appcode":
appCode(cmd, args, currpath)
case "migration":

View File

@ -157,7 +157,7 @@ func parsePackageFromDir(path string) error {
}
// GenerateDocs generates documentations for a given path.
func GenerateDocs(curpath string, tags []string) {
func GenerateDocs(curpath string, tags []string, apis []string) {
pkgspath := curpath
workspace := os.Getenv("BeeWorkspace")
if workspace != "" {
@ -302,6 +302,21 @@ func GenerateDocs(curpath string, tags []string) {
}
}
// fileter the controllerList by apis
if len(apis)>0 {
for key, controllerValue := range controllerList {
apiMap := make(map[string]*swagger.Item)
for _, api := range apis{
if item, ok := controllerValue[api]; ok {
apiMap[api] = item
} else {
beeLogger.Log.Warnf("api %s not found in controller %s", api, key)
}
}
controllerList[key] = apiMap
}
}
for _, d := range f.Decls {
switch specDecl := d.(type) {
case *ast.FuncDecl: