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"
|
2022-11-05 06:52:08 +03:00
|
|
|
"path/filepath"
|
2020-04-16 05:29:18 +03:00
|
|
|
"reflect"
|
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)
|
2021-06-13 05:32:03 +03:00
|
|
|
// compatible solution to get gorm source directory with various operating systems
|
2022-11-05 06:52:08 +03:00
|
|
|
gormSourceDir = sourceDir(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sourceDir(file string) string {
|
|
|
|
dir := filepath.Dir(file)
|
|
|
|
dir = filepath.Dir(dir)
|
|
|
|
|
|
|
|
s := filepath.Dir(dir)
|
|
|
|
if filepath.Base(s) != "gorm.io" {
|
|
|
|
s = dir
|
|
|
|
}
|
2022-11-21 05:48:13 +03:00
|
|
|
return filepath.ToSlash(s) + "/"
|
2020-05-23 16:35:12 +03:00
|
|
|
}
|
2020-01-31 01:35:25 +03:00
|
|
|
|
2021-06-10 05:21:28 +03:00
|
|
|
// FileWithLineNum return the file name and line number of the current file
|
2020-01-31 01:35:25 +03:00
|
|
|
func FileWithLineNum() string {
|
2021-06-13 05:32:03 +03:00
|
|
|
// the second caller usually from gorm internal, so set i start from 2
|
|
|
|
for i := 2; i < 15; i++ {
|
2020-01-31 01:35:25 +03:00
|
|
|
_, file, line, ok := runtime.Caller(i)
|
2024-01-12 12:09:22 +03:00
|
|
|
if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) &&
|
|
|
|
!strings.HasSuffix(file, ".gen.go") {
|
2020-06-07 13:27:12 +03:00
|
|
|
return file + ":" + strconv.FormatInt(int64(line), 10)
|
2020-01-31 01:35:25 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-10 05:21:28 +03:00
|
|
|
|
2020-01-31 01:35:25 +03:00
|
|
|
return ""
|
|
|
|
}
|
2020-03-09 08:10:48 +03:00
|
|
|
|
2020-08-27 14:15:40 +03:00
|
|
|
func IsValidDBNameChar(c rune) bool {
|
2020-09-10 16:46:18 +03:00
|
|
|
return !unicode.IsLetter(c) && !unicode.IsNumber(c) && c != '.' && c != '*' && c != '_' && c != '$' && c != '@'
|
2020-03-09 08:10:48 +03:00
|
|
|
}
|
2020-04-16 05:29:18 +03:00
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// CheckTruth check string true or not
|
|
|
|
func CheckTruth(vals ...string) bool {
|
|
|
|
for _, val := range vals {
|
2022-02-20 16:19:15 +03:00
|
|
|
if val != "" && !strings.EqualFold(val, "false") {
|
2022-02-16 10:30:43 +03:00
|
|
|
return true
|
|
|
|
}
|
2020-04-16 05:29:18 +03:00
|
|
|
}
|
2022-02-16 10:30:43 +03:00
|
|
|
return false
|
2020-04-16 05:29:18 +03:00
|
|
|
}
|
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
|
|
|
|
2021-10-28 02:24:38 +03:00
|
|
|
func Contains(elems []string, elem string) bool {
|
|
|
|
for _, e := range elems {
|
|
|
|
if elem == e {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-10-10 09:46:32 +03:00
|
|
|
func AssertEqual(x, y interface{}) bool {
|
|
|
|
if reflect.DeepEqual(x, y) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if x == nil || y == nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-06-30 11:53:54 +03:00
|
|
|
|
2023-10-10 09:46:32 +03:00
|
|
|
xval := reflect.ValueOf(x)
|
|
|
|
yval := reflect.ValueOf(y)
|
|
|
|
if xval.Kind() == reflect.Ptr && xval.IsNil() ||
|
|
|
|
yval.Kind() == reflect.Ptr && yval.IsNil() {
|
|
|
|
return false
|
|
|
|
}
|
2020-06-30 11:53:54 +03:00
|
|
|
|
2023-10-10 09:46:32 +03:00
|
|
|
if valuer, ok := x.(driver.Valuer); ok {
|
|
|
|
x, _ = valuer.Value()
|
|
|
|
}
|
|
|
|
if valuer, ok := y.(driver.Valuer); ok {
|
|
|
|
y, _ = valuer.Value()
|
2020-06-30 11:53:54 +03:00
|
|
|
}
|
2023-10-10 09:46:32 +03:00
|
|
|
return reflect.DeepEqual(x, y)
|
2020-06-30 11:53:54 +03:00
|
|
|
}
|
2020-09-01 16:03:37 +03:00
|
|
|
|
|
|
|
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 ""
|
|
|
|
}
|
2023-03-10 12:21:56 +03:00
|
|
|
|
|
|
|
const nestedRelationSplit = "__"
|
|
|
|
|
|
|
|
// NestedRelationName nested relationships like `Manager__Company`
|
|
|
|
func NestedRelationName(prefix, name string) string {
|
|
|
|
return prefix + nestedRelationSplit + name
|
|
|
|
}
|
|
|
|
|
|
|
|
// SplitNestedRelationName Split nested relationships to `[]string{"Manager","Company"}`
|
|
|
|
func SplitNestedRelationName(name string) []string {
|
|
|
|
return strings.Split(name, nestedRelationSplit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinNestedRelationNames nested relationships like `Manager__Company`
|
|
|
|
func JoinNestedRelationNames(relationNames []string) string {
|
|
|
|
return strings.Join(relationNames, nestedRelationSplit)
|
|
|
|
}
|