2020-01-31 01:35:25 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2020-05-14 08:48:51 +03:00
|
|
|
"database/sql/driver"
|
2020-01-31 01:35:25 +03:00
|
|
|
"fmt"
|
2020-04-16 05:29:18 +03:00
|
|
|
"reflect"
|
2020-06-07 13:27:12 +03:00
|
|
|
"regexp"
|
2020-01-31 01:35:25 +03:00
|
|
|
"runtime"
|
2020-05-14 07:19:12 +03:00
|
|
|
"strconv"
|
2020-04-16 05:29:18 +03:00
|
|
|
"strings"
|
2020-03-09 08:10:48 +03:00
|
|
|
"unicode"
|
2020-01-31 01:35:25 +03:00
|
|
|
)
|
|
|
|
|
2020-06-03 09:39:36 +03:00
|
|
|
var gormSourceDir string
|
2020-05-23 16:35:12 +03:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
_, file, _, _ := runtime.Caller(0)
|
2020-07-16 06:27:04 +03:00
|
|
|
gormSourceDir = regexp.MustCompile(`utils.utils\.go`).ReplaceAllString(file, "")
|
2020-05-23 16:35:12 +03:00
|
|
|
}
|
2020-01-31 01:35:25 +03:00
|
|
|
|
|
|
|
func FileWithLineNum() string {
|
|
|
|
for i := 2; i < 15; i++ {
|
|
|
|
_, file, line, ok := runtime.Caller(i)
|
2020-06-03 09:39:36 +03:00
|
|
|
|
|
|
|
if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
|
2020-06-07 13:27:12 +03:00
|
|
|
return file + ":" + strconv.FormatInt(int64(line), 10)
|
2020-01-31 01:35:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2020-03-09 08:10:48 +03:00
|
|
|
|
|
|
|
func IsChar(c rune) bool {
|
|
|
|
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
|
|
|
|
}
|
2020-04-16 05:29:18 +03:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2020-05-14 07:19:12 +03:00
|
|
|
|
2020-05-23 16:03:28 +03:00
|
|
|
func ToStringKey(values ...interface{}) string {
|
2020-05-14 07:19:12 +03:00
|
|
|
results := make([]string, len(values))
|
|
|
|
|
|
|
|
for idx, value := range values {
|
2020-05-23 16:03:28 +03:00
|
|
|
if valuer, ok := value.(driver.Valuer); ok {
|
|
|
|
value, _ = valuer.Value()
|
2020-05-14 08:48:51 +03:00
|
|
|
}
|
2020-05-14 07:19:12 +03:00
|
|
|
|
2020-05-23 16:03:28 +03:00
|
|
|
switch v := value.(type) {
|
2020-05-14 07:19:12 +03:00
|
|
|
case string:
|
|
|
|
results[idx] = v
|
|
|
|
case []byte:
|
|
|
|
results[idx] = string(v)
|
|
|
|
case uint:
|
|
|
|
results[idx] = strconv.FormatUint(uint64(v), 10)
|
|
|
|
default:
|
2020-05-23 16:03:28 +03:00
|
|
|
results[idx] = fmt.Sprint(reflect.Indirect(reflect.ValueOf(v)).Interface())
|
2020-05-14 07:19:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(results, "_")
|
|
|
|
}
|
2020-06-30 11:53:54 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|