github.com/aldelo/common@v1.5.1/helper-conv.go (about) 1 // Package helper: 2 // common project provides commonly used helper utility functions, custom utility types, and third party package wrappers. 3 // common project helps code reuse, and faster composition of logic without having to delve into commonly recurring code logic and related testings. 4 // common project source directories and brief description: 5 // + /ascii = helper types and/or functions related to ascii manipulations. 6 // + /crypto = helper types and/or functions related to encryption, decryption, hashing, such as rsa, aes, sha, tls etc. 7 // + /csv = helper types and/or functions related to csv file manipulations. 8 // + /rest = helper types and/or functions related to http rest api GET, POST, PUT, DELETE actions invoked from client side. 9 // + /tcp = helper types providing wrapped tcp client and tcp server logic. 10 // - /wrapper = wrappers provides a simpler usage path to third party packages, as well as adding additional enhancements. 11 // - /aws = contains aws sdk helper types and functions. 12 // - /cloudmap = wrapper for aws cloudmap service discovery. 13 // - /dynamodb = wrapper for aws dynamodb data access and manipulations. 14 // - /gin = wrapper for gin-gonic web server, and important middleware, into a ready to use solution. 15 // - /hystrixgo = wrapper for circuit breaker logic. 16 // - /kms = wrapper for aws key management services. 17 // - /mysql = wrapper for mysql + sqlx data access and manipulation. 18 // - /ratelimit = wrapper for ratelimit logic. 19 // - /redis = wrapper for redis data access and manipulation, using go-redis. 20 // - /s3 = wrapper for aws s3 data access and manipulation. 21 // - /ses = wrapper for aws simple email services. 22 // - /sns = wrapper for aws simple notification services. 23 // - /sqlite = wrapper for sqlite + sqlx data access and manipulation. 24 // - /sqlserver = wrapper for sqlserver + sqlx data access and manipulation. 25 // - /sqs = wrapper for aws simple queue services. 26 // - /systemd = wrapper for kardianos service to support systemd, windows, launchd service creations. 27 // - /viper = wrapper for viper config. 28 // - /waf2 = wrapper for aws waf2 (web application firewall v2). 29 // - /xray = wrapper for aws xray distributed tracing. 30 // - /zap = wrapper for zap logging. 31 // 32 // /helper-conv.go = helpers for data conversion operations. 33 // /helper-db.go = helpers for database data type operations. 34 // /helper-emv.go = helpers for emv chip card related operations. 35 // /helper-io.go = helpers for io related operations. 36 // /helper-net.go = helpers for network related operations. 37 // /helper-num.go = helpers for numeric related operations. 38 // /helper-other.go = helpers for misc. uncategorized operations. 39 // /helper-reflect.go = helpers for reflection based operations. 40 // /helper-regex.go = helpers for regular express related operations. 41 // /helper-str.go = helpers for string operations. 42 // /helper-struct.go = helpers for struct related operations. 43 // /helper-time.go = helpers for time related operations. 44 // /helper-uuid.go = helpers for generating globally unique ids. 45 package helper 46 47 /* 48 * Copyright 2020-2023 Aldelo, LP 49 * 50 * Licensed under the Apache License, Version 2.0 (the "License"); 51 * you may not use this file except in compliance with the License. 52 * You may obtain a copy of the License at 53 * 54 * http://www.apache.org/licenses/LICENSE-2.0 55 * 56 * Unless required by applicable law or agreed to in writing, software 57 * distributed under the License is distributed on an "AS IS" BASIS, 58 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 59 * See the License for the specific language governing permissions and 60 * limitations under the License. 61 */ 62 63 import ( 64 "fmt" 65 "math" 66 "reflect" 67 "strconv" 68 "strings" 69 "time" 70 ) 71 72 // Itoa converts integer into string. 73 func Itoa(i int) string { 74 return strconv.Itoa(i) 75 } 76 77 // ItoaZeroBlank converts integer into string, if integer is zero, blank is returned. 78 func ItoaZeroBlank(i int) string { 79 if i == 0 { 80 return "" 81 } else { 82 return strconv.Itoa(i) 83 } 84 } 85 86 // Atoi converts string to integer. 87 func Atoi(s string) int { 88 i, err := strconv.Atoi(s) 89 90 if err != nil { 91 return 0 92 } 93 94 return i 95 } 96 97 // Btoa converts bool to string, true = "true", false = "false". 98 func Btoa(b bool) string { 99 if b == true { 100 return "true" 101 } 102 103 return "false" 104 } 105 106 // Atob converts string to bool. 107 // string values with first char lower cased "t", "y", "1" treated as true, otherwise false. 108 func Atob(s string) bool { 109 if LenTrim(s) == 0 { 110 return false 111 } 112 113 var b bool 114 b = false 115 116 switch strings.ToLower(s[:1]) { 117 case "t": 118 b = true 119 case "y": 120 b = true 121 case "1": 122 b = true 123 case "f": 124 b = false 125 case "n": 126 b = false 127 case "0": 128 b = false 129 } 130 131 return b 132 } 133 134 // IntPtr int pointer from int value. 135 func IntPtr(i int) *int { 136 return &i 137 } 138 139 // IntVal gets int value from int pointer. 140 // if pointer is nil, zero is returned. 141 func IntVal(i *int) int { 142 if i == nil { 143 return 0 144 } else { 145 return *i 146 } 147 } 148 149 // Int32PtrToInt returns 0 if nil, otherwise actual int value. 150 func Int32PtrToInt(n *int) int { 151 if n == nil { 152 return 0 153 } else { 154 return *n 155 } 156 } 157 158 // UintToStr converts from uint to string. 159 func UintToStr(i uint) string { 160 return fmt.Sprintf("%d", i) 161 } 162 163 // UintPtr gets uint pointer from uint value. 164 func UintPtr(i uint) *uint { 165 return &i 166 } 167 168 // UintVal gets uint value from uint pointer, if uint pointer is nil, 0 is returned. 169 func UintVal(i *uint) uint { 170 if i == nil { 171 return 0 172 } else { 173 return *i 174 } 175 } 176 177 // StrToUint converts from string to uint, if string is not a valid uint, 0 is returned. 178 func StrToUint(s string) uint { 179 if v, e := strconv.ParseUint(s, 10, 32); e != nil { 180 return 0 181 } else { 182 return uint(v) 183 } 184 } 185 186 // Int64Ptr gets int64 pointer from int64 value. 187 func Int64Ptr(i int64) *int64 { 188 return &i 189 } 190 191 // Int64Val gets int64 value from int64 pointer, if pointer is nil, 0 is returned. 192 func Int64Val(i *int64) int64 { 193 if i == nil { 194 return 0 195 } else { 196 return *i 197 } 198 } 199 200 // Int64ToString converts int64 into string value. 201 func Int64ToString(n int64) string { 202 return strconv.FormatInt(n, 10) 203 } 204 205 // Int64PtrToInt64 returns 0 if pointer is nil, otherwise actual int64 value. 206 func Int64PtrToInt64(n *int64) int64 { 207 if n == nil { 208 return 0 209 } else { 210 return *n 211 } 212 } 213 214 // UInt64ToString converts uint64 value to string. 215 func UInt64ToString(n uint64) string { 216 return strconv.FormatUint(n, 10) 217 } 218 219 // StrToUint64 converts from string to uint64, if string is not a valid uint64, 0 is returned. 220 func StrToUint64(s string) uint64 { 221 if v, e := strconv.ParseUint(s, 10, 64); e != nil { 222 return 0 223 } else { 224 return v 225 } 226 } 227 228 // StrToInt64 converts from string to int64, if string is not a valid int64, 0 is returned. 229 func StrToInt64(s string) int64 { 230 if v, e := strconv.ParseInt(s, 10, 64); e != nil { 231 return 0 232 } else { 233 return v 234 } 235 } 236 237 // Float32Ptr gets float32 pointer from float32 value. 238 func Float32Ptr(f float32) *float32 { 239 return &f 240 } 241 242 // Float32ToString converts float32 value to string. 243 func Float32ToString(f float32) string { 244 return fmt.Sprintf("%f", f) 245 } 246 247 // Float32ToStringCents converts float32 value into string representing cent values, 2.12 returned as "212". 248 // Since float32 can not be precisely calculated in some cases, use math.Round returns the nearest integer 249 func Float32ToStringCents(f float32) string { 250 centsInt := int(math.Round(math.Abs(float64(f) * 100))) 251 if f >= 0.00 { 252 return strings.ReplaceAll(PadLeft(Itoa(centsInt), 3), " ", "0") 253 } else { 254 return "-" + strings.ReplaceAll(PadLeft(Itoa(centsInt), 3), " ", "0") 255 } 256 } 257 258 // Float64ToIntCents converts float64 value into int, representing cent values, 2.12 returned as 212. 259 // Since float64 can not be precisely calculated in some cases, use math.Round returns the nearest integer 260 func Float64ToIntCents(f float64) int { 261 return int(math.Round(f * 100)) 262 } 263 264 // Float32PtrToFloat32 returns 0 if pointer is nil, otherwise actual float32 value. 265 func Float32PtrToFloat32(f *float32) float32 { 266 if f == nil { 267 return 0.00 268 } else { 269 return *f 270 } 271 } 272 273 // Float64Ptr gets float64 pointer from float64 value. 274 func Float64Ptr(f float64) *float64 { 275 return &f 276 } 277 278 // Float64Val gets float64 value from float64 pointer, if pointer is nil, 0 is returned. 279 func Float64Val(f *float64) float64 { 280 if f == nil { 281 return 0 282 } else { 283 return *f 284 } 285 } 286 287 // Float64ToInt converts from float64 into int, if conversion fails, 0 is returned. 288 func Float64ToInt(f float64) int { 289 if v, b := ParseInt32(FloatToString(f)); !b { 290 return 0 291 } else { 292 return v 293 } 294 } 295 296 // CentsToFloat64 converts int (cents) into float64 value with two decimal value. 297 func CentsToFloat64(i int) float64 { 298 f, _ := ParseFloat64(fmt.Sprintf("%.2f", float64(i)*0.01)) 299 return f 300 } 301 302 // FloatToString converts float64 value into string value. 303 func FloatToString(f float64) string { 304 return fmt.Sprintf("%f", f) 305 } 306 307 // Float64ToString converts float64 value into string value. 308 func Float64ToString(f float64) string { 309 return fmt.Sprintf("%f", f) 310 } 311 312 // Float64PtrToFloat64 returns 0 if nil, otherwise actual float64 value. 313 func Float64PtrToFloat64(f *float64) float64 { 314 if f == nil { 315 return 0.00 316 } else { 317 return *f 318 } 319 } 320 321 // BoolToInt converts bool to int value, true = 1, false = 0. 322 func BoolToInt(b bool) int { 323 if b { 324 return 1 325 } else { 326 return 0 327 } 328 } 329 330 // BoolToString converts bool to string value, true = "true", false = "false". 331 func BoolToString(b bool) string { 332 if b { 333 return "true" 334 } else { 335 return "false" 336 } 337 } 338 339 // BoolPtrToBool converts bool pointer to bool value. 340 func BoolPtrToBool(b *bool) bool { 341 if b == nil { 342 return false 343 } else { 344 return *b 345 } 346 } 347 348 // BoolPtr converts bool value to bool pointer. 349 func BoolPtr(b bool) *bool { 350 return &b 351 } 352 353 // BoolVal gets bool value from bool pointer, if pointer is nil, false is returned. 354 func BoolVal(b *bool) bool { 355 if b == nil { 356 return false 357 } else { 358 return *b 359 } 360 } 361 362 // DatePtrToString formats pointer time.Time to string date format yyyy-mm-dd. 363 func DatePtrToString(t *time.Time) string { 364 if t == nil { 365 return "" 366 } 367 368 return FormatDate(*t) 369 } 370 371 // DateTimePtrToString formats pointer time.Time to string date time format yyyy-mm-dd hh:mm:ss AM/PM. 372 func DateTimePtrToString(t *time.Time) string { 373 if t == nil { 374 return "" 375 } 376 377 return FormatDateTime(*t) 378 } 379 380 // DateTimePtrToDateTime formats pointer time.Time to time.Time struct. 381 func DateTimePtrToDateTime(t *time.Time) time.Time { 382 if t == nil { 383 return time.Time{} 384 } else { 385 return *t 386 } 387 } 388 389 // TimePtr casts Time struct to Time pointer. 390 func TimePtr(t time.Time) *time.Time { 391 return &t 392 } 393 394 // TimeVal gets Time struct from Time pointer, if pointer is nil, a time.Time{} is returned. 395 func TimeVal(t *time.Time) time.Time { 396 if t == nil { 397 return time.Time{} 398 } else { 399 return *t 400 } 401 } 402 403 // DurationPtr casts Duration to Duration pointer. 404 func DurationPtr(d time.Duration) *time.Duration { 405 return &d 406 } 407 408 // DurationVal gets Duration value from Duration pointer, if pointer is nil, 0 is returned. 409 func DurationVal(d *time.Duration) time.Duration { 410 if d == nil { 411 return 0 412 } else { 413 return *d 414 } 415 } 416 417 // StringPtr gets string pointer from string value. 418 func StringPtr(s string) *string { 419 return &s 420 } 421 422 // StringVal gets string value from string pointer, if pointer is nil, blank string is returned. 423 func StringVal(s *string) string { 424 if s == nil { 425 return "" 426 } else { 427 return *s 428 } 429 } 430 431 // StringPtrToString gets string value from string pointer, if pointer is nil, blank string is returned. 432 func StringPtrToString(s *string) string { 433 if s == nil { 434 return "" 435 } else { 436 return *s 437 } 438 } 439 440 // SliceObjectsToSliceInterface converts slice of objects into slice of interfaces. 441 // objectsSlice is received via interface parameter, and is expected to be a Slice, 442 // the slice is enumerated to convert each object within the slice to interface{}, 443 // the final converted slice of interface is returned, if operation failed, nil is returned. 444 func SliceObjectsToSliceInterface(objectsSlice interface{}) (output []interface{}) { 445 if reflect.TypeOf(objectsSlice).Kind() == reflect.Slice { 446 s := reflect.ValueOf(objectsSlice) 447 448 for i := 0; i < s.Len(); i++ { 449 output = append(output, s.Index(i).Interface()) 450 } 451 452 return output 453 } else { 454 return nil 455 } 456 } 457 458 // IntToHex returns HEX string representation of i, in 2 digit blocks. 459 func IntToHex(i int) string { 460 buf := StringToHex(Itoa(i)) 461 462 if len(buf)%2 != 0 { 463 buf = "0" + buf 464 } 465 466 return buf 467 }