-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
71 lines (55 loc) · 1.05 KB
/
types.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package container
import (
"reflect"
"sync"
)
type Types struct {
types sync.Map
}
var ContainerTypes = &Types{}
func nameOfType(p reflect.Type) *PkgType {
name := p.Name()
path := p.PkgPath()
fullPath := ""
if path != "" {
fullPath = path + "/" + name
}
return &PkgType{
Name: name,
Path: path,
FullName: fullPath,
TypeStr: p.String(),
Kind: p.Kind(),
Type: p,
}
}
func (t *Types) Of(typ any) *PkgType {
reflected := getType(typ)
if reflected.Kind() == reflect.Ptr {
return nameOfType(reflected.Elem())
}
return nameOfType(reflected)
}
func (t *Types) Clear() {
t.types.Range(func(key any, value any) bool {
t.types.Delete(key)
return true
})
}
func (t *Types) Has(r any) bool {
typ := ContainerTypes.Of(r)
_, ok := t.types.Load(typ.FullName)
return ok
}
type PkgType struct {
Name string
Path string
// FullName - The Path + Name split by a /
FullName string
Type reflect.Type
Kind reflect.Kind
TypeStr string
}
func (t *PkgType) Save() {
ContainerTypes.types.LoadOrStore(t.FullName, t)
}