2024-02-22 10:43:39 +07:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const TIME_PARSE_LAYOUT = "2/1/2006"
|
|
|
|
const NilIntVal = int8(0)
|
|
|
|
|
|
|
|
func StringToIntPtr[T int8 | int32 | int64](s string) *T {
|
|
|
|
var val T
|
|
|
|
switch any(val).(type) {
|
|
|
|
case int8, int32, int64:
|
|
|
|
num, err := strconv.ParseInt(s, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch any(val).(type) {
|
|
|
|
case int8:
|
|
|
|
val := int8(num)
|
|
|
|
return any(&val).(*T)
|
|
|
|
case int32:
|
|
|
|
val := int32(num)
|
|
|
|
return any(&val).(*T)
|
|
|
|
case int64:
|
|
|
|
val := int64(num)
|
|
|
|
return any(&val).(*T)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
fmt.Printf("Unsupported type: %s\n", reflect.TypeOf(val).Name())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StringToStringPtr(s string) *string {
|
|
|
|
if s == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
2024-02-23 14:10:40 +07:00
|
|
|
func PadWithZeros(number int, zerosCount int) string {
|
|
|
|
numStr := strconv.Itoa(number)
|
|
|
|
|
|
|
|
padding := ""
|
|
|
|
for i := 0; i < zerosCount; i++ {
|
|
|
|
padding += "0"
|
2024-02-22 10:43:39 +07:00
|
|
|
}
|
|
|
|
|
2024-02-23 14:10:40 +07:00
|
|
|
return padding + numStr
|
2024-02-22 10:43:39 +07:00
|
|
|
}
|