Skip to content

Commit

Permalink
feat: support generating Swagger docs by api (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiao-kong-long authored Jan 21, 2024
1 parent 9d16ab1 commit b05528d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
20 changes: 19 additions & 1 deletion cmd/commands/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
17 changes: 16 additions & 1 deletion generate/swaggergen/g_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit b05528d

Please sign in to comment.