// 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")

type float64EProvider interface {
	Float64() (float64, error)
}

type float64Provider interface {
	Float64() float64
}

// ToTimeE casts an interface to a time.Time type.
func ( interface{}) ( time.Time,  error) {
	return ToTimeInDefaultLocationE(, time.UTC)
}

// ToTimeInDefaultLocationE casts an empty interface to time.Time,
// interpreting inputs without a timezone to be in the given location,
// or the local timezone if nil.
func ( interface{},  *time.Location) ( time.Time,  error) {
	 = indirect()

	switch v := .(type) {
	case time.Time:
		return , nil
	case string:
		return StringToDateInDefaultLocation(, )
	case json.Number:
		,  := ToInt64E()
		if  != nil {
			return time.Time{}, fmt.Errorf("unable to cast %#v of type %T to Time", , )
		}
		return time.Unix(, 0), nil
	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
	case float64EProvider:
		var  float64
		,  = .Float64()
		 = time.Duration()
		return
	case float64Provider:
		 = time.Duration(.Float64())
		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:
		return  != 0, nil
	case int64:
		return  != 0, nil
	case int32:
		return  != 0, nil
	case int16:
		return  != 0, nil
	case int8:
		return  != 0, nil
	case uint:
		return  != 0, nil
	case uint64:
		return  != 0, nil
	case uint32:
		return  != 0, nil
	case uint16:
		return  != 0, nil
	case uint8:
		return  != 0, nil
	case float64:
		return  != 0, nil
	case float32:
		return  != 0, nil
	case time.Duration:
		return  != 0, nil
	case string:
		return strconv.ParseBool(.(string))
	case json.Number:
		,  := ToInt64E()
		if  == nil {
			return  != 0, nil
		}
		return false, fmt.Errorf("unable to cast %#v of type %T to bool", , )
	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()

	,  := toInt()
	if  {
		return float64(), nil
	}

	switch s := .(type) {
	case float64:
		return , nil
	case float32:
		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 float64EProvider:
		,  := .Float64()
		if  == nil {
			return , nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to float64", , )
	case float64Provider:
		return .Float64(), 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 float64", , )
	}
}

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

	,  := toInt()
	if  {
		return float32(), nil
	}

	switch s := .(type) {
	case float64:
		return float32(), nil
	case float32:
		return , 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 float64EProvider:
		,  := .Float64()
		if  == nil {
			return float32(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to float32", , )
	case float64Provider:
		return float32(.Float64()), 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 float32", , )
	}
}

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

	,  := toInt()
	if  {
		return int64(), nil
	}

	switch s := .(type) {
	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(trimZeroDecimal(), 0, 0)
		if  == nil {
			return , nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to int64", , )
	case json.Number:
		return (string())
	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()

	,  := toInt()
	if  {
		return int32(), nil
	}

	switch s := .(type) {
	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(trimZeroDecimal(), 0, 0)
		if  == nil {
			return int32(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to int32", , )
	case json.Number:
		return (string())
	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()

	,  := toInt()
	if  {
		return int16(), nil
	}

	switch s := .(type) {
	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(trimZeroDecimal(), 0, 0)
		if  == nil {
			return int16(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to int16", , )
	case json.Number:
		return (string())
	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()

	,  := toInt()
	if  {
		return int8(), nil
	}

	switch s := .(type) {
	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(trimZeroDecimal(), 0, 0)
		if  == nil {
			return int8(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to int8", , )
	case json.Number:
		return (string())
	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()

	,  := toInt()
	if  {
		return , nil
	}

	switch s := .(type) {
	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(trimZeroDecimal(), 0, 0)
		if  == nil {
			return int(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to int64", , )
	case json.Number:
		return (string())
	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()

	,  := toInt()
	if  {
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint(), nil
	}

	switch s := .(type) {
	case string:
		,  := strconv.ParseInt(trimZeroDecimal(), 0, 0)
		if  == nil {
			if  < 0 {
				return 0, errNegativeNotAllowed
			}
			return uint(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to uint", , )
	case json.Number:
		return (string())
	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()

	,  := toInt()
	if  {
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint64(), nil
	}

	switch s := .(type) {
	case string:
		,  := strconv.ParseUint(trimZeroDecimal(), 0, 0)
		if  == nil {
			if  < 0 {
				return 0, errNegativeNotAllowed
			}
			return , nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", , )
	case json.Number:
		return (string())
	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()

	,  := toInt()
	if  {
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint32(), nil
	}

	switch s := .(type) {
	case string:
		,  := strconv.ParseInt(trimZeroDecimal(), 0, 0)
		if  == nil {
			if  < 0 {
				return 0, errNegativeNotAllowed
			}
			return uint32(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to uint32", , )
	case json.Number:
		return (string())
	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()

	,  := toInt()
	if  {
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint16(), nil
	}

	switch s := .(type) {
	case string:
		,  := strconv.ParseInt(trimZeroDecimal(), 0, 0)
		if  == nil {
			if  < 0 {
				return 0, errNegativeNotAllowed
			}
			return uint16(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to uint16", , )
	case json.Number:
		return (string())
	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()

	,  := toInt()
	if  {
		if  < 0 {
			return 0, errNegativeNotAllowed
		}
		return uint8(), nil
	}

	switch s := .(type) {
	case string:
		,  := strconv.ParseInt(trimZeroDecimal(), 0, 0)
		if  == nil {
			if  < 0 {
				return 0, errNegativeNotAllowed
			}
			return uint8(), nil
		}
		return 0, fmt.Errorf("unable to cast %#v of type %T to uint8", , )
	case json.Number:
		return (string())
	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
	}

	 := reflect.TypeOf((*error)(nil)).Elem()
	 := 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 json.Number:
		return .String(), 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) {
	 := 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) {
	 := 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) {
	 := 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) {
	 := 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) {
	 := 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) {
	 := 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 []int8:
		for ,  := range  {
			 = append(, ToString())
		}
		return , nil
	case []int:
		for ,  := range  {
			 = append(, ToString())
		}
		return , nil
	case []int32:
		for ,  := range  {
			 = append(, ToString())
		}
		return , nil
	case []int64:
		for ,  := range  {
			 = append(, ToString())
		}
		return , nil
	case []float32:
		for ,  := range  {
			 = append(, ToString())
		}
		return , nil
	case []float64:
		for ,  := range  {
			 = append(, ToString())
		}
		return , nil
	case string:
		return strings.Fields(), nil
	case []error:
		for ,  := range .([]error) {
			 = append(, .Error())
		}
		return , 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(, time.UTC, timeFormats)
}

// StringToDateInDefaultLocation casts an empty interface to a time.Time,
// interpreting inputs without a timezone to be in the given location,
// or the local timezone if nil.
func ( string,  *time.Location) (time.Time, error) {
	return parseDateWith(, , timeFormats)
}

type timeFormatType int

const (
	timeFormatNoTimezone timeFormatType = iota
	timeFormatNamedTimezone
	timeFormatNumericTimezone
	timeFormatNumericAndNamedTimezone
	timeFormatTimeOnly
)

type timeFormat struct {
	format string
	typ    timeFormatType
}

func ( timeFormat) () bool {
	// We don't include the formats with only named timezones, see
	// https://github.com/golang/go/issues/19694#issuecomment-289103522
	return .typ >= timeFormatNumericTimezone && .typ <= timeFormatNumericAndNamedTimezone
}

var timeFormats = []timeFormat{
	// Keep common formats at the top.
	{"2006-01-02", timeFormatNoTimezone},
	{time.RFC3339, timeFormatNumericTimezone},
	{"2006-01-02T15:04:05", timeFormatNoTimezone}, // iso8601 without timezone
	{time.RFC1123Z, timeFormatNumericTimezone},
	{time.RFC1123, timeFormatNamedTimezone},
	{time.RFC822Z, timeFormatNumericTimezone},
	{time.RFC822, timeFormatNamedTimezone},
	{time.RFC850, timeFormatNamedTimezone},
	{"2006-01-02 15:04:05.999999999 -0700 MST", timeFormatNumericAndNamedTimezone}, // Time.String()
	{"2006-01-02T15:04:05-0700", timeFormatNumericTimezone},                        // RFC3339 without timezone hh:mm colon
	{"2006-01-02 15:04:05Z0700", timeFormatNumericTimezone},                        // RFC3339 without T or timezone hh:mm colon
	{"2006-01-02 15:04:05", timeFormatNoTimezone},
	{time.ANSIC, timeFormatNoTimezone},
	{time.UnixDate, timeFormatNamedTimezone},
	{time.RubyDate, timeFormatNumericTimezone},
	{"2006-01-02 15:04:05Z07:00", timeFormatNumericTimezone},
	{"02 Jan 2006", timeFormatNoTimezone},
	{"2006-01-02 15:04:05 -07:00", timeFormatNumericTimezone},
	{"2006-01-02 15:04:05 -0700", timeFormatNumericTimezone},
	{time.Kitchen, timeFormatTimeOnly},
	{time.Stamp, timeFormatTimeOnly},
	{time.StampMilli, timeFormatTimeOnly},
	{time.StampMicro, timeFormatTimeOnly},
	{time.StampNano, timeFormatTimeOnly},
}

func parseDateWith( string,  *time.Location,  []timeFormat) ( time.Time,  error) {
	for ,  := range  {
		if ,  = time.Parse(.format, );  == nil {

			// Some time formats have a zone name, but no offset, so it gets
			// put in that zone name (not the default one passed in to us), but
			// without that zone's offset. So set the location manually.
			if .typ <= timeFormatNamedTimezone {
				if  == nil {
					 = time.Local
				}
				, ,  := .Date()
				, ,  := .Clock()
				 = time.Date(, , , , , , .Nanosecond(), )
			}

			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(, )
}

// toInt returns the int value of v if v or v's underlying type
// is an int.
// Note that this will return false for int64 etc. types.
func toInt( interface{}) (int, bool) {
	switch v := .(type) {
	case int:
		return , true
	case time.Weekday:
		return int(), true
	case time.Month:
		return int(), true
	default:
		return 0, false
	}
}

func trimZeroDecimal( string) string {
	var  bool
	for  := len();  > 0; -- {
		switch [-1] {
		case '.':
			if  {
				return [:-1]
			}
		case '0':
			 = true
		default:
			return 
		}
	}
	return 
}