github.com/webx-top/com@v1.2.12/transform.go (about) 1 /* 2 3 Copyright 2016 Wenhui Shen <www.webx.top> 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16 17 */ 18 19 package com 20 21 import ( 22 "fmt" 23 "log" 24 "math" 25 "strconv" 26 "strings" 27 ) 28 29 func Int64(i interface{}) int64 { 30 switch v := i.(type) { 31 case int64: 32 return v 33 case int32: 34 return int64(v) 35 case uint32: 36 return int64(v) 37 case int: 38 return int64(v) 39 case uint: 40 return int64(v) 41 case float32: 42 return int64(v) 43 case float64: 44 return int64(v) 45 case string: 46 out, _ := strconv.ParseInt(v, 10, 64) 47 return out 48 case nil: 49 return 0 50 default: 51 in := fmt.Sprint(i) 52 if len(in) == 0 { 53 return 0 54 } 55 out, err := strconv.ParseInt(in, 10, 64) 56 if err != nil { 57 log.Printf("string[%s] covert int64 fail. %s", in, err) 58 return 0 59 } 60 return out 61 } 62 } 63 64 func Int(i interface{}) int { 65 switch v := i.(type) { 66 case int32: 67 return int(v) 68 case int: 69 return v 70 case float32: 71 return int(v) 72 case float64: 73 return int(v) 74 case string: 75 out, _ := strconv.Atoi(v) 76 return out 77 case nil: 78 return 0 79 default: 80 in := fmt.Sprint(i) 81 if len(in) == 0 { 82 return 0 83 } 84 out, err := strconv.Atoi(in) 85 if err != nil { 86 log.Printf("string[%s] covert int fail. %s", in, err) 87 return 0 88 } 89 return out 90 } 91 } 92 93 func Int32(i interface{}) int32 { 94 switch v := i.(type) { 95 case int32: 96 return v 97 case float32: 98 return int32(v) 99 case float64: 100 return int32(v) 101 case string: 102 out, _ := strconv.ParseInt(v, 10, 32) 103 return int32(out) 104 case nil: 105 return 0 106 default: 107 in := fmt.Sprint(i) 108 if len(in) == 0 { 109 return 0 110 } 111 out, err := strconv.ParseInt(in, 10, 32) 112 if err != nil { 113 log.Printf("string[%s] covert int32 fail. %s", in, err) 114 return 0 115 } 116 return int32(out) 117 } 118 } 119 120 func Uint64(i interface{}) uint64 { 121 switch v := i.(type) { 122 case uint32: 123 return uint64(v) 124 case uint: 125 return uint64(v) 126 case uint64: 127 return v 128 case float32: 129 if v > 0 && v <= math.MaxUint64 { 130 return uint64(v) 131 } 132 return 0 133 case float64: 134 if v > 0 && v <= math.MaxUint64 { 135 return uint64(v) 136 } 137 return 0 138 case string: 139 out, _ := strconv.ParseUint(v, 10, 64) 140 return out 141 case nil: 142 return 0 143 default: 144 in := fmt.Sprint(i) 145 if len(in) == 0 { 146 return 0 147 } 148 out, err := strconv.ParseUint(in, 10, 64) 149 if err != nil { 150 log.Printf("string[%s] covert uint64 fail. %s", in, err) 151 return 0 152 } 153 return out 154 } 155 } 156 157 func Uint(i interface{}) uint { 158 switch v := i.(type) { 159 case uint32: 160 return uint(v) 161 case uint: 162 return v 163 case float32: 164 if v > 0 { 165 return uint(v) 166 } 167 return 0 168 case float64: 169 if v > 0 { 170 return uint(v) 171 } 172 return 0 173 case string: 174 out, _ := strconv.ParseUint(v, 10, 0) 175 return uint(out) 176 case nil: 177 return 0 178 default: 179 in := fmt.Sprint(i) 180 if len(in) == 0 { 181 return 0 182 } 183 out, err := strconv.ParseUint(in, 10, 0) 184 if err != nil { 185 log.Printf("string[%s] covert uint fail. %s", in, err) 186 return 0 187 } 188 return uint(out) 189 } 190 } 191 192 func Uint32(i interface{}) uint32 { 193 switch v := i.(type) { 194 case uint32: 195 return v 196 case uint: 197 return uint32(v) 198 case uint64: 199 return uint32(v) 200 case float32: 201 if v > 0 { 202 return uint32(v) 203 } 204 return 0 205 case float64: 206 if v > 0 { 207 return uint32(v) 208 } 209 return 0 210 case string: 211 out, _ := strconv.ParseUint(v, 10, 32) 212 return uint32(out) 213 case nil: 214 return 0 215 default: 216 in := fmt.Sprint(i) 217 if len(in) == 0 { 218 return 0 219 } 220 out, err := strconv.ParseUint(in, 10, 32) 221 if err != nil { 222 log.Printf("string[%s] covert uint32 fail. %s", in, err) 223 return 0 224 } 225 return uint32(out) 226 } 227 } 228 229 func Float32(i interface{}) float32 { 230 switch v := i.(type) { 231 case float32: 232 return v 233 case float64: 234 if v > math.MaxFloat32 { 235 return 0 236 } 237 return float32(v) 238 case int8: 239 return float32(v) 240 case uint8: 241 return float32(v) 242 case int16: 243 return float32(v) 244 case uint16: 245 return float32(v) 246 case int32: 247 return float32(v) 248 case uint32: 249 return float32(v) 250 case int: 251 return float32(v) 252 case uint: 253 return float32(v) 254 case int64: 255 return float32(v) 256 case uint64: 257 return float32(v) 258 case string: 259 out, _ := strconv.ParseFloat(v, 32) 260 return float32(out) 261 case nil: 262 return 0 263 default: 264 in := fmt.Sprint(i) 265 if len(in) == 0 { 266 return 0 267 } 268 out, err := strconv.ParseFloat(in, 32) 269 if err != nil { 270 log.Printf("string[%s] covert float32 fail. %s", in, err) 271 return 0 272 } 273 return float32(out) 274 } 275 } 276 277 func Float64(i interface{}) float64 { 278 switch v := i.(type) { 279 case float32: 280 return float64(v) 281 case float64: 282 return v 283 case int8: 284 return float64(v) 285 case uint8: 286 return float64(v) 287 case int16: 288 return float64(v) 289 case uint16: 290 return float64(v) 291 case int32: 292 return float64(v) 293 case uint32: 294 return float64(v) 295 case int: 296 return float64(v) 297 case uint: 298 return float64(v) 299 case int64: 300 return float64(v) 301 case uint64: 302 return float64(v) 303 case string: 304 out, _ := strconv.ParseFloat(v, 64) 305 return out 306 case nil: 307 return 0 308 default: 309 in := fmt.Sprint(i) 310 if len(in) == 0 { 311 return 0 312 } 313 out, err := strconv.ParseFloat(in, 64) 314 if err != nil { 315 log.Printf("string[%s] covert float64 fail. %s", in, err) 316 return 0 317 } 318 return out 319 } 320 } 321 322 func Bool(i interface{}) bool { 323 switch v := i.(type) { 324 case bool: 325 return v 326 case nil: 327 return false 328 case string: 329 if len(v) == 0 { 330 return false 331 } 332 out, err := strconv.ParseBool(v) 333 if err != nil { 334 log.Printf("string[%s] covert bool fail. %s", v, err) 335 return false 336 } 337 return out 338 default: 339 in := fmt.Sprint(i) 340 if len(in) == 0 { 341 return false 342 } 343 out, err := strconv.ParseBool(in) 344 if err != nil { 345 log.Printf("string[%s] covert bool fail. %s", in, err) 346 return false 347 } 348 return out 349 } 350 } 351 352 func Str(i interface{}) string { 353 return ToStr(i) 354 } 355 356 func String(v interface{}) string { 357 return Str(v) 358 } 359 360 // SeekRangeNumbers 遍历范围数值,支持设置步进值。格式例如:1-2,2-3:2 361 func SeekRangeNumbers(expr string, fn func(int) bool) { 362 expa := strings.SplitN(expr, ":", 2) 363 step := 1 364 switch len(expa) { 365 case 2: 366 if i, e := strconv.Atoi(strings.TrimSpace(expa[1])); e == nil { 367 step = i 368 } 369 fallthrough 370 case 1: 371 for _, exp := range strings.Split(strings.TrimSpace(expa[0]), `,`) { 372 exp = strings.TrimSpace(exp) 373 if len(exp) == 0 { 374 continue 375 } 376 expb := strings.SplitN(exp, `-`, 2) 377 var minN, maxN int 378 switch len(expb) { 379 case 2: 380 maxN, _ = strconv.Atoi(strings.TrimSpace(expb[1])) 381 fallthrough 382 case 1: 383 minN, _ = strconv.Atoi(strings.TrimSpace(expb[0])) 384 } 385 if maxN == 0 { 386 if !fn(minN) { 387 return 388 } 389 continue 390 } 391 for ; minN <= maxN; minN += step { 392 if !fn(minN) { 393 return 394 } 395 } 396 } 397 } 398 }