123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package utils
- import (
- "database/sql/driver"
- "fmt"
- "reflect"
- "regexp"
- "runtime"
- "strconv"
- "strings"
- "unicode"
- )
- var gormSourceDir string
- func init() {
- _, file, _, _ := runtime.Caller(0)
- gormSourceDir = regexp.MustCompile(`utils.utils\.go`).ReplaceAllString(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 file + ":" + strconv.FormatInt(int64(line), 10)
- }
- }
- return ""
- }
- func IsValidDBNameChar(c rune) bool {
- return !unicode.IsLetter(c) && !unicode.IsNumber(c) && c != '.' && c != '*' && c != '_' && c != '$' && 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, "_")
- }
- func AssertEqual(src, dst interface{}) bool {
- if !reflect.DeepEqual(src, dst) {
- if valuer, ok := src.(driver.Valuer); ok {
- src, _ = valuer.Value()
- }
- if valuer, ok := dst.(driver.Valuer); ok {
- dst, _ = valuer.Value()
- }
- return reflect.DeepEqual(src, dst)
- }
- return true
- }
- func ToString(value interface{}) string {
- switch v := value.(type) {
- case string:
- return v
- case int:
- return strconv.FormatInt(int64(v), 10)
- case int8:
- return strconv.FormatInt(int64(v), 10)
- case int16:
- return strconv.FormatInt(int64(v), 10)
- case int32:
- return strconv.FormatInt(int64(v), 10)
- case int64:
- return strconv.FormatInt(v, 10)
- case uint:
- return strconv.FormatUint(uint64(v), 10)
- case uint8:
- return strconv.FormatUint(uint64(v), 10)
- case uint16:
- return strconv.FormatUint(uint64(v), 10)
- case uint32:
- return strconv.FormatUint(uint64(v), 10)
- case uint64:
- return strconv.FormatUint(v, 10)
- }
- return ""
- }
|