2023-08-07 03:32:46 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2023-08-17 23:32:40 +08:00
|
|
|
"os"
|
2023-08-26 01:32:05 +08:00
|
|
|
"path/filepath"
|
2023-08-14 04:58:06 +08:00
|
|
|
"regexp"
|
2023-08-07 03:32:46 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"golang.org/x/exp/constraints"
|
|
|
|
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Max[T constraints.Ordered](a, b T) T {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-09-24 04:26:04 +08:00
|
|
|
func Min[T constraints.Ordered](a, b T) T {
|
|
|
|
if a > b {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-08-07 03:32:46 +08:00
|
|
|
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-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
|
|
|
|
}
|
2023-08-26 01:32:05 +08:00
|
|
|
|
|
|
|
func getExecutableDir() string {
|
|
|
|
|
|
|
|
path, _ := os.Executable()
|
|
|
|
|
|
|
|
executableDir := filepath.Dir(path)
|
|
|
|
|
|
|
|
return executableDir
|
|
|
|
}
|