2017-06-28 02:56:17 +02:00
|
|
|
package generics
|
|
|
|
|
|
|
|
import (
|
2017-06-29 15:35:42 +02:00
|
|
|
"fmt"
|
2017-06-28 02:56:17 +02:00
|
|
|
"reflect"
|
2017-06-28 19:28:55 +02:00
|
|
|
"strconv"
|
2017-06-28 02:56:17 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var timeType = reflect.TypeOf(time.Now())
|
|
|
|
|
2017-06-30 14:03:55 +02:00
|
|
|
func conversionError(modifier string, value interface{}, typ reflect.Type) error {
|
|
|
|
if modifier != "" {
|
|
|
|
modifier += " "
|
|
|
|
}
|
|
|
|
switch ref := value.(type) {
|
|
|
|
case reflect.Value:
|
|
|
|
value = ref.Interface()
|
|
|
|
}
|
|
|
|
return genericErrorf("can't convert %s%T(%v) to type %s", modifier, value, value, typ)
|
|
|
|
}
|
|
|
|
|
2017-06-28 02:56:17 +02:00
|
|
|
// Convert value to the type. This is a more aggressive conversion, that will
|
|
|
|
// recursively create new map and slice values as necessary. It doesn't
|
|
|
|
// handle circular references.
|
2017-06-30 03:21:52 +02:00
|
|
|
func Convert(value interface{}, target reflect.Type) (interface{}, error) {
|
2017-06-28 02:56:17 +02:00
|
|
|
r := reflect.ValueOf(value)
|
2017-06-30 03:21:52 +02:00
|
|
|
if r.Type().ConvertibleTo(target) {
|
|
|
|
return r.Convert(target).Interface(), nil
|
2017-06-28 02:56:17 +02:00
|
|
|
}
|
2017-06-30 03:21:52 +02:00
|
|
|
if reflect.PtrTo(r.Type()) == target {
|
2017-06-29 14:42:24 +02:00
|
|
|
return &value, nil
|
2017-06-28 02:56:17 +02:00
|
|
|
}
|
2017-06-30 03:21:52 +02:00
|
|
|
if r.Kind() == reflect.String && target == timeType {
|
2017-06-29 14:42:24 +02:00
|
|
|
return ParseTime(value.(string))
|
2017-06-28 02:56:17 +02:00
|
|
|
}
|
2017-06-30 03:21:52 +02:00
|
|
|
switch target.Kind() {
|
2017-06-28 19:28:55 +02:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2017-06-29 15:35:42 +02:00
|
|
|
switch value := value.(type) {
|
|
|
|
case bool:
|
|
|
|
if value {
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
case string:
|
|
|
|
return strconv.Atoi(value)
|
|
|
|
}
|
2017-06-28 19:28:55 +02:00
|
|
|
case reflect.Float32, reflect.Float64:
|
2017-06-29 15:35:42 +02:00
|
|
|
switch value := value.(type) {
|
|
|
|
case int:
|
|
|
|
return float64(value), nil
|
|
|
|
case string:
|
|
|
|
return strconv.ParseFloat(value, 64)
|
|
|
|
}
|
2017-06-30 03:21:52 +02:00
|
|
|
case reflect.Map:
|
|
|
|
out := reflect.MakeMap(target)
|
|
|
|
for _, key := range r.MapKeys() {
|
2017-06-30 14:03:55 +02:00
|
|
|
if target.Key().Kind() == reflect.String {
|
|
|
|
key = reflect.ValueOf(fmt.Sprint(key))
|
|
|
|
}
|
2017-06-30 03:21:52 +02:00
|
|
|
if !key.Type().ConvertibleTo(target.Key()) {
|
2017-06-30 14:03:55 +02:00
|
|
|
return nil, conversionError("map key", key, target.Key())
|
2017-06-30 03:21:52 +02:00
|
|
|
}
|
|
|
|
key = key.Convert(target.Key())
|
|
|
|
value := r.MapIndex(key)
|
2017-06-30 14:03:55 +02:00
|
|
|
if target.Elem().Kind() == reflect.String {
|
|
|
|
value = reflect.ValueOf(fmt.Sprint(value))
|
|
|
|
}
|
|
|
|
if !value.Type().ConvertibleTo(target.Elem()) {
|
|
|
|
return nil, conversionError("map value", value, target.Elem())
|
2017-06-30 03:21:52 +02:00
|
|
|
}
|
|
|
|
out.SetMapIndex(key, value.Convert(target.Elem()))
|
|
|
|
}
|
2017-06-30 14:03:55 +02:00
|
|
|
return out.Interface(), nil
|
2017-06-28 02:56:17 +02:00
|
|
|
case reflect.Slice:
|
|
|
|
if r.Kind() != reflect.Array && r.Kind() != reflect.Slice {
|
|
|
|
break
|
|
|
|
}
|
2017-06-30 03:21:52 +02:00
|
|
|
out := reflect.MakeSlice(target, 0, r.Len())
|
2017-06-28 02:56:17 +02:00
|
|
|
for i := 0; i < r.Len(); i++ {
|
2017-06-30 03:21:52 +02:00
|
|
|
item, err := Convert(r.Index(i).Interface(), target.Elem())
|
2017-06-29 14:42:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = reflect.Append(out, reflect.ValueOf(item))
|
2017-06-28 02:56:17 +02:00
|
|
|
}
|
2017-06-29 14:42:24 +02:00
|
|
|
return out.Interface(), nil
|
2017-06-28 02:56:17 +02:00
|
|
|
}
|
2017-06-30 14:03:55 +02:00
|
|
|
return nil, conversionError("", value, target)
|
2017-06-28 02:56:17 +02:00
|
|
|
}
|
|
|
|
|
2017-06-29 15:35:42 +02:00
|
|
|
// MustConvert wraps Convert, but panics on error.
|
|
|
|
func MustConvert(value interface{}, t reflect.Type) interface{} {
|
|
|
|
out, err := Convert(value, t)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustConvertItem converts item to conform to array, else panics.
|
|
|
|
func MustConvertItem(item interface{}, array []interface{}) interface{} {
|
|
|
|
item, err := Convert(item, reflect.TypeOf(array).Elem())
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("can't convert %#v to %s: %s", item, reflect.TypeOf(array).Elem(), err))
|
|
|
|
}
|
|
|
|
return item
|
|
|
|
}
|