This repository was archived by the owner on May 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterface.go
More file actions
72 lines (62 loc) · 1.41 KB
/
interface.go
File metadata and controls
72 lines (62 loc) · 1.41 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package support
import (
"fmt"
"github.com/spf13/cast"
"reflect"
"strings"
)
func Name(element interface{}) string {
switch Kind(element) {
case reflect.String:
if element == "" {
return "string"
}
return cast.ToString(element)
case reflect.Int:
if element == 0 {
return "int"
}
return cast.ToString(element)
case reflect.Bool:
return "bool"
case reflect.Float64:
if element == 0. {
return "float64"
}
return cast.ToString(element)
case reflect.Float32:
var emptyFloat float32 = 0
if element == emptyFloat {
return "float32"
}
return cast.ToString(element)
case reflect.Struct:
return reflect.TypeOf(element).String()
}
if Kind(element) == reflect.Ptr && element == nil {
panic("Nil value found. To bind an interface, use the following syntax: (*INTERFACE)(nil)")
}
// Get name of element and ignore pointer
return strings.TrimLeft(fmt.Sprintf("%T", element), "*")
}
func Package(element interface{}) string {
if element == nil {
return reflect.TypeOf(&element).Elem().PkgPath()
}
return reflect.TypeOf(element).Elem().PkgPath()
}
func Kind(element interface{}) reflect.Kind {
if element == nil {
return reflect.TypeOf(&element).Kind()
}
switch element.(type) {
case Collection:
return reflect.Slice
case Map:
return reflect.Map
}
return reflect.TypeOf(element).Kind()
}
func Dump(expression ...interface{}) {
fmt.Println(fmt.Sprintf("%#v", expression))
}