Skip to content

Commit

Permalink
feat: ADD collection format flag (#1482)
Browse files Browse the repository at this point in the history
* feat: ADD collection format flag

Co-authored-by: 정주현 <[email protected]>
  • Loading branch information
wooongchi and 정주현 authored Mar 21, 2023
1 parent 3f0a4b0 commit f56e0bb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ OPTIONS:
--overridesFile value File to read global type overrides from. (default: ".swaggo")
--parseGoList Parse dependency via 'go list' (default: true)
--tags value, -t value A comma-separated list of tags to filter the APIs for which the documentation is generated.Special case if the tag is prefixed with the '!' character then the APIs with that tag will be excluded
--collectionFormat value, --cf value Set default collection format (default: "csv")
--help, -h show help (default: false)
```
Expand Down
13 changes: 13 additions & 0 deletions cmd/swag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
quietFlag = "quiet"
tagsFlag = "tags"
parseExtensionFlag = "parseExtension"
collectionFormatFlag = "collectionFormat"
)

var initFlags = []cli.Flag{
Expand Down Expand Up @@ -141,6 +142,12 @@ var initFlags = []cli.Flag{
Value: "",
Usage: "A comma-separated list of tags to filter the APIs for which the documentation is generated.Special case if the tag is prefixed with the '!' character then the APIs with that tag will be excluded",
},
&cli.StringFlag{
Name: collectionFormatFlag,
Aliases: []string{"cf"},
Value: "csv",
Usage: "Set default collection format",
},
}

func initAction(ctx *cli.Context) error {
Expand All @@ -161,6 +168,11 @@ func initAction(ctx *cli.Context) error {
logger = log.New(io.Discard, "", log.LstdFlags)
}

collectionFormat := swag.TransToValidCollectionFormat(ctx.String(collectionFormatFlag))
if collectionFormat == "" {
return fmt.Errorf("not supported %s collectionFormat", ctx.String(collectionFormat))
}

return gen.New().Build(&gen.Config{
SearchDir: ctx.String(searchDirFlag),
Excludes: ctx.String(excludeFlag),
Expand All @@ -182,6 +194,7 @@ func initAction(ctx *cli.Context) error {
ParseGoList: ctx.Bool(parseGoListFlag),
Tags: ctx.String(tagsFlag),
Debugger: logger,
CollectionFormat: collectionFormat,
})
}

Expand Down
4 changes: 4 additions & 0 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ type Config struct {

// include only tags mentioned when searching, comma separated
Tags string

// CollectionFormat set default collection format
CollectionFormat string
}

// Build builds swagger json file for given searchDir and mainAPIFile. Returns json.
Expand Down Expand Up @@ -176,6 +179,7 @@ func (g *Gen) Build(config *Config) error {
swag.SetOverrides(overrides),
swag.ParseUsingGoList(config.ParseGoList),
swag.SetTags(config.Tags),
swag.SetCollectionFormat(config.CollectionFormat),
)

p.PropNamingStrategy = config.PropNamingStrategy
Expand Down
7 changes: 7 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ func SetOverrides(overrides map[string]string) func(parser *Parser) {
}
}

// SetCollectionFormat set default collection format
func SetCollectionFormat(collectionFormat string) func(*Parser) {
return func(p *Parser) {
p.collectionFormatInQuery = collectionFormat
}
}

// ParseUsingGoList sets whether swag use go list to parse dependency
func ParseUsingGoList(enabled bool) func(parser *Parser) {
return func(p *Parser) {
Expand Down
31 changes: 31 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3944,3 +3944,34 @@ func TestParser_parseExtension(t *testing.T) {

}
}

func TestParser_collectionFormat(t *testing.T) {
tests := []struct {
name string
parser *Parser
format string
}{
{
name: "no collectionFormat",
parser: New(),
format: "",
},
{
name: "multi collectionFormat",
parser: New(SetCollectionFormat("multi")),
format: "multi",
},
{
name: "ssv collectionFormat",
parser: New(SetCollectionFormat("ssv")),
format: "ssv",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.parser.collectionFormatInQuery != tt.format {
t.Errorf("Parser.collectionFormatInQuery = %s, want %s", tt.parser.collectionFormatInQuery, tt.format)
}
})
}
}

0 comments on commit f56e0bb

Please sign in to comment.