teldrive/internal/utils/utils.go

84 lines
1.3 KiB
Go
Raw Permalink Normal View History

2023-08-07 03:32:46 +08:00
package utils
import (
2023-08-17 23:32:40 +08:00
"os"
"path/filepath"
2023-08-14 04:58:06 +08:00
"regexp"
2023-08-07 03:32:46 +08:00
"strings"
"time"
"reflect"
"unicode"
)
func CamelToPascalCase(input string) string {
var result strings.Builder
upperNext := true
for _, char := range input {
if unicode.IsLetter(char) || unicode.IsDigit(char) {
if upperNext {
result.WriteRune(unicode.ToUpper(char))
upperNext = false
} else {
result.WriteRune(char)
}
} else {
upperNext = true
}
}
return result.String()
}
2023-08-14 04:58:06 +08:00
func CamelToSnake(input string) string {
re := regexp.MustCompile("([a-z0-9])([A-Z])")
snake := re.ReplaceAllString(input, "${1}_${2}")
return strings.ToLower(snake)
}
2023-08-07 03:32:46 +08:00
func GetField(v interface{}, field string) string {
r := reflect.ValueOf(v)
f := reflect.Indirect(r).FieldByName(field)
fieldValue := f.Interface()
switch v := fieldValue.(type) {
case string:
return v
case time.Time:
return v.Format(time.RFC3339)
default:
return ""
}
}
func BoolPointer(b bool) *bool {
return &b
}
2023-08-13 04:15:19 +08:00
func IntPointer(b int) *int {
return &b
}
2023-12-03 03:47:23 +08:00
func Int64Pointer(b int64) *int64 {
return &b
}
2023-08-17 23:32:40 +08:00
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func ExecutableDir() string {
path, _ := os.Executable()
return filepath.Dir(path)
}