github.com/chain5j/chain5j-pkg@v1.0.7/util/convutil/cast.go (about) 1 package convutil 2 3 import ( 4 "fmt" 5 "time" 6 ) 7 8 // ToBool casts an interface to a bool type. 9 func ToBool(i interface{}) bool { 10 v, _ := ToBoolE(i) 11 return v 12 } 13 14 // ToTime casts an interface to a time.Time type. 15 func ToTime(i interface{}) time.Time { 16 v, _ := ToTimeE(i) 17 return v 18 } 19 20 // ToDuration casts an interface to a time.Duration type. 21 func ToDuration(i interface{}) time.Duration { 22 v, _ := ToDurationE(i) 23 return v 24 } 25 26 // ToFloat64 casts an interface to a float64 type. 27 func ToFloat64(i interface{}) float64 { 28 v, _ := ToFloat64E(i) 29 return v 30 } 31 32 // ToFloat32 casts an interface to a float32 type. 33 func ToFloat32(i interface{}) float32 { 34 v, _ := ToFloat32E(i) 35 return v 36 } 37 38 // ToInt64 casts an interface to an int64 type. 39 func ToInt64(i interface{}) int64 { 40 v, _ := ToInt64E(i) 41 return v 42 } 43 44 // ToInt32 casts an interface to an int32 type. 45 func ToInt32(i interface{}) int32 { 46 v, _ := ToInt32E(i) 47 return v 48 } 49 50 // ToInt16 casts an interface to an int16 type. 51 func ToInt16(i interface{}) int16 { 52 v, _ := ToInt16E(i) 53 return v 54 } 55 56 // ToInt8 casts an interface to an int8 type. 57 func ToInt8(i interface{}) int8 { 58 v, _ := ToInt8E(i) 59 return v 60 } 61 62 // ToInt casts an interface to an int type. 63 func ToInt(i interface{}) int { 64 v, _ := ToIntE(i) 65 return v 66 } 67 68 // ToUint casts an interface to a uint type. 69 func ToUint(i interface{}) uint { 70 v, _ := ToUintE(i) 71 return v 72 } 73 74 // ToUint64 casts an interface to a uint64 type. 75 func ToUint64(i interface{}) uint64 { 76 v, _ := ToUint64E(i) 77 return v 78 } 79 80 // ToUint32 casts an interface to a uint32 type. 81 func ToUint32(i interface{}) uint32 { 82 v, _ := ToUint32E(i) 83 return v 84 } 85 86 // ToUint16 casts an interface to a uint16 type. 87 func ToUint16(i interface{}) uint16 { 88 v, _ := ToUint16E(i) 89 return v 90 } 91 92 // ToUint8 casts an interface to a uint8 type. 93 func ToUint8(i interface{}) uint8 { 94 v, _ := ToUint8E(i) 95 return v 96 } 97 98 // ToString casts an interface to a string type. 99 func ToString(i interface{}) string { 100 v, _ := ToStringE(i) 101 return v 102 } 103 104 // ToStringMapString casts an interface to a map[string]string type. 105 func ToStringMapString(i interface{}) map[string]string { 106 v, _ := ToStringMapStringE(i) 107 return v 108 } 109 110 // ToStringMapStringSlice casts an interface to a map[string][]string type. 111 func ToStringMapStringSlice(i interface{}) map[string][]string { 112 v, _ := ToStringMapStringSliceE(i) 113 return v 114 } 115 116 // ToStringMapBool casts an interface to a map[string]bool type. 117 func ToStringMapBool(i interface{}) map[string]bool { 118 v, _ := ToStringMapBoolE(i) 119 return v 120 } 121 122 // ToStringMapInt casts an interface to a map[string]int type. 123 func ToStringMapInt(i interface{}) map[string]int { 124 v, _ := ToStringMapIntE(i) 125 return v 126 } 127 128 // ToStringMapInt64 casts an interface to a map[string]int64 type. 129 func ToStringMapInt64(i interface{}) map[string]int64 { 130 v, _ := ToStringMapInt64E(i) 131 return v 132 } 133 134 // ToStringMap casts an interface to a map[string]interface{} type. 135 func ToStringMap(i interface{}) map[string]interface{} { 136 v, _ := ToStringMapE(i) 137 return v 138 } 139 140 // ToSlice casts an interface to a []interface{} type. 141 func ToSlice(i interface{}) []interface{} { 142 v, _ := ToSliceE(i) 143 return v 144 } 145 146 // ToBoolSlice casts an interface to a []bool type. 147 func ToBoolSlice(i interface{}) []bool { 148 v, _ := ToBoolSliceE(i) 149 return v 150 } 151 152 // ToStringSlice casts an interface to a []string type. 153 func ToStringSlice(i interface{}) []string { 154 v, _ := ToStringSliceE(i) 155 return v 156 } 157 158 // ToIntSlice casts an interface to a []int type. 159 func ToIntSlice(i interface{}) []int { 160 v, _ := ToIntSliceE(i) 161 return v 162 } 163 164 // ToDurationSlice casts an interface to a []time.Duration type. 165 func ToDurationSlice(i interface{}) []time.Duration { 166 v, _ := ToDurationSliceE(i) 167 return v 168 } 169 170 func ToInterface(typeStr string, s string) (interface{}, error) { 171 switch typeStr { 172 case "string": 173 return s, nil 174 case "int": 175 return ToIntE(s) 176 case "int8": 177 return ToInt8E(s) 178 case "int16": 179 return ToInt16E(s) 180 case "int32": 181 return ToInt32E(s) 182 case "int64": 183 return ToInt64E(s) 184 case "uint": 185 return ToUintE(s) 186 case "uint8": 187 return ToUint8E(s) 188 case "uint16": 189 return ToUint16E(s) 190 case "uint32": 191 return ToUint32E(s) 192 case "uint64": 193 return ToUint64E(s) 194 case "float32": 195 return ToFloat32E(s) 196 case "float64": 197 return ToFloat64E(s) 198 case "bool": 199 return ToBoolE(s) 200 case "slice": 201 return ToSliceE(s) 202 case "map[string]interface{}": 203 return ToStringMapE(s) 204 case "map[string]string": 205 return ToStringMapStringE(s) 206 case "map[string]int": 207 return ToStringMapIntE(s) 208 case "map[string]int64": 209 return ToStringMapInt64E(s) 210 case "map[string]bool": 211 return ToStringMapBoolE(s) 212 case "[]string": 213 return ToStringSliceE(s) 214 case "[]int": 215 return ToIntSliceE(s) 216 case "[]bool": 217 return ToBoolSliceE(s) 218 default: 219 return nil, fmt.Errorf("unsupprot the type:%s", typeStr) 220 } 221 }