Skip to content

Commit 9823545

Browse files
committed
chore(lint): golangci-lint: fix various
1 parent d8d6bd6 commit 9823545

File tree

17 files changed

+113
-106
lines changed

17 files changed

+113
-106
lines changed

cmd/openapi2to3/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ func main() {
3333
log.Fatalf("E_INPUT_FILE_IS_NOT_NONEMPTY_FILE [%v]", opts.OAS2File)
3434
}
3535

36-
wantPretty := false
36+
var wantPretty bool
3737
if len(opts.Pretty) > 0 {
3838
wantPretty = true
39+
} else {
40+
wantPretty = false
3941
}
4042

4143
err = openapi2.ConvertOAS2FileToOAS3File(opts.OAS2File, opts.OAS3File, 0644, wantPretty)

examples/openapi3table/main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"log"
6+
"os"
77

88
"github.com/grokify/gocharts/v2/data/table/tabulator"
99
"github.com/grokify/spectrum/openapi3"
@@ -32,12 +32,15 @@ func main() {
3232
PageLink: "https://developers.ringcentral.com",
3333
TableDomID: "apitable",
3434
ColumnSet: ColumnTexts()}
35-
pageParams.AddSpec(spec)
35+
err = pageParams.AddSpec(spec)
36+
if err != nil {
37+
log.Fatal(err)
38+
}
3639

3740
pageHTML := openapi3html.SpectrumUIPage(pageParams)
3841

3942
filename := "api-regisry.html"
40-
err = ioutil.WriteFile(filename, []byte(pageHTML), 0644)
43+
err = os.WriteFile(filename, []byte(pageHTML), 0600)
4144
if err != nil {
4245
log.Fatal(err)
4346
}

examples/ringcentral/convert.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,6 @@ func main() {
2525
log.Fatal(err)
2626
}
2727

28-
if 1 == 1 {
29-
spec, err := openapi3.ReadFile(opts.OpenAPISpec, true)
30-
if err != nil {
31-
log.Fatal(err)
32-
}
33-
sm := openapi3.SpecMore{Spec: spec}
34-
if 1 == 1 {
35-
err := sm.SchemaPropertiesWithoutDescriptionsWriteFile("rc-platform.yml.schema-properties_missing-descriptions.txt")
36-
if err != nil {
37-
log.Fatal(err)
38-
}
39-
}
40-
if 1 == 1 {
41-
err := sm.OperationParametersWithoutDescriptionsWriteFile("rc-platform.yml.op-params_missing-descriptions.txt")
42-
if err != nil {
43-
log.Fatal(err)
44-
}
45-
46-
}
47-
panic("ZZZ")
48-
}
49-
5028
cfg := openapi2postman2.Configuration{
5129
PostmanURLBase: "{{RINGCENTRAL_SERVER_URL}}",
5230
PostmanHeaders: []postman2.Header{{
@@ -64,3 +42,21 @@ func main() {
6442

6543
fmt.Println("DONE")
6644
}
45+
46+
func ReadFile(filename string) {
47+
spec, err := openapi3.ReadFile(filename, true)
48+
if err != nil {
49+
log.Fatal(err)
50+
}
51+
sm := openapi3.SpecMore{Spec: spec}
52+
53+
err = sm.SchemaPropertiesWithoutDescriptionsWriteFile("rc-platform.yml.schema-properties_missing-descriptions.txt")
54+
if err != nil {
55+
log.Fatal(err)
56+
}
57+
58+
err = sm.OperationParametersWithoutDescriptionsWriteFile("rc-platform.yml.op-params_missing-descriptions.txt")
59+
if err != nil {
60+
log.Fatal(err)
61+
}
62+
}

openapi2/copy.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,51 @@ package openapi2
33
import (
44
"net/http"
55
"strings"
6+
7+
"github.com/qdm12/reprint"
68
)
79

810
func CopyEndpointsByTag(tag string, specOld, specNew Specification) (Specification, error) {
911
var err error
1012
for url, path := range specOld.Paths {
1113
if path.Delete != nil {
12-
specNew, err = copyOrIgnoreEndpoint(http.MethodDelete, *path.Delete, url, path, tag, specOld, specNew)
14+
specNew, err = copyOrIgnoreEndpoint(http.MethodDelete, *path.Delete, url, path, tag, specNew)
1315
if err != nil {
1416
return specNew, err
1517
}
1618
}
1719
if path.Get != nil {
18-
specNew, err = copyOrIgnoreEndpoint(http.MethodGet, *path.Get, url, path, tag, specOld, specNew)
20+
specNew, err = copyOrIgnoreEndpoint(http.MethodGet, *path.Get, url, path, tag, specNew)
1921
if err != nil {
2022
return specNew, err
2123
}
2224
}
2325
if path.Head != nil {
24-
specNew, err = copyOrIgnoreEndpoint(http.MethodHead, *path.Head, url, path, tag, specOld, specNew)
26+
specNew, err = copyOrIgnoreEndpoint(http.MethodHead, *path.Head, url, path, tag, specNew)
2527
if err != nil {
2628
return specNew, err
2729
}
2830
}
2931
if path.Options != nil {
30-
specNew, err = copyOrIgnoreEndpoint(http.MethodOptions, *path.Options, url, path, tag, specOld, specNew)
32+
specNew, err = copyOrIgnoreEndpoint(http.MethodOptions, *path.Options, url, path, tag, specNew)
3133
if err != nil {
3234
return specNew, err
3335
}
3436
}
3537
if path.Patch != nil {
36-
specNew, err = copyOrIgnoreEndpoint(http.MethodPatch, *path.Patch, url, path, tag, specOld, specNew)
38+
specNew, err = copyOrIgnoreEndpoint(http.MethodPatch, *path.Patch, url, path, tag, specNew)
3739
if err != nil {
3840
return specNew, err
3941
}
4042
}
4143
if path.Post != nil {
42-
specNew, err = copyOrIgnoreEndpoint(http.MethodPost, *path.Post, url, path, tag, specOld, specNew)
44+
specNew, err = copyOrIgnoreEndpoint(http.MethodPost, *path.Post, url, path, tag, specNew)
4345
if err != nil {
4446
return specNew, err
4547
}
4648
}
4749
if path.Put != nil {
48-
specNew, err = copyOrIgnoreEndpoint(http.MethodPut, *path.Put, url, path, tag, specOld, specNew)
50+
specNew, err = copyOrIgnoreEndpoint(http.MethodPut, *path.Put, url, path, tag, specNew)
4951
if err != nil {
5052
return specNew, err
5153
}
@@ -54,7 +56,8 @@ func CopyEndpointsByTag(tag string, specOld, specNew Specification) (Specificati
5456
return specNew, nil
5557
}
5658

57-
func copyOrIgnoreEndpoint(method string, endpoint Endpoint, url string, path Path, wantTag string, specOld, specNew Specification) (Specification, error) {
59+
func copyOrIgnoreEndpoint(method string, endpoint Endpoint, url string, path Path, wantTag string, specNew Specification) (Specification, error) {
60+
// TODO: copy referenced objects, e.g. schema objects, which may need `specOld`.
5861
wantTag = strings.TrimSpace(wantTag)
5962
if len(wantTag) != 0 {
6063
match := false
@@ -67,10 +70,11 @@ func copyOrIgnoreEndpoint(method string, endpoint Endpoint, url string, path Pat
6770
return specNew, nil
6871
}
6972
}
70-
pathNew, ok := specNew.Paths[url]
71-
if !ok {
72-
pathNew = Path{}
73+
if _, ok := specNew.Paths[url]; ok {
74+
return specNew, nil
7375
}
76+
77+
pathNew := reprint.This(path).(Path) // ref: https://stackoverflow.com/a/77412997/1908967
7478
err := pathNew.SetEndpoint(method, endpoint)
7579
if err != nil {
7680
return specNew, err

openapi3/merge.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,11 @@ func MergeSchemas(specMaster, specExtra *Spec, specExtraNote string, mergeOpts *
326326
checkCollisionResult := mergeOpts.CheckSchemaCollision(schemaName, schemaMaster, schemaExtra, specExtraNote)
327327
if checkCollisionResult != CollisionCheckSame &&
328328
mergeOpts.CollisionCheckResult != CollisionCheckSkip {
329-
if mergeOpts.CollisionCheckResult == CollisionCheckOverwrite {
329+
switch mergeOpts.CollisionCheckResult {
330+
case CollisionCheckOverwrite:
330331
delete(specMaster.Components.Schemas, schemaName)
331332
specMaster.Components.Schemas[schemaName] = schemaExtra
332-
} else if mergeOpts.CollisionCheckResult == CollisionCheckError {
333+
case CollisionCheckError:
333334
return nil, fmt.Errorf("E_SCHEMA_COLLISION [%v] EXTRA_SPEC [%s]", schemaName, specExtraNote)
334335
}
335336
}

openapi3/ontology/tag_ontology.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,38 +137,38 @@ func (tod *TagOnologyData) Equal(data TagOnologyData) ([]string, bool) {
137137
}
138138

139139
func (to *TagOnology) SpecFilename() string {
140-
casefunc := stringcase.FuncToWantCaseOrDefault(to.Ontology.SpecFileCase, DefaultCaseToFunc)
140+
casefunc := stringcase.FuncToWantCaseOrDefault(to.SpecFileCase, DefaultCaseToFunc)
141141
resourceName := to.ResourceNameSingular
142-
if to.Ontology.SpecFileResourceIsPlural {
142+
if to.SpecFileResourceIsPlural {
143143
resourceName = to.ResourceNamePlural
144144
}
145145
filename := resourceName
146-
if len(to.Ontology.SpecFilePrefix) > 0 {
147-
filename = to.Ontology.SpecFilePrefix + " " + filename
146+
if len(to.SpecFilePrefix) > 0 {
147+
filename = to.SpecFilePrefix + " " + filename
148148
}
149-
if len(to.Ontology.SpecFileSuffix) > 0 {
150-
filename = filename + " " + to.Ontology.SpecFileSuffix
149+
if len(to.SpecFileSuffix) > 0 {
150+
filename = filename + " " + to.SpecFileSuffix
151151
}
152-
return casefunc(filename) + to.Ontology.SpecFileExt
152+
return casefunc(filename) + to.SpecFileExt
153153
}
154154

155155
func (to *TagOnology) ResourcePathVar() string {
156156
resourceNameSingle := strings.TrimSpace(to.ResourceNameSingularShort)
157157
if len(resourceNameSingle) == 0 {
158158
resourceNameSingle = to.ResourceNameSingular
159159
}
160-
casefunc := stringcase.FuncToWantCaseOrDefault(to.Ontology.PathVarCase, DefaultCaseToFunc)
161-
return casefunc(to.Ontology.PathIDPrefix + " " + resourceNameSingle + " " + to.Ontology.PathIDSuffix)
160+
casefunc := stringcase.FuncToWantCaseOrDefault(to.PathVarCase, DefaultCaseToFunc)
161+
return casefunc(to.PathIDPrefix + " " + resourceNameSingle + " " + to.PathIDSuffix)
162162
}
163163

164164
func (to *TagOnology) ResourceSchemaNameRequest() string {
165-
casefunc := stringcase.FuncToWantCaseOrDefault(to.Ontology.SchemaNameCase, DefaultCaseToFunc)
165+
casefunc := stringcase.FuncToWantCaseOrDefault(to.SchemaNameCase, DefaultCaseToFunc)
166166
return casefunc(to.ResourceNameSingular)
167167
}
168168

169169
func (to *TagOnology) ResourceSchemaNameResponse() string {
170-
casefunc := stringcase.FuncToWantCaseOrDefault(to.Ontology.SchemaNameCase, DefaultCaseToFunc)
171-
return casefunc(to.ResourceNameSingular + " " + to.Ontology.SchemaNameReponseSuffix)
170+
casefunc := stringcase.FuncToWantCaseOrDefault(to.SchemaNameCase, DefaultCaseToFunc)
171+
return casefunc(to.ResourceNameSingular + " " + to.SchemaNameReponseSuffix)
172172
}
173173

174174
func (to *TagOnology) CreateSummary() string { return to.ActionSummary(ActionCreateDescription, false) }
@@ -193,7 +193,7 @@ func (to *TagOnology) DeleteOperationID() string { return to.ActionOperationID(A
193193
func (to *TagOnology) ListOperationID() string { return to.ActionOperationID(ActionList, true) }
194194

195195
func (to *TagOnology) ActionOperationID(action string, plural bool) string {
196-
xcasefuncOpID := stringcase.FuncToWantCaseOrNoOp(to.Ontology.OperationIDCase)
196+
xcasefuncOpID := stringcase.FuncToWantCaseOrNoOp(to.OperationIDCase)
197197
if plural {
198198
return xcasefuncOpID(strings.Join([]string{action, to.ResourceNamePlural}, " "))
199199
}

openapi3/openapi3_url.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/grokify/mogo/net/urlutil"
77
)
88

9-
func BuildApiURLOAS(specServerURL, overrideServerURL, specPath string) string {
9+
func BuildAPIURLOAS(specServerURL, overrideServerURL, specPath string) string {
1010
overrideServerURL = strings.TrimSpace(overrideServerURL)
1111
specServerURL = strings.TrimSpace(specServerURL)
1212
specPath = strings.TrimSpace(specPath)

openapi3/openapi3postman2/convert.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,13 @@ func postmanAddItemToFolder(pman postman2.Collection, pmItem *postman2.Item, pmF
211211
}
212212
*/
213213

214-
func Openapi3OperationToPostman2APIItem(cfg Configuration, oas3spec *openapi3.Spec, oasUrl string, method string, operation *oas3.Operation) (*postman2.Item, error) {
215-
pmUrl := BuildPostmanURL(cfg, oas3spec, oasUrl, operation)
214+
func Openapi3OperationToPostman2APIItem(cfg Configuration, oas3spec *openapi3.Spec, oasURL string, method string, operation *oas3.Operation) (*postman2.Item, error) {
215+
pmURL := BuildPostmanURL(cfg, oas3spec, oasURL, operation)
216216
item := &postman2.Item{
217217
Name: operation.Summary,
218218
Request: &postman2.Request{
219219
Method: strings.ToUpper(method),
220-
URL: &pmUrl,
220+
URL: &pmURL,
221221
},
222222
}
223223

@@ -247,7 +247,7 @@ func Openapi3OperationToPostman2APIItem(cfg Configuration, oas3spec *openapi3.Sp
247247
}
248248

249249
if cfg.RequestBodyFunc != nil {
250-
bodyString := strings.TrimSpace(cfg.RequestBodyFunc(oasUrl))
250+
bodyString := strings.TrimSpace(cfg.RequestBodyFunc(oasURL))
251251
if len(bodyString) > 0 {
252252
item.Request.Body = &postman2.RequestBody{
253253
Mode: "raw",
@@ -272,18 +272,18 @@ func BuildPostmanURL(cfg Configuration, spec *openapi3.Spec, specPath string, op
272272
}
273273
overrideServerURL := urlutil.JoinAbsolute(partsOverrideURL...)
274274

275-
specURLString := openapi3.BuildApiURLOAS(specServerURL, overrideServerURL, specPath)
275+
specURLString := openapi3.BuildAPIURLOAS(specServerURL, overrideServerURL, specPath)
276276
pmanURLString := postman2.APIURLOasToPostman(specURLString)
277277
pmanURL := postman2.NewURL(pmanURLString)
278278
pmanURL = PostmanURLAddDefaultsOAS3(pmanURL, operation)
279279
return pmanURL
280280
}
281281

282-
var postmanUrlDefaultsRx *regexp.Regexp = regexp.MustCompile(`^\s*(:(.+))\s*$`)
282+
var postmanURLDefaultsRx *regexp.Regexp = regexp.MustCompile(`^\s*(:(.+))\s*$`)
283283

284284
func PostmanURLAddDefaultsOAS3(pmanURL postman2.URL, operation *oas3.Operation) postman2.URL {
285285
for _, part := range pmanURL.Path {
286-
match := postmanUrlDefaultsRx.FindAllStringSubmatch(part, -1)
286+
match := postmanURLDefaultsRx.FindAllStringSubmatch(part, -1)
287287
if len(match) > 0 {
288288
baseVariable := match[0][2]
289289
var defaultValue interface{}

openapi3edit/operation_edit.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ func NewOperationEdit(opPath, opMethod string, op *oas3.Operation) OperationEdit
2626

2727
func (ope *OperationEdit) SetExternalDocs(docURL, docDescription string, preserveIfReqEmpty bool) error {
2828
// return operationAddExternalDocs(ope.OperationMore.Operation, docURL, docDescription, preserveIfReqEmpty)
29-
if ope.OperationMore.Operation == nil {
29+
if ope.Operation == nil {
3030
return openapi3.ErrOperationNotSet
3131
}
32-
op := ope.OperationMore.Operation
32+
op := ope.Operation
3333
docURL = strings.TrimSpace(docURL)
3434
docDescription = strings.TrimSpace(docDescription)
3535
if len(docURL) > 0 || len(docDescription) > 0 {
@@ -53,10 +53,10 @@ func (ope *OperationEdit) SetExternalDocs(docURL, docDescription string, preserv
5353
}
5454

5555
func (ope *OperationEdit) SetRequestBodyAttrs(description string, required bool) error {
56-
if ope.OperationMore.Operation == nil {
56+
if ope.Operation == nil {
5757
return openapi3.ErrOperationNotSet
5858
}
59-
op := ope.OperationMore.Operation
59+
op := ope.Operation
6060
if op.RequestBody == nil {
6161
op.RequestBody = &oas3.RequestBodyRef{}
6262
}
@@ -70,10 +70,10 @@ func (ope *OperationEdit) SetRequestBodyAttrs(description string, required bool)
7070
}
7171

7272
func (ope *OperationEdit) SetRequestBodySchemaRef(mediaType string, schemaRef *oas3.SchemaRef) error {
73-
if ope.OperationMore.Operation == nil {
73+
if ope.Operation == nil {
7474
return openapi3.ErrOperationNotSet
7575
}
76-
op := ope.OperationMore.Operation
76+
op := ope.Operation
7777
mediaType = strings.TrimSpace(mediaType)
7878
if op.RequestBody == nil {
7979
op.RequestBody = &oas3.RequestBodyRef{}
@@ -88,10 +88,10 @@ func (ope *OperationEdit) SetRequestBodySchemaRef(mediaType string, schemaRef *o
8888

8989
// AddSecurityRequirement adds a scheme name and value to an operation.
9090
func (ope *OperationEdit) AddSecurityRequirement(schemeName string, schemeValue []string) error {
91-
if ope.OperationMore.Operation == nil {
91+
if ope.Operation == nil {
9292
return openapi3.ErrOperationNotSet
9393
}
94-
op := ope.OperationMore.Operation
94+
op := ope.Operation
9595
if op.Security == nil {
9696
op.Security = &oas3.SecurityRequirements{}
9797
}
@@ -109,17 +109,17 @@ func (ope *OperationEdit) SetRequestBodySchemaRefMore(description string, requir
109109
*/
110110

111111
func (ope *OperationEdit) SetResponseBodySchemaRefMore(statusCode, description, contentType string, schemaRef *oas3.SchemaRef) error {
112-
return operationAddResponseBodySchemaRef(ope.OperationMore.Operation, statusCode, description, contentType, schemaRef)
112+
return operationAddResponseBodySchemaRef(ope.Operation, statusCode, description, contentType, schemaRef)
113113
}
114114

115115
func (ope *OperationEdit) AddToSpec(spec *openapi3.Spec, force bool) (bool, error) {
116116
sm := openapi3.SpecMore{Spec: spec}
117-
op, err := sm.OperationByPathMethod(ope.OperationMore.Path, ope.OperationMore.Method)
117+
op, err := sm.OperationByPathMethod(ope.Path, ope.Method)
118118
if err != nil {
119119
return false, err
120120
}
121121
if op == nil || force {
122-
spec.AddOperation(ope.OperationMore.Path, ope.OperationMore.Method, ope.OperationMore.Operation)
122+
spec.AddOperation(ope.Path, ope.Method, ope.Operation)
123123
return true, nil
124124
}
125125
return false, nil

openapi3edit/paths.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Paths struct {
1515

1616
func (p Paths) PathKeys() []string {
1717
var paths []string
18-
pathsMap := p.Paths.Map()
18+
pathsMap := p.Map()
1919
for k := range pathsMap {
2020
paths = append(paths, k)
2121
}

0 commit comments

Comments
 (0)