github.com/systematiccaos/gorm@v1.22.6/utils/utils.go (about)

     1  package utils
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"fmt"
     6  	"reflect"
     7  	"regexp"
     8  	"runtime"
     9  	"strconv"
    10  	"strings"
    11  	"unicode"
    12  )
    13  
    14  var gormSourceDir string
    15  
    16  func init() {
    17  	_, file, _, _ := runtime.Caller(0)
    18  	// compatible solution to get gorm source directory with various operating systems
    19  	gormSourceDir = regexp.MustCompile(`utils.utils\.go`).ReplaceAllString(file, "")
    20  }
    21  
    22  // FileWithLineNum return the file name and line number of the current file
    23  func FileWithLineNum() string {
    24  	// the second caller usually from gorm internal, so set i start from 2
    25  	for i := 2; i < 15; i++ {
    26  		_, file, line, ok := runtime.Caller(i)
    27  		if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
    28  			return file + ":" + strconv.FormatInt(int64(line), 10)
    29  		}
    30  	}
    31  
    32  	return ""
    33  }
    34  
    35  func IsValidDBNameChar(c rune) bool {
    36  	return !unicode.IsLetter(c) && !unicode.IsNumber(c) && c != '.' && c != '*' && c != '_' && c != '$' && c != '@'
    37  }
    38  
    39  func CheckTruth(val interface{}) bool {
    40  	if v, ok := val.(bool); ok {
    41  		return v
    42  	}
    43  
    44  	if v, ok := val.(string); ok {
    45  		v = strings.ToLower(v)
    46  		return v != "false"
    47  	}
    48  
    49  	return !reflect.ValueOf(val).IsZero()
    50  }
    51  
    52  func ToStringKey(values ...interface{}) string {
    53  	results := make([]string, len(values))
    54  
    55  	for idx, value := range values {
    56  		if valuer, ok := value.(driver.Valuer); ok {
    57  			value, _ = valuer.Value()
    58  		}
    59  
    60  		switch v := value.(type) {
    61  		case string:
    62  			results[idx] = v
    63  		case []byte:
    64  			results[idx] = string(v)
    65  		case uint:
    66  			results[idx] = strconv.FormatUint(uint64(v), 10)
    67  		default:
    68  			results[idx] = fmt.Sprint(reflect.Indirect(reflect.ValueOf(v)).Interface())
    69  		}
    70  	}
    71  
    72  	return strings.Join(results, "_")
    73  }
    74  
    75  func Contains(elems []string, elem string) bool {
    76  	for _, e := range elems {
    77  		if elem == e {
    78  			return true
    79  		}
    80  	}
    81  	return false
    82  }
    83  
    84  func AssertEqual(src, dst interface{}) bool {
    85  	if !reflect.DeepEqual(src, dst) {
    86  		if valuer, ok := src.(driver.Valuer); ok {
    87  			src, _ = valuer.Value()
    88  		}
    89  
    90  		if valuer, ok := dst.(driver.Valuer); ok {
    91  			dst, _ = valuer.Value()
    92  		}
    93  
    94  		return reflect.DeepEqual(src, dst)
    95  	}
    96  	return true
    97  }
    98  
    99  func ToString(value interface{}) string {
   100  	switch v := value.(type) {
   101  	case string:
   102  		return v
   103  	case int:
   104  		return strconv.FormatInt(int64(v), 10)
   105  	case int8:
   106  		return strconv.FormatInt(int64(v), 10)
   107  	case int16:
   108  		return strconv.FormatInt(int64(v), 10)
   109  	case int32:
   110  		return strconv.FormatInt(int64(v), 10)
   111  	case int64:
   112  		return strconv.FormatInt(v, 10)
   113  	case uint:
   114  		return strconv.FormatUint(uint64(v), 10)
   115  	case uint8:
   116  		return strconv.FormatUint(uint64(v), 10)
   117  	case uint16:
   118  		return strconv.FormatUint(uint64(v), 10)
   119  	case uint32:
   120  		return strconv.FormatUint(uint64(v), 10)
   121  	case uint64:
   122  		return strconv.FormatUint(v, 10)
   123  	}
   124  	return ""
   125  }