-
Notifications
You must be signed in to change notification settings - Fork 28
/
copy.go
83 lines (80 loc) · 2.1 KB
/
copy.go
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
package openapi2
import (
"net/http"
"strings"
)
func CopyEndpointsByTag(tag string, specOld, specNew Specification) (Specification, error) {
var err error
for url, path := range specOld.Paths {
if path.Delete != nil {
specNew, err = copyOrIgnoreEndpoint(http.MethodDelete, *path.Delete, url, path, tag, specOld, specNew)
if err != nil {
return specNew, err
}
}
if path.Get != nil {
specNew, err = copyOrIgnoreEndpoint(http.MethodGet, *path.Get, url, path, tag, specOld, specNew)
if err != nil {
return specNew, err
}
}
if path.Head != nil {
specNew, err = copyOrIgnoreEndpoint(http.MethodHead, *path.Head, url, path, tag, specOld, specNew)
if err != nil {
return specNew, err
}
}
if path.Options != nil {
specNew, err = copyOrIgnoreEndpoint(http.MethodOptions, *path.Options, url, path, tag, specOld, specNew)
if err != nil {
return specNew, err
}
}
if path.Patch != nil {
specNew, err = copyOrIgnoreEndpoint(http.MethodPatch, *path.Patch, url, path, tag, specOld, specNew)
if err != nil {
return specNew, err
}
}
if path.Post != nil {
specNew, err = copyOrIgnoreEndpoint(http.MethodPost, *path.Post, url, path, tag, specOld, specNew)
if err != nil {
return specNew, err
}
}
if path.Put != nil {
specNew, err = copyOrIgnoreEndpoint(http.MethodPut, *path.Put, url, path, tag, specOld, specNew)
if err != nil {
return specNew, err
}
}
}
return specNew, nil
}
func copyOrIgnoreEndpoint(method string, endpoint Endpoint, url string, path Path, wantTag string, specOld, specNew Specification) (Specification, error) {
wantTag = strings.TrimSpace(wantTag)
if len(wantTag) != 0 {
match := false
for _, tryTag := range endpoint.Tags {
if strings.TrimSpace(tryTag) == wantTag {
match = true
}
}
if !match {
return specNew, nil
}
}
pathNew, ok := specNew.Paths[url]
if !ok {
pathNew = Path{}
}
err := pathNew.SetEndpoint(method, endpoint)
if err != nil {
return specNew, err
}
if specNew.Paths == nil {
specNew.Paths = map[string]Path{}
}
specNew.Paths[url] = pathNew
return specNew, nil
}