Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add command for format the swag comment. #1056

Merged
merged 17 commits into from
Nov 24, 2021
Merged

Conversation

HYY-yu
Copy link
Contributor

@HYY-yu HYY-yu commented Nov 22, 2021

Describe the PR
Hello, Please see a example:

// GetStructArrayByString example
// @Description get struct array by ID
// @ID get-struct-array-by-string
// @Accept  json
// @Produce  json
// @Param   some_id     path    string     true        "Some ID"
// @Param   offset     query    int     true        "Offset"
// @Param   limit      query    int     true        "Offset"
// @Param   pet_id  query  int false "pet' id"
// @Param   book_id query int false "book' id"
// @Param   app_id query int false "app' id"
// @Param   x_key query string false "secret key"
// @Success 200 {string} string	"ok"
// @Failure 400 {object} web.APIError "We need ID!!"
// @Failure 404 {object} web.APIError "Can not find ID"
// @Router /testapi/get-struct-array-by-string/{some_id} [get]

As you can see, If I have a lot of params, The format of comment will look very messy.

So, we can create a command like gofmt, It automatically formats the "swag" comments.
Like this:

$ swag fmt -h
NAME:
   swag fmt - format swag comments

USAGE:
   swag fmt [command options] [arguments...]

OPTIONS:
   --dir value, -d value          Directories you want to parse,comma separated and general-info file must be in the first one (default: "./")
   --exclude value                Exclude directories and files when searching, comma separated
   --generalInfo value, -g value  Go file path in which 'swagger general API Info' is written (default: "main.go")
   --help, -h                     show help (default: false)
// GetStructArrayByString example
// @Description  get struct array by ID
// @ID           get-struct-array-by-string
// @Accept       json
// @Produce      json
// @Param        some_id  path      string        true   "Some ID"
// @Param        offset   query     int           true   "Offset"
// @Param        limit    query     int           true   "Offset"
// @Param        pet_id   query     int           false  "pet' id"
// @Param        book_id  query     int           false  "book' id"
// @Param        app_id   query     int           false  "app' id"
// @Param        x_key    query     string        false  "secret key"
// @Success      200      {string}  string        "ok"
// @Failure      400      {object}  web.APIError  "We need ID!!"
// @Failure      404      {object}  web.APIError  "Can not find ID"
// @Router       /testapi/get-struct-array-by-string/{some_id} [get]

How it works

just like go fmt, It use text/tabwriter to format the comment. It scans the swag comments, especially '@param', '@success', '@response', etc, recognize their boundaries, call text/tabwriter to format them. See the source code for more details.

Relation issue

Additional context

@HYY-yu HYY-yu changed the title Format feat: add command for format the swag comment. Nov 22, 2021
@codecov
Copy link

codecov bot commented Nov 22, 2021

Codecov Report

Merging #1056 (85b7606) into master (9b23c9e) will increase coverage by 0.96%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1056      +/-   ##
==========================================
+ Coverage   93.46%   94.42%   +0.96%     
==========================================
  Files           8        9       +1     
  Lines        2127     2261     +134     
==========================================
+ Hits         1988     2135     +147     
+ Misses         77       66      -11     
+ Partials       62       60       -2     
Impacted Files Coverage Δ
formater.go 100.00% <100.00%> (ø)
parser.go 92.09% <0.00%> (-0.18%) ⬇️
field_parser.go 100.00% <0.00%> (ø)
operation.go 95.88% <0.00%> (+0.88%) ⬆️
packages.go 80.22% <0.00%> (+3.38%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9b23c9e...85b7606. Read the comment docs.

@ubogdan
Copy link
Contributor

ubogdan commented Nov 22, 2021

@HYY-yu This is pretty impressive PR, and I feel it will be a nice and valuable feature for our community. Would you mind fixing the coverage so we may merge and include this in the 1.7.5 version?

@HYY-yu
Copy link
Contributor Author

HYY-yu commented Nov 23, 2021

It's very hard to improving coverage, I had to use the Monkey test.

ubogdan
ubogdan previously approved these changes Nov 23, 2021
Copy link
Contributor

@ubogdan ubogdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ubogdan
Copy link
Contributor

ubogdan commented Nov 23, 2021

I know the main focus on PR is the code. In the end, we are adding these features to improve the development experience.
Would you mind documenting this feature into README.md?

@HYY-yu
Copy link
Contributor Author

HYY-yu commented Nov 24, 2021

Sure, I'll add to README later.

Copy link
Contributor

@ubogdan ubogdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ubogdan
Copy link
Contributor

ubogdan commented Nov 24, 2021

@HYY-yu Thanks for your contribution.

@ubogdan ubogdan merged commit 93acd2a into swaggo:master Nov 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants