54 lines
860 B
Go
54 lines
860 B
Go
|
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
|
||
|
}
|
||
|
|
||
|
func Pad(numStr string, size int32) string {
|
||
|
for int32(len(numStr)) < size {
|
||
|
numStr = "0" + numStr
|
||
|
}
|
||
|
|
||
|
return numStr
|
||
|
}
|