Skip to content

Commit

Permalink
Refactor tests (#46)
Browse files Browse the repository at this point in the history
* Make methode more compact

* Remove dupicate switch statement

* Remove duplicate test

* Ignore doc.go for codeclimate

* Remove unneeded linebreaks

* Add more test and refactor existing ones

* MSI(keyAndValuePairs ...interface{}) Map does not panik

* Upload github.com/stretchr/testify/require to vendor

* Replace map[string]interface{} with Map
  • Loading branch information
hanzei authored Jan 25, 2018
1 parent c8018d9 commit 1a077bf
Show file tree
Hide file tree
Showing 21 changed files with 2,170 additions and 216 deletions.
4 changes: 1 addition & 3 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ engines:
enabled: true
golint:
enabled: true
checks:
GoLint/Comments/PackageComments:
enabled: false
govet:
enabled: true

exclude_patterns:
- ".github/"
- "vendor/"
- "codegen/"
- "doc.go"
7 changes: 5 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 5 additions & 28 deletions accessors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package objx

import (
"fmt"
"regexp"
"strconv"
"strings"
Expand All @@ -28,7 +27,7 @@ var arrayAccesRegex = regexp.MustCompile(arrayAccesRegexString)
//
// o.Get("books[1].chapters[2].title")
func (m Map) Get(selector string) *Value {
rawObj := access(m, selector, nil, false, false)
rawObj := access(m, selector, nil, false)
return &Value{data: rawObj}
}

Expand All @@ -43,34 +42,25 @@ func (m Map) Get(selector string) *Value {
//
// o.Set("books[1].chapters[2].title","Time to Go")
func (m Map) Set(selector string, value interface{}) Map {
access(m, selector, value, true, false)
access(m, selector, value, true)
return m
}

// access accesses the object using the selector and performs the
// appropriate action.
func access(current, selector, value interface{}, isSet, panics bool) interface{} {

func access(current, selector, value interface{}, isSet bool) interface{} {
switch selector.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:

if array, ok := current.([]interface{}); ok {
index := intFromInterface(selector)

if index >= len(array) {
if panics {
panic(fmt.Sprintf("objx: Index %d is out of range. Slice only contains %d items.", index, len(array)))
}
return nil
}

return array[index]
}

return nil

case string:

selStr := selector.(string)
selSegs := strings.SplitN(selStr, PathSeparator, 2)
thisSel := selSegs[0]
Expand All @@ -79,7 +69,6 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{

if strings.Contains(thisSel, "[") {
arrayMatches := arrayAccesRegex.FindStringSubmatch(thisSel)

if len(arrayMatches) > 0 {
// Get the key into the map
thisSel = arrayMatches[1]
Expand All @@ -94,11 +83,9 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{
}
}
}

if curMap, ok := current.(Map); ok {
current = map[string]interface{}(curMap)
}

// get the object in question
switch current.(type) {
case map[string]interface{}:
Expand All @@ -111,29 +98,19 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{
default:
current = nil
}

if current == nil && panics {
panic(fmt.Sprintf("objx: '%v' invalid on object.", selector))
}

// do we need to access the item of an array?
if index > -1 {
if array, ok := current.([]interface{}); ok {
if index < len(array) {
current = array[index]
} else {
if panics {
panic(fmt.Sprintf("objx: Index %d is out of range. Slice only contains %d items.", index, len(array)))
}
current = nil
}
}
}

if len(selSegs) > 1 {
current = access(current, selSegs[1], value, isSet, panics)
current = access(current, selSegs[1], value, isSet)
}

}
return current
}
Expand Down Expand Up @@ -165,7 +142,7 @@ func intFromInterface(selector interface{}) int {
case uint64:
value = int(selector.(uint64))
default:
panic("objx: array access argument is not an integer type (this should never happen)")
return 0
}
return value
}
Loading

0 comments on commit 1a077bf

Please # to comment.