Skip to content

Commit

Permalink
feat: support generating Swagger docs by tags (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiao-kong-long authored Jan 21, 2024
1 parent ce32a96 commit 9d16ab1
Show file tree
Hide file tree
Showing 2 changed files with 50 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 @@ -81,12 +81,30 @@ func GenerateCode(cmd *commands.Command, args []string) int {
beeLogger.Log.Fatal("Command is missing")
}

// get tags value
tagsIndex := -1
for i, arg := range args {
if arg == "--tags" && i+1 < len(args) {
tagsIndex = i + 1
break
}
}
var tags []string
if tagsIndex != -1 {
tags = strings.Split(args[tagsIndex], ",")
for i, str := range tags {
tags[i] = strings.TrimSpace(str)
}
} else {
tags = []string{}
}

gcmd := args[0]
switch gcmd {
case "scaffold":
scaffold(cmd, args, currpath)
case "docs":
swaggergen.GenerateDocs(currpath)
swaggergen.GenerateDocs(currpath, tags)
case "appcode":
appCode(cmd, args, currpath)
case "migration":
Expand Down
32 changes: 31 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) {
func GenerateDocs(curpath string, tags []string) {
pkgspath := curpath
workspace := os.Getenv("BeeWorkspace")
if workspace != "" {
Expand Down Expand Up @@ -272,6 +272,36 @@ func GenerateDocs(curpath string) {
}
analyseControllerPkg(localName, im.Path.Value)
}

// fileter the controllerList by tags
if len(tags)>0 {
for _, controllerValue := range controllerList {
for routerPath, item := range controllerValue {
var tags_ []string
if item.Get != nil {
tags_ = item.Get.Tags
} else {
tags_ = item.Post.Tags
}
isContained := false
for _, tag := range tags {
for _, tag_ := range tags_{
if strings.EqualFold(tag, tag_) {
isContained = true
break
}
}
if isContained {
break
}
}
if !isContained {
delete(controllerValue, routerPath)
}
}
}
}

for _, d := range f.Decls {
switch specDecl := d.(type) {
case *ast.FuncDecl:
Expand Down

0 comments on commit 9d16ab1

Please sign in to comment.