Skip to content

Commit facf9a8

Browse files
authored
Add linter (#35)
* Add linter * Lint files * Split template files
1 parent 5fbe94b commit facf9a8

21 files changed

+456
-1392
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ install:
88
- go get github.com/go-task/task/cmd/task
99

1010
script:
11+
- task dl-deps
12+
- task lint
1113
- task test

Taskfile.yml

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
default:
2+
deps: [test]
3+
14
dl-deps:
25
desc: Downloads cli dependencies
36
cmds:
@@ -11,7 +14,13 @@ update-deps:
1114
- dep ensure -update
1215
- dep prune
1316

17+
lint:
18+
desc: Runs golint
19+
cmds:
20+
- golint $(ls *.go | grep -v "doc.go")
21+
silent: true
22+
1423
test:
15-
desc: Run all the go tests.
24+
desc: Runs go tests
1625
cmds:
1726
- go test -race .

accessors.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,10 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{
7777
index := -1
7878
var err error
7979

80-
// https://github.com/stretchr/objx/issues/12
8180
if strings.Contains(thisSel, "[") {
82-
8381
arrayMatches := arrayAccesRegex.FindStringSubmatch(thisSel)
8482

8583
if len(arrayMatches) > 0 {
86-
8784
// Get the key into the map
8885
thisSel = arrayMatches[1]
8986

@@ -95,7 +92,6 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{
9592
// seriously wrong. Panic.
9693
panic("objx: Array index is not an integer. Must use array[int].")
9794
}
98-
9995
}
10096
}
10197

@@ -110,9 +106,8 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{
110106
if len(selSegs) <= 1 && isSet {
111107
curMSI[thisSel] = value
112108
return nil
113-
} else {
114-
current = curMSI[thisSel]
115109
}
110+
current = curMSI[thisSel]
116111
default:
117112
current = nil
118113
}
@@ -140,9 +135,7 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{
140135
}
141136

142137
}
143-
144138
return current
145-
146139
}
147140

148141
// intFromInterface converts an interface object to the largest
@@ -174,6 +167,5 @@ func intFromInterface(selector interface{}) int {
174167
default:
175168
panic("objx: array access argument is not an integer type (this should never happen)")
176169
}
177-
178170
return value
179171
}

accessors_test.go

+20-32
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
11
package objx
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

89
func TestAccessorsAccessGetSingleField(t *testing.T) {
9-
1010
current := map[string]interface{}{"name": "Tyler"}
11-
assert.Equal(t, "Tyler", access(current, "name", nil, false, true))
1211

12+
assert.Equal(t, "Tyler", access(current, "name", nil, false, true))
1313
}
14-
func TestAccessorsAccessGetDeep(t *testing.T) {
1514

15+
func TestAccessorsAccessGetDeep(t *testing.T) {
1616
current := map[string]interface{}{"name": map[string]interface{}{"first": "Tyler", "last": "Bunnell"}}
17+
1718
assert.Equal(t, "Tyler", access(current, "name.first", nil, false, true))
1819
assert.Equal(t, "Bunnell", access(current, "name.last", nil, false, true))
19-
2020
}
21-
func TestAccessorsAccessGetDeepDeep(t *testing.T) {
2221

22+
func TestAccessorsAccessGetDeepDeep(t *testing.T) {
2323
current := map[string]interface{}{"one": map[string]interface{}{"two": map[string]interface{}{"three": map[string]interface{}{"four": 4}}}}
24-
assert.Equal(t, 4, access(current, "one.two.three.four", nil, false, true))
2524

25+
assert.Equal(t, 4, access(current, "one.two.three.four", nil, false, true))
2626
}
27-
func TestAccessorsAccessGetInsideArray(t *testing.T) {
2827

28+
func TestAccessorsAccessGetInsideArray(t *testing.T) {
2929
current := map[string]interface{}{"names": []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}}
30+
3031
assert.Equal(t, "Tyler", access(current, "names[0].first", nil, false, true))
3132
assert.Equal(t, "Bunnell", access(current, "names[0].last", nil, false, true))
3233
assert.Equal(t, "Capitol", access(current, "names[1].first", nil, false, true))
3334
assert.Equal(t, "Bollocks", access(current, "names[1].last", nil, false, true))
34-
3535
assert.Panics(t, func() {
3636
access(current, "names[2]", nil, false, true)
3737
})
3838
assert.Nil(t, access(current, "names[2]", nil, false, false))
39-
4039
}
4140

4241
func TestAccessorsAccessGetFromArrayWithInt(t *testing.T) {
43-
4442
current := []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}
4543
one := access(current, 0, nil, false, false)
4644
two := access(current, 1, nil, false, false)
@@ -49,66 +47,59 @@ func TestAccessorsAccessGetFromArrayWithInt(t *testing.T) {
4947
assert.Equal(t, "Tyler", one.(map[string]interface{})["first"])
5048
assert.Equal(t, "Capitol", two.(map[string]interface{})["first"])
5149
assert.Nil(t, three)
52-
5350
}
5451

5552
func TestAccessorsGet(t *testing.T) {
56-
5753
current := New(map[string]interface{}{"name": "Tyler"})
58-
assert.Equal(t, "Tyler", current.Get("name").data)
5954

55+
assert.Equal(t, "Tyler", current.Get("name").data)
6056
}
6157

6258
func TestAccessorsAccessSetSingleField(t *testing.T) {
63-
6459
current := map[string]interface{}{"name": "Tyler"}
65-
access(current, "name", "Mat", true, false)
66-
assert.Equal(t, current["name"], "Mat")
6760

61+
access(current, "name", "Mat", true, false)
6862
access(current, "age", 29, true, true)
69-
assert.Equal(t, current["age"], 29)
7063

64+
assert.Equal(t, current["name"], "Mat")
65+
assert.Equal(t, current["age"], 29)
7166
}
7267

7368
func TestAccessorsAccessSetSingleFieldNotExisting(t *testing.T) {
74-
7569
current := map[string]interface{}{}
70+
7671
access(current, "name", "Mat", true, false)
77-
assert.Equal(t, current["name"], "Mat")
7872

73+
assert.Equal(t, current["name"], "Mat")
7974
}
8075

8176
func TestAccessorsAccessSetDeep(t *testing.T) {
82-
8377
current := map[string]interface{}{"name": map[string]interface{}{"first": "Tyler", "last": "Bunnell"}}
8478

8579
access(current, "name.first", "Mat", true, true)
8680
access(current, "name.last", "Ryer", true, true)
8781

8882
assert.Equal(t, "Mat", access(current, "name.first", nil, false, true))
8983
assert.Equal(t, "Ryer", access(current, "name.last", nil, false, true))
90-
9184
}
92-
func TestAccessorsAccessSetDeepDeep(t *testing.T) {
9385

86+
func TestAccessorsAccessSetDeepDeep(t *testing.T) {
9487
current := map[string]interface{}{"one": map[string]interface{}{"two": map[string]interface{}{"three": map[string]interface{}{"four": 4}}}}
9588

9689
access(current, "one.two.three.four", 5, true, true)
9790

9891
assert.Equal(t, 5, access(current, "one.two.three.four", nil, false, true))
99-
10092
}
101-
func TestAccessorsAccessSetArray(t *testing.T) {
10293

94+
func TestAccessorsAccessSetArray(t *testing.T) {
10395
current := map[string]interface{}{"names": []interface{}{"Tyler"}}
10496

10597
access(current, "names[0]", "Mat", true, true)
10698

10799
assert.Equal(t, "Mat", access(current, "names[0]", nil, false, true))
108-
109100
}
110-
func TestAccessorsAccessSetInsideArray(t *testing.T) {
111101

102+
func TestAccessorsAccessSetInsideArray(t *testing.T) {
112103
current := map[string]interface{}{"names": []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}}
113104

114105
access(current, "names[0].first", "Mat", true, true)
@@ -120,11 +111,9 @@ func TestAccessorsAccessSetInsideArray(t *testing.T) {
120111
assert.Equal(t, "Ryer", access(current, "names[0].last", nil, false, true))
121112
assert.Equal(t, "Captain", access(current, "names[1].first", nil, false, true))
122113
assert.Equal(t, "Underpants", access(current, "names[1].last", nil, false, true))
123-
124114
}
125115

126116
func TestAccessorsAccessSetFromArrayWithInt(t *testing.T) {
127-
128117
current := []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}
129118
one := access(current, 0, nil, false, false)
130119
two := access(current, 1, nil, false, false)
@@ -133,13 +122,12 @@ func TestAccessorsAccessSetFromArrayWithInt(t *testing.T) {
133122
assert.Equal(t, "Tyler", one.(map[string]interface{})["first"])
134123
assert.Equal(t, "Capitol", two.(map[string]interface{})["first"])
135124
assert.Nil(t, three)
136-
137125
}
138126

139127
func TestAccessorsSet(t *testing.T) {
140-
141128
current := New(map[string]interface{}{"name": "Tyler"})
129+
142130
current.Set("name", "Mat")
143-
assert.Equal(t, "Mat", current.Get("name").data)
144131

132+
assert.Equal(t, "Mat", current.Get("name").data)
145133
}

0 commit comments

Comments
 (0)