Skip to content

Commit 6ffbd30

Browse files
committed
Fix map function's validation of output return type
1 parent af02e76 commit 6ffbd30

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

map.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ import (
55
"reflect"
66
)
77

8-
// Map applies mapperFn on each item of in and puts it in out
8+
// Map applies mapperFn on each item of in and puts it in out.
9+
// Currently, input and output for type slice is supported.
10+
//
11+
// Validations:
12+
// 1. Mapper function should take in one argument and return one argument
13+
// 2. Mapper function's argument should be of the same type of each element of input slice.
14+
// 3. Mapper function's output should be of the same type of each element of output slice.
15+
//
16+
// Validation failures are returned as error by the godash.Map to the caller.
917
func Map(in, out, mapperFn interface{}) error {
1018
input := reflect.ValueOf(in)
1119
output := reflect.ValueOf(out)
@@ -28,14 +36,14 @@ func Map(in, out, mapperFn interface{}) error {
2836
}
2937

3038
if input.Kind() == reflect.Slice {
31-
if output.Kind() != reflect.Slice {
39+
if output.Elem().Kind() != reflect.Slice {
3240
return fmt.Errorf("output should be a slice for input of type slice")
3341
}
34-
if input.Type().Elem().Kind() != mapper.Type().In(0).Kind() {
35-
return fmt.Errorf("mapper function's first argument has to be the type of element of input slice")
42+
if input.Type().Elem() != mapper.Type().In(0) {
43+
return fmt.Errorf("mapper function's first argument (%s) has to be (%s)", mapper.Type().In(0), input.Type().Elem())
3644
}
37-
if output.Type().Elem().Elem().Kind() != mapper.Type().Out(0).Kind() {
38-
return fmt.Errorf("mapper function's return type has to be the type of element of output slice")
45+
if output.Elem().Type().Elem() != mapper.Type().Out(0) {
46+
return fmt.Errorf("mapper function's return type has to be (%s) but is (%s)", mapper.Type().Out(0), output.Elem().Type().Elem())
3947
}
4048

4149
result := reflect.MakeSlice(output.Elem().Type(), 0, input.Len())

map_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package godash_test
22

33
import (
4+
"fmt"
45
"github.com/stretchr/testify/assert"
56
"github.com/thecasualcoder/godash"
67
"testing"
@@ -116,7 +117,7 @@ func TestMap(t *testing.T) {
116117

117118
{
118119
err := godash.Map(in, &out, func(string) string { return "" })
119-
assert.EqualError(t, err, "mapper function's first argument has to be the type of element of input slice")
120+
assert.EqualError(t, err, "mapper function's first argument (string) has to be (int)")
120121
}
121122

122123
{
@@ -131,7 +132,7 @@ func TestMap(t *testing.T) {
131132

132133
{
133134
err := godash.Map(in, &out, func(int) int { return 0 })
134-
assert.EqualError(t, err, "mapper function's return type has to be the type of element of output slice")
135+
assert.EqualError(t, err, "mapper function's return type has to be (int) but is (string)")
135136
}
136137

137138
{
@@ -140,3 +141,16 @@ func TestMap(t *testing.T) {
140141
}
141142
})
142143
}
144+
145+
func ExampleMap() {
146+
input := []int{0, 1, 2, 3, 4}
147+
var output []string
148+
149+
_ = godash.Map(input, &output, func(num int) string {
150+
return fmt.Sprintf("%d", num*num)
151+
})
152+
153+
fmt.Println(output)
154+
155+
// Output: [0 1 4 9 16]
156+
}

0 commit comments

Comments
 (0)