@@ -2,6 +2,7 @@ package dew
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"reflect"
6
7
"sync"
7
8
)
@@ -168,6 +169,38 @@ func (mx *mux) updateHandler(m middlewareType) {
168
169
// Register adds the handler to the mux for the given command type.
169
170
func (mx * mux ) Register (handler any ) {
170
171
hdlTyp := reflect .TypeOf (handler )
172
+
173
+ // if it's not a pointer, convert it to a pointer so we can get
174
+ // access to all the methods of the struct.
175
+ if hdlTyp .Kind () != reflect .Ptr {
176
+ newVal := reflect .New (reflect .PointerTo (hdlTyp ))
177
+ ptrToHandler := reflect .ValueOf (& handler )
178
+ if ptrToHandler .Elem ().Type ().AssignableTo (newVal .Elem ().Type ()) {
179
+ newVal .Elem ().Set (ptrToHandler .Elem ())
180
+ } else {
181
+ // If direct assignment is not possible, we need to create an intermediate value
182
+ intermediateVal := reflect .New (hdlTyp )
183
+ intermediateVal .Elem ().Set (reflect .ValueOf (handler ))
184
+ newVal .Elem ().Set (intermediateVal )
185
+ }
186
+ handler = newVal .Interface ()
187
+ hdlTyp = reflect .TypeOf (handler )
188
+
189
+ if hdlTyp .Kind () == reflect .Ptr && hdlTyp .Elem ().Kind () == reflect .Ptr {
190
+ // We have a pointer to a pointer, let's get to the single pointer
191
+ newVal := reflect .ValueOf (handler ).Elem ()
192
+ handler = newVal .Interface ()
193
+ hdlTyp = reflect .TypeOf (handler )
194
+ }
195
+
196
+ }
197
+
198
+ kind := hdlTyp .Kind ()
199
+ println (fmt .Sprintf ("Register: %v %s" , kind , hdlTyp .String ()))
200
+
201
+ numMethod := hdlTyp .NumMethod ()
202
+ println (fmt .Sprintf ("numMethod: %v" , numMethod ))
203
+
171
204
for i := 0 ; i < hdlTyp .NumMethod (); i ++ {
172
205
mtdTyp := hdlTyp .Method (i )
173
206
if isHandlerMethod (mtdTyp ) {
@@ -187,6 +220,7 @@ func (mx *mux) setupHandler() {
187
220
mx .updateHandler (mQuery )
188
221
}
189
222
if mx .mHandlers [mDispatch ] == nil {
223
+ println ("setupHandler" )
190
224
mx .updateHandler (mDispatch )
191
225
}
192
226
if mx .parent != nil {
@@ -195,6 +229,7 @@ func (mx *mux) setupHandler() {
195
229
}
196
230
197
231
func (mx * mux ) addHandler (t reflect.Type , h any ) {
232
+ println (fmt .Sprintf ("addHandler: %v" , t ))
198
233
mx .entries .Store (t , & handler {handler : h , mux : mx })
199
234
}
200
235
0 commit comments