forked from mirror/gorm
71 lines
1.3 KiB
Go
71 lines
1.3 KiB
Go
package utils
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"fmt"
|
|
"path/filepath"
|
|
"reflect"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
var gormSourceDir string
|
|
|
|
func init() {
|
|
_, file, _, _ := runtime.Caller(0)
|
|
gormSourceDir = filepath.Dir(filepath.Dir(file))
|
|
}
|
|
|
|
func FileWithLineNum() string {
|
|
for i := 2; i < 15; i++ {
|
|
_, file, line, ok := runtime.Caller(i)
|
|
|
|
if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
|
|
return fmt.Sprintf("%v:%v", file, line)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func IsChar(c rune) bool {
|
|
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
|
|
}
|
|
|
|
func CheckTruth(val interface{}) bool {
|
|
if v, ok := val.(bool); ok {
|
|
return v
|
|
}
|
|
|
|
if v, ok := val.(string); ok {
|
|
v = strings.ToLower(v)
|
|
return v != "false"
|
|
}
|
|
|
|
return !reflect.ValueOf(val).IsZero()
|
|
}
|
|
|
|
func ToStringKey(values ...interface{}) string {
|
|
results := make([]string, len(values))
|
|
|
|
for idx, value := range values {
|
|
if valuer, ok := value.(driver.Valuer); ok {
|
|
value, _ = valuer.Value()
|
|
}
|
|
|
|
switch v := value.(type) {
|
|
case string:
|
|
results[idx] = v
|
|
case []byte:
|
|
results[idx] = string(v)
|
|
case uint:
|
|
results[idx] = strconv.FormatUint(uint64(v), 10)
|
|
default:
|
|
results[idx] = fmt.Sprint(reflect.Indirect(reflect.ValueOf(v)).Interface())
|
|
}
|
|
}
|
|
|
|
return strings.Join(results, "_")
|
|
}
|