2017-07-28 00:11:37 +02:00
|
|
|
package values
|
2017-06-28 02:56:17 +02:00
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2017-07-05 20:24:15 +02:00
|
|
|
// A TypeError is an error during type conversion.
|
|
|
|
type TypeError string
|
|
|
|
|
|
|
|
func (e TypeError) Error() string { return string(e) }
|
2017-07-06 01:09:59 +02:00
|
|
|
|
|
|
|
func typeErrorf(format string, a ...interface{}) TypeError {
|
2017-07-05 20:24:15 +02:00
|
|
|
return TypeError(fmt.Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
|
2017-06-28 02:56:17 +02:00
|
|
|
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()
|
|
|
|
}
|
2017-07-06 01:09:59 +02:00
|
|
|
return typeErrorf("can't convert %s%T(%v) to type %s", modifier, value, value, typ)
|
2017-06-30 14:03:55 +02:00
|
|
|
}
|
|
|
|
|
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-07-08 15:08:06 +02:00
|
|
|
func Convert(value interface{}, typ reflect.Type) (interface{}, error) { // nolint: gocyclo
|
2017-07-03 18:00:43 +02:00
|
|
|
value = ToLiquid(value)
|
2017-06-28 02:56:17 +02:00
|
|
|
r := reflect.ValueOf(value)
|
2017-07-14 20:28:02 +02:00
|
|
|
// int.Convert(string) returns "\x01" not "1", so guard against that in the following test
|
|
|
|
if typ.Kind() != reflect.String && value != nil && r.Type().ConvertibleTo(typ) {
|
2017-07-08 15:08:06 +02:00
|
|
|
return r.Convert(typ).Interface(), nil
|
2017-07-14 20:28:02 +02:00
|
|
|
}
|
|
|
|
if typ == timeType && r.Kind() == reflect.String {
|
2017-07-10 20:33:29 +02:00
|
|
|
return ParseDate(value.(string))
|
2017-06-28 02:56:17 +02:00
|
|
|
}
|
2017-07-14 20:28:02 +02:00
|
|
|
// currently unused:
|
|
|
|
// case reflect.PtrTo(r.Type()) == typ:
|
|
|
|
// return &value, nil
|
|
|
|
// }
|
2017-07-08 15:08:06 +02:00
|
|
|
switch typ.Kind() {
|
2017-06-30 14:41:47 +02:00
|
|
|
case reflect.Bool:
|
|
|
|
return !(value == nil || value == false), nil
|
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) {
|
2017-07-14 20:28:02 +02:00
|
|
|
// case int is handled by r.Convert(type) above
|
2017-06-29 15:35:42 +02:00
|
|
|
case string:
|
|
|
|
return strconv.ParseFloat(value, 64)
|
|
|
|
}
|
2017-06-30 03:21:52 +02:00
|
|
|
case reflect.Map:
|
2017-07-08 15:08:06 +02:00
|
|
|
out := reflect.MakeMap(typ)
|
2017-06-30 03:21:52 +02:00
|
|
|
for _, key := range r.MapKeys() {
|
2017-07-08 15:08:06 +02:00
|
|
|
if typ.Key().Kind() == reflect.String {
|
2017-06-30 14:03:55 +02:00
|
|
|
key = reflect.ValueOf(fmt.Sprint(key))
|
|
|
|
}
|
2017-07-08 15:08:06 +02:00
|
|
|
if !key.Type().ConvertibleTo(typ.Key()) {
|
|
|
|
return nil, conversionError("map key", key, typ.Key())
|
2017-06-30 03:21:52 +02:00
|
|
|
}
|
2017-07-08 15:08:06 +02:00
|
|
|
key = key.Convert(typ.Key())
|
2017-06-30 03:21:52 +02:00
|
|
|
value := r.MapIndex(key)
|
2017-07-08 15:08:06 +02:00
|
|
|
if typ.Elem().Kind() == reflect.String {
|
2017-06-30 14:03:55 +02:00
|
|
|
value = reflect.ValueOf(fmt.Sprint(value))
|
|
|
|
}
|
2017-07-08 15:08:06 +02:00
|
|
|
if !value.Type().ConvertibleTo(typ.Elem()) {
|
|
|
|
return nil, conversionError("map value", value, typ.Elem())
|
2017-06-30 03:21:52 +02:00
|
|
|
}
|
2017-07-08 15:08:06 +02:00
|
|
|
out.SetMapIndex(key, value.Convert(typ.Elem()))
|
2017-06-30 03:21:52 +02:00
|
|
|
}
|
2017-06-30 14:03:55 +02:00
|
|
|
return out.Interface(), nil
|
2017-06-28 02:56:17 +02:00
|
|
|
case reflect.Slice:
|
2017-06-30 14:41:47 +02:00
|
|
|
switch r.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
2017-07-08 15:08:06 +02:00
|
|
|
out := reflect.MakeSlice(typ, 0, r.Len())
|
2017-06-30 14:41:47 +02:00
|
|
|
for i := 0; i < r.Len(); i++ {
|
2017-07-08 15:08:06 +02:00
|
|
|
item, err := Convert(r.Index(i).Interface(), typ.Elem())
|
2017-06-30 14:41:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = reflect.Append(out, reflect.ValueOf(item))
|
|
|
|
}
|
|
|
|
return out.Interface(), nil
|
|
|
|
case reflect.Map:
|
2017-07-08 15:08:06 +02:00
|
|
|
out := reflect.MakeSlice(typ, 0, r.Len())
|
2017-06-30 14:41:47 +02:00
|
|
|
for _, key := range r.MapKeys() {
|
2017-07-08 15:08:06 +02:00
|
|
|
item, err := Convert(r.MapIndex(key).Interface(), typ.Elem())
|
2017-06-30 14:41:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = reflect.Append(out, reflect.ValueOf(item))
|
2017-06-29 14:42:24 +02:00
|
|
|
}
|
2017-07-01 16:36:47 +02:00
|
|
|
return out.Interface(), nil
|
2017-06-28 02:56:17 +02:00
|
|
|
}
|
2017-07-02 13:51:24 +02:00
|
|
|
case reflect.String:
|
2017-07-23 17:49:09 +02:00
|
|
|
switch value := value.(type) {
|
|
|
|
case []byte:
|
|
|
|
return string(value), nil
|
|
|
|
case fmt.Stringer:
|
|
|
|
return value.String(), nil
|
|
|
|
default:
|
|
|
|
return fmt.Sprint(value), nil
|
|
|
|
}
|
2017-06-28 02:56:17 +02:00
|
|
|
}
|
2017-07-08 15:08:06 +02:00
|
|
|
return nil, conversionError("", value, typ)
|
2017-06-28 02:56:17 +02:00
|
|
|
}
|
|
|
|
|
2017-07-01 16:36:47 +02:00
|
|
|
// MustConvert is like Convert, but panics if conversion fails.
|
2017-06-29 15:35:42 +02:00
|
|
|
func MustConvert(value interface{}, t reflect.Type) interface{} {
|
|
|
|
out, err := Convert(value, t)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2017-07-03 18:00:43 +02:00
|
|
|
// MustConvertItem converts item to conform to the type array's element, else panics.
|
2017-07-19 00:37:28 +02:00
|
|
|
// Unlike MustConvert, the second argument is a value not a type.
|
|
|
|
func MustConvertItem(item interface{}, array interface{}) interface{} {
|
2017-06-29 15:35:42 +02:00
|
|
|
item, err := Convert(item, reflect.TypeOf(array).Elem())
|
|
|
|
if err != nil {
|
2017-07-06 01:09:59 +02:00
|
|
|
panic(typeErrorf("can't convert %#v to %s: %s", item, reflect.TypeOf(array).Elem(), err))
|
2017-06-29 15:35:42 +02:00
|
|
|
}
|
|
|
|
return item
|
|
|
|
}
|