Skip to content

Commit 5afe166

Browse files
committed
Relint
* updated linter config * fixed linter issues (esp. errors wrapping) Signed-off-by: Frederic BIDON <[email protected]>
1 parent d677bf7 commit 5afe166

File tree

6 files changed

+47
-32
lines changed

6 files changed

+47
-32
lines changed

.golangci.yml

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
linters-settings:
2-
govet:
3-
check-shadowing: true
4-
golint:
5-
min-confidence: 0
62
gocyclo:
73
min-complexity: 45
8-
maligned:
9-
suggest-new: true
104
dupl:
115
threshold: 200
126
goconst:
@@ -16,7 +10,6 @@ linters-settings:
1610
linters:
1711
enable-all: true
1812
disable:
19-
- maligned
2013
- unparam
2114
- lll
2215
- gochecknoinits
@@ -29,17 +22,13 @@ linters:
2922
- wrapcheck
3023
- testpackage
3124
- nlreturn
32-
- gomnd
33-
- exhaustivestruct
34-
- goerr113
3525
- errorlint
3626
- nestif
3727
- godot
3828
- gofumpt
3929
- paralleltest
4030
- tparallel
4131
- thelper
42-
- ifshort
4332
- exhaustruct
4433
- varnamelen
4534
- gci
@@ -52,10 +41,15 @@ linters:
5241
- forcetypeassert
5342
- cyclop
5443
# deprecated linters
55-
- deadcode
56-
- interfacer
57-
- scopelint
58-
- varcheck
59-
- structcheck
60-
- golint
61-
- nosnakecase
44+
#- deadcode
45+
#- interfacer
46+
#- scopelint
47+
#- varcheck
48+
#- structcheck
49+
#- golint
50+
#- nosnakecase
51+
#- maligned
52+
#- goerr113
53+
#- ifshort
54+
#- gomnd
55+
#- exhaustivestruct

errors.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package loads
2+
3+
type loaderError string
4+
5+
func (e loaderError) Error() string {
6+
return string(e)
7+
}
8+
9+
const (
10+
// ErrLoads is an error returned by the loads package
11+
ErrLoads loaderError = "loaderrs error"
12+
13+
// ErrNoLoader indicates that no configured loader matched the input
14+
ErrNoLoader loaderError = "no loader matched"
15+
)

fmts/yaml_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ import (
2828
"github.com/stretchr/testify/require"
2929
)
3030

31+
var errTest = errors.New("expected")
32+
3133
type failJSONMarshal struct {
3234
}
3335

3436
func (f failJSONMarshal) MarshalJSON() ([]byte, error) {
35-
return nil, errors.New("expected")
37+
return nil, errTest
3638
}
3739

3840
func TestLoadHTTPBytes(t *testing.T) {
@@ -124,7 +126,7 @@ name: a string value
124126

125127
d, err := YAMLToJSON(dd)
126128
require.NoError(t, err)
127-
assert.Equal(t, json.RawMessage(`{"description":"object created"}`), d)
129+
assert.YAMLEq(t, `{"description":"object created"}`, string(d))
128130
})
129131
}
130132

@@ -138,7 +140,7 @@ func TestLoadStrategy(t *testing.T) {
138140

139141
ld := swag.LoadStrategy("blah", loader, remLoader)
140142
b, _ := ld("")
141-
assert.Equal(t, []byte(yamlPetStore), b)
143+
assert.YAMLEq(t, yamlPetStore, string(b))
142144

143145
serv := httptest.NewServer(http.HandlerFunc(yamlPestoreServer))
144146
defer serv.Close()

loaders.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ func (l *loader) WithNext(next *loader) *loader {
8383
func (l *loader) Load(path string) (json.RawMessage, error) {
8484
_, erp := url.Parse(path)
8585
if erp != nil {
86-
return nil, erp
86+
return nil, errors.Join(erp, ErrLoads)
8787
}
8888

89-
lastErr := errors.New("no loader matched") // default error if no match was found
89+
var lastErr error = ErrNoLoader // default error if no match was found
9090
for ldr := l; ldr != nil; ldr = ldr.Next {
9191
if ldr.Match != nil && !ldr.Match(path) {
9292
continue
@@ -101,14 +101,14 @@ func (l *loader) Load(path string) (json.RawMessage, error) {
101101
lastErr = err
102102
}
103103

104-
return nil, lastErr
104+
return nil, errors.Join(lastErr, ErrLoads)
105105
}
106106

107107
// JSONDoc loads a json document from either a file or a remote url
108108
func JSONDoc(path string) (json.RawMessage, error) {
109109
data, err := swag.LoadFromFileOrHTTP(path)
110110
if err != nil {
111-
return nil, err
111+
return nil, errors.Join(err, ErrLoads)
112112
}
113113
return json.RawMessage(data), nil
114114
}

options_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
const optionFixture = "fixtures/json/resources/pathLoaderIssue.json"
1616

17+
var errTest = errors.New("test")
18+
1719
func TestOptionsWithDocLoader(t *testing.T) {
1820
document, err := Spec(optionFixture, WithDocLoader(func(pth string) (json.RawMessage, error) {
1921
buf, err := os.ReadFile(pth)
@@ -105,11 +107,11 @@ func TestOptionsWithDocLoaderMatches(t *testing.T) {
105107

106108
// the nil matcher always matches
107109
nilMatcher := NewDocLoaderWithMatch(func(_ string) (json.RawMessage, error) {
108-
return nil, errors.New("test")
110+
return nil, errTest
109111
}, nil)
110112
_, err = Spec(optionFixture, WithDocLoaderMatches(nilMatcher))
111113
require.Error(t, err)
112-
require.Equal(t, "test", err.Error())
114+
require.ErrorIs(t, err, errTest)
113115

114116
// when a matcher returns an errors, the next one is tried
115117
document, err = Spec(optionFixture, WithDocLoaderMatches(nilMatcher, jsonLoader, yamlLoader))

spec.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"bytes"
1919
"encoding/gob"
2020
"encoding/json"
21+
"errors"
2122
"fmt"
2223

2324
"github.com/go-openapi/analysis"
@@ -102,7 +103,7 @@ func Analyzed(data json.RawMessage, version string, options ...LoaderOption) (*D
102103
version = "2.0"
103104
}
104105
if version != "2.0" {
105-
return nil, fmt.Errorf("spec version %q is not supported", version)
106+
return nil, fmt.Errorf("spec version %q is not supported: %w", version, ErrLoads)
106107
}
107108

108109
raw, err := trimData(data) // trim blanks, then convert yaml docs into json
@@ -112,12 +113,12 @@ func Analyzed(data json.RawMessage, version string, options ...LoaderOption) (*D
112113

113114
swspec := new(spec.Swagger)
114115
if err = json.Unmarshal(raw, swspec); err != nil {
115-
return nil, err
116+
return nil, errors.Join(err, ErrLoads)
116117
}
117118

118119
origsqspec, err := cloneSpec(swspec)
119120
if err != nil {
120-
return nil, err
121+
return nil, errors.Join(err, ErrLoads)
121122
}
122123

123124
d := &Document{
@@ -145,12 +146,12 @@ func trimData(in json.RawMessage) (json.RawMessage, error) {
145146
// assume yaml doc: convert it to json
146147
yml, err := swag.BytesToYAMLDoc(trimmed)
147148
if err != nil {
148-
return nil, fmt.Errorf("analyzed: %v", err)
149+
return nil, fmt.Errorf("analyzed: %v: %w", err, ErrLoads)
149150
}
150151

151152
d, err := swag.YAMLToJSON(yml)
152153
if err != nil {
153-
return nil, fmt.Errorf("analyzed: %v", err)
154+
return nil, fmt.Errorf("analyzed: %v: %w", err, ErrLoads)
154155
}
155156

156157
return d, nil
@@ -271,5 +272,6 @@ func cloneSpec(src *spec.Swagger) (*spec.Swagger, error) {
271272
if err := gob.NewDecoder(&b).Decode(&dst); err != nil {
272273
return nil, err
273274
}
275+
274276
return &dst, nil
275277
}

0 commit comments

Comments
 (0)