@@ -5,7 +5,15 @@ import (
5
5
"reflect"
6
6
)
7
7
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.
9
17
func Map (in , out , mapperFn interface {}) error {
10
18
input := reflect .ValueOf (in )
11
19
output := reflect .ValueOf (out )
@@ -28,14 +36,14 @@ func Map(in, out, mapperFn interface{}) error {
28
36
}
29
37
30
38
if input .Kind () == reflect .Slice {
31
- if output .Kind () != reflect .Slice {
39
+ if output .Elem (). Kind () != reflect .Slice {
32
40
return fmt .Errorf ("output should be a slice for input of type slice" )
33
41
}
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 () )
36
44
}
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 () )
39
47
}
40
48
41
49
result := reflect .MakeSlice (output .Elem ().Type (), 0 , input .Len ())
0 commit comments