Skip to content

Commit 446f922

Browse files
committed
wip
1 parent 720e409 commit 446f922

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

mux.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dew
22

33
import (
44
"context"
5+
"fmt"
56
"reflect"
67
"sync"
78
)
@@ -168,6 +169,38 @@ func (mx *mux) updateHandler(m middlewareType) {
168169
// Register adds the handler to the mux for the given command type.
169170
func (mx *mux) Register(handler any) {
170171
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+
171204
for i := 0; i < hdlTyp.NumMethod(); i++ {
172205
mtdTyp := hdlTyp.Method(i)
173206
if isHandlerMethod(mtdTyp) {
@@ -187,6 +220,7 @@ func (mx *mux) setupHandler() {
187220
mx.updateHandler(mQuery)
188221
}
189222
if mx.mHandlers[mDispatch] == nil {
223+
println("setupHandler")
190224
mx.updateHandler(mDispatch)
191225
}
192226
if mx.parent != nil {
@@ -195,6 +229,7 @@ func (mx *mux) setupHandler() {
195229
}
196230

197231
func (mx *mux) addHandler(t reflect.Type, h any) {
232+
println(fmt.Sprintf("addHandler: %v", t))
198233
mx.entries.Store(t, &handler{handler: h, mux: mx})
199234
}
200235

0 commit comments

Comments
 (0)