Debian Codegraph @

utils/utils.go

raw
1 package utils
2
3 import (
4 "database/sql/driver"
5 "fmt"
6 "path/filepath"
7 "reflect"
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 = sourceDir(file)
20 }
21
22 func sourceDir(file string) string {
23 dir := filepath.Dir(file)
24 dir = filepath.Dir(dir)
25
26 s := filepath.Dir(dir)
27 if filepath.Base(s) != "gorm.io" {
28 s = dir
29 }
30 return filepath.ToSlash(s) + "/"
31 }
32
33 // CallerFrame retrieves the first relevant stack frame outside of GORM's internal implementation files.
34 // It skips:
35 // - GORM's core source files (identified by gormSourceDir prefix)
36 // - Exclude test files (*_test.go)
37 // - go-gorm/gen's Generated files (*.gen.go)
38 func CallerFrame() runtime.Frame {
39 pcs := [13]uintptr{}
40 // the third caller usually from gorm internal
41 len := runtime.Callers(3, pcs[:])
42 frames := runtime.CallersFrames(pcs[:len])
43 for i := 0; i < len; i++ {
44 // second return value is "more", not "ok"
45 frame, _ := frames.Next()
46 if (!strings.HasPrefix(frame.File, gormSourceDir) ||
47 strings.HasSuffix(frame.File, "_test.go")) && !strings.HasSuffix(frame.File, ".gen.go") {
48 return frame
49 }
50 }
51
52 return runtime.Frame{}
53 }
54
55 // FileWithLineNum return the file name and line number of the current file
56 func FileWithLineNum() string {
57 frame := CallerFrame()
58 if frame.PC != 0 {
59 return string(strconv.AppendInt(append([]byte(frame.File), ':'), int64(frame.Line), 10))
60 }
61
62 return ""
63 }
64
65 func IsInvalidDBNameChar(c rune) bool {
66 return !unicode.IsLetter(c) && !unicode.IsNumber(c) && c != '.' && c != '*' && c != '_' && c != '$' && c != '@'
67 }
68
69 // CheckTruth check string true or not
70 func CheckTruth(vals ...string) bool {
71 for _, val := range vals {
72 if val != "" && !strings.EqualFold(val, "false") {
73 return true
74 }
75 }
76 return false
77 }
78
79 func ToStringKey(values ...interface{}) string {
80 results := make([]string, len(values))
81
82 for idx, value := range values {
83 if valuer, ok := value.(driver.Valuer); ok {
84 value, _ = valuer.Value()
85 }
86
87 switch v := value.(type) {
88 case string:
89 results[idx] = v
90 case []byte:
91 results[idx] = string(v)
92 case uint:
93 results[idx] = strconv.FormatUint(uint64(v), 10)
94 default:
95 results[idx] = "nil"
96 vv := reflect.ValueOf(v)
97 if vv.IsValid() && !vv.IsZero() {
98 results[idx] = fmt.Sprint(reflect.Indirect(vv).Interface())
99 }
100 }
101 }
102
103 return strings.Join(results, "_")
104 }
105
106 func Contains(elems []string, elem string) bool {
107 for _, e := range elems {
108 if elem == e {
109 return true
110 }
111 }
112 return false
113 }
114
115 func AssertEqual(x, y interface{}) bool {
116 if reflect.DeepEqual(x, y) {
117 return true
118 }
119 if x == nil || y == nil {
120 return false
121 }
122
123 xval := reflect.ValueOf(x)
124 yval := reflect.ValueOf(y)
125 if xval.Kind() == reflect.Ptr && xval.IsNil() ||
126 yval.Kind() == reflect.Ptr && yval.IsNil() {
127 return false
128 }
129
130 if valuer, ok := x.(driver.Valuer); ok {
131 x, _ = valuer.Value()
132 }
133 if valuer, ok := y.(driver.Valuer); ok {
134 y, _ = valuer.Value()
135 }
136 return reflect.DeepEqual(x, y)
137 }
138
139 func ToString(value interface{}) string {
140 switch v := value.(type) {
141 case string:
142 return v
143 case int:
144 return strconv.FormatInt(int64(v), 10)
145 case int8:
146 return strconv.FormatInt(int64(v), 10)
147 case int16:
148 return strconv.FormatInt(int64(v), 10)
149 case int32:
150 return strconv.FormatInt(int64(v), 10)
151 case int64:
152 return strconv.FormatInt(v, 10)
153 case uint:
154 return strconv.FormatUint(uint64(v), 10)
155 case uint8:
156 return strconv.FormatUint(uint64(v), 10)
157 case uint16:
158 return strconv.FormatUint(uint64(v), 10)
159 case uint32:
160 return strconv.FormatUint(uint64(v), 10)
161 case uint64:
162 return strconv.FormatUint(v, 10)
163 }
164 return ""
165 }
166
167 const nestedRelationSplit = "__"
168
169 // NestedRelationName nested relationships like `Manager__Company`
170 func NestedRelationName(prefix, name string) string {
171 return prefix + nestedRelationSplit + name
172 }
173
174 // SplitNestedRelationName Split nested relationships to `[]string{"Manager","Company"}`
175 func SplitNestedRelationName(name string) []string {
176 return strings.Split(name, nestedRelationSplit)
177 }
178
179 // JoinNestedRelationNames nested relationships like `Manager__Company`
180 func JoinNestedRelationNames(relationNames []string) string {
181 return strings.Join(relationNames, nestedRelationSplit)
182 }
183
184 // RTrimSlice Right trims the given slice by given length
185 func RTrimSlice[T any](v []T, trimLen int) []T {
186 if trimLen >= len(v) { // trimLen greater than slice len means fully sliced
187 return v[:0]
188 }
189 if trimLen < 0 { // negative trimLen is ignored
190 return v[:]
191 }
192 return v[:len(v)-trimLen]
193 }