Skip to content
This repository has been archived by the owner on Mar 21, 2023. It is now read-only.

Commit

Permalink
look at comments for vars (#18)
Browse files Browse the repository at this point in the history
* look at comments for vars

* add lowercasing tags
  • Loading branch information
Dan O'Brien authored Jun 21, 2021
1 parent 91ae285 commit ac6f6ef
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
13 changes: 13 additions & 0 deletions example/foo.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Environment struct {
}

// @Title Get all foos
// @Tag Foo
// @Description Get all foos
// @OperationId getAllFoos
// @Route /api/v2/foo [get]
Expand All @@ -59,3 +60,15 @@ func getAllFoos() {
func putFoo() {

}

// @Title Get Foo as var
// @Tag Vfoo
// @Description get a foo var
// @Route /api/v2/vfoo [get]
// @Success 200 object FooResponse "Successful foo response"
// @Failure 401 "Invalid access token"
// @Failure 403 "Forbidden"
// @Failure 404 "Invalid resource identifier"
var getVarFoo = func() {

}
9 changes: 8 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,13 @@ func (p *parser) parsePaths() error {
return err
}
}
} else if astVarDeclaration, ok := astDeclaration.(*ast.GenDecl); ok {
if astVarDeclaration.Doc != nil && astVarDeclaration.Doc.List != nil {
err = p.parseOperation(pkgPath, pkgName, astVarDeclaration.Doc.List)
if err != nil {
return err
}
}
}
}
}
Expand Down Expand Up @@ -751,7 +758,7 @@ func (p *parser) parseOperation(pkgPath, pkgName string, astComments []*ast.Comm
case "@success", "@failure":
err = p.parseResponseComment(pkgPath, pkgName, operation, strings.TrimSpace(comment[len(attribute):]))
case "@resource", "@tag":
resource := strings.TrimSpace(comment[len(attribute):])
resource := strings.ToLower(strings.TrimSpace(comment[len(attribute):]))
if resource == "" {
resource = "others"
}
Expand Down
35 changes: 34 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func TestExample(t *testing.T) {
},
"summary": "Get all foos",
"description": " Get all foos",
"operationId": "getAllFoos"
"operationId": "getAllFoos",
"tags": [
"foo"
]
},
"put": {
"responses": {
Expand Down Expand Up @@ -122,6 +125,36 @@ func TestExample(t *testing.T) {
"summary": "Get inner foos",
"description": " Get Inner Foos"
}
},
"/api/v2/vfoo": {
"get": {
"responses": {
"200": {
"description": "Successful foo response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/example.FooResponse"
}
}
}
},
"401": {
"description": "Invalid access token"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Invalid resource identifier"
}
},
"summary": "Get Foo as var",
"description": " get a foo var",
"tags": [
"vfoo"
]
}
}
},
"components": {
Expand Down

0 comments on commit ac6f6ef

Please sign in to comment.