Skip to content

Commit 7bcd94d

Browse files
Add include option as alternative to ignore
1 parent a9b7e27 commit 7bcd94d

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ go install github.com/technicallyjosh/protoc-gen-openapi@latest
3434
| `version` | The version of the API. | 0.0.1 |
3535
| `title` | The title of the API. | |
3636
| `description` | A description of the API. | |
37+
| `include` | A list of proto package names to include only. `ignore` is ran after this | |
3738
| `ignore` | A list of proto package names to ignore delimited by pipes. | |
3839
| `default_response` | The default response to be used.<sup>1</sup> | |
3940
| `content_type` | The content type to be associated with all operations.<sup>1</sup> | application/json |

internal/generator/generator.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Config struct {
2323
Filename *string
2424
Host *string
2525
Ignore *string
26+
Include *string
2627
JSONOutput *bool
2728
Title *string
2829
UseJSONNames *bool
@@ -246,8 +247,22 @@ func (g *Generator) buildDocument() (*openapi3.T, error) {
246247
Tags: make(openapi3.Tags, 0),
247248
}
248249

250+
included := strings.Split(*g.config.Include, "|")
249251
ignored := strings.Split(*g.config.Ignore, "|")
250-
files := filterFiles(g.plugin.Files, ignored)
252+
253+
files := make([]*protogen.File, 0)
254+
255+
if len(included) > 0 {
256+
files = filterIncludedFiles(g.plugin.Files, included)
257+
}
258+
259+
if len(ignored) > 0 {
260+
if len(files) == 0 {
261+
files = g.plugin.Files
262+
}
263+
264+
files = filterIgnoredFiles(files, ignored)
265+
}
251266

252267
for _, file := range files {
253268
g.buildMessageMap(file.Messages)
@@ -353,7 +368,7 @@ func addFileSecurityToDoc(doc *openapi3.T, file *protogen.File) error {
353368
return nil
354369
}
355370

356-
func filterFiles(allFiles []*protogen.File, ignored []string) []*protogen.File {
371+
func filterIgnoredFiles(allFiles []*protogen.File, ignored []string) []*protogen.File {
357372
files := make([]*protogen.File, 0)
358373

359374
Files:
@@ -373,3 +388,24 @@ Files:
373388

374389
return files
375390
}
391+
392+
func filterIncludedFiles(allFiles []*protogen.File, included []string) []*protogen.File {
393+
files := make([]*protogen.File, 0)
394+
395+
Files:
396+
for _, file := range allFiles {
397+
if !file.Generate {
398+
continue
399+
}
400+
401+
for _, ignoredPackage := range included {
402+
if file.Proto.GetPackage() != ignoredPackage {
403+
continue Files
404+
}
405+
}
406+
407+
files = append(files, file)
408+
}
409+
410+
return files
411+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func main() {
1717
Filename: flags.String("filename", "openapi", "Name of the file generated without the extension."),
1818
Host: flags.String("host", "", "Host to be used for all routes."),
1919
Ignore: flags.String("ignore", "", "Packages to ignore."),
20+
Include: flags.String("include", "", "Packages to include. Ignore overrides this."),
2021
JSONOutput: flags.Bool("json_out", false, "Generate a JSON file instead of YAML."),
2122
Title: flags.String("title", "", "Title of the API"),
2223
UseJSONNames: flags.Bool("json_names", false, "Use JSON names instead of the proto names of fields."),

main_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type TestOptions struct {
1717
testProto string
1818
title string
1919
version string
20+
include string
21+
ignore string
2022
}
2123

2224
type TestSuite struct {
@@ -105,6 +107,8 @@ func (s *TestSuite) BeforeTest(suite, name string) {
105107
"--openapi_opt=title="+s.options.title,
106108
"--openapi_opt=description="+s.options.description,
107109
"--openapi_opt=default_response="+s.options.defaultResponse,
110+
"--openapi_opt=include="+s.options.include,
111+
"--openapi_opt=ignore="+s.options.ignore,
108112
"test/"+filename,
109113
).CombinedOutput()
110114
if err != nil {

0 commit comments

Comments
 (0)