-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathfilter.go
More file actions
88 lines (78 loc) · 2.32 KB
/
filter.go
File metadata and controls
88 lines (78 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package codegen
import "github.com/getkin/kin-openapi/openapi3"
func sliceToMap(items []string) map[string]bool {
m := make(map[string]bool, len(items))
for _, item := range items {
m[item] = true
}
return m
}
func filterOperationsByTag(swagger *openapi3.T, opts Configuration) {
if len(opts.OutputOptions.ExcludeTags) > 0 {
operationsWithTags(swagger.Paths, sliceToMap(opts.OutputOptions.ExcludeTags), true)
}
if len(opts.OutputOptions.IncludeTags) > 0 {
operationsWithTags(swagger.Paths, sliceToMap(opts.OutputOptions.IncludeTags), false)
}
}
func operationsWithTags(paths *openapi3.Paths, tags map[string]bool, exclude bool) {
if paths == nil {
return
}
for _, pathItem := range paths.Map() {
ops := pathItem.Operations()
names := make([]string, 0, len(ops))
for name, op := range ops {
if operationHasTag(op, tags) == exclude {
names = append(names, name)
}
}
for _, name := range names {
pathItem.SetOperation(name, nil)
}
}
}
// operationHasTag returns true if the operation is tagged with any of tags
func operationHasTag(op *openapi3.Operation, tags map[string]bool) bool {
if op == nil {
return false
}
for _, hasTag := range op.Tags {
if tags[hasTag] {
return true
}
}
return false
}
func filterOperationsByOperationID(swagger *openapi3.T, opts Configuration) {
if len(opts.OutputOptions.ExcludeOperationIDs) > 0 {
operationsWithOperationIDs(swagger.Paths, sliceToMap(opts.OutputOptions.ExcludeOperationIDs), true)
}
if len(opts.OutputOptions.IncludeOperationIDs) > 0 {
operationsWithOperationIDs(swagger.Paths, sliceToMap(opts.OutputOptions.IncludeOperationIDs), false)
}
}
func operationsWithOperationIDs(paths *openapi3.Paths, operationIDs map[string]bool, exclude bool) {
if paths == nil {
return
}
for _, pathItem := range paths.Map() {
ops := pathItem.Operations()
names := make([]string, 0, len(ops))
for name, op := range ops {
if operationHasOperationID(op, operationIDs) == exclude {
names = append(names, name)
}
}
for _, name := range names {
pathItem.SetOperation(name, nil)
}
}
}
// operationHasOperationID returns true if the operation has operation id is included in operation ids
func operationHasOperationID(op *openapi3.Operation, operationIDs map[string]bool) bool {
if op == nil {
return false
}
return operationIDs[op.OperationID]
}