-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
53 lines (48 loc) · 831 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package tower
import (
"fmt"
)
func printDebugImpl(a any) bool {
if a == nil {
fmt.Println("nil")
return true
}
if dbg, ok := a.(interface {
Debug()
}); ok {
dbg.Debug()
return true
}
return false
}
func printDisplayImpl(a any) bool {
if a == nil {
fmt.Println("nil")
return true
}
if dbg, ok := a.(Display); ok {
fmt.Println(dbg.Display())
return true
}
return false
}
// Dbg Prints the variable and returns the given item after printing.
// Useful for Debugging without breaking the code flow.
func Dbg[T any](a T) T {
if printDebugImpl(a) {
return a
}
if printDisplayImpl(a) {
return a
}
fmt.Printf("%#v\n", a)
return a
}
// Cast turns any slice into a slice of any.
func Cast[T any](in []T) []any {
out := make([]any, len(in))
for i, v := range in {
out[i] = v
}
return out
}