-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathtypeid.go
More file actions
170 lines (155 loc) · 4.18 KB
/
Copy pathtypeid.go
File metadata and controls
170 lines (155 loc) · 4.18 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) Microsoft. All rights reserved.
package workflow
import (
"fmt"
"reflect"
"sync"
)
var typeIDRegistry sync.Map
// TypeID is a representation of a type's identity, including its package and
// type names. Pointer types are identified by their element type.
type TypeID struct {
// PackageName is the package path for named Go types. It is empty for
// built-in and unnamed types.
PackageName string
// TypeName is the type name for named Go types, or the string form for
// unnamed types such as pointers, slices, and maps.
TypeName string
}
// NewTypeID creates a TypeID for typ and caches typ as a runtime type that can
// be resolved from that identity. A nil type returns the zero TypeID. Pointer
// types are identified by their element type.
func NewTypeID(typ reflect.Type) TypeID {
id := typeIDOf(typ)
cacheRuntimeType(id, typ)
return id
}
func typeIDOf(typ reflect.Type) TypeID {
var ok bool
typ, ok = dereferencePointerType(typ)
if !ok || typ == nil {
return TypeID{}
}
name := typ.Name()
if name == "" {
name = typ.String()
}
id := TypeID{
PackageName: typ.PkgPath(),
TypeName: name,
}
return id
}
func cacheRuntimeType(id TypeID, typ reflect.Type) {
if id == (TypeID{}) || typ == nil {
return
}
candidate := runtimeTypeForCache(typ)
if candidate == nil {
return
}
current, loaded := typeIDRegistry.LoadOrStore(id, candidate)
if !loaded || preferRuntimeType(candidate, current.(reflect.Type)) {
typeIDRegistry.Store(id, candidate)
}
}
func runtimeTypeForCache(typ reflect.Type) reflect.Type {
if typ == nil {
return nil
}
wasPointer := typ.Kind() == reflect.Pointer
base, ok := dereferencePointerType(typ)
if !ok || base == nil {
return nil
}
if !wasPointer || base.Kind() == reflect.Interface {
return base
}
return reflect.PointerTo(base)
}
func dereferencePointerType(typ reflect.Type) (reflect.Type, bool) {
if typ == nil {
return nil, true
}
slowpoke := typ
indir := 0
for typ.Kind() == reflect.Pointer {
typ = typ.Elem()
if typ == slowpoke {
return nil, false
}
if indir%2 == 0 {
slowpoke = slowpoke.Elem()
}
indir++
}
return typ, true
}
// preferRuntimeType reports whether candidate should replace current in the
// TypeID runtime cache. Interface types are preferred because they enable
// polymorphic matching, and pointer concrete types are preferred over value
// concrete types because TypeID canonicalizes both to the element identity.
func preferRuntimeType(candidate, current reflect.Type) bool {
if candidate == nil {
return false
}
if current == nil {
return true
}
if candidate.Kind() == reflect.Interface {
return current.Kind() != reflect.Interface
}
if current.Kind() == reflect.Interface {
return false
}
return candidate.Kind() == reflect.Pointer && current.Kind() != reflect.Pointer
}
// Match reports whether typ has the same package and type names as t.
// The zero TypeID represents an unknown type and does not match any type.
func (t TypeID) Match(typ reflect.Type) bool {
if t == (TypeID{}) {
return false
}
return NewTypeID(typ) == t
}
// MatchPolymorphic reports whether typ either matches this type identity
// exactly or can be assigned to the runtime type cached or discovered for this
// identity. Concrete types can match interface response ports when the interface
// type has been cached through [NewTypeID] or discovered from runtime type
// metadata.
func (t TypeID) MatchPolymorphic(typ reflect.Type) bool {
if t.Match(typ) {
return true
}
if typ == nil || t == (TypeID{}) {
return false
}
target, ok := runtimeTypeForTypeID(t)
return ok && typ.AssignableTo(target)
}
func runtimeTypeForTypeID(id TypeID) (reflect.Type, bool) {
if id == (TypeID{}) {
return nil, false
}
typ, ok := typeIDRegistry.Load(id)
if ok {
return typ.(reflect.Type), true
}
fallback, ok := lookupRuntimeTypeByID(id)
if !ok {
return nil, false
}
cacheRuntimeType(id, fallback)
typ, ok = typeIDRegistry.Load(id)
if ok {
return typ.(reflect.Type), true
}
return fallback, true
}
// String returns the type identity in a compact diagnostic form.
func (t TypeID) String() string {
if t.PackageName == "" {
return t.TypeName
}
return fmt.Sprintf("%s, %s", t.TypeName, t.PackageName)
}