@@ -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.
917func 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 ())
0 commit comments