-
Notifications
You must be signed in to change notification settings - Fork 28
/
multi.go
112 lines (105 loc) · 2.8 KB
/
multi.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
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
package openapi3edit
import (
"fmt"
oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/mogo/errors/errorsutil"
"github.com/grokify/mogo/fmt/fmtutil"
"github.com/grokify/spectrum/openapi3"
)
type SpecMoreModifyMultiOpts struct {
OperationsDeleteFunc func(opPath, opMethod string, op *oas3.Operation) bool
OperationsRenameIDsFunc func(string, string, *oas3.Operation)
OperationsRemoveSecurity bool
OperationsShowIDs bool
OperationsExec bool
Paths SpecPathsModifyOpts
PathsShow bool
PathsExec bool
TagsOperationFunc func(string, string, *oas3.Operation)
Tags map[string]string
TagsShow bool
TagsExec bool
}
// SpecMoreModifyMulti is used to perform multiple updates on an OpenAPI 3 spec.
func SpecMoreModifyMulti(sm *openapi3.SpecMore, opts SpecMoreModifyMultiOpts) error {
if opts.OperationsShowIDs {
// fmtutil.PrintJSON(SpecOperationIds(sm.Spec))
oldIDs := sm.OperationIDsCounts()
if opts.OperationsShowIDs {
err := fmtutil.PrintJSON(oldIDs)
if err != nil {
return err
}
}
for id, count := range oldIDs {
if count != 1 {
return fmt.Errorf("E_OPERATION_ID_BAD_COUNT ID[%s]COUNT[%d]", id, count)
}
}
}
if opts.OperationsExec {
if opts.OperationsDeleteFunc != nil {
SpecDeleteOperations(sm.Spec, opts.OperationsDeleteFunc)
}
if opts.OperationsRenameIDsFunc != nil {
openapi3.VisitOperations(sm.Spec, opts.OperationsRenameIDsFunc)
}
// UpdateOperationIds(sm.Spec, opts.OperationIdsRename)
newIDs := sm.OperationIDsCounts()
if opts.OperationsShowIDs {
err := fmtutil.PrintJSON(newIDs)
if err != nil {
return err
}
}
for id, count := range newIDs {
if count != 1 {
return fmt.Errorf("E_OPERATION_ID_BAD_COUNT_AFTER_RENAME ID[%s]COUNT[%d]", id, count)
}
}
if opts.OperationsRemoveSecurity {
RemoveOperationsSecurity(sm.Spec)
}
}
// Update Paths
if opts.PathsShow {
err := fmtutil.PrintJSON(InspectPaths(sm.Spec))
if err != nil {
return err
}
}
if opts.PathsExec {
err := SpecPathsModify(sm.Spec, opts.Paths)
if err != nil {
return errorsutil.Wrap(err, "specModifyMulti")
}
if opts.PathsShow {
err := fmtutil.PrintJSON(InspectPaths(sm.Spec))
if err != nil {
return err
}
}
}
// Update Tags
if opts.TagsOperationFunc != nil || len(opts.Tags) > 0 {
if opts.TagsShow {
err := fmtutil.PrintJSON(sm.TagsMap(true, true))
if err != nil {
return err
}
}
if opts.TagsExec {
if opts.TagsOperationFunc != nil {
openapi3.VisitOperations(sm.Spec, opts.TagsOperationFunc)
}
SpecTagsModify(sm.Spec, opts.Tags)
if opts.TagsShow {
err := fmtutil.PrintJSON(sm.TagsMap(true, true))
if err != nil {
return err
}
}
}
}
return nil
}