-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
infer.go
266 lines (215 loc) · 5.2 KB
/
infer.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Package infer implements reflect functions that package bot uses.
//
// Functions in this package may run recursively forever. This shouldn't happen
// with Arikawa's structures, but use these functions with care.
package infer
import (
"reflect"
"strings"
"github.com/diamondburned/arikawa/discord"
)
// ChannelID looks for fields with name ChannelID, Channel, or in some special
// cases, ID.
func ChannelID(event interface{}) discord.Snowflake {
return reflectID(reflect.ValueOf(event), "Channel")
}
// GuildID looks for fields with name GuildID, Guild, or in some special cases,
// ID.
func GuildID(event interface{}) discord.Snowflake {
return reflectID(reflect.ValueOf(event), "Guild")
}
// UserID looks for fields with name UserID, User, or in some special cases, ID.
func UserID(event interface{}) discord.Snowflake {
// This may have a very fatal bug of accidentally mistaking another User's
// ID. It also probably wouldn't work with things like RecipientID.
return reflectID(reflect.ValueOf(event), "User")
}
func reflectID(v reflect.Value, thing string) discord.Snowflake {
if !v.IsValid() {
return 0
}
t := v.Type()
if t.Kind() == reflect.Ptr {
v = v.Elem()
// Recheck after dereferring
if !v.IsValid() {
return 0
}
t = v.Type()
}
if t.Kind() != reflect.Struct {
return 0
}
numFields := t.NumField()
for i := 0; i < numFields; i++ {
field := t.Field(i)
fType := field.Type
if fType.Kind() == reflect.Ptr {
fType = fType.Elem()
}
switch fType.Kind() {
case reflect.Struct:
if chID := reflectID(v.Field(i), thing); chID.Valid() {
return chID
}
case reflect.Int64:
switch {
case false,
// Contains works with "LastMessageID" and such.
strings.Contains(field.Name, thing+"ID"),
// Special case where the struct name has Channel in it.
field.Name == "ID" && strings.Contains(t.Name(), thing):
return discord.Snowflake(v.Field(i).Int())
}
}
}
return 0
}
/*
var reflectCache sync.Map
type cacheKey struct {
t reflect.Type
f string
}
func getID(v reflect.Value, thing string) discord.Snowflake {
if !v.IsValid() {
return 0
}
t := v.Type()
if t.Kind() == reflect.Ptr {
v = v.Elem()
// Recheck after dereferring
if !v.IsValid() {
return 0
}
t = v.Type()
}
if t.Kind() != reflect.Struct {
return 0
}
return reflectID(thing, v, t)
}
type reflector struct {
steps []step
thing string
thingID string
}
type step struct {
field int
ptr bool
rec []step
}
func reflectID(thing string, v reflect.Value, t reflect.Type) discord.Snowflake {
r := &reflector{thing: thing}
// copy original type
key := r.thing + t.String()
// check the cache
if instructions, ok := reflectCache.Load(key); ok {
if instructions == nil {
return 0
}
return applyInstructions(v, instructions.([]step))
}
r.thingID = r.thing + "ID"
r.steps = make([]step, 0, 1)
id := r._id(v, t)
if r.steps != nil {
reflectCache.Store(key, r.instructions())
}
return id
}
func applyInstructions(v reflect.Value, instructions []step) discord.Snowflake {
// Use a type here to detect recursion:
// var originalT = v.Type()
var laststep reflect.Value
log.Println(v.Type(), instructions)
for i, step := range instructions {
if !v.IsValid() {
return 0
}
if i > 0 && step.ptr {
v = v.Elem()
}
if !v.IsValid() {
// is this the bottom of the instructions?
if i == len(instructions)-1 && step.rec != nil {
for _, ins := range step.rec {
var value = laststep.Field(ins.field)
if ins.ptr {
value = value.Elem()
}
if id := applyInstructions(value, instructions); id.Valid() {
return id
}
}
}
return 0
}
laststep = v
v = laststep.Field(step.field)
}
return discord.Snowflake(v.Int())
}
func (r *reflector) instructions() []step {
if len(r.steps) == 0 {
return nil
}
var instructions = make([]step, len(r.steps))
for i := 0; i < len(instructions); i++ {
instructions[i] = r.steps[len(r.steps)-i-1]
}
// instructions := r.steps
return instructions
}
func (r *reflector) step(s step) {
r.steps = append(r.steps, s)
}
func (r *reflector) _id(v reflect.Value, t reflect.Type) (chID discord.Snowflake) {
numFields := t.NumField()
var ptr bool
var ins = step{field: -1}
for i := 0; i < numFields; i++ {
field := t.Field(i)
fType := field.Type
value := v.Field(i)
ptr = false
if fType.Kind() == reflect.Ptr {
fType = fType.Elem()
value = value.Elem()
ptr = true
}
// does laststep have the same field type?
if fType == t {
ins.rec = append(ins.rec, step{field: i, ptr: ptr})
}
if !value.IsValid() {
continue
}
// If we've already found the field:
if ins.field > 0 {
continue
}
switch fType.Kind() {
case reflect.Struct:
if chID = r._id(value, fType); chID.Valid() {
ins.field = i
ins.ptr = ptr
}
case reflect.Int64:
switch {
case false,
// Contains works with "LastMessageID" and such.
strings.Contains(field.Name, r.thingID),
// Special case where the struct name has Channel in it.
field.Name == "ID" && strings.Contains(t.Name(), r.thing):
ins.field = i
ins.ptr = ptr
chID = discord.Snowflake(value.Int())
}
}
}
// If we've found the field:
r.step(ins)
return
}
*/