github.com/runner-mei/ql@v1.1.0/coerce.go (about) 1 // Copyright 2013 The ql Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // CAUTION: This file was generated automatically by 6 // 7 // $ go run helper/helper.go -o coerce.go 8 // 9 // DO NOT EDIT! 10 11 package ql 12 13 import ( 14 "math" 15 "math/big" 16 "reflect" 17 "time" 18 ) 19 20 func coerce(a, b interface{}) (x, y interface{}) { 21 if reflect.TypeOf(a) == reflect.TypeOf(b) { 22 return a, b 23 } 24 25 switch a.(type) { 26 case idealComplex, idealFloat, idealInt, idealRune, idealUint: 27 switch b.(type) { 28 case idealComplex, idealFloat, idealInt, idealRune, idealUint: 29 x, y = coerce1(a, b), b 30 if reflect.TypeOf(x) == reflect.TypeOf(y) { 31 return 32 } 33 34 return a, coerce1(b, a) 35 default: 36 return coerce1(a, b), b 37 } 38 default: 39 switch b.(type) { 40 case idealComplex, idealFloat, idealInt, idealRune, idealUint: 41 return a, coerce1(b, a) 42 default: 43 return a, b 44 } 45 } 46 } 47 48 func coerce1(inVal, otherVal interface{}) (coercedInVal interface{}) { 49 coercedInVal = inVal 50 if otherVal == nil { 51 return 52 } 53 54 switch x := inVal.(type) { 55 case nil: 56 return 57 case idealComplex: 58 switch otherVal.(type) { 59 //case idealComplex: 60 //case idealFloat: 61 //case idealInt: 62 //case idealRune: 63 //case idealUint: 64 //case bool: 65 case complex64: 66 return complex64(x) 67 case complex128: 68 return complex128(x) 69 //case float32: 70 //case float64: 71 //case int8: 72 //case int16: 73 //case int32: 74 //case int64: 75 //case string: 76 //case uint8: 77 //case uint16: 78 //case uint32: 79 //case uint64: 80 //case *big.Int: 81 //case *big.Rat: 82 //case time.Time: 83 //case time.Duration: 84 } 85 case idealFloat: 86 switch otherVal.(type) { 87 case idealComplex: 88 return idealComplex(complex(float64(x), 0)) 89 case idealFloat: 90 return idealFloat(float64(x)) 91 //case idealInt: 92 //case idealRune: 93 //case idealUint: 94 //case bool: 95 case complex64: 96 return complex64(complex(float32(x), 0)) 97 case complex128: 98 return complex128(complex(float64(x), 0)) 99 case float32: 100 return float32(float64(x)) 101 case float64: 102 return float64(float64(x)) 103 //case int8: 104 //case int16: 105 //case int32: 106 //case int64: 107 //case string: 108 //case uint8: 109 //case uint16: 110 //case uint32: 111 //case uint64: 112 //case *big.Int: 113 case *big.Rat: 114 return big.NewRat(1, 1).SetFloat64(float64(x)) 115 //case time.Time: 116 //case time.Duration: 117 } 118 case idealInt: 119 switch otherVal.(type) { 120 case idealComplex: 121 return idealComplex(complex(float64(x), 0)) 122 case idealFloat: 123 return idealFloat(int64(x)) 124 case idealInt: 125 return idealInt(int64(x)) 126 //case idealRune: 127 case idealUint: 128 if x >= 0 { 129 return idealUint(int64(x)) 130 } 131 //case bool: 132 case complex64: 133 return complex64(complex(float32(x), 0)) 134 case complex128: 135 return complex128(complex(float64(x), 0)) 136 case float32: 137 return float32(int64(x)) 138 case float64: 139 return float64(int64(x)) 140 case int8: 141 if x >= math.MinInt8 && x <= math.MaxInt8 { 142 return int8(int64(x)) 143 } 144 case int16: 145 if x >= math.MinInt16 && x <= math.MaxInt16 { 146 return int16(int64(x)) 147 } 148 case int32: 149 if x >= math.MinInt32 && x <= math.MaxInt32 { 150 return int32(int64(x)) 151 } 152 case int64: 153 return int64(int64(x)) 154 //case string: 155 case uint8: 156 if x >= 0 && x <= math.MaxUint8 { 157 return uint8(int64(x)) 158 } 159 case uint16: 160 if x >= 0 && x <= math.MaxUint16 { 161 return uint16(int64(x)) 162 } 163 case uint32: 164 if x >= 0 && x <= math.MaxUint32 { 165 return uint32(int64(x)) 166 } 167 case uint64: 168 if x >= 0 { 169 return uint64(int64(x)) 170 } 171 case *big.Int: 172 return big.NewInt(int64(x)) 173 case *big.Rat: 174 return big.NewRat(1, 1).SetInt64(int64(x)) 175 //case time.Time: 176 case time.Duration: 177 return time.Duration(int64(x)) 178 } 179 case idealRune: 180 switch otherVal.(type) { 181 case idealComplex: 182 return idealComplex(complex(float64(x), 0)) 183 case idealFloat: 184 return idealFloat(int64(x)) 185 case idealInt: 186 return idealInt(int64(x)) 187 case idealRune: 188 return idealRune(int64(x)) 189 case idealUint: 190 return idealUint(int64(x)) 191 //case bool: 192 case complex64: 193 return complex64(complex(float32(x), 0)) 194 case complex128: 195 return complex128(complex(float64(x), 0)) 196 case float32: 197 return float32(int64(x)) 198 case float64: 199 return float64(int64(x)) 200 case int8: 201 return int8(int64(x)) 202 case int16: 203 return int16(int64(x)) 204 case int32: 205 return int32(int64(x)) 206 case int64: 207 return int64(int64(x)) 208 //case string: 209 case uint8: 210 return uint8(int64(x)) 211 case uint16: 212 return uint16(int64(x)) 213 case uint32: 214 return uint32(int64(x)) 215 case uint64: 216 return uint64(int64(x)) 217 case *big.Int: 218 return big.NewInt(int64(x)) 219 case *big.Rat: 220 return big.NewRat(1, 1).SetInt64(int64(x)) 221 //case time.Time: 222 case time.Duration: 223 return time.Duration(int64(x)) 224 } 225 case idealUint: 226 switch otherVal.(type) { 227 case idealComplex: 228 return idealComplex(complex(float64(x), 0)) 229 case idealFloat: 230 return idealFloat(uint64(x)) 231 case idealInt: 232 if x <= math.MaxInt64 { 233 return idealInt(int64(x)) 234 } 235 //case idealRune: 236 case idealUint: 237 return idealUint(uint64(x)) 238 //case bool: 239 case complex64: 240 return complex64(complex(float32(x), 0)) 241 case complex128: 242 return complex128(complex(float64(x), 0)) 243 case float32: 244 return float32(uint64(x)) 245 case float64: 246 return float64(uint64(x)) 247 case int8: 248 if x <= math.MaxInt8 { 249 return int8(int64(x)) 250 } 251 case int16: 252 if x <= math.MaxInt16 { 253 return int16(int64(x)) 254 } 255 case int32: 256 if x <= math.MaxInt32 { 257 return int32(int64(x)) 258 } 259 case int64: 260 if x <= math.MaxInt64 { 261 return int64(int64(x)) 262 } 263 //case string: 264 case uint8: 265 if x >= 0 && x <= math.MaxUint8 { 266 return uint8(int64(x)) 267 } 268 case uint16: 269 if x >= 0 && x <= math.MaxUint16 { 270 return uint16(int64(x)) 271 } 272 case uint32: 273 if x >= 0 && x <= math.MaxUint32 { 274 return uint32(int64(x)) 275 } 276 case uint64: 277 return uint64(uint64(x)) 278 case *big.Int: 279 return big.NewInt(0).SetUint64(uint64(x)) 280 case *big.Rat: 281 return big.NewRat(1, 1).SetInt(big.NewInt(0).SetUint64(uint64(x))) 282 //case time.Time: 283 case time.Duration: 284 if x <= math.MaxInt64 { 285 return time.Duration(int64(x)) 286 } 287 } 288 } 289 return 290 }