Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b501822

Browse files
committed
Refactor
1 parent 12e502a commit b501822

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

coderd/coderdtest/swagger_test.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,29 @@ func parseSwaggerComments(dir string) ([]swaggerComment, error) {
9494
func parseSwaggerComment(commentGroup *ast.CommentGroup) swaggerComment {
9595
var c swaggerComment
9696
for _, line := range commentGroup.List {
97-
text := strings.TrimSpace(line.Text)
98-
if strings.Contains(text, "@Router ") {
99-
args := strings.SplitN(text, " ", 4)
100-
c.router = args[2]
101-
c.method = args[3][1 : len(args[3])-1]
102-
} else if strings.Contains(text, "@Summary ") {
103-
args := strings.SplitN(text, " ", 3)
104-
c.summary = args[2]
105-
} else if strings.Contains(text, "@ID ") {
97+
splitN := strings.SplitN(strings.TrimSpace(line.Text), " ", 2)
98+
if len(splitN) < 2 {
99+
continue // comment prefix without any content
100+
}
101+
text := splitN[1] // Skip the comment prefix (double-slash)
102+
103+
if strings.HasPrefix(text, "@Router ") {
106104
args := strings.SplitN(text, " ", 3)
107-
c.id = args[2]
108-
} else if strings.Contains(text, "@Success ") {
105+
c.router = args[1]
106+
c.method = args[2][1 : len(args[2])-1]
107+
} else if strings.HasPrefix(text, "@Summary ") {
108+
args := strings.SplitN(text, " ", 2)
109+
c.summary = args[1]
110+
} else if strings.HasPrefix(text, "@ID ") {
111+
args := strings.SplitN(text, " ", 2)
112+
c.id = args[1]
113+
} else if strings.HasPrefix(text, "@Success ") {
109114
c.hasSuccess = true
110-
} else if strings.Contains(text, "@Failure ") {
115+
} else if strings.HasPrefix(text, "@Failure ") {
111116
c.hasFailure = true
112-
} else if strings.Contains(text, "@Tags ") {
113-
args := strings.SplitN(text, " ", 3)
114-
c.tags = args[2]
117+
} else if strings.HasPrefix(text, "@Tags ") {
118+
args := strings.SplitN(text, " ", 2)
119+
c.tags = args[1]
115120
}
116121
}
117122
return c

0 commit comments

Comments
 (0)