diff --git a/.travis.yml b/.travis.yml index 97d2142..1456363 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,4 +8,6 @@ install: - go get github.com/go-task/task/cmd/task script: +- task dl-deps +- task lint - task test diff --git a/Taskfile.yml b/Taskfile.yml index 02460b1..403b5f0 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -1,3 +1,6 @@ +default: + deps: [test] + dl-deps: desc: Downloads cli dependencies cmds: @@ -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 . diff --git a/accessors.go b/accessors.go index 721bcac..d95be0c 100644 --- a/accessors.go +++ b/accessors.go @@ -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] @@ -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].") } - } } @@ -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 } @@ -140,9 +135,7 @@ func access(current, selector, value interface{}, isSet, panics bool) interface{ } } - return current - } // intFromInterface converts an interface object to the largest @@ -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 } diff --git a/accessors_test.go b/accessors_test.go index ce5d8e4..cc3816a 100644 --- a/accessors_test.go +++ b/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) @@ -49,37 +47,33 @@ 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) @@ -87,28 +81,25 @@ func TestAccessorsAccessSetDeep(t *testing.T) { 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) @@ -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) @@ -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) } diff --git a/codegen/template.txt b/codegen/template.txt index b396900..047bfc1 100644 --- a/codegen/template.txt +++ b/codegen/template.txt @@ -1,6 +1,5 @@ /* {4} ({1} and []{1}) - -------------------------------------------------- */ // {4} gets the value as a {1}, returns the optionalDefault @@ -58,44 +57,35 @@ func (v *Value) Is{4}Slice() bool { // // Panics if the object is the wrong type. func (v *Value) Each{4}(callback func(int, {1}) bool) *Value { - for index, val := range v.Must{4}Slice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // Where{4} uses the specified decider function to select items // from the []{1}. The object contained in the result will contain // only the selected items. func (v *Value) Where{4}(decider func(int, {1}) bool) *Value { - var selected []{1} - v.Each{4}(func(index int, val {1}) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data:selected} - } // Group{4} uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]{1}. func (v *Value) Group{4}(grouper func(int, {1}) string) *Value { - groups := make(map[string][]{1}) - v.Each{4}(func(index int, val {1}) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -104,183 +94,31 @@ func (v *Value) Group{4}(grouper func(int, {1}) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data:groups} - } // Replace{4} uses the specified function to replace each {1}s // by iterating each item. The data in the returned result will be a // []{1} containing the replaced items. func (v *Value) Replace{4}(replacer func(int, {1}) {1}) *Value { - arr := v.Must{4}Slice() replaced := make([]{1}, len(arr)) - v.Each{4}(func(index int, val {1}) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data:replaced} - } // Collect{4} uses the specified collector function to collect a value // for each of the {1}s in the slice. The data returned will be a // []interface{}. func (v *Value) Collect{4}(collector func(int, {1}) interface{}) *Value { - arr := v.Must{4}Slice() collected := make([]interface{}, len(arr)) - v.Each{4}(func(index int, val {1}) bool { collected[index] = collector(index, val) return true }) - return &Value{data:collected} } - -// ************************************************************ -// TESTS -// ************************************************************ - -func Test{4}(t *testing.T) { - - val := {1}( {2} ) - m := map[string]interface{}{"value": val, "nothing": nil} - assert.Equal(t, val, New(m).Get("value").{4}()) - assert.Equal(t, val, New(m).Get("value").Must{4}()) - assert.Equal(t, {1}({3}), New(m).Get("nothing").{4}()) - assert.Equal(t, val, New(m).Get("nothing").{4}({2})) - - assert.Panics(t, func() { - New(m).Get("age").Must{4}() - }) - -} - -func Test{4}Slice(t *testing.T) { - - val := {1}( {2} ) - m := map[string]interface{}{"value": []{1}{ val }, "nothing": nil} - assert.Equal(t, val, New(m).Get("value").{4}Slice()[0]) - assert.Equal(t, val, New(m).Get("value").Must{4}Slice()[0]) - assert.Equal(t, []{1}(nil), New(m).Get("nothing").{4}Slice()) - assert.Equal(t, val, New(m).Get("nothing").{4}Slice( []{1}{ {1}({2}) } )[0]) - - assert.Panics(t, func() { - New(m).Get("nothing").Must{4}Slice() - }) - -} - -func TestIs{4}(t *testing.T) { - - var v *Value - - v = &Value{data: {1}({2})} - assert.True(t, v.Is{4}()) - - v = &Value{data: []{1}{ {1}({2}) }} - assert.True(t, v.Is{4}Slice()) - -} - -func TestEach{4}(t *testing.T) { - - v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }} - count := 0 - replacedVals := make([]{1}, 0) - assert.Equal(t, v, v.Each{4}(func(i int, val {1}) bool { - - count++ - replacedVals = append(replacedVals, val) - - // abort early - if i == 2 { - return false - } - - return true - - })) - - assert.Equal(t, count, 3) - assert.Equal(t, replacedVals[0], v.Must{4}Slice()[0]) - assert.Equal(t, replacedVals[1], v.Must{4}Slice()[1]) - assert.Equal(t, replacedVals[2], v.Must{4}Slice()[2]) - -} - -func TestWhere{4}(t *testing.T) { - - v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }} - - selected := v.Where{4}(func(i int, val {1}) bool { - return i%2==0 - }).Must{4}Slice() - - assert.Equal(t, 3, len(selected)) - -} - -func TestGroup{4}(t *testing.T) { - - v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }} - - grouped := v.Group{4}(func(i int, val {1}) string { - return fmt.Sprintf("%v", i%2==0) - }).data.(map[string][]{1}) - - assert.Equal(t, 2, len(grouped)) - assert.Equal(t, 3, len(grouped["true"])) - assert.Equal(t, 3, len(grouped["false"])) - -} - -func TestReplace{4}(t *testing.T) { - - v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }} - - rawArr := v.Must{4}Slice() - - replaced := v.Replace{4}(func(index int, val {1}) {1} { - if index < len(rawArr)-1 { - return rawArr[index+1] - } - return rawArr[0] - }) - - replacedArr := replaced.Must{4}Slice() - if assert.Equal(t, 6, len(replacedArr)) { - assert.Equal(t, replacedArr[0], rawArr[1]) - assert.Equal(t, replacedArr[1], rawArr[2]) - assert.Equal(t, replacedArr[2], rawArr[3]) - assert.Equal(t, replacedArr[3], rawArr[4]) - assert.Equal(t, replacedArr[4], rawArr[5]) - assert.Equal(t, replacedArr[5], rawArr[0]) - } - -} - -func TestCollect{4}(t *testing.T) { - - v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }} - - collected := v.Collect{4}(func(index int, val {1}) interface{} { - return index - }) - - collectedArr := collected.MustInterSlice() - if assert.Equal(t, 6, len(collectedArr)) { - assert.Equal(t, collectedArr[0], 0) - assert.Equal(t, collectedArr[1], 1) - assert.Equal(t, collectedArr[2], 2) - assert.Equal(t, collectedArr[3], 3) - assert.Equal(t, collectedArr[4], 4) - assert.Equal(t, collectedArr[5], 5) - } - -} diff --git a/codegen/template_test.txt b/codegen/template_test.txt new file mode 100644 index 0000000..7643af3 --- /dev/null +++ b/codegen/template_test.txt @@ -0,0 +1,121 @@ +/* + Tests for {4} ({1} and []{1}) +*/ + +func Test{4}(t *testing.T) { + val := {1}( {2} ) + + m := map[string]interface{}{"value": val, "nothing": nil} + assert.Equal(t, val, New(m).Get("value").{4}()) + assert.Equal(t, val, New(m).Get("value").Must{4}()) + assert.Equal(t, {1}({3}), New(m).Get("nothing").{4}()) + assert.Equal(t, val, New(m).Get("nothing").{4}({2})) + assert.Panics(t, func() { + New(m).Get("age").Must{4}() + }) +} + +func Test{4}Slice(t *testing.T) { + val := {1}( {2} ) + + m := map[string]interface{}{"value": []{1}{ val }, "nothing": nil} + assert.Equal(t, val, New(m).Get("value").{4}Slice()[0]) + assert.Equal(t, val, New(m).Get("value").Must{4}Slice()[0]) + assert.Equal(t, []{1}(nil), New(m).Get("nothing").{4}Slice()) + assert.Equal(t, val, New(m).Get("nothing").{4}Slice( []{1}{ {1}({2}) } )[0]) + assert.Panics(t, func() { + New(m).Get("nothing").Must{4}Slice() + }) +} + +func TestIs{4}(t *testing.T) { + v := &Value{data: {1}({2})} + assert.True(t, v.Is{4}()) + +} + +func TestIs{4}Slice(t *testing.T) { + v := &Value{data: []{1}{ {1}({2}) }} + assert.True(t, v.Is{4}Slice()) +} + +func TestEach{4}(t *testing.T) { + v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }} + count := 0 + replacedVals := make([]{1}, 0) + assert.Equal(t, v, v.Each{4}(func(i int, val {1}) bool { + count++ + replacedVals = append(replacedVals, val) + + // abort early + return i != 2 + })) + + assert.Equal(t, count, 3) + assert.Equal(t, replacedVals[0], v.Must{4}Slice()[0]) + assert.Equal(t, replacedVals[1], v.Must{4}Slice()[1]) + assert.Equal(t, replacedVals[2], v.Must{4}Slice()[2]) +} + +func TestWhere{4}(t *testing.T) { + v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }} + + selected := v.Where{4}(func(i int, val {1}) bool { + return i%2==0 + }).Must{4}Slice() + + assert.Equal(t, 3, len(selected)) +} + +func TestGroup{4}(t *testing.T) { + v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }} + + grouped := v.Group{4}(func(i int, val {1}) string { + return fmt.Sprintf("%v", i%2==0) + }).data.(map[string][]{1}) + + assert.Equal(t, 2, len(grouped)) + assert.Equal(t, 3, len(grouped["true"])) + assert.Equal(t, 3, len(grouped["false"])) +} + +func TestReplace{4}(t *testing.T) { + v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }} + + rawArr := v.Must{4}Slice() + + replaced := v.Replace{4}(func(index int, val {1}) {1} { + if index < len(rawArr)-1 { + return rawArr[index+1] + } + return rawArr[0] + }) + + replacedArr := replaced.Must{4}Slice() + if assert.Equal(t, 6, len(replacedArr)) { + assert.Equal(t, replacedArr[0], rawArr[1]) + assert.Equal(t, replacedArr[1], rawArr[2]) + assert.Equal(t, replacedArr[2], rawArr[3]) + assert.Equal(t, replacedArr[3], rawArr[4]) + assert.Equal(t, replacedArr[4], rawArr[5]) + assert.Equal(t, replacedArr[5], rawArr[0]) + } +} + +func TestCollect{4}(t *testing.T) { + v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }} + + collected := v.Collect{4}(func(index int, val {1}) interface{} { + return index + }) + + collectedArr := collected.MustInterSlice() + if assert.Equal(t, 6, len(collectedArr)) { + assert.Equal(t, collectedArr[0], 0) + assert.Equal(t, collectedArr[1], 1) + assert.Equal(t, collectedArr[2], 2) + assert.Equal(t, collectedArr[3], 3) + assert.Equal(t, collectedArr[4], 4) + assert.Equal(t, collectedArr[5], 5) + } +} diff --git a/conversions.go b/conversions.go index 9cdfa9f..5e020f3 100644 --- a/conversions.go +++ b/conversions.go @@ -12,15 +12,11 @@ import ( // JSON converts the contained object to a JSON string // representation func (m Map) JSON() (string, error) { - result, err := json.Marshal(m) - if err != nil { err = errors.New("objx: JSON encode failed with: " + err.Error()) } - return string(result), err - } // MustJSON converts the contained object to a JSON string @@ -36,7 +32,6 @@ func (m Map) MustJSON() string { // Base64 converts the contained object to a Base64 string // representation of the JSON string representation func (m Map) Base64() (string, error) { - var buf bytes.Buffer jsonData, err := m.JSON() @@ -45,11 +40,13 @@ func (m Map) Base64() (string, error) { } encoder := base64.NewEncoder(base64.StdEncoding, &buf) - encoder.Write([]byte(jsonData)) - encoder.Close() + _, err = encoder.Write([]byte(jsonData)) + if err != nil { + return "", err + } + _ = encoder.Close() return buf.String(), nil - } // MustBase64 converts the contained object to a Base64 string @@ -67,16 +64,13 @@ func (m Map) MustBase64() string { // representation of the JSON string representation and signs it // using the provided key. func (m Map) SignedBase64(key string) (string, error) { - base64, err := m.Base64() if err != nil { return "", err } sig := HashWithKey(base64, key) - return base64 + SignatureSeparator + sig, nil - } // MustSignedBase64 converts the contained object to a Base64 string @@ -98,14 +92,11 @@ func (m Map) MustSignedBase64(key string) string { // URLValues creates a url.Values object from an Obj. This // function requires that the wrapped object be a map[string]interface{} func (m Map) URLValues() url.Values { - vals := make(url.Values) - for k, v := range m { //TODO: can this be done without sprintf? vals.Set(k, fmt.Sprintf("%v", v)) } - return vals } diff --git a/conversions_test.go b/conversions_test.go index e9ccd29..89abaef 100644 --- a/conversions_test.go +++ b/conversions_test.go @@ -1,12 +1,12 @@ package objx import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestConversionJSON(t *testing.T) { - jsonString := `{"name":"Mat"}` o := MustFromJSON(jsonString) @@ -15,13 +15,10 @@ func TestConversionJSON(t *testing.T) { if assert.NoError(t, err) { assert.Equal(t, jsonString, result) } - assert.Equal(t, jsonString, o.MustJSON()) - } func TestConversionJSONWithError(t *testing.T) { - o := MSI() o["test"] = func() {} @@ -32,11 +29,9 @@ func TestConversionJSONWithError(t *testing.T) { _, err := o.JSON() assert.Error(t, err) - } func TestConversionBase64(t *testing.T) { - o := New(map[string]interface{}{"name": "Mat"}) result, err := o.Base64() @@ -44,13 +39,10 @@ func TestConversionBase64(t *testing.T) { if assert.NoError(t, err) { assert.Equal(t, "eyJuYW1lIjoiTWF0In0=", result) } - assert.Equal(t, "eyJuYW1lIjoiTWF0In0=", o.MustBase64()) - } func TestConversionBase64WithError(t *testing.T) { - o := MSI() o["test"] = func() {} @@ -61,11 +53,9 @@ func TestConversionBase64WithError(t *testing.T) { _, err := o.Base64() assert.Error(t, err) - } func TestConversionSignedBase64(t *testing.T) { - o := New(map[string]interface{}{"name": "Mat"}) result, err := o.SignedBase64("key") @@ -73,13 +63,10 @@ func TestConversionSignedBase64(t *testing.T) { if assert.NoError(t, err) { assert.Equal(t, "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6", result) } - assert.Equal(t, "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6", o.MustSignedBase64("key")) - } func TestConversionSignedBase64WithError(t *testing.T) { - o := MSI() o["test"] = func() {} @@ -90,5 +77,4 @@ func TestConversionSignedBase64WithError(t *testing.T) { _, err := o.SignedBase64("key") assert.Error(t, err) - } diff --git a/fixture_test.go b/fixture_test.go index 27f7d90..bcc9e8f 100644 --- a/fixture_test.go +++ b/fixture_test.go @@ -1,8 +1,9 @@ package objx import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) var fixtures = []struct { @@ -79,9 +80,7 @@ var fixtures = []struct { } func TestFixtures(t *testing.T) { - for _, fixture := range fixtures { - m := MustFromJSON(fixture.data) // get the value @@ -92,7 +91,5 @@ func TestFixtures(t *testing.T) { assert.Equal(t, fixture.output, value.data, "Get fixture \"%s\" failed: %v", fixture.name, fixture, ) - } - } diff --git a/map.go b/map.go index 5795a5b..7e9389a 100644 --- a/map.go +++ b/map.go @@ -27,7 +27,7 @@ func (m Map) Value() *Value { } // Nil represents a nil Map. -var Nil Map = New(nil) +var Nil = New(nil) // New creates a new Map containing the map[string]interface{} in the data argument. // If the data argument is not a map[string]interface, New attempts to call the @@ -60,16 +60,13 @@ func New(data interface{}) Map { // // creates an Map equivalent to // m := objx.New(map[string]interface{}{"name": "Mat", "age": 29, "subobj": map[string]interface{}{"active": true}}) func MSI(keyAndValuePairs ...interface{}) Map { - newMap := make(map[string]interface{}) keyAndValuePairsLen := len(keyAndValuePairs) - if keyAndValuePairsLen%2 != 0 { panic("objx: MSI must have an even number of arguments following the 'key, value' pattern.") } for i := 0; i < keyAndValuePairsLen; i = i + 2 { - key := keyAndValuePairs[i] value := keyAndValuePairs[i+1] @@ -78,11 +75,8 @@ func MSI(keyAndValuePairs ...interface{}) Map { if !keyStringOK { panic("objx: MSI must follow 'string, interface{}' pattern. " + keyString + " is not a valid key.") } - newMap[keyString] = value - } - return New(newMap) } @@ -94,11 +88,9 @@ func MSI(keyAndValuePairs ...interface{}) Map { // Panics if the JSON is invalid. func MustFromJSON(jsonString string) Map { o, err := FromJSON(jsonString) - if err != nil { panic("objx: MustFromJSON failed with error: " + err.Error()) } - return o } @@ -107,16 +99,12 @@ func MustFromJSON(jsonString string) Map { // // Returns an error if the JSON is invalid. func FromJSON(jsonString string) (Map, error) { - var data interface{} err := json.Unmarshal([]byte(jsonString), &data) - if err != nil { return Nil, err } - return New(data), nil - } // FromBase64 creates a new Obj containing the data specified @@ -124,14 +112,11 @@ func FromJSON(jsonString string) (Map, error) { // // The string is an encoded JSON string returned by Base64 func FromBase64(base64String string) (Map, error) { - decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(base64String)) - decoded, err := ioutil.ReadAll(decoder) if err != nil { return nil, err } - return FromJSON(string(decoded)) } @@ -140,13 +125,10 @@ func FromBase64(base64String string) (Map, error) { // // The string is an encoded JSON string returned by Base64 func MustFromBase64(base64String string) Map { - result, err := FromBase64(base64String) - if err != nil { panic("objx: MustFromBase64 failed with error: " + err.Error()) } - return result } @@ -157,14 +139,13 @@ func MustFromBase64(base64String string) Map { func FromSignedBase64(base64String, key string) (Map, error) { parts := strings.Split(base64String, SignatureSeparator) if len(parts) != 2 { - return nil, errors.New("objx: Signed base64 string is malformed.") + return nil, errors.New("objx: Signed base64 string is malformed") } sig := HashWithKey(parts[0], key) if parts[1] != sig { - return nil, errors.New("objx: Signature for base64 data does not match.") + return nil, errors.New("objx: Signature for base64 data does not match") } - return FromBase64(parts[0]) } @@ -173,13 +154,10 @@ func FromSignedBase64(base64String, key string) (Map, error) { // // The string is an encoded JSON string returned by Base64 func MustFromSignedBase64(base64String, key string) Map { - result, err := FromSignedBase64(base64String, key) - if err != nil { panic("objx: MustFromSignedBase64 failed with error: " + err.Error()) } - return result } @@ -188,9 +166,7 @@ func MustFromSignedBase64(base64String, key string) Map { // // For queries with multiple values, the first value is selected. func FromURLQuery(query string) (Map, error) { - vals, err := url.ParseQuery(query) - if err != nil { return nil, err } @@ -199,7 +175,6 @@ func FromURLQuery(query string) (Map, error) { for k, vals := range vals { m[k] = vals[0] } - return New(m), nil } @@ -210,13 +185,9 @@ func FromURLQuery(query string) (Map, error) { // // Panics if it encounters an error func MustFromURLQuery(query string) Map { - o, err := FromURLQuery(query) - if err != nil { panic("objx: MustFromURLQuery failed with error: " + err.Error()) } - return o - } diff --git a/map_for_test.go b/map_for_test.go index 6beb506..3b4d406 100644 --- a/map_for_test.go +++ b/map_for_test.go @@ -1,6 +1,6 @@ package objx -var TestMap map[string]interface{} = map[string]interface{}{ +var TestMap = map[string]interface{}{ "name": "Tyler", "address": map[string]interface{}{ "city": "Salt Lake City", diff --git a/map_test.go b/map_test.go index 1f8b45c..214196c 100644 --- a/map_test.go +++ b/map_test.go @@ -1,8 +1,9 @@ package objx import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) type Convertable struct { @@ -18,7 +19,6 @@ type Unconvertable struct { } func TestMapCreation(t *testing.T) { - o := New(nil) assert.Nil(t, o) @@ -46,18 +46,14 @@ func TestMapCreation(t *testing.T) { assert.Equal(t, o["name"], "Tyler") } } - } func TestMapMustFromJSONWithError(t *testing.T) { - _, err := FromJSON(`"name":"Mat"}`) assert.Error(t, err) - } func TestMapFromJSON(t *testing.T) { - o := MustFromJSON(`{"name":"Mat"}`) if assert.NotNil(t, o) { @@ -65,51 +61,38 @@ func TestMapFromJSON(t *testing.T) { assert.Equal(t, "Mat", o["name"]) } } - } func TestMapFromJSONWithError(t *testing.T) { - var m Map assert.Panics(t, func() { m = MustFromJSON(`"name":"Mat"}`) }) - assert.Nil(t, m) - } func TestMapFromBase64String(t *testing.T) { - base64String := "eyJuYW1lIjoiTWF0In0=" - o, err := FromBase64(base64String) if assert.NoError(t, err) { assert.Equal(t, o.Get("name").Str(), "Mat") } - assert.Equal(t, MustFromBase64(base64String).Get("name").Str(), "Mat") - } func TestMapFromBase64StringWithError(t *testing.T) { - base64String := "eyJuYW1lIjoiTWFasd0In0=" - _, err := FromBase64(base64String) assert.Error(t, err) - assert.Panics(t, func() { MustFromBase64(base64String) }) - } func TestMapFromSignedBase64String(t *testing.T) { - base64String := "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6" o, err := FromSignedBase64(base64String, "key") @@ -117,31 +100,24 @@ func TestMapFromSignedBase64String(t *testing.T) { if assert.NoError(t, err) { assert.Equal(t, o.Get("name").Str(), "Mat") } - assert.Equal(t, MustFromSignedBase64(base64String, "key").Get("name").Str(), "Mat") - } func TestMapFromSignedBase64StringWithError(t *testing.T) { - base64String := "eyJuYW1lasdIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6" - _, err := FromSignedBase64(base64String, "key") assert.Error(t, err) - assert.Panics(t, func() { MustFromSignedBase64(base64String, "key") }) - } func TestMapFromURLQuery(t *testing.T) { - m, err := FromURLQuery("name=tyler&state=UT") + if assert.NoError(t, err) && assert.NotNil(t, m) { assert.Equal(t, "tyler", m.Get("name").Str()) assert.Equal(t, "UT", m.Get("state").Str()) } - } diff --git a/mutations.go b/mutations.go index b35c863..e7b8eb7 100644 --- a/mutations.go +++ b/mutations.go @@ -2,11 +2,10 @@ package objx // Exclude returns a new Map with the keys in the specified []string // excluded. -func (d Map) Exclude(exclude []string) Map { - +func (m Map) Exclude(exclude []string) Map { excluded := make(Map) - for k, v := range d { - var shouldInclude bool = true + for k, v := range m { + var shouldInclude = true for _, toExclude := range exclude { if k == toExclude { shouldInclude = false @@ -17,7 +16,6 @@ func (d Map) Exclude(exclude []string) Map { excluded[k] = v } } - return excluded } @@ -38,19 +36,16 @@ func (m Map) Merge(merge Map) Map { return m.Copy().MergeHere(merge) } -// Merge blends the specified map with this map and returns the current map. +// MergeHere blends the specified map with this map and returns the current map. // -// Keys that appear in both will be selected from the specified map. The original map +// Keys that appear in both will be selected from the specified map. The original map // will be modified. This method requires that // the wrapped object be a map[string]interface{} func (m Map) MergeHere(merge Map) Map { - for k, v := range merge { m[k] = v } - return m - } // Transform builds a new Obj giving the transformer a chance @@ -71,11 +66,9 @@ func (m Map) Transform(transformer func(key string, value interface{}) (string, // This method requires that the wrapped object be a map[string]interface{} func (m Map) TransformKeys(mapping map[string]string) Map { return m.Transform(func(key string, value interface{}) (string, interface{}) { - if newKey, ok := mapping[key]; ok { return newKey, value } - return key, value }) } diff --git a/mutations_test.go b/mutations_test.go index e20ee23..5587944 100644 --- a/mutations_test.go +++ b/mutations_test.go @@ -1,12 +1,12 @@ package objx import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestExclude(t *testing.T) { - d := make(Map) d["name"] = "Mat" d["age"] = 29 @@ -17,11 +17,9 @@ func TestExclude(t *testing.T) { assert.Equal(t, d["name"], excluded["name"]) assert.Equal(t, d["age"], excluded["age"]) assert.False(t, excluded.Has("secret"), "secret should be excluded") - } func TestCopy(t *testing.T) { - d1 := make(map[string]interface{}) d1["name"] = "Tyler" d1["location"] = "UT" @@ -37,7 +35,6 @@ func TestCopy(t *testing.T) { } func TestMerge(t *testing.T) { - d := make(map[string]interface{}) d["name"] = "Mat" @@ -53,11 +50,9 @@ func TestMerge(t *testing.T) { assert.Equal(t, merged.Get("name").Str(), d1Obj.Get("name").Str()) assert.Equal(t, merged.Get("location").Str(), d1Obj.Get("location").Str()) assert.Empty(t, dObj.Get("location").Str()) - } func TestMergeHere(t *testing.T) { - d := make(map[string]interface{}) d["name"] = "Mat" diff --git a/security.go b/security.go index fdd6be9..e052ff8 100644 --- a/security.go +++ b/security.go @@ -9,6 +9,9 @@ import ( // key. func HashWithKey(data, key string) string { hash := sha1.New() - hash.Write([]byte(data + ":" + key)) + _, err := hash.Write([]byte(data + ":" + key)) + if err != nil { + return "" + } return hex.EncodeToString(hash.Sum(nil)) } diff --git a/security_test.go b/security_test.go index 8f0898f..aef365e 100644 --- a/security_test.go +++ b/security_test.go @@ -1,12 +1,11 @@ package objx import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestHashWithKey(t *testing.T) { - assert.Equal(t, "0ce84d8d01f2c7b6e0882b784429c54d280ea2d9", HashWithKey("abc", "def")) - } diff --git a/simple_example_test.go b/simple_example_test.go index 5408c7f..ecba958 100644 --- a/simple_example_test.go +++ b/simple_example_test.go @@ -1,12 +1,12 @@ package objx import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestSimpleExample(t *testing.T) { - // build a map from a JSON object o := MustFromJSON(`{"name":"Mat","foods":["indian","chinese"], "location":{"county":"hobbiton","city":"the shire"}}`) @@ -37,5 +37,4 @@ func TestSimpleExample(t *testing.T) { // Get a value by using dot notation assert.Equal(t, "hobbiton", o.Get("location.county").Str()) - } diff --git a/tests_test.go b/tests_test.go index bcc1eb0..d0e62a5 100644 --- a/tests_test.go +++ b/tests_test.go @@ -1,12 +1,12 @@ package objx import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestHas(t *testing.T) { - m := New(TestMap) assert.True(t, m.Has("name")) @@ -19,6 +19,6 @@ func TestHas(t *testing.T) { assert.False(t, m.Has("numbers[5]")) m = nil - assert.False(t, m.Has("nothing")) + assert.False(t, m.Has("nothing")) } diff --git a/type_specific_codegen.go b/type_specific_codegen.go index f3ecb29..202a91f 100644 --- a/type_specific_codegen.go +++ b/type_specific_codegen.go @@ -2,7 +2,6 @@ package objx /* Inter (interface{} and []interface{}) - -------------------------------------------------- */ // Inter gets the value as a interface{}, returns the optionalDefault @@ -60,44 +59,35 @@ func (v *Value) IsInterSlice() bool { // // Panics if the object is the wrong type. func (v *Value) EachInter(callback func(int, interface{}) bool) *Value { - for index, val := range v.MustInterSlice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereInter uses the specified decider function to select items // from the []interface{}. The object contained in the result will contain // only the selected items. func (v *Value) WhereInter(decider func(int, interface{}) bool) *Value { - var selected []interface{} - v.EachInter(func(index int, val interface{}) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupInter uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]interface{}. func (v *Value) GroupInter(grouper func(int, interface{}) string) *Value { - groups := make(map[string][]interface{}) - v.EachInter(func(index int, val interface{}) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -106,47 +96,37 @@ func (v *Value) GroupInter(grouper func(int, interface{}) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceInter uses the specified function to replace each interface{}s // by iterating each item. The data in the returned result will be a // []interface{} containing the replaced items. func (v *Value) ReplaceInter(replacer func(int, interface{}) interface{}) *Value { - arr := v.MustInterSlice() replaced := make([]interface{}, len(arr)) - v.EachInter(func(index int, val interface{}) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectInter uses the specified collector function to collect a value // for each of the interface{}s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectInter(collector func(int, interface{}) interface{}) *Value { - arr := v.MustInterSlice() collected := make([]interface{}, len(arr)) - v.EachInter(func(index int, val interface{}) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* MSI (map[string]interface{} and []map[string]interface{}) - -------------------------------------------------- */ // MSI gets the value as a map[string]interface{}, returns the optionalDefault @@ -204,44 +184,35 @@ func (v *Value) IsMSISlice() bool { // // Panics if the object is the wrong type. func (v *Value) EachMSI(callback func(int, map[string]interface{}) bool) *Value { - for index, val := range v.MustMSISlice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereMSI uses the specified decider function to select items // from the []map[string]interface{}. The object contained in the result will contain // only the selected items. func (v *Value) WhereMSI(decider func(int, map[string]interface{}) bool) *Value { - var selected []map[string]interface{} - v.EachMSI(func(index int, val map[string]interface{}) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupMSI uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]map[string]interface{}. func (v *Value) GroupMSI(grouper func(int, map[string]interface{}) string) *Value { - groups := make(map[string][]map[string]interface{}) - v.EachMSI(func(index int, val map[string]interface{}) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -250,47 +221,37 @@ func (v *Value) GroupMSI(grouper func(int, map[string]interface{}) string) *Valu groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceMSI uses the specified function to replace each map[string]interface{}s // by iterating each item. The data in the returned result will be a // []map[string]interface{} containing the replaced items. func (v *Value) ReplaceMSI(replacer func(int, map[string]interface{}) map[string]interface{}) *Value { - arr := v.MustMSISlice() replaced := make([]map[string]interface{}, len(arr)) - v.EachMSI(func(index int, val map[string]interface{}) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectMSI uses the specified collector function to collect a value // for each of the map[string]interface{}s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectMSI(collector func(int, map[string]interface{}) interface{}) *Value { - arr := v.MustMSISlice() collected := make([]interface{}, len(arr)) - v.EachMSI(func(index int, val map[string]interface{}) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* ObjxMap ((Map) and [](Map)) - -------------------------------------------------- */ // ObjxMap gets the value as a (Map), returns the optionalDefault @@ -348,44 +309,35 @@ func (v *Value) IsObjxMapSlice() bool { // // Panics if the object is the wrong type. func (v *Value) EachObjxMap(callback func(int, Map) bool) *Value { - for index, val := range v.MustObjxMapSlice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereObjxMap uses the specified decider function to select items // from the [](Map). The object contained in the result will contain // only the selected items. func (v *Value) WhereObjxMap(decider func(int, Map) bool) *Value { - var selected [](Map) - v.EachObjxMap(func(index int, val Map) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupObjxMap uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][](Map). func (v *Value) GroupObjxMap(grouper func(int, Map) string) *Value { - groups := make(map[string][](Map)) - v.EachObjxMap(func(index int, val Map) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -394,47 +346,37 @@ func (v *Value) GroupObjxMap(grouper func(int, Map) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceObjxMap uses the specified function to replace each (Map)s // by iterating each item. The data in the returned result will be a // [](Map) containing the replaced items. func (v *Value) ReplaceObjxMap(replacer func(int, Map) Map) *Value { - arr := v.MustObjxMapSlice() replaced := make([](Map), len(arr)) - v.EachObjxMap(func(index int, val Map) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectObjxMap uses the specified collector function to collect a value // for each of the (Map)s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectObjxMap(collector func(int, Map) interface{}) *Value { - arr := v.MustObjxMapSlice() collected := make([]interface{}, len(arr)) - v.EachObjxMap(func(index int, val Map) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Bool (bool and []bool) - -------------------------------------------------- */ // Bool gets the value as a bool, returns the optionalDefault @@ -492,44 +434,35 @@ func (v *Value) IsBoolSlice() bool { // // Panics if the object is the wrong type. func (v *Value) EachBool(callback func(int, bool) bool) *Value { - for index, val := range v.MustBoolSlice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereBool uses the specified decider function to select items // from the []bool. The object contained in the result will contain // only the selected items. func (v *Value) WhereBool(decider func(int, bool) bool) *Value { - var selected []bool - v.EachBool(func(index int, val bool) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupBool uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]bool. func (v *Value) GroupBool(grouper func(int, bool) string) *Value { - groups := make(map[string][]bool) - v.EachBool(func(index int, val bool) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -538,47 +471,37 @@ func (v *Value) GroupBool(grouper func(int, bool) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceBool uses the specified function to replace each bools // by iterating each item. The data in the returned result will be a // []bool containing the replaced items. func (v *Value) ReplaceBool(replacer func(int, bool) bool) *Value { - arr := v.MustBoolSlice() replaced := make([]bool, len(arr)) - v.EachBool(func(index int, val bool) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectBool uses the specified collector function to collect a value // for each of the bools in the slice. The data returned will be a // []interface{}. func (v *Value) CollectBool(collector func(int, bool) interface{}) *Value { - arr := v.MustBoolSlice() collected := make([]interface{}, len(arr)) - v.EachBool(func(index int, val bool) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Str (string and []string) - -------------------------------------------------- */ // Str gets the value as a string, returns the optionalDefault @@ -636,44 +559,35 @@ func (v *Value) IsStrSlice() bool { // // Panics if the object is the wrong type. func (v *Value) EachStr(callback func(int, string) bool) *Value { - for index, val := range v.MustStrSlice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereStr uses the specified decider function to select items // from the []string. The object contained in the result will contain // only the selected items. func (v *Value) WhereStr(decider func(int, string) bool) *Value { - var selected []string - v.EachStr(func(index int, val string) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupStr uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]string. func (v *Value) GroupStr(grouper func(int, string) string) *Value { - groups := make(map[string][]string) - v.EachStr(func(index int, val string) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -682,47 +596,37 @@ func (v *Value) GroupStr(grouper func(int, string) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceStr uses the specified function to replace each strings // by iterating each item. The data in the returned result will be a // []string containing the replaced items. func (v *Value) ReplaceStr(replacer func(int, string) string) *Value { - arr := v.MustStrSlice() replaced := make([]string, len(arr)) - v.EachStr(func(index int, val string) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectStr uses the specified collector function to collect a value // for each of the strings in the slice. The data returned will be a // []interface{}. func (v *Value) CollectStr(collector func(int, string) interface{}) *Value { - arr := v.MustStrSlice() collected := make([]interface{}, len(arr)) - v.EachStr(func(index int, val string) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Int (int and []int) - -------------------------------------------------- */ // Int gets the value as a int, returns the optionalDefault @@ -780,44 +684,35 @@ func (v *Value) IsIntSlice() bool { // // Panics if the object is the wrong type. func (v *Value) EachInt(callback func(int, int) bool) *Value { - for index, val := range v.MustIntSlice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereInt uses the specified decider function to select items // from the []int. The object contained in the result will contain // only the selected items. func (v *Value) WhereInt(decider func(int, int) bool) *Value { - var selected []int - v.EachInt(func(index int, val int) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupInt uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]int. func (v *Value) GroupInt(grouper func(int, int) string) *Value { - groups := make(map[string][]int) - v.EachInt(func(index int, val int) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -826,47 +721,37 @@ func (v *Value) GroupInt(grouper func(int, int) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceInt uses the specified function to replace each ints // by iterating each item. The data in the returned result will be a // []int containing the replaced items. func (v *Value) ReplaceInt(replacer func(int, int) int) *Value { - arr := v.MustIntSlice() replaced := make([]int, len(arr)) - v.EachInt(func(index int, val int) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectInt uses the specified collector function to collect a value // for each of the ints in the slice. The data returned will be a // []interface{}. func (v *Value) CollectInt(collector func(int, int) interface{}) *Value { - arr := v.MustIntSlice() collected := make([]interface{}, len(arr)) - v.EachInt(func(index int, val int) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Int8 (int8 and []int8) - -------------------------------------------------- */ // Int8 gets the value as a int8, returns the optionalDefault @@ -924,44 +809,35 @@ func (v *Value) IsInt8Slice() bool { // // Panics if the object is the wrong type. func (v *Value) EachInt8(callback func(int, int8) bool) *Value { - for index, val := range v.MustInt8Slice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereInt8 uses the specified decider function to select items // from the []int8. The object contained in the result will contain // only the selected items. func (v *Value) WhereInt8(decider func(int, int8) bool) *Value { - var selected []int8 - v.EachInt8(func(index int, val int8) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupInt8 uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]int8. func (v *Value) GroupInt8(grouper func(int, int8) string) *Value { - groups := make(map[string][]int8) - v.EachInt8(func(index int, val int8) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -970,47 +846,37 @@ func (v *Value) GroupInt8(grouper func(int, int8) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceInt8 uses the specified function to replace each int8s // by iterating each item. The data in the returned result will be a // []int8 containing the replaced items. func (v *Value) ReplaceInt8(replacer func(int, int8) int8) *Value { - arr := v.MustInt8Slice() replaced := make([]int8, len(arr)) - v.EachInt8(func(index int, val int8) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectInt8 uses the specified collector function to collect a value // for each of the int8s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectInt8(collector func(int, int8) interface{}) *Value { - arr := v.MustInt8Slice() collected := make([]interface{}, len(arr)) - v.EachInt8(func(index int, val int8) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Int16 (int16 and []int16) - -------------------------------------------------- */ // Int16 gets the value as a int16, returns the optionalDefault @@ -1068,44 +934,35 @@ func (v *Value) IsInt16Slice() bool { // // Panics if the object is the wrong type. func (v *Value) EachInt16(callback func(int, int16) bool) *Value { - for index, val := range v.MustInt16Slice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereInt16 uses the specified decider function to select items // from the []int16. The object contained in the result will contain // only the selected items. func (v *Value) WhereInt16(decider func(int, int16) bool) *Value { - var selected []int16 - v.EachInt16(func(index int, val int16) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupInt16 uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]int16. func (v *Value) GroupInt16(grouper func(int, int16) string) *Value { - groups := make(map[string][]int16) - v.EachInt16(func(index int, val int16) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -1114,47 +971,37 @@ func (v *Value) GroupInt16(grouper func(int, int16) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceInt16 uses the specified function to replace each int16s // by iterating each item. The data in the returned result will be a // []int16 containing the replaced items. func (v *Value) ReplaceInt16(replacer func(int, int16) int16) *Value { - arr := v.MustInt16Slice() replaced := make([]int16, len(arr)) - v.EachInt16(func(index int, val int16) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectInt16 uses the specified collector function to collect a value // for each of the int16s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectInt16(collector func(int, int16) interface{}) *Value { - arr := v.MustInt16Slice() collected := make([]interface{}, len(arr)) - v.EachInt16(func(index int, val int16) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Int32 (int32 and []int32) - -------------------------------------------------- */ // Int32 gets the value as a int32, returns the optionalDefault @@ -1212,44 +1059,35 @@ func (v *Value) IsInt32Slice() bool { // // Panics if the object is the wrong type. func (v *Value) EachInt32(callback func(int, int32) bool) *Value { - for index, val := range v.MustInt32Slice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereInt32 uses the specified decider function to select items // from the []int32. The object contained in the result will contain // only the selected items. func (v *Value) WhereInt32(decider func(int, int32) bool) *Value { - var selected []int32 - v.EachInt32(func(index int, val int32) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupInt32 uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]int32. func (v *Value) GroupInt32(grouper func(int, int32) string) *Value { - groups := make(map[string][]int32) - v.EachInt32(func(index int, val int32) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -1258,47 +1096,37 @@ func (v *Value) GroupInt32(grouper func(int, int32) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceInt32 uses the specified function to replace each int32s // by iterating each item. The data in the returned result will be a // []int32 containing the replaced items. func (v *Value) ReplaceInt32(replacer func(int, int32) int32) *Value { - arr := v.MustInt32Slice() replaced := make([]int32, len(arr)) - v.EachInt32(func(index int, val int32) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectInt32 uses the specified collector function to collect a value // for each of the int32s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectInt32(collector func(int, int32) interface{}) *Value { - arr := v.MustInt32Slice() collected := make([]interface{}, len(arr)) - v.EachInt32(func(index int, val int32) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Int64 (int64 and []int64) - -------------------------------------------------- */ // Int64 gets the value as a int64, returns the optionalDefault @@ -1356,44 +1184,35 @@ func (v *Value) IsInt64Slice() bool { // // Panics if the object is the wrong type. func (v *Value) EachInt64(callback func(int, int64) bool) *Value { - for index, val := range v.MustInt64Slice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereInt64 uses the specified decider function to select items // from the []int64. The object contained in the result will contain // only the selected items. func (v *Value) WhereInt64(decider func(int, int64) bool) *Value { - var selected []int64 - v.EachInt64(func(index int, val int64) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupInt64 uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]int64. func (v *Value) GroupInt64(grouper func(int, int64) string) *Value { - groups := make(map[string][]int64) - v.EachInt64(func(index int, val int64) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -1402,47 +1221,37 @@ func (v *Value) GroupInt64(grouper func(int, int64) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceInt64 uses the specified function to replace each int64s // by iterating each item. The data in the returned result will be a // []int64 containing the replaced items. func (v *Value) ReplaceInt64(replacer func(int, int64) int64) *Value { - arr := v.MustInt64Slice() replaced := make([]int64, len(arr)) - v.EachInt64(func(index int, val int64) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectInt64 uses the specified collector function to collect a value // for each of the int64s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectInt64(collector func(int, int64) interface{}) *Value { - arr := v.MustInt64Slice() collected := make([]interface{}, len(arr)) - v.EachInt64(func(index int, val int64) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Uint (uint and []uint) - -------------------------------------------------- */ // Uint gets the value as a uint, returns the optionalDefault @@ -1500,44 +1309,35 @@ func (v *Value) IsUintSlice() bool { // // Panics if the object is the wrong type. func (v *Value) EachUint(callback func(int, uint) bool) *Value { - for index, val := range v.MustUintSlice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereUint uses the specified decider function to select items // from the []uint. The object contained in the result will contain // only the selected items. func (v *Value) WhereUint(decider func(int, uint) bool) *Value { - var selected []uint - v.EachUint(func(index int, val uint) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupUint uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]uint. func (v *Value) GroupUint(grouper func(int, uint) string) *Value { - groups := make(map[string][]uint) - v.EachUint(func(index int, val uint) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -1546,47 +1346,37 @@ func (v *Value) GroupUint(grouper func(int, uint) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceUint uses the specified function to replace each uints // by iterating each item. The data in the returned result will be a // []uint containing the replaced items. func (v *Value) ReplaceUint(replacer func(int, uint) uint) *Value { - arr := v.MustUintSlice() replaced := make([]uint, len(arr)) - v.EachUint(func(index int, val uint) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectUint uses the specified collector function to collect a value // for each of the uints in the slice. The data returned will be a // []interface{}. func (v *Value) CollectUint(collector func(int, uint) interface{}) *Value { - arr := v.MustUintSlice() collected := make([]interface{}, len(arr)) - v.EachUint(func(index int, val uint) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Uint8 (uint8 and []uint8) - -------------------------------------------------- */ // Uint8 gets the value as a uint8, returns the optionalDefault @@ -1644,44 +1434,35 @@ func (v *Value) IsUint8Slice() bool { // // Panics if the object is the wrong type. func (v *Value) EachUint8(callback func(int, uint8) bool) *Value { - for index, val := range v.MustUint8Slice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereUint8 uses the specified decider function to select items // from the []uint8. The object contained in the result will contain // only the selected items. func (v *Value) WhereUint8(decider func(int, uint8) bool) *Value { - var selected []uint8 - v.EachUint8(func(index int, val uint8) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupUint8 uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]uint8. func (v *Value) GroupUint8(grouper func(int, uint8) string) *Value { - groups := make(map[string][]uint8) - v.EachUint8(func(index int, val uint8) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -1690,47 +1471,37 @@ func (v *Value) GroupUint8(grouper func(int, uint8) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceUint8 uses the specified function to replace each uint8s // by iterating each item. The data in the returned result will be a // []uint8 containing the replaced items. func (v *Value) ReplaceUint8(replacer func(int, uint8) uint8) *Value { - arr := v.MustUint8Slice() replaced := make([]uint8, len(arr)) - v.EachUint8(func(index int, val uint8) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectUint8 uses the specified collector function to collect a value // for each of the uint8s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectUint8(collector func(int, uint8) interface{}) *Value { - arr := v.MustUint8Slice() collected := make([]interface{}, len(arr)) - v.EachUint8(func(index int, val uint8) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Uint16 (uint16 and []uint16) - -------------------------------------------------- */ // Uint16 gets the value as a uint16, returns the optionalDefault @@ -1788,44 +1559,35 @@ func (v *Value) IsUint16Slice() bool { // // Panics if the object is the wrong type. func (v *Value) EachUint16(callback func(int, uint16) bool) *Value { - for index, val := range v.MustUint16Slice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereUint16 uses the specified decider function to select items // from the []uint16. The object contained in the result will contain // only the selected items. func (v *Value) WhereUint16(decider func(int, uint16) bool) *Value { - var selected []uint16 - v.EachUint16(func(index int, val uint16) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupUint16 uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]uint16. func (v *Value) GroupUint16(grouper func(int, uint16) string) *Value { - groups := make(map[string][]uint16) - v.EachUint16(func(index int, val uint16) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -1834,47 +1596,37 @@ func (v *Value) GroupUint16(grouper func(int, uint16) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceUint16 uses the specified function to replace each uint16s // by iterating each item. The data in the returned result will be a // []uint16 containing the replaced items. func (v *Value) ReplaceUint16(replacer func(int, uint16) uint16) *Value { - arr := v.MustUint16Slice() replaced := make([]uint16, len(arr)) - v.EachUint16(func(index int, val uint16) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectUint16 uses the specified collector function to collect a value // for each of the uint16s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectUint16(collector func(int, uint16) interface{}) *Value { - arr := v.MustUint16Slice() collected := make([]interface{}, len(arr)) - v.EachUint16(func(index int, val uint16) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Uint32 (uint32 and []uint32) - -------------------------------------------------- */ // Uint32 gets the value as a uint32, returns the optionalDefault @@ -1932,44 +1684,35 @@ func (v *Value) IsUint32Slice() bool { // // Panics if the object is the wrong type. func (v *Value) EachUint32(callback func(int, uint32) bool) *Value { - for index, val := range v.MustUint32Slice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereUint32 uses the specified decider function to select items // from the []uint32. The object contained in the result will contain // only the selected items. func (v *Value) WhereUint32(decider func(int, uint32) bool) *Value { - var selected []uint32 - v.EachUint32(func(index int, val uint32) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupUint32 uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]uint32. func (v *Value) GroupUint32(grouper func(int, uint32) string) *Value { - groups := make(map[string][]uint32) - v.EachUint32(func(index int, val uint32) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -1978,47 +1721,37 @@ func (v *Value) GroupUint32(grouper func(int, uint32) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceUint32 uses the specified function to replace each uint32s // by iterating each item. The data in the returned result will be a // []uint32 containing the replaced items. func (v *Value) ReplaceUint32(replacer func(int, uint32) uint32) *Value { - arr := v.MustUint32Slice() replaced := make([]uint32, len(arr)) - v.EachUint32(func(index int, val uint32) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectUint32 uses the specified collector function to collect a value // for each of the uint32s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectUint32(collector func(int, uint32) interface{}) *Value { - arr := v.MustUint32Slice() collected := make([]interface{}, len(arr)) - v.EachUint32(func(index int, val uint32) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Uint64 (uint64 and []uint64) - -------------------------------------------------- */ // Uint64 gets the value as a uint64, returns the optionalDefault @@ -2076,44 +1809,35 @@ func (v *Value) IsUint64Slice() bool { // // Panics if the object is the wrong type. func (v *Value) EachUint64(callback func(int, uint64) bool) *Value { - for index, val := range v.MustUint64Slice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereUint64 uses the specified decider function to select items // from the []uint64. The object contained in the result will contain // only the selected items. func (v *Value) WhereUint64(decider func(int, uint64) bool) *Value { - var selected []uint64 - v.EachUint64(func(index int, val uint64) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupUint64 uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]uint64. func (v *Value) GroupUint64(grouper func(int, uint64) string) *Value { - groups := make(map[string][]uint64) - v.EachUint64(func(index int, val uint64) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -2122,47 +1846,37 @@ func (v *Value) GroupUint64(grouper func(int, uint64) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceUint64 uses the specified function to replace each uint64s // by iterating each item. The data in the returned result will be a // []uint64 containing the replaced items. func (v *Value) ReplaceUint64(replacer func(int, uint64) uint64) *Value { - arr := v.MustUint64Slice() replaced := make([]uint64, len(arr)) - v.EachUint64(func(index int, val uint64) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectUint64 uses the specified collector function to collect a value // for each of the uint64s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectUint64(collector func(int, uint64) interface{}) *Value { - arr := v.MustUint64Slice() collected := make([]interface{}, len(arr)) - v.EachUint64(func(index int, val uint64) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Uintptr (uintptr and []uintptr) - -------------------------------------------------- */ // Uintptr gets the value as a uintptr, returns the optionalDefault @@ -2220,44 +1934,35 @@ func (v *Value) IsUintptrSlice() bool { // // Panics if the object is the wrong type. func (v *Value) EachUintptr(callback func(int, uintptr) bool) *Value { - for index, val := range v.MustUintptrSlice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereUintptr uses the specified decider function to select items // from the []uintptr. The object contained in the result will contain // only the selected items. func (v *Value) WhereUintptr(decider func(int, uintptr) bool) *Value { - var selected []uintptr - v.EachUintptr(func(index int, val uintptr) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupUintptr uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]uintptr. func (v *Value) GroupUintptr(grouper func(int, uintptr) string) *Value { - groups := make(map[string][]uintptr) - v.EachUintptr(func(index int, val uintptr) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -2266,47 +1971,37 @@ func (v *Value) GroupUintptr(grouper func(int, uintptr) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceUintptr uses the specified function to replace each uintptrs // by iterating each item. The data in the returned result will be a // []uintptr containing the replaced items. func (v *Value) ReplaceUintptr(replacer func(int, uintptr) uintptr) *Value { - arr := v.MustUintptrSlice() replaced := make([]uintptr, len(arr)) - v.EachUintptr(func(index int, val uintptr) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectUintptr uses the specified collector function to collect a value // for each of the uintptrs in the slice. The data returned will be a // []interface{}. func (v *Value) CollectUintptr(collector func(int, uintptr) interface{}) *Value { - arr := v.MustUintptrSlice() collected := make([]interface{}, len(arr)) - v.EachUintptr(func(index int, val uintptr) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Float32 (float32 and []float32) - -------------------------------------------------- */ // Float32 gets the value as a float32, returns the optionalDefault @@ -2364,44 +2059,35 @@ func (v *Value) IsFloat32Slice() bool { // // Panics if the object is the wrong type. func (v *Value) EachFloat32(callback func(int, float32) bool) *Value { - for index, val := range v.MustFloat32Slice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereFloat32 uses the specified decider function to select items // from the []float32. The object contained in the result will contain // only the selected items. func (v *Value) WhereFloat32(decider func(int, float32) bool) *Value { - var selected []float32 - v.EachFloat32(func(index int, val float32) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupFloat32 uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]float32. func (v *Value) GroupFloat32(grouper func(int, float32) string) *Value { - groups := make(map[string][]float32) - v.EachFloat32(func(index int, val float32) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -2410,47 +2096,37 @@ func (v *Value) GroupFloat32(grouper func(int, float32) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceFloat32 uses the specified function to replace each float32s // by iterating each item. The data in the returned result will be a // []float32 containing the replaced items. func (v *Value) ReplaceFloat32(replacer func(int, float32) float32) *Value { - arr := v.MustFloat32Slice() replaced := make([]float32, len(arr)) - v.EachFloat32(func(index int, val float32) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectFloat32 uses the specified collector function to collect a value // for each of the float32s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectFloat32(collector func(int, float32) interface{}) *Value { - arr := v.MustFloat32Slice() collected := make([]interface{}, len(arr)) - v.EachFloat32(func(index int, val float32) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Float64 (float64 and []float64) - -------------------------------------------------- */ // Float64 gets the value as a float64, returns the optionalDefault @@ -2508,44 +2184,35 @@ func (v *Value) IsFloat64Slice() bool { // // Panics if the object is the wrong type. func (v *Value) EachFloat64(callback func(int, float64) bool) *Value { - for index, val := range v.MustFloat64Slice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereFloat64 uses the specified decider function to select items // from the []float64. The object contained in the result will contain // only the selected items. func (v *Value) WhereFloat64(decider func(int, float64) bool) *Value { - var selected []float64 - v.EachFloat64(func(index int, val float64) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupFloat64 uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]float64. func (v *Value) GroupFloat64(grouper func(int, float64) string) *Value { - groups := make(map[string][]float64) - v.EachFloat64(func(index int, val float64) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -2554,47 +2221,37 @@ func (v *Value) GroupFloat64(grouper func(int, float64) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceFloat64 uses the specified function to replace each float64s // by iterating each item. The data in the returned result will be a // []float64 containing the replaced items. func (v *Value) ReplaceFloat64(replacer func(int, float64) float64) *Value { - arr := v.MustFloat64Slice() replaced := make([]float64, len(arr)) - v.EachFloat64(func(index int, val float64) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectFloat64 uses the specified collector function to collect a value // for each of the float64s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectFloat64(collector func(int, float64) interface{}) *Value { - arr := v.MustFloat64Slice() collected := make([]interface{}, len(arr)) - v.EachFloat64(func(index int, val float64) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Complex64 (complex64 and []complex64) - -------------------------------------------------- */ // Complex64 gets the value as a complex64, returns the optionalDefault @@ -2652,44 +2309,35 @@ func (v *Value) IsComplex64Slice() bool { // // Panics if the object is the wrong type. func (v *Value) EachComplex64(callback func(int, complex64) bool) *Value { - for index, val := range v.MustComplex64Slice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereComplex64 uses the specified decider function to select items // from the []complex64. The object contained in the result will contain // only the selected items. func (v *Value) WhereComplex64(decider func(int, complex64) bool) *Value { - var selected []complex64 - v.EachComplex64(func(index int, val complex64) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupComplex64 uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]complex64. func (v *Value) GroupComplex64(grouper func(int, complex64) string) *Value { - groups := make(map[string][]complex64) - v.EachComplex64(func(index int, val complex64) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -2698,47 +2346,37 @@ func (v *Value) GroupComplex64(grouper func(int, complex64) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceComplex64 uses the specified function to replace each complex64s // by iterating each item. The data in the returned result will be a // []complex64 containing the replaced items. func (v *Value) ReplaceComplex64(replacer func(int, complex64) complex64) *Value { - arr := v.MustComplex64Slice() replaced := make([]complex64, len(arr)) - v.EachComplex64(func(index int, val complex64) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectComplex64 uses the specified collector function to collect a value // for each of the complex64s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectComplex64(collector func(int, complex64) interface{}) *Value { - arr := v.MustComplex64Slice() collected := make([]interface{}, len(arr)) - v.EachComplex64(func(index int, val complex64) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } /* Complex128 (complex128 and []complex128) - -------------------------------------------------- */ // Complex128 gets the value as a complex128, returns the optionalDefault @@ -2796,44 +2434,35 @@ func (v *Value) IsComplex128Slice() bool { // // Panics if the object is the wrong type. func (v *Value) EachComplex128(callback func(int, complex128) bool) *Value { - for index, val := range v.MustComplex128Slice() { carryon := callback(index, val) - if carryon == false { + if !carryon { break } } - return v - } // WhereComplex128 uses the specified decider function to select items // from the []complex128. The object contained in the result will contain // only the selected items. func (v *Value) WhereComplex128(decider func(int, complex128) bool) *Value { - var selected []complex128 - v.EachComplex128(func(index int, val complex128) bool { shouldSelect := decider(index, val) - if shouldSelect == false { + if !shouldSelect { selected = append(selected, val) } return true }) - return &Value{data: selected} - } // GroupComplex128 uses the specified grouper function to group the items // keyed by the return of the grouper. The object contained in the // result will contain a map[string][]complex128. func (v *Value) GroupComplex128(grouper func(int, complex128) string) *Value { - groups := make(map[string][]complex128) - v.EachComplex128(func(index int, val complex128) bool { group := grouper(index, val) if _, ok := groups[group]; !ok { @@ -2842,40 +2471,31 @@ func (v *Value) GroupComplex128(grouper func(int, complex128) string) *Value { groups[group] = append(groups[group], val) return true }) - return &Value{data: groups} - } // ReplaceComplex128 uses the specified function to replace each complex128s // by iterating each item. The data in the returned result will be a // []complex128 containing the replaced items. func (v *Value) ReplaceComplex128(replacer func(int, complex128) complex128) *Value { - arr := v.MustComplex128Slice() replaced := make([]complex128, len(arr)) - v.EachComplex128(func(index int, val complex128) bool { replaced[index] = replacer(index, val) return true }) - return &Value{data: replaced} - } // CollectComplex128 uses the specified collector function to collect a value // for each of the complex128s in the slice. The data returned will be a // []interface{}. func (v *Value) CollectComplex128(collector func(int, complex128) interface{}) *Value { - arr := v.MustComplex128Slice() collected := make([]interface{}, len(arr)) - v.EachComplex128(func(index int, val complex128) bool { collected[index] = collector(index, val) return true }) - return &Value{data: collected} } diff --git a/type_specific_codegen_test.go b/type_specific_codegen_test.go index f7a4fce..c79c7e0 100644 --- a/type_specific_codegen_test.go +++ b/type_specific_codegen_test.go @@ -2,84 +2,71 @@ package objx import ( "fmt" - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Inter (interface{} and []interface{}) +*/ func TestInter(t *testing.T) { - val := interface{}("something") + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Inter()) assert.Equal(t, val, New(m).Get("value").MustInter()) assert.Equal(t, interface{}(nil), New(m).Get("nothing").Inter()) assert.Equal(t, val, New(m).Get("nothing").Inter("something")) - assert.Panics(t, func() { New(m).Get("age").MustInter() }) - } func TestInterSlice(t *testing.T) { - val := interface{}("something") + m := map[string]interface{}{"value": []interface{}{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").InterSlice()[0]) assert.Equal(t, val, New(m).Get("value").MustInterSlice()[0]) assert.Equal(t, []interface{}(nil), New(m).Get("nothing").InterSlice()) assert.Equal(t, val, New(m).Get("nothing").InterSlice([]interface{}{interface{}("something")})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustInterSlice() }) - } func TestIsInter(t *testing.T) { - - var v *Value - - v = &Value{data: interface{}("something")} + v := &Value{data: interface{}("something")} assert.True(t, v.IsInter()) - v = &Value{data: []interface{}{interface{}("something")}} - assert.True(t, v.IsInterSlice()) +} +func TestIsInterSlice(t *testing.T) { + v := &Value{data: []interface{}{interface{}("something")}} + assert.True(t, v.IsInterSlice()) } func TestEachInter(t *testing.T) { - v := &Value{data: []interface{}{interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something")}} count := 0 replacedVals := make([]interface{}, 0) assert.Equal(t, v, v.EachInter(func(i int, val interface{}) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustInterSlice()[0]) assert.Equal(t, replacedVals[1], v.MustInterSlice()[1]) assert.Equal(t, replacedVals[2], v.MustInterSlice()[2]) - } func TestWhereInter(t *testing.T) { - v := &Value{data: []interface{}{interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something")}} selected := v.WhereInter(func(i int, val interface{}) bool { @@ -87,11 +74,9 @@ func TestWhereInter(t *testing.T) { }).MustInterSlice() assert.Equal(t, 3, len(selected)) - } func TestGroupInter(t *testing.T) { - v := &Value{data: []interface{}{interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something")}} grouped := v.GroupInter(func(i int, val interface{}) string { @@ -101,11 +86,9 @@ func TestGroupInter(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceInter(t *testing.T) { - v := &Value{data: []interface{}{interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something")}} rawArr := v.MustInterSlice() @@ -126,11 +109,9 @@ func TestReplaceInter(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectInter(t *testing.T) { - v := &Value{data: []interface{}{interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something"), interface{}("something")}} collected := v.CollectInter(func(index int, val interface{}) interface{} { @@ -146,83 +127,68 @@ func TestCollectInter(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for MSI (map[string]interface{} and []map[string]interface{}) +*/ func TestMSI(t *testing.T) { - val := map[string]interface{}(map[string]interface{}{"name": "Tyler"}) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").MSI()) assert.Equal(t, val, New(m).Get("value").MustMSI()) assert.Equal(t, map[string]interface{}(nil), New(m).Get("nothing").MSI()) assert.Equal(t, val, New(m).Get("nothing").MSI(map[string]interface{}{"name": "Tyler"})) - assert.Panics(t, func() { New(m).Get("age").MustMSI() }) - } func TestMSISlice(t *testing.T) { - val := map[string]interface{}(map[string]interface{}{"name": "Tyler"}) + m := map[string]interface{}{"value": []map[string]interface{}{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").MSISlice()[0]) assert.Equal(t, val, New(m).Get("value").MustMSISlice()[0]) assert.Equal(t, []map[string]interface{}(nil), New(m).Get("nothing").MSISlice()) assert.Equal(t, val, New(m).Get("nothing").MSISlice([]map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Tyler"})})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustMSISlice() }) - } func TestIsMSI(t *testing.T) { - - var v *Value - - v = &Value{data: map[string]interface{}(map[string]interface{}{"name": "Tyler"})} + v := &Value{data: map[string]interface{}(map[string]interface{}{"name": "Tyler"})} assert.True(t, v.IsMSI()) - v = &Value{data: []map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Tyler"})}} - assert.True(t, v.IsMSISlice()) +} +func TestIsMSISlice(t *testing.T) { + v := &Value{data: []map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Tyler"})}} + assert.True(t, v.IsMSISlice()) } func TestEachMSI(t *testing.T) { - v := &Value{data: []map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"})}} count := 0 replacedVals := make([]map[string]interface{}, 0) assert.Equal(t, v, v.EachMSI(func(i int, val map[string]interface{}) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustMSISlice()[0]) assert.Equal(t, replacedVals[1], v.MustMSISlice()[1]) assert.Equal(t, replacedVals[2], v.MustMSISlice()[2]) - } func TestWhereMSI(t *testing.T) { - v := &Value{data: []map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"})}} selected := v.WhereMSI(func(i int, val map[string]interface{}) bool { @@ -230,11 +196,9 @@ func TestWhereMSI(t *testing.T) { }).MustMSISlice() assert.Equal(t, 3, len(selected)) - } func TestGroupMSI(t *testing.T) { - v := &Value{data: []map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"})}} grouped := v.GroupMSI(func(i int, val map[string]interface{}) string { @@ -244,11 +208,9 @@ func TestGroupMSI(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceMSI(t *testing.T) { - v := &Value{data: []map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"})}} rawArr := v.MustMSISlice() @@ -269,11 +231,9 @@ func TestReplaceMSI(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectMSI(t *testing.T) { - v := &Value{data: []map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"}), map[string]interface{}(map[string]interface{}{"name": "Tyler"})}} collected := v.CollectMSI(func(index int, val map[string]interface{}) interface{} { @@ -289,83 +249,68 @@ func TestCollectMSI(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for ObjxMap ((Map) and [](Map)) +*/ func TestObjxMap(t *testing.T) { - val := (Map)(New(1)) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").ObjxMap()) assert.Equal(t, val, New(m).Get("value").MustObjxMap()) assert.Equal(t, (Map)(New(nil)), New(m).Get("nothing").ObjxMap()) assert.Equal(t, val, New(m).Get("nothing").ObjxMap(New(1))) - assert.Panics(t, func() { New(m).Get("age").MustObjxMap() }) - } func TestObjxMapSlice(t *testing.T) { - val := (Map)(New(1)) + m := map[string]interface{}{"value": [](Map){val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").ObjxMapSlice()[0]) assert.Equal(t, val, New(m).Get("value").MustObjxMapSlice()[0]) assert.Equal(t, [](Map)(nil), New(m).Get("nothing").ObjxMapSlice()) assert.Equal(t, val, New(m).Get("nothing").ObjxMapSlice([](Map){(Map)(New(1))})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustObjxMapSlice() }) - } func TestIsObjxMap(t *testing.T) { - - var v *Value - - v = &Value{data: (Map)(New(1))} + v := &Value{data: (Map)(New(1))} assert.True(t, v.IsObjxMap()) - v = &Value{data: [](Map){(Map)(New(1))}} - assert.True(t, v.IsObjxMapSlice()) +} +func TestIsObjxMapSlice(t *testing.T) { + v := &Value{data: [](Map){(Map)(New(1))}} + assert.True(t, v.IsObjxMapSlice()) } func TestEachObjxMap(t *testing.T) { - v := &Value{data: [](Map){(Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1))}} count := 0 replacedVals := make([](Map), 0) assert.Equal(t, v, v.EachObjxMap(func(i int, val Map) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustObjxMapSlice()[0]) assert.Equal(t, replacedVals[1], v.MustObjxMapSlice()[1]) assert.Equal(t, replacedVals[2], v.MustObjxMapSlice()[2]) - } func TestWhereObjxMap(t *testing.T) { - v := &Value{data: [](Map){(Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1))}} selected := v.WhereObjxMap(func(i int, val Map) bool { @@ -373,11 +318,9 @@ func TestWhereObjxMap(t *testing.T) { }).MustObjxMapSlice() assert.Equal(t, 3, len(selected)) - } func TestGroupObjxMap(t *testing.T) { - v := &Value{data: [](Map){(Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1))}} grouped := v.GroupObjxMap(func(i int, val Map) string { @@ -387,11 +330,9 @@ func TestGroupObjxMap(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceObjxMap(t *testing.T) { - v := &Value{data: [](Map){(Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1))}} rawArr := v.MustObjxMapSlice() @@ -412,11 +353,9 @@ func TestReplaceObjxMap(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectObjxMap(t *testing.T) { - v := &Value{data: [](Map){(Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1)), (Map)(New(1))}} collected := v.CollectObjxMap(func(index int, val Map) interface{} { @@ -432,83 +371,68 @@ func TestCollectObjxMap(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Bool (bool and []bool) +*/ func TestBool(t *testing.T) { - val := bool(true) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Bool()) assert.Equal(t, val, New(m).Get("value").MustBool()) assert.Equal(t, bool(false), New(m).Get("nothing").Bool()) assert.Equal(t, val, New(m).Get("nothing").Bool(true)) - assert.Panics(t, func() { New(m).Get("age").MustBool() }) - } func TestBoolSlice(t *testing.T) { - val := bool(true) + m := map[string]interface{}{"value": []bool{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").BoolSlice()[0]) assert.Equal(t, val, New(m).Get("value").MustBoolSlice()[0]) assert.Equal(t, []bool(nil), New(m).Get("nothing").BoolSlice()) assert.Equal(t, val, New(m).Get("nothing").BoolSlice([]bool{bool(true)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustBoolSlice() }) - } func TestIsBool(t *testing.T) { - - var v *Value - - v = &Value{data: bool(true)} + v := &Value{data: bool(true)} assert.True(t, v.IsBool()) - v = &Value{data: []bool{bool(true)}} - assert.True(t, v.IsBoolSlice()) +} +func TestIsBoolSlice(t *testing.T) { + v := &Value{data: []bool{bool(true)}} + assert.True(t, v.IsBoolSlice()) } func TestEachBool(t *testing.T) { - v := &Value{data: []bool{bool(true), bool(true), bool(true), bool(true), bool(true)}} count := 0 replacedVals := make([]bool, 0) assert.Equal(t, v, v.EachBool(func(i int, val bool) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustBoolSlice()[0]) assert.Equal(t, replacedVals[1], v.MustBoolSlice()[1]) assert.Equal(t, replacedVals[2], v.MustBoolSlice()[2]) - } func TestWhereBool(t *testing.T) { - v := &Value{data: []bool{bool(true), bool(true), bool(true), bool(true), bool(true), bool(true)}} selected := v.WhereBool(func(i int, val bool) bool { @@ -516,11 +440,9 @@ func TestWhereBool(t *testing.T) { }).MustBoolSlice() assert.Equal(t, 3, len(selected)) - } func TestGroupBool(t *testing.T) { - v := &Value{data: []bool{bool(true), bool(true), bool(true), bool(true), bool(true), bool(true)}} grouped := v.GroupBool(func(i int, val bool) string { @@ -530,11 +452,9 @@ func TestGroupBool(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceBool(t *testing.T) { - v := &Value{data: []bool{bool(true), bool(true), bool(true), bool(true), bool(true), bool(true)}} rawArr := v.MustBoolSlice() @@ -555,11 +475,9 @@ func TestReplaceBool(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectBool(t *testing.T) { - v := &Value{data: []bool{bool(true), bool(true), bool(true), bool(true), bool(true), bool(true)}} collected := v.CollectBool(func(index int, val bool) interface{} { @@ -575,83 +493,68 @@ func TestCollectBool(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Str (string and []string) +*/ func TestStr(t *testing.T) { - val := string("hello") + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Str()) assert.Equal(t, val, New(m).Get("value").MustStr()) assert.Equal(t, string(""), New(m).Get("nothing").Str()) assert.Equal(t, val, New(m).Get("nothing").Str("hello")) - assert.Panics(t, func() { New(m).Get("age").MustStr() }) - } func TestStrSlice(t *testing.T) { - val := string("hello") + m := map[string]interface{}{"value": []string{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").StrSlice()[0]) assert.Equal(t, val, New(m).Get("value").MustStrSlice()[0]) assert.Equal(t, []string(nil), New(m).Get("nothing").StrSlice()) assert.Equal(t, val, New(m).Get("nothing").StrSlice([]string{string("hello")})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustStrSlice() }) - } func TestIsStr(t *testing.T) { - - var v *Value - - v = &Value{data: string("hello")} + v := &Value{data: string("hello")} assert.True(t, v.IsStr()) - v = &Value{data: []string{string("hello")}} - assert.True(t, v.IsStrSlice()) +} +func TestIsStrSlice(t *testing.T) { + v := &Value{data: []string{string("hello")}} + assert.True(t, v.IsStrSlice()) } func TestEachStr(t *testing.T) { - v := &Value{data: []string{string("hello"), string("hello"), string("hello"), string("hello"), string("hello")}} count := 0 replacedVals := make([]string, 0) assert.Equal(t, v, v.EachStr(func(i int, val string) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustStrSlice()[0]) assert.Equal(t, replacedVals[1], v.MustStrSlice()[1]) assert.Equal(t, replacedVals[2], v.MustStrSlice()[2]) - } func TestWhereStr(t *testing.T) { - v := &Value{data: []string{string("hello"), string("hello"), string("hello"), string("hello"), string("hello"), string("hello")}} selected := v.WhereStr(func(i int, val string) bool { @@ -659,11 +562,9 @@ func TestWhereStr(t *testing.T) { }).MustStrSlice() assert.Equal(t, 3, len(selected)) - } func TestGroupStr(t *testing.T) { - v := &Value{data: []string{string("hello"), string("hello"), string("hello"), string("hello"), string("hello"), string("hello")}} grouped := v.GroupStr(func(i int, val string) string { @@ -673,11 +574,9 @@ func TestGroupStr(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceStr(t *testing.T) { - v := &Value{data: []string{string("hello"), string("hello"), string("hello"), string("hello"), string("hello"), string("hello")}} rawArr := v.MustStrSlice() @@ -698,11 +597,9 @@ func TestReplaceStr(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectStr(t *testing.T) { - v := &Value{data: []string{string("hello"), string("hello"), string("hello"), string("hello"), string("hello"), string("hello")}} collected := v.CollectStr(func(index int, val string) interface{} { @@ -718,83 +615,68 @@ func TestCollectStr(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Int (int and []int) +*/ func TestInt(t *testing.T) { - val := int(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Int()) assert.Equal(t, val, New(m).Get("value").MustInt()) assert.Equal(t, int(0), New(m).Get("nothing").Int()) assert.Equal(t, val, New(m).Get("nothing").Int(1)) - assert.Panics(t, func() { New(m).Get("age").MustInt() }) - } func TestIntSlice(t *testing.T) { - val := int(1) + m := map[string]interface{}{"value": []int{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").IntSlice()[0]) assert.Equal(t, val, New(m).Get("value").MustIntSlice()[0]) assert.Equal(t, []int(nil), New(m).Get("nothing").IntSlice()) assert.Equal(t, val, New(m).Get("nothing").IntSlice([]int{int(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustIntSlice() }) - } func TestIsInt(t *testing.T) { - - var v *Value - - v = &Value{data: int(1)} + v := &Value{data: int(1)} assert.True(t, v.IsInt()) - v = &Value{data: []int{int(1)}} - assert.True(t, v.IsIntSlice()) +} +func TestIsIntSlice(t *testing.T) { + v := &Value{data: []int{int(1)}} + assert.True(t, v.IsIntSlice()) } func TestEachInt(t *testing.T) { - v := &Value{data: []int{int(1), int(1), int(1), int(1), int(1)}} count := 0 replacedVals := make([]int, 0) assert.Equal(t, v, v.EachInt(func(i int, val int) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustIntSlice()[0]) assert.Equal(t, replacedVals[1], v.MustIntSlice()[1]) assert.Equal(t, replacedVals[2], v.MustIntSlice()[2]) - } func TestWhereInt(t *testing.T) { - v := &Value{data: []int{int(1), int(1), int(1), int(1), int(1), int(1)}} selected := v.WhereInt(func(i int, val int) bool { @@ -802,11 +684,9 @@ func TestWhereInt(t *testing.T) { }).MustIntSlice() assert.Equal(t, 3, len(selected)) - } func TestGroupInt(t *testing.T) { - v := &Value{data: []int{int(1), int(1), int(1), int(1), int(1), int(1)}} grouped := v.GroupInt(func(i int, val int) string { @@ -816,11 +696,9 @@ func TestGroupInt(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceInt(t *testing.T) { - v := &Value{data: []int{int(1), int(1), int(1), int(1), int(1), int(1)}} rawArr := v.MustIntSlice() @@ -841,11 +719,9 @@ func TestReplaceInt(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectInt(t *testing.T) { - v := &Value{data: []int{int(1), int(1), int(1), int(1), int(1), int(1)}} collected := v.CollectInt(func(index int, val int) interface{} { @@ -861,83 +737,68 @@ func TestCollectInt(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Int8 (int8 and []int8) +*/ func TestInt8(t *testing.T) { - val := int8(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Int8()) assert.Equal(t, val, New(m).Get("value").MustInt8()) assert.Equal(t, int8(0), New(m).Get("nothing").Int8()) assert.Equal(t, val, New(m).Get("nothing").Int8(1)) - assert.Panics(t, func() { New(m).Get("age").MustInt8() }) - } func TestInt8Slice(t *testing.T) { - val := int8(1) + m := map[string]interface{}{"value": []int8{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Int8Slice()[0]) assert.Equal(t, val, New(m).Get("value").MustInt8Slice()[0]) assert.Equal(t, []int8(nil), New(m).Get("nothing").Int8Slice()) assert.Equal(t, val, New(m).Get("nothing").Int8Slice([]int8{int8(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustInt8Slice() }) - } func TestIsInt8(t *testing.T) { - - var v *Value - - v = &Value{data: int8(1)} + v := &Value{data: int8(1)} assert.True(t, v.IsInt8()) - v = &Value{data: []int8{int8(1)}} - assert.True(t, v.IsInt8Slice()) +} +func TestIsInt8Slice(t *testing.T) { + v := &Value{data: []int8{int8(1)}} + assert.True(t, v.IsInt8Slice()) } func TestEachInt8(t *testing.T) { - v := &Value{data: []int8{int8(1), int8(1), int8(1), int8(1), int8(1)}} count := 0 replacedVals := make([]int8, 0) assert.Equal(t, v, v.EachInt8(func(i int, val int8) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustInt8Slice()[0]) assert.Equal(t, replacedVals[1], v.MustInt8Slice()[1]) assert.Equal(t, replacedVals[2], v.MustInt8Slice()[2]) - } func TestWhereInt8(t *testing.T) { - v := &Value{data: []int8{int8(1), int8(1), int8(1), int8(1), int8(1), int8(1)}} selected := v.WhereInt8(func(i int, val int8) bool { @@ -945,11 +806,9 @@ func TestWhereInt8(t *testing.T) { }).MustInt8Slice() assert.Equal(t, 3, len(selected)) - } func TestGroupInt8(t *testing.T) { - v := &Value{data: []int8{int8(1), int8(1), int8(1), int8(1), int8(1), int8(1)}} grouped := v.GroupInt8(func(i int, val int8) string { @@ -959,11 +818,9 @@ func TestGroupInt8(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceInt8(t *testing.T) { - v := &Value{data: []int8{int8(1), int8(1), int8(1), int8(1), int8(1), int8(1)}} rawArr := v.MustInt8Slice() @@ -984,11 +841,9 @@ func TestReplaceInt8(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectInt8(t *testing.T) { - v := &Value{data: []int8{int8(1), int8(1), int8(1), int8(1), int8(1), int8(1)}} collected := v.CollectInt8(func(index int, val int8) interface{} { @@ -1004,83 +859,68 @@ func TestCollectInt8(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Int16 (int16 and []int16) +*/ func TestInt16(t *testing.T) { - val := int16(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Int16()) assert.Equal(t, val, New(m).Get("value").MustInt16()) assert.Equal(t, int16(0), New(m).Get("nothing").Int16()) assert.Equal(t, val, New(m).Get("nothing").Int16(1)) - assert.Panics(t, func() { New(m).Get("age").MustInt16() }) - } func TestInt16Slice(t *testing.T) { - val := int16(1) + m := map[string]interface{}{"value": []int16{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Int16Slice()[0]) assert.Equal(t, val, New(m).Get("value").MustInt16Slice()[0]) assert.Equal(t, []int16(nil), New(m).Get("nothing").Int16Slice()) assert.Equal(t, val, New(m).Get("nothing").Int16Slice([]int16{int16(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustInt16Slice() }) - } func TestIsInt16(t *testing.T) { - - var v *Value - - v = &Value{data: int16(1)} + v := &Value{data: int16(1)} assert.True(t, v.IsInt16()) - v = &Value{data: []int16{int16(1)}} - assert.True(t, v.IsInt16Slice()) +} +func TestIsInt16Slice(t *testing.T) { + v := &Value{data: []int16{int16(1)}} + assert.True(t, v.IsInt16Slice()) } func TestEachInt16(t *testing.T) { - v := &Value{data: []int16{int16(1), int16(1), int16(1), int16(1), int16(1)}} count := 0 replacedVals := make([]int16, 0) assert.Equal(t, v, v.EachInt16(func(i int, val int16) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustInt16Slice()[0]) assert.Equal(t, replacedVals[1], v.MustInt16Slice()[1]) assert.Equal(t, replacedVals[2], v.MustInt16Slice()[2]) - } func TestWhereInt16(t *testing.T) { - v := &Value{data: []int16{int16(1), int16(1), int16(1), int16(1), int16(1), int16(1)}} selected := v.WhereInt16(func(i int, val int16) bool { @@ -1088,11 +928,9 @@ func TestWhereInt16(t *testing.T) { }).MustInt16Slice() assert.Equal(t, 3, len(selected)) - } func TestGroupInt16(t *testing.T) { - v := &Value{data: []int16{int16(1), int16(1), int16(1), int16(1), int16(1), int16(1)}} grouped := v.GroupInt16(func(i int, val int16) string { @@ -1102,11 +940,9 @@ func TestGroupInt16(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceInt16(t *testing.T) { - v := &Value{data: []int16{int16(1), int16(1), int16(1), int16(1), int16(1), int16(1)}} rawArr := v.MustInt16Slice() @@ -1127,11 +963,9 @@ func TestReplaceInt16(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectInt16(t *testing.T) { - v := &Value{data: []int16{int16(1), int16(1), int16(1), int16(1), int16(1), int16(1)}} collected := v.CollectInt16(func(index int, val int16) interface{} { @@ -1147,83 +981,68 @@ func TestCollectInt16(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Int32 (int32 and []int32) +*/ func TestInt32(t *testing.T) { - val := int32(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Int32()) assert.Equal(t, val, New(m).Get("value").MustInt32()) assert.Equal(t, int32(0), New(m).Get("nothing").Int32()) assert.Equal(t, val, New(m).Get("nothing").Int32(1)) - assert.Panics(t, func() { New(m).Get("age").MustInt32() }) - } func TestInt32Slice(t *testing.T) { - val := int32(1) + m := map[string]interface{}{"value": []int32{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Int32Slice()[0]) assert.Equal(t, val, New(m).Get("value").MustInt32Slice()[0]) assert.Equal(t, []int32(nil), New(m).Get("nothing").Int32Slice()) assert.Equal(t, val, New(m).Get("nothing").Int32Slice([]int32{int32(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustInt32Slice() }) - } func TestIsInt32(t *testing.T) { - - var v *Value - - v = &Value{data: int32(1)} + v := &Value{data: int32(1)} assert.True(t, v.IsInt32()) - v = &Value{data: []int32{int32(1)}} - assert.True(t, v.IsInt32Slice()) +} +func TestIsInt32Slice(t *testing.T) { + v := &Value{data: []int32{int32(1)}} + assert.True(t, v.IsInt32Slice()) } func TestEachInt32(t *testing.T) { - v := &Value{data: []int32{int32(1), int32(1), int32(1), int32(1), int32(1)}} count := 0 replacedVals := make([]int32, 0) assert.Equal(t, v, v.EachInt32(func(i int, val int32) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustInt32Slice()[0]) assert.Equal(t, replacedVals[1], v.MustInt32Slice()[1]) assert.Equal(t, replacedVals[2], v.MustInt32Slice()[2]) - } func TestWhereInt32(t *testing.T) { - v := &Value{data: []int32{int32(1), int32(1), int32(1), int32(1), int32(1), int32(1)}} selected := v.WhereInt32(func(i int, val int32) bool { @@ -1231,11 +1050,9 @@ func TestWhereInt32(t *testing.T) { }).MustInt32Slice() assert.Equal(t, 3, len(selected)) - } func TestGroupInt32(t *testing.T) { - v := &Value{data: []int32{int32(1), int32(1), int32(1), int32(1), int32(1), int32(1)}} grouped := v.GroupInt32(func(i int, val int32) string { @@ -1245,11 +1062,9 @@ func TestGroupInt32(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceInt32(t *testing.T) { - v := &Value{data: []int32{int32(1), int32(1), int32(1), int32(1), int32(1), int32(1)}} rawArr := v.MustInt32Slice() @@ -1270,11 +1085,9 @@ func TestReplaceInt32(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectInt32(t *testing.T) { - v := &Value{data: []int32{int32(1), int32(1), int32(1), int32(1), int32(1), int32(1)}} collected := v.CollectInt32(func(index int, val int32) interface{} { @@ -1290,83 +1103,68 @@ func TestCollectInt32(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Int64 (int64 and []int64) +*/ func TestInt64(t *testing.T) { - val := int64(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Int64()) assert.Equal(t, val, New(m).Get("value").MustInt64()) assert.Equal(t, int64(0), New(m).Get("nothing").Int64()) assert.Equal(t, val, New(m).Get("nothing").Int64(1)) - assert.Panics(t, func() { New(m).Get("age").MustInt64() }) - } func TestInt64Slice(t *testing.T) { - val := int64(1) + m := map[string]interface{}{"value": []int64{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Int64Slice()[0]) assert.Equal(t, val, New(m).Get("value").MustInt64Slice()[0]) assert.Equal(t, []int64(nil), New(m).Get("nothing").Int64Slice()) assert.Equal(t, val, New(m).Get("nothing").Int64Slice([]int64{int64(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustInt64Slice() }) - } func TestIsInt64(t *testing.T) { - - var v *Value - - v = &Value{data: int64(1)} + v := &Value{data: int64(1)} assert.True(t, v.IsInt64()) - v = &Value{data: []int64{int64(1)}} - assert.True(t, v.IsInt64Slice()) +} +func TestIsInt64Slice(t *testing.T) { + v := &Value{data: []int64{int64(1)}} + assert.True(t, v.IsInt64Slice()) } func TestEachInt64(t *testing.T) { - v := &Value{data: []int64{int64(1), int64(1), int64(1), int64(1), int64(1)}} count := 0 replacedVals := make([]int64, 0) assert.Equal(t, v, v.EachInt64(func(i int, val int64) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustInt64Slice()[0]) assert.Equal(t, replacedVals[1], v.MustInt64Slice()[1]) assert.Equal(t, replacedVals[2], v.MustInt64Slice()[2]) - } func TestWhereInt64(t *testing.T) { - v := &Value{data: []int64{int64(1), int64(1), int64(1), int64(1), int64(1), int64(1)}} selected := v.WhereInt64(func(i int, val int64) bool { @@ -1374,11 +1172,9 @@ func TestWhereInt64(t *testing.T) { }).MustInt64Slice() assert.Equal(t, 3, len(selected)) - } func TestGroupInt64(t *testing.T) { - v := &Value{data: []int64{int64(1), int64(1), int64(1), int64(1), int64(1), int64(1)}} grouped := v.GroupInt64(func(i int, val int64) string { @@ -1388,11 +1184,9 @@ func TestGroupInt64(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceInt64(t *testing.T) { - v := &Value{data: []int64{int64(1), int64(1), int64(1), int64(1), int64(1), int64(1)}} rawArr := v.MustInt64Slice() @@ -1413,11 +1207,9 @@ func TestReplaceInt64(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectInt64(t *testing.T) { - v := &Value{data: []int64{int64(1), int64(1), int64(1), int64(1), int64(1), int64(1)}} collected := v.CollectInt64(func(index int, val int64) interface{} { @@ -1433,83 +1225,68 @@ func TestCollectInt64(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Uint (uint and []uint) +*/ func TestUint(t *testing.T) { - val := uint(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Uint()) assert.Equal(t, val, New(m).Get("value").MustUint()) assert.Equal(t, uint(0), New(m).Get("nothing").Uint()) assert.Equal(t, val, New(m).Get("nothing").Uint(1)) - assert.Panics(t, func() { New(m).Get("age").MustUint() }) - } func TestUintSlice(t *testing.T) { - val := uint(1) + m := map[string]interface{}{"value": []uint{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").UintSlice()[0]) assert.Equal(t, val, New(m).Get("value").MustUintSlice()[0]) assert.Equal(t, []uint(nil), New(m).Get("nothing").UintSlice()) assert.Equal(t, val, New(m).Get("nothing").UintSlice([]uint{uint(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustUintSlice() }) - } func TestIsUint(t *testing.T) { - - var v *Value - - v = &Value{data: uint(1)} + v := &Value{data: uint(1)} assert.True(t, v.IsUint()) - v = &Value{data: []uint{uint(1)}} - assert.True(t, v.IsUintSlice()) +} +func TestIsUintSlice(t *testing.T) { + v := &Value{data: []uint{uint(1)}} + assert.True(t, v.IsUintSlice()) } func TestEachUint(t *testing.T) { - v := &Value{data: []uint{uint(1), uint(1), uint(1), uint(1), uint(1)}} count := 0 replacedVals := make([]uint, 0) assert.Equal(t, v, v.EachUint(func(i int, val uint) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustUintSlice()[0]) assert.Equal(t, replacedVals[1], v.MustUintSlice()[1]) assert.Equal(t, replacedVals[2], v.MustUintSlice()[2]) - } func TestWhereUint(t *testing.T) { - v := &Value{data: []uint{uint(1), uint(1), uint(1), uint(1), uint(1), uint(1)}} selected := v.WhereUint(func(i int, val uint) bool { @@ -1517,11 +1294,9 @@ func TestWhereUint(t *testing.T) { }).MustUintSlice() assert.Equal(t, 3, len(selected)) - } func TestGroupUint(t *testing.T) { - v := &Value{data: []uint{uint(1), uint(1), uint(1), uint(1), uint(1), uint(1)}} grouped := v.GroupUint(func(i int, val uint) string { @@ -1531,11 +1306,9 @@ func TestGroupUint(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceUint(t *testing.T) { - v := &Value{data: []uint{uint(1), uint(1), uint(1), uint(1), uint(1), uint(1)}} rawArr := v.MustUintSlice() @@ -1556,11 +1329,9 @@ func TestReplaceUint(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectUint(t *testing.T) { - v := &Value{data: []uint{uint(1), uint(1), uint(1), uint(1), uint(1), uint(1)}} collected := v.CollectUint(func(index int, val uint) interface{} { @@ -1576,83 +1347,68 @@ func TestCollectUint(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Uint8 (uint8 and []uint8) +*/ func TestUint8(t *testing.T) { - val := uint8(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Uint8()) assert.Equal(t, val, New(m).Get("value").MustUint8()) assert.Equal(t, uint8(0), New(m).Get("nothing").Uint8()) assert.Equal(t, val, New(m).Get("nothing").Uint8(1)) - assert.Panics(t, func() { New(m).Get("age").MustUint8() }) - } func TestUint8Slice(t *testing.T) { - val := uint8(1) + m := map[string]interface{}{"value": []uint8{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Uint8Slice()[0]) assert.Equal(t, val, New(m).Get("value").MustUint8Slice()[0]) assert.Equal(t, []uint8(nil), New(m).Get("nothing").Uint8Slice()) assert.Equal(t, val, New(m).Get("nothing").Uint8Slice([]uint8{uint8(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustUint8Slice() }) - } func TestIsUint8(t *testing.T) { - - var v *Value - - v = &Value{data: uint8(1)} + v := &Value{data: uint8(1)} assert.True(t, v.IsUint8()) - v = &Value{data: []uint8{uint8(1)}} - assert.True(t, v.IsUint8Slice()) +} +func TestIsUint8Slice(t *testing.T) { + v := &Value{data: []uint8{uint8(1)}} + assert.True(t, v.IsUint8Slice()) } func TestEachUint8(t *testing.T) { - v := &Value{data: []uint8{uint8(1), uint8(1), uint8(1), uint8(1), uint8(1)}} count := 0 replacedVals := make([]uint8, 0) assert.Equal(t, v, v.EachUint8(func(i int, val uint8) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustUint8Slice()[0]) assert.Equal(t, replacedVals[1], v.MustUint8Slice()[1]) assert.Equal(t, replacedVals[2], v.MustUint8Slice()[2]) - } func TestWhereUint8(t *testing.T) { - v := &Value{data: []uint8{uint8(1), uint8(1), uint8(1), uint8(1), uint8(1), uint8(1)}} selected := v.WhereUint8(func(i int, val uint8) bool { @@ -1660,11 +1416,9 @@ func TestWhereUint8(t *testing.T) { }).MustUint8Slice() assert.Equal(t, 3, len(selected)) - } func TestGroupUint8(t *testing.T) { - v := &Value{data: []uint8{uint8(1), uint8(1), uint8(1), uint8(1), uint8(1), uint8(1)}} grouped := v.GroupUint8(func(i int, val uint8) string { @@ -1674,11 +1428,9 @@ func TestGroupUint8(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceUint8(t *testing.T) { - v := &Value{data: []uint8{uint8(1), uint8(1), uint8(1), uint8(1), uint8(1), uint8(1)}} rawArr := v.MustUint8Slice() @@ -1699,11 +1451,9 @@ func TestReplaceUint8(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectUint8(t *testing.T) { - v := &Value{data: []uint8{uint8(1), uint8(1), uint8(1), uint8(1), uint8(1), uint8(1)}} collected := v.CollectUint8(func(index int, val uint8) interface{} { @@ -1719,83 +1469,68 @@ func TestCollectUint8(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Uint16 (uint16 and []uint16) +*/ func TestUint16(t *testing.T) { - val := uint16(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Uint16()) assert.Equal(t, val, New(m).Get("value").MustUint16()) assert.Equal(t, uint16(0), New(m).Get("nothing").Uint16()) assert.Equal(t, val, New(m).Get("nothing").Uint16(1)) - assert.Panics(t, func() { New(m).Get("age").MustUint16() }) - } func TestUint16Slice(t *testing.T) { - val := uint16(1) + m := map[string]interface{}{"value": []uint16{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Uint16Slice()[0]) assert.Equal(t, val, New(m).Get("value").MustUint16Slice()[0]) assert.Equal(t, []uint16(nil), New(m).Get("nothing").Uint16Slice()) assert.Equal(t, val, New(m).Get("nothing").Uint16Slice([]uint16{uint16(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustUint16Slice() }) - } func TestIsUint16(t *testing.T) { - - var v *Value - - v = &Value{data: uint16(1)} + v := &Value{data: uint16(1)} assert.True(t, v.IsUint16()) - v = &Value{data: []uint16{uint16(1)}} - assert.True(t, v.IsUint16Slice()) +} +func TestIsUint16Slice(t *testing.T) { + v := &Value{data: []uint16{uint16(1)}} + assert.True(t, v.IsUint16Slice()) } func TestEachUint16(t *testing.T) { - v := &Value{data: []uint16{uint16(1), uint16(1), uint16(1), uint16(1), uint16(1)}} count := 0 replacedVals := make([]uint16, 0) assert.Equal(t, v, v.EachUint16(func(i int, val uint16) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustUint16Slice()[0]) assert.Equal(t, replacedVals[1], v.MustUint16Slice()[1]) assert.Equal(t, replacedVals[2], v.MustUint16Slice()[2]) - } func TestWhereUint16(t *testing.T) { - v := &Value{data: []uint16{uint16(1), uint16(1), uint16(1), uint16(1), uint16(1), uint16(1)}} selected := v.WhereUint16(func(i int, val uint16) bool { @@ -1803,11 +1538,9 @@ func TestWhereUint16(t *testing.T) { }).MustUint16Slice() assert.Equal(t, 3, len(selected)) - } func TestGroupUint16(t *testing.T) { - v := &Value{data: []uint16{uint16(1), uint16(1), uint16(1), uint16(1), uint16(1), uint16(1)}} grouped := v.GroupUint16(func(i int, val uint16) string { @@ -1817,11 +1550,9 @@ func TestGroupUint16(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceUint16(t *testing.T) { - v := &Value{data: []uint16{uint16(1), uint16(1), uint16(1), uint16(1), uint16(1), uint16(1)}} rawArr := v.MustUint16Slice() @@ -1842,11 +1573,9 @@ func TestReplaceUint16(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectUint16(t *testing.T) { - v := &Value{data: []uint16{uint16(1), uint16(1), uint16(1), uint16(1), uint16(1), uint16(1)}} collected := v.CollectUint16(func(index int, val uint16) interface{} { @@ -1862,83 +1591,68 @@ func TestCollectUint16(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Uint32 (uint32 and []uint32) +*/ func TestUint32(t *testing.T) { - val := uint32(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Uint32()) assert.Equal(t, val, New(m).Get("value").MustUint32()) assert.Equal(t, uint32(0), New(m).Get("nothing").Uint32()) assert.Equal(t, val, New(m).Get("nothing").Uint32(1)) - assert.Panics(t, func() { New(m).Get("age").MustUint32() }) - } func TestUint32Slice(t *testing.T) { - val := uint32(1) + m := map[string]interface{}{"value": []uint32{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Uint32Slice()[0]) assert.Equal(t, val, New(m).Get("value").MustUint32Slice()[0]) assert.Equal(t, []uint32(nil), New(m).Get("nothing").Uint32Slice()) assert.Equal(t, val, New(m).Get("nothing").Uint32Slice([]uint32{uint32(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustUint32Slice() }) - } func TestIsUint32(t *testing.T) { - - var v *Value - - v = &Value{data: uint32(1)} + v := &Value{data: uint32(1)} assert.True(t, v.IsUint32()) - v = &Value{data: []uint32{uint32(1)}} - assert.True(t, v.IsUint32Slice()) +} +func TestIsUint32Slice(t *testing.T) { + v := &Value{data: []uint32{uint32(1)}} + assert.True(t, v.IsUint32Slice()) } func TestEachUint32(t *testing.T) { - v := &Value{data: []uint32{uint32(1), uint32(1), uint32(1), uint32(1), uint32(1)}} count := 0 replacedVals := make([]uint32, 0) assert.Equal(t, v, v.EachUint32(func(i int, val uint32) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustUint32Slice()[0]) assert.Equal(t, replacedVals[1], v.MustUint32Slice()[1]) assert.Equal(t, replacedVals[2], v.MustUint32Slice()[2]) - } func TestWhereUint32(t *testing.T) { - v := &Value{data: []uint32{uint32(1), uint32(1), uint32(1), uint32(1), uint32(1), uint32(1)}} selected := v.WhereUint32(func(i int, val uint32) bool { @@ -1946,11 +1660,9 @@ func TestWhereUint32(t *testing.T) { }).MustUint32Slice() assert.Equal(t, 3, len(selected)) - } func TestGroupUint32(t *testing.T) { - v := &Value{data: []uint32{uint32(1), uint32(1), uint32(1), uint32(1), uint32(1), uint32(1)}} grouped := v.GroupUint32(func(i int, val uint32) string { @@ -1960,11 +1672,9 @@ func TestGroupUint32(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceUint32(t *testing.T) { - v := &Value{data: []uint32{uint32(1), uint32(1), uint32(1), uint32(1), uint32(1), uint32(1)}} rawArr := v.MustUint32Slice() @@ -1985,11 +1695,9 @@ func TestReplaceUint32(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectUint32(t *testing.T) { - v := &Value{data: []uint32{uint32(1), uint32(1), uint32(1), uint32(1), uint32(1), uint32(1)}} collected := v.CollectUint32(func(index int, val uint32) interface{} { @@ -2005,83 +1713,68 @@ func TestCollectUint32(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Uint64 (uint64 and []uint64) +*/ func TestUint64(t *testing.T) { - val := uint64(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Uint64()) assert.Equal(t, val, New(m).Get("value").MustUint64()) assert.Equal(t, uint64(0), New(m).Get("nothing").Uint64()) assert.Equal(t, val, New(m).Get("nothing").Uint64(1)) - assert.Panics(t, func() { New(m).Get("age").MustUint64() }) - } func TestUint64Slice(t *testing.T) { - val := uint64(1) + m := map[string]interface{}{"value": []uint64{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Uint64Slice()[0]) assert.Equal(t, val, New(m).Get("value").MustUint64Slice()[0]) assert.Equal(t, []uint64(nil), New(m).Get("nothing").Uint64Slice()) assert.Equal(t, val, New(m).Get("nothing").Uint64Slice([]uint64{uint64(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustUint64Slice() }) - } func TestIsUint64(t *testing.T) { - - var v *Value - - v = &Value{data: uint64(1)} + v := &Value{data: uint64(1)} assert.True(t, v.IsUint64()) - v = &Value{data: []uint64{uint64(1)}} - assert.True(t, v.IsUint64Slice()) +} +func TestIsUint64Slice(t *testing.T) { + v := &Value{data: []uint64{uint64(1)}} + assert.True(t, v.IsUint64Slice()) } func TestEachUint64(t *testing.T) { - v := &Value{data: []uint64{uint64(1), uint64(1), uint64(1), uint64(1), uint64(1)}} count := 0 replacedVals := make([]uint64, 0) assert.Equal(t, v, v.EachUint64(func(i int, val uint64) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustUint64Slice()[0]) assert.Equal(t, replacedVals[1], v.MustUint64Slice()[1]) assert.Equal(t, replacedVals[2], v.MustUint64Slice()[2]) - } func TestWhereUint64(t *testing.T) { - v := &Value{data: []uint64{uint64(1), uint64(1), uint64(1), uint64(1), uint64(1), uint64(1)}} selected := v.WhereUint64(func(i int, val uint64) bool { @@ -2089,11 +1782,9 @@ func TestWhereUint64(t *testing.T) { }).MustUint64Slice() assert.Equal(t, 3, len(selected)) - } func TestGroupUint64(t *testing.T) { - v := &Value{data: []uint64{uint64(1), uint64(1), uint64(1), uint64(1), uint64(1), uint64(1)}} grouped := v.GroupUint64(func(i int, val uint64) string { @@ -2103,11 +1794,9 @@ func TestGroupUint64(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceUint64(t *testing.T) { - v := &Value{data: []uint64{uint64(1), uint64(1), uint64(1), uint64(1), uint64(1), uint64(1)}} rawArr := v.MustUint64Slice() @@ -2128,11 +1817,9 @@ func TestReplaceUint64(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectUint64(t *testing.T) { - v := &Value{data: []uint64{uint64(1), uint64(1), uint64(1), uint64(1), uint64(1), uint64(1)}} collected := v.CollectUint64(func(index int, val uint64) interface{} { @@ -2148,83 +1835,68 @@ func TestCollectUint64(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Uintptr (uintptr and []uintptr) +*/ func TestUintptr(t *testing.T) { - val := uintptr(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Uintptr()) assert.Equal(t, val, New(m).Get("value").MustUintptr()) assert.Equal(t, uintptr(0), New(m).Get("nothing").Uintptr()) assert.Equal(t, val, New(m).Get("nothing").Uintptr(1)) - assert.Panics(t, func() { New(m).Get("age").MustUintptr() }) - } func TestUintptrSlice(t *testing.T) { - val := uintptr(1) + m := map[string]interface{}{"value": []uintptr{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").UintptrSlice()[0]) assert.Equal(t, val, New(m).Get("value").MustUintptrSlice()[0]) assert.Equal(t, []uintptr(nil), New(m).Get("nothing").UintptrSlice()) assert.Equal(t, val, New(m).Get("nothing").UintptrSlice([]uintptr{uintptr(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustUintptrSlice() }) - } func TestIsUintptr(t *testing.T) { - - var v *Value - - v = &Value{data: uintptr(1)} + v := &Value{data: uintptr(1)} assert.True(t, v.IsUintptr()) - v = &Value{data: []uintptr{uintptr(1)}} - assert.True(t, v.IsUintptrSlice()) +} +func TestIsUintptrSlice(t *testing.T) { + v := &Value{data: []uintptr{uintptr(1)}} + assert.True(t, v.IsUintptrSlice()) } func TestEachUintptr(t *testing.T) { - v := &Value{data: []uintptr{uintptr(1), uintptr(1), uintptr(1), uintptr(1), uintptr(1)}} count := 0 replacedVals := make([]uintptr, 0) assert.Equal(t, v, v.EachUintptr(func(i int, val uintptr) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustUintptrSlice()[0]) assert.Equal(t, replacedVals[1], v.MustUintptrSlice()[1]) assert.Equal(t, replacedVals[2], v.MustUintptrSlice()[2]) - } func TestWhereUintptr(t *testing.T) { - v := &Value{data: []uintptr{uintptr(1), uintptr(1), uintptr(1), uintptr(1), uintptr(1), uintptr(1)}} selected := v.WhereUintptr(func(i int, val uintptr) bool { @@ -2232,11 +1904,9 @@ func TestWhereUintptr(t *testing.T) { }).MustUintptrSlice() assert.Equal(t, 3, len(selected)) - } func TestGroupUintptr(t *testing.T) { - v := &Value{data: []uintptr{uintptr(1), uintptr(1), uintptr(1), uintptr(1), uintptr(1), uintptr(1)}} grouped := v.GroupUintptr(func(i int, val uintptr) string { @@ -2246,11 +1916,9 @@ func TestGroupUintptr(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceUintptr(t *testing.T) { - v := &Value{data: []uintptr{uintptr(1), uintptr(1), uintptr(1), uintptr(1), uintptr(1), uintptr(1)}} rawArr := v.MustUintptrSlice() @@ -2271,11 +1939,9 @@ func TestReplaceUintptr(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectUintptr(t *testing.T) { - v := &Value{data: []uintptr{uintptr(1), uintptr(1), uintptr(1), uintptr(1), uintptr(1), uintptr(1)}} collected := v.CollectUintptr(func(index int, val uintptr) interface{} { @@ -2291,83 +1957,68 @@ func TestCollectUintptr(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Float32 (float32 and []float32) +*/ func TestFloat32(t *testing.T) { - val := float32(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Float32()) assert.Equal(t, val, New(m).Get("value").MustFloat32()) assert.Equal(t, float32(0), New(m).Get("nothing").Float32()) assert.Equal(t, val, New(m).Get("nothing").Float32(1)) - assert.Panics(t, func() { New(m).Get("age").MustFloat32() }) - } func TestFloat32Slice(t *testing.T) { - val := float32(1) + m := map[string]interface{}{"value": []float32{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Float32Slice()[0]) assert.Equal(t, val, New(m).Get("value").MustFloat32Slice()[0]) assert.Equal(t, []float32(nil), New(m).Get("nothing").Float32Slice()) assert.Equal(t, val, New(m).Get("nothing").Float32Slice([]float32{float32(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustFloat32Slice() }) - } func TestIsFloat32(t *testing.T) { - - var v *Value - - v = &Value{data: float32(1)} + v := &Value{data: float32(1)} assert.True(t, v.IsFloat32()) - v = &Value{data: []float32{float32(1)}} - assert.True(t, v.IsFloat32Slice()) +} +func TestIsFloat32Slice(t *testing.T) { + v := &Value{data: []float32{float32(1)}} + assert.True(t, v.IsFloat32Slice()) } func TestEachFloat32(t *testing.T) { - v := &Value{data: []float32{float32(1), float32(1), float32(1), float32(1), float32(1)}} count := 0 replacedVals := make([]float32, 0) assert.Equal(t, v, v.EachFloat32(func(i int, val float32) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustFloat32Slice()[0]) assert.Equal(t, replacedVals[1], v.MustFloat32Slice()[1]) assert.Equal(t, replacedVals[2], v.MustFloat32Slice()[2]) - } func TestWhereFloat32(t *testing.T) { - v := &Value{data: []float32{float32(1), float32(1), float32(1), float32(1), float32(1), float32(1)}} selected := v.WhereFloat32(func(i int, val float32) bool { @@ -2375,11 +2026,9 @@ func TestWhereFloat32(t *testing.T) { }).MustFloat32Slice() assert.Equal(t, 3, len(selected)) - } func TestGroupFloat32(t *testing.T) { - v := &Value{data: []float32{float32(1), float32(1), float32(1), float32(1), float32(1), float32(1)}} grouped := v.GroupFloat32(func(i int, val float32) string { @@ -2389,11 +2038,9 @@ func TestGroupFloat32(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceFloat32(t *testing.T) { - v := &Value{data: []float32{float32(1), float32(1), float32(1), float32(1), float32(1), float32(1)}} rawArr := v.MustFloat32Slice() @@ -2414,11 +2061,9 @@ func TestReplaceFloat32(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectFloat32(t *testing.T) { - v := &Value{data: []float32{float32(1), float32(1), float32(1), float32(1), float32(1), float32(1)}} collected := v.CollectFloat32(func(index int, val float32) interface{} { @@ -2434,83 +2079,68 @@ func TestCollectFloat32(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Float64 (float64 and []float64) +*/ func TestFloat64(t *testing.T) { - val := float64(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Float64()) assert.Equal(t, val, New(m).Get("value").MustFloat64()) assert.Equal(t, float64(0), New(m).Get("nothing").Float64()) assert.Equal(t, val, New(m).Get("nothing").Float64(1)) - assert.Panics(t, func() { New(m).Get("age").MustFloat64() }) - } func TestFloat64Slice(t *testing.T) { - val := float64(1) + m := map[string]interface{}{"value": []float64{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Float64Slice()[0]) assert.Equal(t, val, New(m).Get("value").MustFloat64Slice()[0]) assert.Equal(t, []float64(nil), New(m).Get("nothing").Float64Slice()) assert.Equal(t, val, New(m).Get("nothing").Float64Slice([]float64{float64(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustFloat64Slice() }) - } func TestIsFloat64(t *testing.T) { - - var v *Value - - v = &Value{data: float64(1)} + v := &Value{data: float64(1)} assert.True(t, v.IsFloat64()) - v = &Value{data: []float64{float64(1)}} - assert.True(t, v.IsFloat64Slice()) +} +func TestIsFloat64Slice(t *testing.T) { + v := &Value{data: []float64{float64(1)}} + assert.True(t, v.IsFloat64Slice()) } func TestEachFloat64(t *testing.T) { - v := &Value{data: []float64{float64(1), float64(1), float64(1), float64(1), float64(1)}} count := 0 replacedVals := make([]float64, 0) assert.Equal(t, v, v.EachFloat64(func(i int, val float64) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustFloat64Slice()[0]) assert.Equal(t, replacedVals[1], v.MustFloat64Slice()[1]) assert.Equal(t, replacedVals[2], v.MustFloat64Slice()[2]) - } func TestWhereFloat64(t *testing.T) { - v := &Value{data: []float64{float64(1), float64(1), float64(1), float64(1), float64(1), float64(1)}} selected := v.WhereFloat64(func(i int, val float64) bool { @@ -2518,11 +2148,9 @@ func TestWhereFloat64(t *testing.T) { }).MustFloat64Slice() assert.Equal(t, 3, len(selected)) - } func TestGroupFloat64(t *testing.T) { - v := &Value{data: []float64{float64(1), float64(1), float64(1), float64(1), float64(1), float64(1)}} grouped := v.GroupFloat64(func(i int, val float64) string { @@ -2532,11 +2160,9 @@ func TestGroupFloat64(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceFloat64(t *testing.T) { - v := &Value{data: []float64{float64(1), float64(1), float64(1), float64(1), float64(1), float64(1)}} rawArr := v.MustFloat64Slice() @@ -2557,11 +2183,9 @@ func TestReplaceFloat64(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectFloat64(t *testing.T) { - v := &Value{data: []float64{float64(1), float64(1), float64(1), float64(1), float64(1), float64(1)}} collected := v.CollectFloat64(func(index int, val float64) interface{} { @@ -2577,83 +2201,68 @@ func TestCollectFloat64(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Complex64 (complex64 and []complex64) +*/ func TestComplex64(t *testing.T) { - val := complex64(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Complex64()) assert.Equal(t, val, New(m).Get("value").MustComplex64()) assert.Equal(t, complex64(0), New(m).Get("nothing").Complex64()) assert.Equal(t, val, New(m).Get("nothing").Complex64(1)) - assert.Panics(t, func() { New(m).Get("age").MustComplex64() }) - } func TestComplex64Slice(t *testing.T) { - val := complex64(1) + m := map[string]interface{}{"value": []complex64{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Complex64Slice()[0]) assert.Equal(t, val, New(m).Get("value").MustComplex64Slice()[0]) assert.Equal(t, []complex64(nil), New(m).Get("nothing").Complex64Slice()) assert.Equal(t, val, New(m).Get("nothing").Complex64Slice([]complex64{complex64(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustComplex64Slice() }) - } func TestIsComplex64(t *testing.T) { - - var v *Value - - v = &Value{data: complex64(1)} + v := &Value{data: complex64(1)} assert.True(t, v.IsComplex64()) - v = &Value{data: []complex64{complex64(1)}} - assert.True(t, v.IsComplex64Slice()) +} +func TestIsComplex64Slice(t *testing.T) { + v := &Value{data: []complex64{complex64(1)}} + assert.True(t, v.IsComplex64Slice()) } func TestEachComplex64(t *testing.T) { - v := &Value{data: []complex64{complex64(1), complex64(1), complex64(1), complex64(1), complex64(1)}} count := 0 replacedVals := make([]complex64, 0) assert.Equal(t, v, v.EachComplex64(func(i int, val complex64) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustComplex64Slice()[0]) assert.Equal(t, replacedVals[1], v.MustComplex64Slice()[1]) assert.Equal(t, replacedVals[2], v.MustComplex64Slice()[2]) - } func TestWhereComplex64(t *testing.T) { - v := &Value{data: []complex64{complex64(1), complex64(1), complex64(1), complex64(1), complex64(1), complex64(1)}} selected := v.WhereComplex64(func(i int, val complex64) bool { @@ -2661,11 +2270,9 @@ func TestWhereComplex64(t *testing.T) { }).MustComplex64Slice() assert.Equal(t, 3, len(selected)) - } func TestGroupComplex64(t *testing.T) { - v := &Value{data: []complex64{complex64(1), complex64(1), complex64(1), complex64(1), complex64(1), complex64(1)}} grouped := v.GroupComplex64(func(i int, val complex64) string { @@ -2675,11 +2282,9 @@ func TestGroupComplex64(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceComplex64(t *testing.T) { - v := &Value{data: []complex64{complex64(1), complex64(1), complex64(1), complex64(1), complex64(1), complex64(1)}} rawArr := v.MustComplex64Slice() @@ -2700,11 +2305,9 @@ func TestReplaceComplex64(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectComplex64(t *testing.T) { - v := &Value{data: []complex64{complex64(1), complex64(1), complex64(1), complex64(1), complex64(1), complex64(1)}} collected := v.CollectComplex64(func(index int, val complex64) interface{} { @@ -2720,83 +2323,68 @@ func TestCollectComplex64(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } -// ************************************************************ -// TESTS -// ************************************************************ +/* + Tests for Complex128 (complex128 and []complex128) +*/ func TestComplex128(t *testing.T) { - val := complex128(1) + m := map[string]interface{}{"value": val, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Complex128()) assert.Equal(t, val, New(m).Get("value").MustComplex128()) assert.Equal(t, complex128(0), New(m).Get("nothing").Complex128()) assert.Equal(t, val, New(m).Get("nothing").Complex128(1)) - assert.Panics(t, func() { New(m).Get("age").MustComplex128() }) - } func TestComplex128Slice(t *testing.T) { - val := complex128(1) + m := map[string]interface{}{"value": []complex128{val}, "nothing": nil} assert.Equal(t, val, New(m).Get("value").Complex128Slice()[0]) assert.Equal(t, val, New(m).Get("value").MustComplex128Slice()[0]) assert.Equal(t, []complex128(nil), New(m).Get("nothing").Complex128Slice()) assert.Equal(t, val, New(m).Get("nothing").Complex128Slice([]complex128{complex128(1)})[0]) - assert.Panics(t, func() { New(m).Get("nothing").MustComplex128Slice() }) - } func TestIsComplex128(t *testing.T) { - - var v *Value - - v = &Value{data: complex128(1)} + v := &Value{data: complex128(1)} assert.True(t, v.IsComplex128()) - v = &Value{data: []complex128{complex128(1)}} - assert.True(t, v.IsComplex128Slice()) +} +func TestIsComplex128Slice(t *testing.T) { + v := &Value{data: []complex128{complex128(1)}} + assert.True(t, v.IsComplex128Slice()) } func TestEachComplex128(t *testing.T) { - v := &Value{data: []complex128{complex128(1), complex128(1), complex128(1), complex128(1), complex128(1)}} count := 0 replacedVals := make([]complex128, 0) assert.Equal(t, v, v.EachComplex128(func(i int, val complex128) bool { - count++ replacedVals = append(replacedVals, val) // abort early - if i == 2 { - return false - } - - return true - + return i != 2 })) assert.Equal(t, count, 3) assert.Equal(t, replacedVals[0], v.MustComplex128Slice()[0]) assert.Equal(t, replacedVals[1], v.MustComplex128Slice()[1]) assert.Equal(t, replacedVals[2], v.MustComplex128Slice()[2]) - } func TestWhereComplex128(t *testing.T) { - v := &Value{data: []complex128{complex128(1), complex128(1), complex128(1), complex128(1), complex128(1), complex128(1)}} selected := v.WhereComplex128(func(i int, val complex128) bool { @@ -2804,11 +2392,9 @@ func TestWhereComplex128(t *testing.T) { }).MustComplex128Slice() assert.Equal(t, 3, len(selected)) - } func TestGroupComplex128(t *testing.T) { - v := &Value{data: []complex128{complex128(1), complex128(1), complex128(1), complex128(1), complex128(1), complex128(1)}} grouped := v.GroupComplex128(func(i int, val complex128) string { @@ -2818,11 +2404,9 @@ func TestGroupComplex128(t *testing.T) { assert.Equal(t, 2, len(grouped)) assert.Equal(t, 3, len(grouped["true"])) assert.Equal(t, 3, len(grouped["false"])) - } func TestReplaceComplex128(t *testing.T) { - v := &Value{data: []complex128{complex128(1), complex128(1), complex128(1), complex128(1), complex128(1), complex128(1)}} rawArr := v.MustComplex128Slice() @@ -2843,11 +2427,9 @@ func TestReplaceComplex128(t *testing.T) { assert.Equal(t, replacedArr[4], rawArr[5]) assert.Equal(t, replacedArr[5], rawArr[0]) } - } func TestCollectComplex128(t *testing.T) { - v := &Value{data: []complex128{complex128(1), complex128(1), complex128(1), complex128(1), complex128(1), complex128(1)}} collected := v.CollectComplex128(func(index int, val complex128) interface{} { @@ -2863,5 +2445,4 @@ func TestCollectComplex128(t *testing.T) { assert.Equal(t, collectedArr[4], 4) assert.Equal(t, collectedArr[5], 5) } - } diff --git a/value_test.go b/value_test.go index 5214058..41fd3a0 100644 --- a/value_test.go +++ b/value_test.go @@ -1,17 +1,20 @@ package objx import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestStringTypeString(t *testing.T) { m := New(map[string]interface{}{"string": "foo"}) + assert.Equal(t, "foo", m.Get("string").String()) } func TestStringTypeBool(t *testing.T) { m := New(map[string]interface{}{"bool": true}) + assert.Equal(t, "true", m.Get("bool").String()) }