Skip to content

Commit

Permalink
Add linter (#35)
Browse files Browse the repository at this point in the history
* Add linter

* Lint files

* Split template files
  • Loading branch information
hanzei committed Jan 6, 2018
1 parent 5fbe94b commit facf9a8
Show file tree
Hide file tree
Showing 21 changed files with 456 additions and 1,392 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -8,4 +8,6 @@ install:
- go get github.com/go-task/task/cmd/task

script:
- task dl-deps
- task lint
- task test
11 changes: 10 additions & 1 deletion Taskfile.yml
@@ -1,3 +1,6 @@
default:
deps: [test]

dl-deps:
desc: Downloads cli dependencies
cmds:
Expand All @@ -11,7 +14,13 @@ update-deps:
- dep ensure -update
- dep prune

lint:
desc: Runs golint
cmds:
- golint $(ls *.go | grep -v "doc.go")
silent: true

test:
desc: Run all the go tests.
desc: Runs go tests
cmds:
- go test -race .
10 changes: 1 addition & 9 deletions accessors.go
Expand Up @@ -77,13 +77,10 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{
index := -1
var err error

// https://github.com/stretchr/objx/issues/12
if strings.Contains(thisSel, "[") {

arrayMatches := arrayAccesRegex.FindStringSubmatch(thisSel)

if len(arrayMatches) > 0 {

// Get the key into the map
thisSel = arrayMatches[1]

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

}
}

Expand All @@ -110,9 +106,8 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{
if len(selSegs) <= 1 && isSet {
curMSI[thisSel] = value
return nil
} else {
current = curMSI[thisSel]
}
current = curMSI[thisSel]
default:
current = nil
}
Expand Down Expand Up @@ -140,9 +135,7 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{
}

}

return current

}

// intFromInterface converts an interface object to the largest
Expand Down Expand Up @@ -174,6 +167,5 @@ func intFromInterface(selector interface{}) int {
default:
panic("objx: array access argument is not an integer type (this should never happen)")
}

return value
}
52 changes: 20 additions & 32 deletions accessors_test.go
@@ -1,46 +1,44 @@
package objx

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestAccessorsAccessGetSingleField(t *testing.T) {

current := map[string]interface{}{"name": "Tyler"}
assert.Equal(t, "Tyler", access(current, "name", nil, false, true))

assert.Equal(t, "Tyler", access(current, "name", nil, false, true))
}
func TestAccessorsAccessGetDeep(t *testing.T) {

func TestAccessorsAccessGetDeep(t *testing.T) {
current := map[string]interface{}{"name": map[string]interface{}{"first": "Tyler", "last": "Bunnell"}}

assert.Equal(t, "Tyler", access(current, "name.first", nil, false, true))
assert.Equal(t, "Bunnell", access(current, "name.last", nil, false, true))

}
func TestAccessorsAccessGetDeepDeep(t *testing.T) {

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

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

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

assert.Equal(t, "Tyler", access(current, "names[0].first", nil, false, true))
assert.Equal(t, "Bunnell", access(current, "names[0].last", nil, false, true))
assert.Equal(t, "Capitol", access(current, "names[1].first", nil, false, true))
assert.Equal(t, "Bollocks", access(current, "names[1].last", nil, false, true))

assert.Panics(t, func() {
access(current, "names[2]", nil, false, true)
})
assert.Nil(t, access(current, "names[2]", nil, false, false))

}

func TestAccessorsAccessGetFromArrayWithInt(t *testing.T) {

current := []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}
one := access(current, 0, nil, false, false)
two := access(current, 1, nil, false, false)
Expand All @@ -49,66 +47,59 @@ func TestAccessorsAccessGetFromArrayWithInt(t *testing.T) {
assert.Equal(t, "Tyler", one.(map[string]interface{})["first"])
assert.Equal(t, "Capitol", two.(map[string]interface{})["first"])
assert.Nil(t, three)

}

func TestAccessorsGet(t *testing.T) {

current := New(map[string]interface{}{"name": "Tyler"})
assert.Equal(t, "Tyler", current.Get("name").data)

assert.Equal(t, "Tyler", current.Get("name").data)
}

func TestAccessorsAccessSetSingleField(t *testing.T) {

current := map[string]interface{}{"name": "Tyler"}
access(current, "name", "Mat", true, false)
assert.Equal(t, current["name"], "Mat")

access(current, "name", "Mat", true, false)
access(current, "age", 29, true, true)
assert.Equal(t, current["age"], 29)

assert.Equal(t, current["name"], "Mat")
assert.Equal(t, current["age"], 29)
}

func TestAccessorsAccessSetSingleFieldNotExisting(t *testing.T) {

current := map[string]interface{}{}

access(current, "name", "Mat", true, false)
assert.Equal(t, current["name"], "Mat")

assert.Equal(t, current["name"], "Mat")
}

func TestAccessorsAccessSetDeep(t *testing.T) {

current := map[string]interface{}{"name": map[string]interface{}{"first": "Tyler", "last": "Bunnell"}}

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

assert.Equal(t, "Mat", access(current, "name.first", nil, false, true))
assert.Equal(t, "Ryer", access(current, "name.last", nil, false, true))

}
func TestAccessorsAccessSetDeepDeep(t *testing.T) {

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

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

assert.Equal(t, 5, access(current, "one.two.three.four", nil, false, true))

}
func TestAccessorsAccessSetArray(t *testing.T) {

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

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

assert.Equal(t, "Mat", access(current, "names[0]", nil, false, true))

}
func TestAccessorsAccessSetInsideArray(t *testing.T) {

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

access(current, "names[0].first", "Mat", true, true)
Expand All @@ -120,11 +111,9 @@ func TestAccessorsAccessSetInsideArray(t *testing.T) {
assert.Equal(t, "Ryer", access(current, "names[0].last", nil, false, true))
assert.Equal(t, "Captain", access(current, "names[1].first", nil, false, true))
assert.Equal(t, "Underpants", access(current, "names[1].last", nil, false, true))

}

func TestAccessorsAccessSetFromArrayWithInt(t *testing.T) {

current := []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}
one := access(current, 0, nil, false, false)
two := access(current, 1, nil, false, false)
Expand All @@ -133,13 +122,12 @@ func TestAccessorsAccessSetFromArrayWithInt(t *testing.T) {
assert.Equal(t, "Tyler", one.(map[string]interface{})["first"])
assert.Equal(t, "Capitol", two.(map[string]interface{})["first"])
assert.Nil(t, three)

}

func TestAccessorsSet(t *testing.T) {

current := New(map[string]interface{}{"name": "Tyler"})

current.Set("name", "Mat")
assert.Equal(t, "Mat", current.Get("name").data)

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

0 comments on commit facf9a8

Please sign in to comment.