// Copyright © 2014 Steve Francia <spf@spf13.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package cast

import (
	
	
	
	
	
	
	
	
)

var errNegativeNotAllowed = errors.New("unable to cast negative value")

// ToTimeE casts an interface to a time.Time type.
func ( interface{}) ( time.Time,  error) {
	 = indirect()

	switch v := .(type) {
	case time.Time:
		return , nil
	case string:
		return StringToDate()
	case int:
		return time.Unix(int64(), 0), nil
	case int64:
		return time.Unix(, 0), nil
	case int32:
		return time.Unix(int64(), 0), nil
	case uint:
		return time.Unix(int64(), 0), nil
	case uint64:
		return time.Unix(int64(), 0), nil
	case uint32:
		return time.Unix(int64(), 0), nil
	default:
		return time.Time{}, fmt.Errorf("unable to cast %#v of type %T to Time", , )
	}
}

// ToDurationE casts an interface to a time.Duration type.
func ( interface{}) ( time.Duration,  error) {
	 = indirect()

	switch s := .(type) {
	case time.Duration:
		return , nil
	case int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8:
		 = time.Duration(ToInt64())
		return
	case float32, float64:
		 = time.Duration(ToFloat64())
		return
	case string:
		if strings.ContainsAny(, "nsuµmh") {
			,  = time.ParseDuration()
		} else {
			,  = time.ParseDuration( + "ns")
		}
		return
	default:
		 = fmt.Errorf("unable to cast %#v of type %T to Duration", , )
		return
	}
}

// ToBoolE casts an interface to a bool type.
func ( interface{}) (bool, error) {
	 = indirect()

	switch b := .(type) {
	case bool:
		return , nil
	case nil:
		return false, nil
	case int:
		if .(int) != 0 {
			return true, nil
		}
		return false, nil
	case string:
		return strconv.ParseBool(.(string))
	default:
		return false, fmt.Errorf("unable to cast %#v of type %T to bool", , )
	}
}

// ToFloat64E casts an interface to a float64 type.
func ( interface{}) (float64, error) {
	 = indirect()

	switch s := .(type) {
	case float64:
		return , nil
	case float32:
		return float64(), nil
	case int:
		return float64(), nil
	case int64:
		return float64(), nil
	case int32:
		return float64(), nil
	case int16:
		return float64(), nil
	case int8:
		return float64(), nil
	case uint:
		return float64(), nil
	case uint64:
		return float64(), nil
	case uint32:
		return float64(), nil
	case uint16:
		return float64(), nil
	case uint8:
		return float64(), nil
	case string:
		,  := strconv.ParseFloat(, 64)
		if  == nil {
			return , nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to float64", , )
	case bool:
		if  {
			return 1, nil
		}
		return 0, nil
	default:
		return 0, fmt.Errorf("unable to cast %#v of type %T to float64", , )
	}
}

// ToFloat32E casts an interface to a float32 type.
func ( interface{}) (float32, error) {
	 = indirect()

	switch s := .(type) {
	case float64:
		return float32(), nil
	case float32:
		return , nil
	case int:
		return float32(), nil
	case int64:
		return float32(), nil
	case int32:
		return float32(), nil
	case int16:
		return float32(), nil
	case int8:
		return float32(), nil
	case uint:
		return float32(), nil
	case uint64:
		return float32(), nil
	case uint32:
		return float32(), nil
	case uint16:
		return float32(), nil
	case uint8:
		return float32(), nil
	case string:
		,  := strconv.ParseFloat(, 32)
		if  == nil {
			return float32(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to float32", , )
	case bool:
		if  {
			return 1, nil
		}
		return 0, nil
	default:
		return 0, fmt.Errorf("unable to cast %#v of type %T to float32", , )
	}
}

// ToInt64E casts an interface to an int64 type.
func ( interface{}) (int64, error) {
	 = indirect()

	switch s := .(type) {
	case int:
		return int64(), nil
	case int64:
		return , nil
	case int32:
		return int64(), nil
	case int16:
		return int64(), nil
	case int8:
		return int64(), nil
	case uint:
		return int64(), nil
	case uint64:
		return int64(), nil
	case uint32:
		return int64(), nil
	case uint16:
		return int64(), nil
	case uint8:
		return int64(), nil
	case float64:
		return int64(), nil
	case float32:
		return int64(), nil
	case string:
		,  := strconv.ParseInt(, 0, 0)
		if  == nil {
			return , nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to int64", , )
	case bool:
		if  {
			return 1, nil
		}
		return 0, nil
	case nil:
		return 0, nil
	default:
		return 0, fmt.Errorf("unable to cast %#v of type %T to int64", , )
	}
}

// ToInt32E casts an interface to an int32 type.
func ( interface{}) (int32, error) {
	 = indirect()

	switch s := .(type) {
	case int:
		return int32(), nil
	case int64:
		return int32(), nil
	case int32:
		return , nil
	case int16:
		return int32(), nil
	case int8:
		return int32(), nil
	case uint:
		return int32(), nil
	case uint64:
		return int32(), nil
	case uint32:
		return int32(), nil
	case uint16:
		return int32(), nil
	case uint8:
		return int32(), nil
	case float64:
		return int32(), nil
	case float32:
		return int32(), nil
	case string:
		,  := strconv.ParseInt(, 0, 0)
		if  == nil {
			return int32(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to int32", , )
	case bool:
		if  {
			return 1, nil
		}
		return 0, nil
	case nil:
		return 0, nil
	default:
		return 0, fmt.Errorf("unable to cast %#v of type %T to int32", , )
	}
}

// ToInt16E casts an interface to an int16 type.
func ( interface{}) (int16, error) {
	 = indirect()

	switch s := .(type) {
	case int:
		return int16(), nil
	case int64:
		return int16(), nil
	case int32:
		return int16(), nil
	case int16:
		return , nil
	case int8:
		return int16(), nil
	case uint:
		return int16(), nil
	case uint64:
		return int16(), nil
	case uint32:
		return int16(), nil
	case uint16:
		return int16(), nil
	case uint8:
		return int16(), nil
	case float64:
		return int16(), nil
	case float32:
		return int16(), nil
	case string:
		,  := strconv.ParseInt(, 0, 0)
		if  == nil {
			return int16(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to int16", , )
	case bool:
		if  {
			return 1, nil
		}
		return 0, nil
	case nil:
		return 0, nil
	default:
		return 0, fmt.Errorf("unable to cast %#v of type %T to int16", , )
	}
}

// ToInt8E casts an interface to an int8 type.
func ( interface{}) (int8, error) {
	 = indirect()

	switch s := .(type) {
	case int:
		return int8(), nil
	case int64:
		return int8(), nil
	case int32:
		return int8(), nil
	case int16:
		return int8(), nil
	case int8:
		return , nil
	case uint:
		return int8(), nil
	case uint64:
		return int8(), nil
	case uint32:
		return int8(), nil
	case uint16:
		return int8(), nil
	case uint8:
		return int8(), nil
	case float64:
		return int8(), nil
	case float32:
		return int8(), nil
	case string:
		,  := strconv.ParseInt(, 0, 0)
		if  == nil {
			return int8(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to int8", , )
	case bool:
		if  {
			return 1, nil
		}
		return 0, nil
	case nil:
		return 0, nil
	default:
		return 0, fmt.Errorf("unable to cast %#v of type %T to int8", , )
	}
}

// ToIntE casts an interface to an int type.
func ( interface{}) (int, error) {
	 = indirect()

	switch s := .(type) {
	case int:
		return , nil
	case int64:
		return int(), nil
	case int32:
		return int(), nil
	case int16:
		return int(), nil
	case int8:
		return int(), nil
	case uint:
		return int(), nil
	case uint64:
		return int(), nil
	case uint32:
		return int(), nil
	case uint16:
		return int(), nil
	case uint8:
		return int(), nil
	case float64:
		return int(), nil
	case float32:
		return int(), nil
	case string:
		,  := strconv.ParseInt(, 0, 0)
		if  == nil {
			return int(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to int", , )
	case bool:
		if  {
			return 1, nil
		}
		return 0, nil
	case nil:
		return 0, nil
	default:
		return 0, fmt.Errorf("unable to cast %#v of type %T to int", , )
	}
}

// ToUintE casts an interface to a uint type.
func ( interface{}) (uint, error) {
	 = indirect()

	switch s := .(type) {
	case string:
		,  := strconv.ParseUint(, 0, 0)
		if  == nil {
			return uint(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v to uint: %s", , )
	case int:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint(), nil
	case int64:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint(), nil
	case int32:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint(), nil
	case int16:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint(), nil
	case int8:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint(), nil
	case uint:
		return , nil
	case uint64:
		return uint(), nil
	case uint32:
		return uint(), nil
	case uint16:
		return uint(), nil
	case uint8:
		return uint(), nil
	case float64:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint(), nil
	case float32:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint(), nil
	case bool:
		if  {
			return 1, nil
		}
		return 0, nil
	case nil:
		return 0, nil
	default:
		return 0, fmt.Errorf("unable to cast %#v of type %T to uint", , )
	}
}

// ToUint64E casts an interface to a uint64 type.
func ( interface{}) (uint64, error) {
	 = indirect()

	switch s := .(type) {
	case string:
		,  := strconv.ParseUint(, 0, 64)
		if  == nil {
			return , nil
		}
		return 0, fmt.Errorf("unable to cast %#v to uint64: %s", , )
	case int:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint64(), nil
	case int64:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint64(), nil
	case int32:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint64(), nil
	case int16:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint64(), nil
	case int8:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint64(), nil
	case uint:
		return uint64(), nil
	case uint64:
		return , nil
	case uint32:
		return uint64(), nil
	case uint16:
		return uint64(), nil
	case uint8:
		return uint64(), nil
	case float32:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint64(), nil
	case float64:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint64(), nil
	case bool:
		if  {
			return 1, nil
		}
		return 0, nil
	case nil:
		return 0, nil
	default:
		return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", , )
	}
}

// ToUint32E casts an interface to a uint32 type.
func ( interface{}) (uint32, error) {
	 = indirect()

	switch s := .(type) {
	case string:
		,  := strconv.ParseUint(, 0, 32)
		if  == nil {
			return uint32(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v to uint32: %s", , )
	case int:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint32(), nil
	case int64:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint32(), nil
	case int32:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint32(), nil
	case int16:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint32(), nil
	case int8:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint32(), nil
	case uint:
		return uint32(), nil
	case uint64:
		return uint32(), nil
	case uint32:
		return , nil
	case uint16:
		return uint32(), nil
	case uint8:
		return uint32(), nil
	case float64:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint32(), nil
	case float32:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint32(), nil
	case bool:
		if  {
			return 1, nil
		}
		return 0, nil
	case nil:
		return 0, nil
	default:
		return 0, fmt.Errorf("unable to cast %#v of type %T to uint32", , )
	}
}

// ToUint16E casts an interface to a uint16 type.
func ( interface{}) (uint16, error) {
	 = indirect()

	switch s := .(type) {
	case string:
		,  := strconv.ParseUint(, 0, 16)
		if  == nil {
			return uint16(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v to uint16: %s", , )
	case int:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint16(), nil
	case int64:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint16(), nil
	case int32:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint16(), nil
	case int16:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint16(), nil
	case int8:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint16(), nil
	case uint:
		return uint16(), nil
	case uint64:
		return uint16(), nil
	case uint32:
		return uint16(), nil
	case uint16:
		return , nil
	case uint8:
		return uint16(), nil
	case float64:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint16(), nil
	case float32:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint16(), nil
	case bool:
		if  {
			return 1, nil
		}
		return 0, nil
	case nil:
		return 0, nil
	default:
		return 0, fmt.Errorf("unable to cast %#v of type %T to uint16", , )
	}
}

// ToUint8E casts an interface to a uint type.
func ( interface{}) (uint8, error) {
	 = indirect()

	switch s := .(type) {
	case string:
		,  := strconv.ParseUint(, 0, 8)
		if  == nil {
			return uint8(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v to uint8: %s", , )
	case int:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint8(), nil
	case int64:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint8(), nil
	case int32:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint8(), nil
	case int16:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint8(), nil
	case int8:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint8(), nil
	case uint:
		return uint8(), nil
	case uint64:
		return uint8(), nil
	case uint32:
		return uint8(), nil
	case uint16:
		return uint8(), nil
	case uint8:
		return , nil
	case float64:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint8(), nil
	case float32:
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint8(), nil
	case bool:
		if  {
			return 1, nil
		}
		return 0, nil
	case nil:
		return 0, nil
	default:
		return 0, fmt.Errorf("unable to cast %#v of type %T to uint8", , )
	}
}

// From html/template/content.go
// Copyright 2011 The Go Authors. All rights reserved.
// indirect returns the value, after dereferencing as many times
// as necessary to reach the base type (or nil).
func indirect( interface{}) interface{} {
	if  == nil {
		return nil
	}
	if  := reflect.TypeOf(); .Kind() != reflect.Ptr {
		// Avoid creating a reflect.Value if it's not a pointer.
		return 
	}
	 := reflect.ValueOf()
	for .Kind() == reflect.Ptr && !.IsNil() {
		 = .Elem()
	}
	return .Interface()
}

// From html/template/content.go
// Copyright 2011 The Go Authors. All rights reserved.
// indirectToStringerOrError returns the value, after dereferencing as many times
// as necessary to reach the base type (or nil) or an implementation of fmt.Stringer
// or error,
func indirectToStringerOrError( interface{}) interface{} {
	if  == nil {
		return nil
	}

	var  = reflect.TypeOf((*error)(nil)).Elem()
	var  = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()

	 := reflect.ValueOf()
	for !.Type().Implements() && !.Type().Implements() && .Kind() == reflect.Ptr && !.IsNil() {
		 = .Elem()
	}
	return .Interface()
}

// ToStringE casts an interface to a string type.
func ( interface{}) (string, error) {
	 = indirectToStringerOrError()

	switch s := .(type) {
	case string:
		return , nil
	case bool:
		return strconv.FormatBool(), nil
	case float64:
		return strconv.FormatFloat(, 'f', -1, 64), nil
	case float32:
		return strconv.FormatFloat(float64(), 'f', -1, 32), nil
	case int:
		return strconv.Itoa(), nil
	case int64:
		return strconv.FormatInt(, 10), nil
	case int32:
		return strconv.Itoa(int()), nil
	case int16:
		return strconv.FormatInt(int64(), 10), nil
	case int8:
		return strconv.FormatInt(int64(), 10), nil
	case uint:
		return strconv.FormatUint(uint64(), 10), nil
	case uint64:
		return strconv.FormatUint(uint64(), 10), nil
	case uint32:
		return strconv.FormatUint(uint64(), 10), nil
	case uint16:
		return strconv.FormatUint(uint64(), 10), nil
	case uint8:
		return strconv.FormatUint(uint64(), 10), nil
	case []byte:
		return string(), nil
	case template.HTML:
		return string(), nil
	case template.URL:
		return string(), nil
	case template.JS:
		return string(), nil
	case template.CSS:
		return string(), nil
	case template.HTMLAttr:
		return string(), nil
	case nil:
		return "", nil
	case fmt.Stringer:
		return .String(), nil
	case error:
		return .Error(), nil
	default:
		return "", fmt.Errorf("unable to cast %#v of type %T to string", , )
	}
}

// ToStringMapStringE casts an interface to a map[string]string type.
func ( interface{}) (map[string]string, error) {
	var  = map[string]string{}

	switch v := .(type) {
	case map[string]string:
		return , nil
	case map[string]interface{}:
		for ,  := range  {
			[ToString()] = ToString()
		}
		return , nil
	case map[interface{}]string:
		for ,  := range  {
			[ToString()] = ToString()
		}
		return , nil
	case map[interface{}]interface{}:
		for ,  := range  {
			[ToString()] = ToString()
		}
		return , nil
	case string:
		 := jsonStringToObject(, &)
		return , 
	default:
		return , fmt.Errorf("unable to cast %#v of type %T to map[string]string", , )
	}
}

// ToStringMapStringSliceE casts an interface to a map[string][]string type.
func ( interface{}) (map[string][]string, error) {
	var  = map[string][]string{}

	switch v := .(type) {
	case map[string][]string:
		return , nil
	case map[string][]interface{}:
		for ,  := range  {
			[ToString()] = ToStringSlice()
		}
		return , nil
	case map[string]string:
		for ,  := range  {
			[ToString()] = []string{}
		}
	case map[string]interface{}:
		for ,  := range  {
			switch vt := .(type) {
			case []interface{}:
				[ToString()] = ToStringSlice()
			case []string:
				[ToString()] = 
			default:
				[ToString()] = []string{ToString()}
			}
		}
		return , nil
	case map[interface{}][]string:
		for ,  := range  {
			[ToString()] = ToStringSlice()
		}
		return , nil
	case map[interface{}]string:
		for ,  := range  {
			[ToString()] = ToStringSlice()
		}
		return , nil
	case map[interface{}][]interface{}:
		for ,  := range  {
			[ToString()] = ToStringSlice()
		}
		return , nil
	case map[interface{}]interface{}:
		for ,  := range  {
			,  := ToStringE()
			if  != nil {
				return , fmt.Errorf("unable to cast %#v of type %T to map[string][]string", , )
			}
			,  := ToStringSliceE()
			if  != nil {
				return , fmt.Errorf("unable to cast %#v of type %T to map[string][]string", , )
			}
			[] = 
		}
	case string:
		 := jsonStringToObject(, &)
		return , 
	default:
		return , fmt.Errorf("unable to cast %#v of type %T to map[string][]string", , )
	}
	return , nil
}

// ToStringMapBoolE casts an interface to a map[string]bool type.
func ( interface{}) (map[string]bool, error) {
	var  = map[string]bool{}

	switch v := .(type) {
	case map[interface{}]interface{}:
		for ,  := range  {
			[ToString()] = ToBool()
		}
		return , nil
	case map[string]interface{}:
		for ,  := range  {
			[ToString()] = ToBool()
		}
		return , nil
	case map[string]bool:
		return , nil
	case string:
		 := jsonStringToObject(, &)
		return , 
	default:
		return , fmt.Errorf("unable to cast %#v of type %T to map[string]bool", , )
	}
}

// ToStringMapE casts an interface to a map[string]interface{} type.
func ( interface{}) (map[string]interface{}, error) {
	var  = map[string]interface{}{}

	switch v := .(type) {
	case map[interface{}]interface{}:
		for ,  := range  {
			[ToString()] = 
		}
		return , nil
	case map[string]interface{}:
		return , nil
	case string:
		 := jsonStringToObject(, &)
		return , 
	default:
		return , fmt.Errorf("unable to cast %#v of type %T to map[string]interface{}", , )
	}
}

// ToStringMapIntE casts an interface to a map[string]int{} type.
func ( interface{}) (map[string]int, error) {
	var  = map[string]int{}
	if  == nil {
		return , fmt.Errorf("unable to cast %#v of type %T to map[string]int", , )
	}

	switch v := .(type) {
	case map[interface{}]interface{}:
		for ,  := range  {
			[ToString()] = ToInt()
		}
		return , nil
	case map[string]interface{}:
		for ,  := range  {
			[] = ToInt()
		}
		return , nil
	case map[string]int:
		return , nil
	case string:
		 := jsonStringToObject(, &)
		return , 
	}

	if reflect.TypeOf().Kind() != reflect.Map {
		return , fmt.Errorf("unable to cast %#v of type %T to map[string]int", , )
	}

	 := reflect.ValueOf()
	 := reflect.ValueOf()
	for ,  := range .MapKeys() {
		,  := ToIntE(.MapIndex().Interface())
		if  != nil {
			return , fmt.Errorf("unable to cast %#v of type %T to map[string]int", , )
		}
		.SetMapIndex(, reflect.ValueOf())
	}
	return , nil
}

// ToStringMapInt64E casts an interface to a map[string]int64{} type.
func ( interface{}) (map[string]int64, error) {
	var  = map[string]int64{}
	if  == nil {
		return , fmt.Errorf("unable to cast %#v of type %T to map[string]int64", , )
	}

	switch v := .(type) {
	case map[interface{}]interface{}:
		for ,  := range  {
			[ToString()] = ToInt64()
		}
		return , nil
	case map[string]interface{}:
		for ,  := range  {
			[] = ToInt64()
		}
		return , nil
	case map[string]int64:
		return , nil
	case string:
		 := jsonStringToObject(, &)
		return , 
	}

	if reflect.TypeOf().Kind() != reflect.Map {
		return , fmt.Errorf("unable to cast %#v of type %T to map[string]int64", , )
	}
	 := reflect.ValueOf()
	 := reflect.ValueOf()
	for ,  := range .MapKeys() {
		,  := ToInt64E(.MapIndex().Interface())
		if  != nil {
			return , fmt.Errorf("unable to cast %#v of type %T to map[string]int64", , )
		}
		.SetMapIndex(, reflect.ValueOf())
	}
	return , nil
}

// ToSliceE casts an interface to a []interface{} type.
func ( interface{}) ([]interface{}, error) {
	var  []interface{}

	switch v := .(type) {
	case []interface{}:
		return append(, ...), nil
	case []map[string]interface{}:
		for ,  := range  {
			 = append(, )
		}
		return , nil
	default:
		return , fmt.Errorf("unable to cast %#v of type %T to []interface{}", , )
	}
}

// ToBoolSliceE casts an interface to a []bool type.
func ( interface{}) ([]bool, error) {
	if  == nil {
		return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", , )
	}

	switch v := .(type) {
	case []bool:
		return , nil
	}

	 := reflect.TypeOf().Kind()
	switch  {
	case reflect.Slice, reflect.Array:
		 := reflect.ValueOf()
		 := make([]bool, .Len())
		for  := 0;  < .Len(); ++ {
			,  := ToBoolE(.Index().Interface())
			if  != nil {
				return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", , )
			}
			[] = 
		}
		return , nil
	default:
		return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", , )
	}
}

// ToStringSliceE casts an interface to a []string type.
func ( interface{}) ([]string, error) {
	var  []string

	switch v := .(type) {
	case []interface{}:
		for ,  := range  {
			 = append(, ToString())
		}
		return , nil
	case []string:
		return , nil
	case string:
		return strings.Fields(), nil
	case interface{}:
		,  := ToStringE()
		if  != nil {
			return , fmt.Errorf("unable to cast %#v of type %T to []string", , )
		}
		return []string{}, nil
	default:
		return , fmt.Errorf("unable to cast %#v of type %T to []string", , )
	}
}

// ToIntSliceE casts an interface to a []int type.
func ( interface{}) ([]int, error) {
	if  == nil {
		return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", , )
	}

	switch v := .(type) {
	case []int:
		return , nil
	}

	 := reflect.TypeOf().Kind()
	switch  {
	case reflect.Slice, reflect.Array:
		 := reflect.ValueOf()
		 := make([]int, .Len())
		for  := 0;  < .Len(); ++ {
			,  := ToIntE(.Index().Interface())
			if  != nil {
				return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", , )
			}
			[] = 
		}
		return , nil
	default:
		return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", , )
	}
}

// ToDurationSliceE casts an interface to a []time.Duration type.
func ( interface{}) ([]time.Duration, error) {
	if  == nil {
		return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", , )
	}

	switch v := .(type) {
	case []time.Duration:
		return , nil
	}

	 := reflect.TypeOf().Kind()
	switch  {
	case reflect.Slice, reflect.Array:
		 := reflect.ValueOf()
		 := make([]time.Duration, .Len())
		for  := 0;  < .Len(); ++ {
			,  := ToDurationE(.Index().Interface())
			if  != nil {
				return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", , )
			}
			[] = 
		}
		return , nil
	default:
		return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", , )
	}
}

// StringToDate attempts to parse a string into a time.Time type using a
// predefined list of formats.  If no suitable format is found, an error is
// returned.
func ( string) (time.Time, error) {
	return parseDateWith(, []string{
		time.RFC3339,
		"2006-01-02T15:04:05", // iso8601 without timezone
		time.RFC1123Z,
		time.RFC1123,
		time.RFC822Z,
		time.RFC822,
		time.RFC850,
		time.ANSIC,
		time.UnixDate,
		time.RubyDate,
		"2006-01-02 15:04:05.999999999 -0700 MST", // Time.String()
		"2006-01-02",
		"02 Jan 2006",
		"2006-01-02T15:04:05-0700", // RFC3339 without timezone hh:mm colon
		"2006-01-02 15:04:05 -07:00",
		"2006-01-02 15:04:05 -0700",
		"2006-01-02 15:04:05Z07:00", // RFC3339 without T
		"2006-01-02 15:04:05Z0700",  // RFC3339 without T or timezone hh:mm colon
		"2006-01-02 15:04:05",
		time.Kitchen,
		time.Stamp,
		time.StampMilli,
		time.StampMicro,
		time.StampNano,
	})
}

func parseDateWith( string,  []string) ( time.Time,  error) {
	for ,  := range  {
		if ,  = time.Parse(, );  == nil {
			return
		}
	}
	return , fmt.Errorf("unable to parse date: %s", )
}

// jsonStringToObject attempts to unmarshall a string as JSON into
// the object passed as pointer.
func jsonStringToObject( string,  interface{}) error {
	 := []byte()
	return json.Unmarshal(, )
}