Skip to content

Commit 223b27e

Browse files
committed
Relint
* updated linter config * fixed reported linting issues Signed-off-by: Frederic BIDON <[email protected]>
1 parent 06305b8 commit 223b27e

9 files changed

+57
-43
lines changed

Diff for: .golangci.yml

+15-19
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
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:
13-
min-len: 3
7+
min-len: 2
148
min-occurrences: 3
159

1610
linters:
1711
enable-all: true
1812
disable:
19-
- maligned
13+
- recvcheck
14+
- unparam
2015
- lll
2116
- gochecknoinits
2217
- gochecknoglobals
@@ -28,17 +23,13 @@ linters:
2823
- wrapcheck
2924
- testpackage
3025
- nlreturn
31-
- gomnd
32-
- exhaustivestruct
33-
- goerr113
3426
- errorlint
3527
- nestif
3628
- godot
3729
- gofumpt
3830
- paralleltest
3931
- tparallel
4032
- thelper
41-
- ifshort
4233
- exhaustruct
4334
- varnamelen
4435
- gci
@@ -51,10 +42,15 @@ linters:
5142
- forcetypeassert
5243
- cyclop
5344
# deprecated linters
54-
- deadcode
55-
- interfacer
56-
- scopelint
57-
- varcheck
58-
- structcheck
59-
- golint
60-
- nosnakecase
45+
#- deadcode
46+
#- interfacer
47+
#- scopelint
48+
#- varcheck
49+
#- structcheck
50+
#- golint
51+
#- nosnakecase
52+
#- maligned
53+
#- goerr113
54+
#- ifshort
55+
#- gomnd
56+
#- exhaustivestruct

Diff for: errors.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package swag
2+
3+
type swagError string
4+
5+
const (
6+
// ErrYAML is an error raised by YAML utilities
7+
ErrYAML swagError = "yaml error"
8+
9+
// ErrLoader is an error raised by the file loader utility
10+
ErrLoader swagError = "loader error"
11+
)
12+
13+
func (e swagError) Error() string {
14+
return string(e)
15+
}

Diff for: json.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ func ConcatJSON(blobs ...[]byte) []byte {
126126
continue // don't know how to concatenate non container objects
127127
}
128128

129-
if len(b) < 3 { // yep empty but also the last one, so closing this thing
129+
const minLengthIfNotEmpty = 3
130+
if len(b) < minLengthIfNotEmpty { // yep empty but also the last one, so closing this thing
130131
if i == last && a > 0 {
131132
if err := buf.WriteByte(closing); err != nil {
132133
log.Println(err)

Diff for: json_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ func TestNameProvider(t *testing.T) {
139139

140140
}
141141

142+
//nolint:testifylint
142143
func TestJSONConcatenation(t *testing.T) {
144+
// we require an exact assertion (with ordering), not just JSON equivalence. Hence: testifylint disabled.
145+
143146
assert.Nil(t, ConcatJSON())
144147
assert.Equal(t, ConcatJSON([]byte(`{"id":1}`)), []byte(`{"id":1}`))
145148
assert.Equal(t, ConcatJSON([]byte(`{}`), []byte(`{}`)), []byte(`{}`))

Diff for: loading.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func loadHTTPBytes(timeout time.Duration) func(path string) ([]byte, error) {
168168
}
169169

170170
if resp.StatusCode != http.StatusOK {
171-
return nil, fmt.Errorf("could not access document at %q [%s] ", path, resp.Status)
171+
return nil, fmt.Errorf("could not access document at %q [%s]: %w", path, resp.Status, ErrLoader)
172172
}
173173

174174
return io.ReadAll(resp.Body)

Diff for: loading_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func TestLoadStrategy(t *testing.T) {
146146

147147
ld := LoadStrategy("blah", loader, remLoader)
148148
b, _ := ld("")
149-
assert.Equal(t, []byte(yamlPetStore), b)
149+
assert.YAMLEq(t, yamlPetStore, string(b))
150150

151151
serv := httptest.NewServer(http.HandlerFunc(yamlPestoreServer))
152152
defer serv.Close()

Diff for: util_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package swag
1616

1717
import (
18-
"fmt"
1918
"strings"
2019
"testing"
2120
"time"
@@ -401,7 +400,7 @@ func TestIsZero(t *testing.T) {
401400
}
402401

403402
for _, it := range data {
404-
assert.Equal(t, it.Expected, IsZero(it.Data), fmt.Sprintf("%#v", it.Data))
403+
assert.Equalf(t, it.Expected, IsZero(it.Data), "expected %#v, but got %#v", it.Expected, it.Data)
405404
}
406405
}
407406

Diff for: yaml.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package swag
1616

1717
import (
1818
"encoding/json"
19-
"errors"
2019
"fmt"
2120
"path/filepath"
2221
"reflect"
@@ -51,7 +50,7 @@ func BytesToYAMLDoc(data []byte) (interface{}, error) {
5150
return nil, err
5251
}
5352
if document.Kind != yaml.DocumentNode || len(document.Content) != 1 || document.Content[0].Kind != yaml.MappingNode {
54-
return nil, errors.New("only YAML documents that are objects are supported")
53+
return nil, fmt.Errorf("only YAML documents that are objects are supported: %w", ErrYAML)
5554
}
5655
return &document, nil
5756
}
@@ -69,31 +68,32 @@ func yamlNode(root *yaml.Node) (interface{}, error) {
6968
case yaml.AliasNode:
7069
return yamlNode(root.Alias)
7170
default:
72-
return nil, fmt.Errorf("unsupported YAML node type: %v", root.Kind)
71+
return nil, fmt.Errorf("unsupported YAML node type: %v: %w", root.Kind, ErrYAML)
7372
}
7473
}
7574

7675
func yamlDocument(node *yaml.Node) (interface{}, error) {
7776
if len(node.Content) != 1 {
78-
return nil, fmt.Errorf("unexpected YAML Document node content length: %d", len(node.Content))
77+
return nil, fmt.Errorf("unexpected YAML Document node content length: %d: %w", len(node.Content), ErrYAML)
7978
}
8079
return yamlNode(node.Content[0])
8180
}
8281

8382
func yamlMapping(node *yaml.Node) (interface{}, error) {
84-
m := make(JSONMapSlice, len(node.Content)/2)
83+
const sensibleAllocDivider = 2
84+
m := make(JSONMapSlice, len(node.Content)/sensibleAllocDivider)
8585

8686
var j int
8787
for i := 0; i < len(node.Content); i += 2 {
8888
var nmi JSONMapItem
8989
k, err := yamlStringScalarC(node.Content[i])
9090
if err != nil {
91-
return nil, fmt.Errorf("unable to decode YAML map key: %w", err)
91+
return nil, fmt.Errorf("unable to decode YAML map key: %w: %w", err, ErrYAML)
9292
}
9393
nmi.Key = k
9494
v, err := yamlNode(node.Content[i+1])
9595
if err != nil {
96-
return nil, fmt.Errorf("unable to process YAML map value for key %q: %w", k, err)
96+
return nil, fmt.Errorf("unable to process YAML map value for key %q: %w: %w", k, err, ErrYAML)
9797
}
9898
nmi.Value = v
9999
m[j] = nmi
@@ -109,7 +109,7 @@ func yamlSequence(node *yaml.Node) (interface{}, error) {
109109

110110
v, err := yamlNode(node.Content[i])
111111
if err != nil {
112-
return nil, fmt.Errorf("unable to decode YAML sequence value: %w", err)
112+
return nil, fmt.Errorf("unable to decode YAML sequence value: %w: %w", err, ErrYAML)
113113
}
114114
s = append(s, v)
115115
}
@@ -132,39 +132,39 @@ func yamlScalar(node *yaml.Node) (interface{}, error) {
132132
case yamlBoolScalar:
133133
b, err := strconv.ParseBool(node.Value)
134134
if err != nil {
135-
return nil, fmt.Errorf("unable to process scalar node. Got %q. Expecting bool content: %w", node.Value, err)
135+
return nil, fmt.Errorf("unable to process scalar node. Got %q. Expecting bool content: %w: %w", node.Value, err, ErrYAML)
136136
}
137137
return b, nil
138138
case yamlIntScalar:
139139
i, err := strconv.ParseInt(node.Value, 10, 64)
140140
if err != nil {
141-
return nil, fmt.Errorf("unable to process scalar node. Got %q. Expecting integer content: %w", node.Value, err)
141+
return nil, fmt.Errorf("unable to process scalar node. Got %q. Expecting integer content: %w: %w", node.Value, err, ErrYAML)
142142
}
143143
return i, nil
144144
case yamlFloatScalar:
145145
f, err := strconv.ParseFloat(node.Value, 64)
146146
if err != nil {
147-
return nil, fmt.Errorf("unable to process scalar node. Got %q. Expecting float content: %w", node.Value, err)
147+
return nil, fmt.Errorf("unable to process scalar node. Got %q. Expecting float content: %w: %w", node.Value, err, ErrYAML)
148148
}
149149
return f, nil
150150
case yamlTimestamp:
151151
return node.Value, nil
152152
case yamlNull:
153153
return nil, nil //nolint:nilnil
154154
default:
155-
return nil, fmt.Errorf("YAML tag %q is not supported", node.LongTag())
155+
return nil, fmt.Errorf("YAML tag %q is not supported: %w", node.LongTag(), ErrYAML)
156156
}
157157
}
158158

159159
func yamlStringScalarC(node *yaml.Node) (string, error) {
160160
if node.Kind != yaml.ScalarNode {
161-
return "", fmt.Errorf("expecting a string scalar but got %q", node.Kind)
161+
return "", fmt.Errorf("expecting a string scalar but got %q: %w", node.Kind, ErrYAML)
162162
}
163163
switch node.LongTag() {
164164
case yamlStringScalar, yamlIntScalar, yamlFloatScalar:
165165
return node.Value, nil
166166
default:
167-
return "", fmt.Errorf("YAML tag %q is not supported as map key", node.LongTag())
167+
return "", fmt.Errorf("YAML tag %q is not supported as map key: %w", node.LongTag(), ErrYAML)
168168
}
169169
}
170170

@@ -349,7 +349,7 @@ func json2yaml(item interface{}) (*yaml.Node, error) {
349349
Value: strconv.FormatBool(val),
350350
}, nil
351351
default:
352-
return nil, fmt.Errorf("unhandled type: %T", val)
352+
return nil, fmt.Errorf("unhandled type: %T: %w", val, ErrYAML)
353353
}
354354
}
355355

@@ -416,7 +416,7 @@ func transformData(input interface{}) (out interface{}, err error) {
416416
case int64:
417417
return strconv.FormatInt(k, 10), nil
418418
default:
419-
return "", fmt.Errorf("unexpected map key type, got: %T", k)
419+
return "", fmt.Errorf("unexpected map key type, got: %T: %w", k, ErrYAML)
420420
}
421421
}
422422

Diff for: yaml_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ name: a string value
110110
d, err := YAMLToJSON(data)
111111
require.NoError(t, err)
112112
require.NotNil(t, d)
113-
assert.Equal(t, `{"1":"the int key value","name":"a string value","y":"some value"}`, string(d))
113+
assert.JSONEq(t, `{"1":"the int key value","name":"a string value","y":"some value"}`, string(d))
114114

115115
ns := []*yaml.Node{
116116
{
@@ -157,7 +157,7 @@ name: a string value
157157

158158
d, err = YAMLToJSON(data)
159159
require.NoError(t, err)
160-
assert.Equal(t, `{"1":"the int key value","name":"a string value","y":"some value","tag":{"name":"tag name"}}`, string(d))
160+
assert.JSONEq(t, `{"1":"the int key value","name":"a string value","y":"some value","tag":{"name":"tag name"}}`, string(d))
161161

162162
tag[1].Content = []*yaml.Node{
163163
{
@@ -198,7 +198,7 @@ name: a string value
198198

199199
d, err = YAMLToJSON(dd)
200200
require.NoError(t, err)
201-
assert.Equal(t, json.RawMessage(`{"description":"object created"}`), d)
201+
assert.JSONEq(t, `{"description":"object created"}`, string(d))
202202
}
203203

204204
var yamlPestoreServer = func(rw http.ResponseWriter, _ *http.Request) {

0 commit comments

Comments
 (0)