Skip to content

Commit 2bfb72e

Browse files
authored
Merge pull request #9 from MrWormHole/master
Support for *[] to *[] and definitive error messages
2 parents d082b55 + 34c549a commit 2bfb72e

4 files changed

Lines changed: 74 additions & 16 deletions

File tree

convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func ToInt64(value interface{}) (d int64) {
178178
case uint, uint8, uint16, uint32, uint64:
179179
d = int64(val.Uint())
180180
default:
181-
panic(fmt.Errorf("ToInt64 need numeric not `%T`", value))
181+
panic(fmt.Errorf("ToInt64 needs numeric type, not `%T`", value))
182182
}
183183
return
184184
}

mapper.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func SetEnabledMapperStructField(isEnabled bool) {
9393
func Register(obj interface{}) error {
9494
objValue := reflect.ValueOf(obj)
9595
if objValue == ZeroValue {
96-
return errors.New("no exists this value")
96+
return errors.New("obj value does not exist")
9797
}
9898
return registerValue(objValue)
9999
}
@@ -102,7 +102,7 @@ func Register(obj interface{}) error {
102102
func registerValue(objValue reflect.Value) error {
103103
regValue := objValue
104104
if objValue == ZeroValue {
105-
return errors.New("no exists this value")
105+
return errors.New("obj value does not exist")
106106
}
107107

108108
if regValue.Type().Kind() == reflect.Ptr {
@@ -205,10 +205,10 @@ func MapToSlice(fromMap map[string]interface{}, toSlice interface{}) error {
205205
var err error
206206
toValue := reflect.ValueOf(toSlice)
207207
if toValue.Kind() != reflect.Ptr {
208-
return errors.New("toSlice must pointer of slice")
208+
return errors.New("toSlice must be a pointer to a slice")
209209
}
210210
if toValue.IsNil() {
211-
return errors.New("toSlice must not nil pointer")
211+
return errors.New("toSlice must not be a nil pointer")
212212
}
213213

214214
toElemType := reflect.TypeOf(toSlice).Elem().Elem()
@@ -247,10 +247,10 @@ func MapperMapSlice(fromMaps map[string]map[string]interface{}, toSlice interfac
247247
var err error
248248
toValue := reflect.ValueOf(toSlice)
249249
if toValue.Kind() != reflect.Ptr {
250-
return errors.New("toSlice must pointer of slice")
250+
return errors.New("toSlice must be a pointer to a slice")
251251
}
252252
if toValue.IsNil() {
253-
return errors.New("toSlice must not nil pointer")
253+
return errors.New("toSlice must not be a nil pointer")
254254
}
255255

256256
toElemType := reflect.TypeOf(toSlice).Elem().Elem()
@@ -280,10 +280,10 @@ func MapperSlice(fromSlice, toSlice interface{}) error {
280280
var err error
281281
toValue := reflect.ValueOf(toSlice)
282282
if toValue.Kind() != reflect.Ptr {
283-
return errors.New("toSlice must pointer of slice")
283+
return errors.New("toSlice must be a pointer to a slice")
284284
}
285285
if toValue.IsNil() {
286-
return errors.New("toSlice must not nil pointer")
286+
return errors.New("toSlice must not be a nil pointer")
287287
}
288288

289289
elemType := reflect.TypeOf(toSlice).Elem().Elem()

mapper_internal.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,15 @@ func checkIsRegister(objElem reflect.Value) bool {
234234
//convert slice interface{} to []interface{}
235235
func convertToSlice(arr interface{}) []interface{} {
236236
v := reflect.ValueOf(arr)
237-
if v.Kind() != reflect.Slice {
238-
panic("toslice arr not slice")
237+
if v.Kind() == reflect.Ptr {
238+
if v.Elem().Kind() != reflect.Slice {
239+
panic("fromSlice arr is not a pointer to a slice")
240+
}
241+
v = v.Elem()
242+
} else {
243+
if v.Kind() != reflect.Slice {
244+
panic("fromSlice arr is not a slice")
245+
}
239246
}
240247
l := v.Len()
241248
ret := make([]interface{}, l)

mapper_test.go

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package mapper
22

33
import (
4-
"fmt"
54
"reflect"
65
"strconv"
76
"sync"
@@ -121,8 +120,34 @@ func Test_MapperSlice(t *testing.T) {
121120
t.Error(err)
122121
} else {
123122
t.Log(toSlice, len(toSlice))
124-
for _, v := range toSlice {
125-
fmt.Println(v)
123+
for i := 0; i < len(fromSlice); i++ {
124+
if !reflect.DeepEqual(fromSlice[i].Name, toSlice[i].Name) ||
125+
!reflect.DeepEqual(fromSlice[i].Sex, toSlice[i].Sex) ||
126+
!reflect.DeepEqual(fromSlice[i].AA, toSlice[i].BB) {
127+
t.Fail()
128+
}
129+
}
130+
}
131+
}
132+
133+
func Test_MapperSlice2(t *testing.T) {
134+
SetEnabledTypeChecking(true)
135+
var fromSlice []*FromStruct
136+
var toSlice []*ToStruct
137+
for i := 0; i < 10; i++ {
138+
fromSlice = append(fromSlice, &FromStruct{Name: "From" + strconv.Itoa(i), Sex: true, AA: "AA" + strconv.Itoa(i)})
139+
}
140+
err := MapperSlice(&fromSlice, &toSlice)
141+
if err != nil {
142+
t.Error(err)
143+
} else {
144+
t.Log(toSlice, len(toSlice))
145+
for i := 0; i < len(fromSlice); i++ {
146+
if !reflect.DeepEqual(fromSlice[i].Name, toSlice[i].Name) ||
147+
!reflect.DeepEqual(fromSlice[i].Sex, toSlice[i].Sex) ||
148+
!reflect.DeepEqual(fromSlice[i].AA, toSlice[i].BB) {
149+
t.Fail()
150+
}
126151
}
127152
}
128153
}
@@ -139,8 +164,34 @@ func Test_MapperStructSlice(t *testing.T) {
139164
t.Error(err)
140165
} else {
141166
t.Log(toSlice, len(toSlice))
142-
for _, v := range toSlice {
143-
fmt.Println(v)
167+
for i := 0; i < len(fromSlice); i++ {
168+
if !reflect.DeepEqual(fromSlice[i].Name, toSlice[i].Name) ||
169+
!reflect.DeepEqual(fromSlice[i].Sex, toSlice[i].Sex) ||
170+
!reflect.DeepEqual(fromSlice[i].AA, toSlice[i].BB) {
171+
t.Fail()
172+
}
173+
}
174+
}
175+
}
176+
177+
func Test_MapperStructSlice2(t *testing.T) {
178+
SetEnabledTypeChecking(true)
179+
var fromSlice []FromStruct
180+
var toSlice []ToStruct
181+
for i := 0; i < 10; i++ {
182+
fromSlice = append(fromSlice, FromStruct{Name: "From" + strconv.Itoa(i), Sex: true, AA: "AA" + strconv.Itoa(i)})
183+
}
184+
err := MapperSlice(&fromSlice, &toSlice)
185+
if err != nil {
186+
t.Error(err)
187+
} else {
188+
t.Log(toSlice, len(toSlice))
189+
for i := 0; i < len(fromSlice); i++ {
190+
if !reflect.DeepEqual(fromSlice[i].Name, toSlice[i].Name) ||
191+
!reflect.DeepEqual(fromSlice[i].Sex, toSlice[i].Sex) ||
192+
!reflect.DeepEqual(fromSlice[i].AA, toSlice[i].BB) {
193+
t.Fail()
194+
}
144195
}
145196
}
146197
}

0 commit comments

Comments
 (0)