فهرست منبع

refactor mapping (#782)

Kevin Wan 3 سال پیش
والد
کامیت
9ccb997ed8
1فایلهای تغییر یافته به همراه8 افزوده شده و 7 حذف شده
  1. 8 7
      core/mapping/unmarshaler.go

+ 8 - 7
core/mapping/unmarshaler.go

@@ -496,28 +496,29 @@ func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.
 }
 
 func (u *Unmarshaler) fillSliceValue(slice reflect.Value, index int, baseKind reflect.Kind, value interface{}) error {
+	ithVal := slice.Index(index)
 	switch v := value.(type) {
 	case json.Number:
-		return setValue(baseKind, slice.Index(index), v.String())
+		return setValue(baseKind, ithVal, v.String())
 	default:
 		// don't need to consider the difference between int, int8, int16, int32, int64,
 		// uint, uint8, uint16, uint32, uint64, because they're handled as json.Number.
-
-		if slice.Index(index).Kind() == reflect.Ptr {
-			baseType := Deref(slice.Index(index).Type())
+		if ithVal.Kind() == reflect.Ptr {
+			baseType := Deref(ithVal.Type())
 			if baseType.Kind() != reflect.TypeOf(value).Kind() {
 				return errTypeMismatch
 			}
+
 			target := reflect.New(baseType).Elem()
 			target.Set(reflect.ValueOf(value))
-			slice.Index(index).Set(target.Addr())
+			ithVal.Set(target.Addr())
 			return nil
 		} else {
-			if slice.Index(index).Kind() != reflect.TypeOf(value).Kind() {
+			if ithVal.Kind() != reflect.TypeOf(value).Kind() {
 				return errTypeMismatch
 			}
 
-			slice.Index(index).Set(reflect.ValueOf(value))
+			ithVal.Set(reflect.ValueOf(value))
 			return nil
 		}
 	}