-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathpaths_diff.go
More file actions
175 lines (137 loc) · 4.32 KB
/
paths_diff.go
File metadata and controls
175 lines (137 loc) · 4.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package diff
import (
"fmt"
"regexp"
"github.com/oasdiff/kin-openapi/openapi3"
)
// PathsDiff describes the changes between a pair of Paths objects: https://swagger.io/specification/#paths-object
type PathsDiff struct {
Added []string `json:"added,omitempty" yaml:"added,omitempty"`
Deleted []string `json:"deleted,omitempty" yaml:"deleted,omitempty"`
Modified ModifiedPaths `json:"modified,omitempty" yaml:"modified,omitempty"`
Base *openapi3.Paths `json:"-" yaml:"-"`
Revision *openapi3.Paths `json:"-" yaml:"-"`
}
// Empty indicates whether a change was found in this element
func (pathsDiff *PathsDiff) Empty() bool {
if pathsDiff == nil {
return true
}
return len(pathsDiff.Added) == 0 &&
len(pathsDiff.Deleted) == 0 &&
len(pathsDiff.Modified) == 0
}
func newPathsDiff() *PathsDiff {
return &PathsDiff{
Added: []string{},
Deleted: []string{},
Modified: ModifiedPaths{},
}
}
func getPathsDiff(config *Config, state *state, paths1, paths2 *openapi3.Paths) (*PathsDiff, error) {
if err := filterPaths(config.MatchPath, config.UnmatchPath, config.FilterExtension, paths1, paths2); err != nil {
return nil, err
}
diff, err := getPathsDiffInternal(config, state, paths1, paths2)
if err != nil {
return nil, err
}
if diff.Empty() {
return nil, nil
}
return diff, nil
}
func getPathsDiffInternal(config *Config, state *state, paths1, paths2 *openapi3.Paths) (*PathsDiff, error) {
result := newPathsDiff()
paths1Mod := rewritePrefix(paths1.Map(), config.PathStripPrefixBase, config.PathPrefixBase)
paths2Mod := rewritePrefix(paths2.Map(), config.PathStripPrefixRevision, config.PathPrefixRevision)
addedPaths, deletedPaths, otherPaths := getPathItemsDiff(config, paths1Mod, paths2Mod)
for endpoint := range addedPaths.Map() {
result.addAddedPath(endpoint)
}
for endpoint := range deletedPaths.Map() {
result.addDeletedPath(endpoint)
}
for endpoint, pathItemPair := range otherPaths {
err := result.addModifiedPath(config, state, endpoint, pathItemPair)
if err != nil {
return nil, err
}
}
result.Base = paths1Mod
result.Revision = paths2Mod
return result, nil
}
func (pathsDiff *PathsDiff) getSummary() *SummaryDetails {
return &SummaryDetails{
Added: len(pathsDiff.Added),
Deleted: len(pathsDiff.Deleted),
Modified: len(pathsDiff.Modified),
}
}
func (pathsDiff *PathsDiff) addAddedPath(path string) {
pathsDiff.Added = append(pathsDiff.Added, path)
}
func (pathsDiff *PathsDiff) addDeletedPath(path string) {
pathsDiff.Deleted = append(pathsDiff.Deleted, path)
}
func (pathsDiff *PathsDiff) addModifiedPath(config *Config, state *state, path1 string, pathItemPair *pathItemPair) error {
return pathsDiff.Modified.addPathDiff(config, state, path1, pathItemPair)
}
func filterPaths(matchPath, unmatchPath, filterExtension string, paths1, paths2 *openapi3.Paths) error {
if err := filterPathsByName(matchPath, true, paths1, paths2); err != nil {
return err
}
if err := filterPathsByName(unmatchPath, false, paths1, paths2); err != nil {
return err
}
if err := filterPathsByExtensions(filterExtension, paths1, paths2); err != nil {
return err
}
return nil
}
func filterPathsByName(filter string, negate bool, paths1, paths2 *openapi3.Paths) error {
if filter == "" {
return nil
}
r, err := regexp.Compile(filter)
if err != nil {
return fmt.Errorf("failed to compile filter regex %q: %w", filter, err)
}
filterPathsInternal(paths1, r, negate)
filterPathsInternal(paths2, r, negate)
return nil
}
func filterPathsInternal(paths *openapi3.Paths, r *regexp.Regexp, negate bool) {
for path := range paths.Map() {
match := r.MatchString(path)
if negate {
match = !match
}
if match {
paths.Delete(path)
}
}
}
func filterPathsByExtensions(filterExtension string, paths1, paths2 *openapi3.Paths) error {
if filterExtension == "" {
return nil
}
r, err := regexp.Compile(filterExtension)
if err != nil {
return fmt.Errorf("failed to compile extension filter regex %q: %w", filterExtension, err)
}
filterPathsByExtensionInternal(paths1, r)
filterPathsByExtensionInternal(paths2, r)
return nil
}
func filterPathsByExtensionInternal(paths *openapi3.Paths, r *regexp.Regexp) {
for path, pathItem := range paths.Map() {
for extension := range pathItem.Extensions {
if r.MatchString(extension) {
paths.Delete(path)
break
}
}
}
}