vendor/github.com/spf13/cast/caste.go
changeset 251 1c52a0eeb952
parent 242 2a9ec03fe5a1
child 260 445e01aede7e
equal deleted inserted replaced
250:c040f992052f 251:1c52a0eeb952
   817 	case int16:
   817 	case int16:
   818 		return strconv.FormatInt(int64(s), 10), nil
   818 		return strconv.FormatInt(int64(s), 10), nil
   819 	case int8:
   819 	case int8:
   820 		return strconv.FormatInt(int64(s), 10), nil
   820 		return strconv.FormatInt(int64(s), 10), nil
   821 	case uint:
   821 	case uint:
   822 		return strconv.FormatInt(int64(s), 10), nil
   822 		return strconv.FormatUint(uint64(s), 10), nil
   823 	case uint64:
   823 	case uint64:
   824 		return strconv.FormatInt(int64(s), 10), nil
   824 		return strconv.FormatUint(uint64(s), 10), nil
   825 	case uint32:
   825 	case uint32:
   826 		return strconv.FormatInt(int64(s), 10), nil
   826 		return strconv.FormatUint(uint64(s), 10), nil
   827 	case uint16:
   827 	case uint16:
   828 		return strconv.FormatInt(int64(s), 10), nil
   828 		return strconv.FormatUint(uint64(s), 10), nil
   829 	case uint8:
   829 	case uint8:
   830 		return strconv.FormatInt(int64(s), 10), nil
   830 		return strconv.FormatUint(uint64(s), 10), nil
   831 	case []byte:
   831 	case []byte:
   832 		return string(s), nil
   832 		return string(s), nil
   833 	case template.HTML:
   833 	case template.HTML:
   834 		return string(s), nil
   834 		return string(s), nil
   835 	case template.URL:
   835 	case template.URL:
   988 	default:
   988 	default:
   989 		return m, fmt.Errorf("unable to cast %#v of type %T to map[string]interface{}", i, i)
   989 		return m, fmt.Errorf("unable to cast %#v of type %T to map[string]interface{}", i, i)
   990 	}
   990 	}
   991 }
   991 }
   992 
   992 
       
   993 // ToStringMapIntE casts an interface to a map[string]int{} type.
       
   994 func ToStringMapIntE(i interface{}) (map[string]int, error) {
       
   995 	var m = map[string]int{}
       
   996 	if i == nil {
       
   997 		return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i)
       
   998 	}
       
   999 
       
  1000 	switch v := i.(type) {
       
  1001 	case map[interface{}]interface{}:
       
  1002 		for k, val := range v {
       
  1003 			m[ToString(k)] = ToInt(val)
       
  1004 		}
       
  1005 		return m, nil
       
  1006 	case map[string]interface{}:
       
  1007 		for k, val := range v {
       
  1008 			m[k] = ToInt(val)
       
  1009 		}
       
  1010 		return m, nil
       
  1011 	case map[string]int:
       
  1012 		return v, nil
       
  1013 	case string:
       
  1014 		err := jsonStringToObject(v, &m)
       
  1015 		return m, err
       
  1016 	}
       
  1017 
       
  1018 	if reflect.TypeOf(i).Kind() != reflect.Map {
       
  1019 		return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i)
       
  1020 	}
       
  1021 
       
  1022 	mVal := reflect.ValueOf(m)
       
  1023 	v := reflect.ValueOf(i)
       
  1024 	for _, keyVal := range v.MapKeys() {
       
  1025 		val, err := ToIntE(v.MapIndex(keyVal).Interface())
       
  1026 		if err != nil {
       
  1027 			return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i)
       
  1028 		}
       
  1029 		mVal.SetMapIndex(keyVal, reflect.ValueOf(val))
       
  1030 	}
       
  1031 	return m, nil
       
  1032 }
       
  1033 
       
  1034 // ToStringMapInt64E casts an interface to a map[string]int64{} type.
       
  1035 func ToStringMapInt64E(i interface{}) (map[string]int64, error) {
       
  1036 	var m = map[string]int64{}
       
  1037 	if i == nil {
       
  1038 		return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i)
       
  1039 	}
       
  1040 
       
  1041 	switch v := i.(type) {
       
  1042 	case map[interface{}]interface{}:
       
  1043 		for k, val := range v {
       
  1044 			m[ToString(k)] = ToInt64(val)
       
  1045 		}
       
  1046 		return m, nil
       
  1047 	case map[string]interface{}:
       
  1048 		for k, val := range v {
       
  1049 			m[k] = ToInt64(val)
       
  1050 		}
       
  1051 		return m, nil
       
  1052 	case map[string]int64:
       
  1053 		return v, nil
       
  1054 	case string:
       
  1055 		err := jsonStringToObject(v, &m)
       
  1056 		return m, err
       
  1057 	}
       
  1058 
       
  1059 	if reflect.TypeOf(i).Kind() != reflect.Map {
       
  1060 		return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i)
       
  1061 	}
       
  1062 	mVal := reflect.ValueOf(m)
       
  1063 	v := reflect.ValueOf(i)
       
  1064 	for _, keyVal := range v.MapKeys() {
       
  1065 		val, err := ToInt64E(v.MapIndex(keyVal).Interface())
       
  1066 		if err != nil {
       
  1067 			return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i)
       
  1068 		}
       
  1069 		mVal.SetMapIndex(keyVal, reflect.ValueOf(val))
       
  1070 	}
       
  1071 	return m, nil
       
  1072 }
       
  1073 
   993 // ToSliceE casts an interface to a []interface{} type.
  1074 // ToSliceE casts an interface to a []interface{} type.
   994 func ToSliceE(i interface{}) ([]interface{}, error) {
  1075 func ToSliceE(i interface{}) ([]interface{}, error) {
   995 	var s []interface{}
  1076 	var s []interface{}
   996 
  1077 
   997 	switch v := i.(type) {
  1078 	switch v := i.(type) {
  1135 		time.UnixDate,
  1216 		time.UnixDate,
  1136 		time.RubyDate,
  1217 		time.RubyDate,
  1137 		"2006-01-02 15:04:05.999999999 -0700 MST", // Time.String()
  1218 		"2006-01-02 15:04:05.999999999 -0700 MST", // Time.String()
  1138 		"2006-01-02",
  1219 		"2006-01-02",
  1139 		"02 Jan 2006",
  1220 		"02 Jan 2006",
       
  1221 		"2006-01-02T15:04:05-0700", // RFC3339 without timezone hh:mm colon
  1140 		"2006-01-02 15:04:05 -07:00",
  1222 		"2006-01-02 15:04:05 -07:00",
  1141 		"2006-01-02 15:04:05 -0700",
  1223 		"2006-01-02 15:04:05 -0700",
  1142 		"2006-01-02 15:04:05Z07:00", // RFC3339 without T
  1224 		"2006-01-02 15:04:05Z07:00", // RFC3339 without T
       
  1225 		"2006-01-02 15:04:05Z0700",  // RFC3339 without T or timezone hh:mm colon
  1143 		"2006-01-02 15:04:05",
  1226 		"2006-01-02 15:04:05",
  1144 		time.Kitchen,
  1227 		time.Kitchen,
  1145 		time.Stamp,
  1228 		time.Stamp,
  1146 		time.StampMilli,
  1229 		time.StampMilli,
  1147 		time.StampMicro,
  1230 		time.StampMicro,