1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-02 14:37:50 +01:00
liquid/values/convert.go

259 lines
6.9 KiB
Go
Raw Normal View History

2017-07-28 00:11:37 +02:00
package values
import (
2019-12-20 12:51:52 +01:00
"encoding/json"
"fmt"
"reflect"
2017-06-28 19:28:55 +02:00
"strconv"
"time"
2017-08-21 16:30:54 +02:00
yaml "gopkg.in/yaml.v2"
)
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...))
}
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
}
func convertValueToInt(value interface{}, typ reflect.Type) (int64, error) {
switch value := value.(type) {
case bool:
if value {
return 1, nil
}
return 0, nil
case string:
v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return 0, conversionError("", value, typ)
}
return v, nil
2019-12-20 12:51:52 +01:00
case json.Number:
v, err := strconv.ParseInt(value.String(), 10, 64)
if err != nil {
return 0, conversionError("", value, typ)
}
return v, nil
}
return 0, conversionError("", value, typ)
}
func convertValueToFloat(value interface{}, typ reflect.Type) (float64, error) {
switch value := value.(type) {
// case int is handled by rv.Convert(typ) in Convert function
case string:
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, conversionError("", value, typ)
}
return v, nil
2019-12-20 12:51:52 +01:00
case json.Number:
v, err := strconv.ParseFloat(value.String(), 64)
if err != nil {
return 0, conversionError("", value, typ)
}
return v, nil
}
return 0, conversionError("", value, typ)
}
// 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.
func Convert(value interface{}, typ reflect.Type) (interface{}, error) { // nolint: gocyclo
2017-07-03 18:00:43 +02:00
value = ToLiquid(value)
2017-08-21 15:50:29 +02:00
rv := 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
2017-08-21 15:50:29 +02:00
if typ.Kind() != reflect.String && value != nil && rv.Type().ConvertibleTo(typ) {
return rv.Convert(typ).Interface(), nil
2017-07-14 20:28:02 +02:00
}
2017-08-21 15:50:29 +02:00
if typ == timeType && rv.Kind() == reflect.String {
2017-07-10 20:33:29 +02:00
return ParseDate(value.(string))
}
2017-07-14 20:28:02 +02:00
// currently unused:
// case reflect.PtrTo(r.Type()) == typ:
// return &value, nil
// }
switch typ.Kind() {
2017-06-30 14:41:47 +02:00
case reflect.Bool:
return !(value == nil || value == false), nil
case reflect.Uint:
v, err := convertValueToInt(value, typ)
return uint(v), err
case reflect.Uint8:
v, err := convertValueToInt(value, typ)
return uint8(v), err
case reflect.Uint16:
v, err := convertValueToInt(value, typ)
return uint16(v), err
case reflect.Uint32:
v, err := convertValueToInt(value, typ)
return uint32(v), err
case reflect.Uint64:
v, err := convertValueToInt(value, typ)
return uint64(v), err
case reflect.Int:
v, err := convertValueToInt(value, typ)
return int(v), err
case reflect.Int8:
v, err := convertValueToInt(value, typ)
return int8(v), err
case reflect.Int16:
v, err := convertValueToInt(value, typ)
return int16(v), err
case reflect.Int32:
v, err := convertValueToInt(value, typ)
return int32(v), err
case reflect.Int64:
v, err := convertValueToInt(value, typ)
return v, err
case reflect.Float32:
v, err := convertValueToFloat(value, typ)
return float32(v), err
case reflect.Float64:
v, err := convertValueToFloat(value, typ)
return v, err
2017-06-30 03:21:52 +02:00
case reflect.Map:
2017-08-21 16:30:54 +02:00
et := typ.Elem()
2017-08-21 15:50:29 +02:00
result := reflect.MakeMap(typ)
2017-08-21 21:06:35 +02:00
if ms, ok := value.(yaml.MapSlice); ok {
for _, item := range ms {
var k, v reflect.Value
if item.Key == nil {
k = reflect.Zero(typ.Key())
} else {
kc, err := Convert(item.Key, typ.Key())
if err != nil {
return nil, err
}
k = reflect.ValueOf(kc)
}
if item.Value == nil {
v = reflect.Zero(et)
} else {
ec, err := Convert(item.Value, et)
if err != nil {
return nil, err
}
v = reflect.ValueOf(ec)
}
result.SetMapIndex(k, v)
}
return result.Interface(), nil
}
if rv.Kind() != reflect.Map {
return nil, conversionError("", value, typ)
}
2017-08-21 15:50:29 +02:00
for _, key := range rv.MapKeys() {
if typ.Key().Kind() == reflect.String {
2017-06-30 14:03:55 +02:00
key = reflect.ValueOf(fmt.Sprint(key))
}
if !key.Type().ConvertibleTo(typ.Key()) {
return nil, conversionError("map key", key, typ.Key())
2017-06-30 03:21:52 +02:00
}
key = key.Convert(typ.Key())
2017-08-21 16:30:54 +02:00
ev := rv.MapIndex(key)
if et.Kind() == reflect.String {
ev = reflect.ValueOf(fmt.Sprint(ev))
2017-06-30 14:03:55 +02:00
}
2017-08-21 16:30:54 +02:00
if !ev.Type().ConvertibleTo(et) {
2017-08-21 21:06:35 +02:00
return nil, conversionError("map element", ev, et)
2017-06-30 03:21:52 +02:00
}
2017-08-21 16:30:54 +02:00
result.SetMapIndex(key, ev.Convert(et))
2017-06-30 03:21:52 +02:00
}
2017-08-21 15:50:29 +02:00
return result.Interface(), nil
case reflect.Slice:
2017-08-21 16:30:54 +02:00
et := typ.Elem()
if ms, ok := value.(yaml.MapSlice); ok {
result := reflect.MakeSlice(typ, 0, rv.Len())
for _, item := range ms {
if item.Value == nil {
if et.Kind() >= reflect.Array {
ev := reflect.Zero(et)
result = reflect.Append(result, ev.Convert(et))
}
continue
}
ev := reflect.ValueOf(item.Value)
if et.Kind() == reflect.String {
ev = reflect.ValueOf(fmt.Sprint(ev))
}
if !ev.Type().ConvertibleTo(et) {
2017-08-21 21:06:35 +02:00
return nil, conversionError("slice element", ev, et)
2017-08-21 16:30:54 +02:00
}
result = reflect.Append(result, ev.Convert(et))
}
return result.Interface(), nil
}
2017-08-21 15:50:29 +02:00
switch rv.Kind() {
2017-06-30 14:41:47 +02:00
case reflect.Array, reflect.Slice:
2017-08-21 15:50:29 +02:00
result := reflect.MakeSlice(typ, 0, rv.Len())
for i := 0; i < rv.Len(); i++ {
item, err := Convert(rv.Index(i).Interface(), typ.Elem())
2017-06-30 14:41:47 +02:00
if err != nil {
return nil, err
}
2017-08-21 15:50:29 +02:00
result = reflect.Append(result, reflect.ValueOf(item))
2017-06-30 14:41:47 +02:00
}
2017-08-21 15:50:29 +02:00
return result.Interface(), nil
2017-06-30 14:41:47 +02:00
case reflect.Map:
2017-08-21 15:50:29 +02:00
result := reflect.MakeSlice(typ, 0, rv.Len())
for _, key := range rv.MapKeys() {
item, err := Convert(rv.MapIndex(key).Interface(), typ.Elem())
2017-06-30 14:41:47 +02:00
if err != nil {
return nil, err
}
2017-08-21 15:50:29 +02:00
result = reflect.Append(result, reflect.ValueOf(item))
}
2017-08-21 15:50:29 +02:00
return result.Interface(), nil
}
2017-07-02 13:51:24 +02:00
case reflect.String:
switch value := value.(type) {
case []byte:
return string(value), nil
case fmt.Stringer:
return value.String(), nil
default:
return fmt.Sprint(value), nil
}
}
return nil, conversionError("", value, typ)
}
2017-07-01 16:36:47 +02:00
// MustConvert is like Convert, but panics if conversion fails.
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{} {
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))
}
return item
}