Skip to content

Commit f2852c3

Browse files
committed
Support value type for Regiser parameter
1 parent 446f922 commit f2852c3

File tree

2 files changed

+29
-40
lines changed

2 files changed

+29
-40
lines changed

mux.go

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -167,48 +167,24 @@ func (mx *mux) updateHandler(m middlewareType) {
167167
}
168168

169169
// Register adds the handler to the mux for the given command type.
170-
func (mx *mux) Register(handler any) {
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-
170+
func (mx *mux) Register(handler interface{}) {
171+
val := reflect.ValueOf(handler)
172+
typ := val.Type()
173+
174+
// Convert to pointer if not already
175+
if typ.Kind() != reflect.Ptr {
176+
val = reflect.New(typ)
177+
val.Elem().Set(reflect.ValueOf(handler))
178+
typ = val.Type()
196179
}
197180

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-
204-
for i := 0; i < hdlTyp.NumMethod(); i++ {
205-
mtdTyp := hdlTyp.Method(i)
206-
if isHandlerMethod(mtdTyp) {
207-
cmdTyp := mtdTyp.Type.In(2).Elem()
208-
if cmdTyp.Implements(reflect.TypeOf((*Action)(nil)).Elem()) {
209-
mx.addHandler(cmdTyp, reflect.ValueOf(handler).Method(i).Interface())
210-
} else if cmdTyp.Implements(reflect.TypeOf((*QueryAction)(nil)).Elem()) {
211-
mx.addHandler(cmdTyp, reflect.ValueOf(handler).Method(i).Interface())
181+
for i := 0; i < typ.NumMethod(); i++ {
182+
method := typ.Method(i)
183+
if isHandlerMethod(method) {
184+
cmdType := method.Type.In(2).Elem()
185+
if cmdType.Implements(reflect.TypeOf((*Action)(nil)).Elem()) ||
186+
cmdType.Implements(reflect.TypeOf((*QueryAction)(nil)).Elem()) {
187+
mx.addHandler(cmdType, val.Method(i).Interface())
212188
}
213189
}
214190
}

mux_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ func TestMux_BasicCommand(t *testing.T) {
2929
}
3030
}
3131

32+
func TestMux_ValueTypeHandler(t *testing.T) {
33+
var userHandler userHandler
34+
35+
mux := dew.New()
36+
mux.Register(userHandler)
37+
38+
createUser := &createUser{Name: "john"}
39+
testRunDispatch(t, dew.NewAction(mux, createUser))
40+
if createUser.Result != "user created" {
41+
t.Fatalf("unexpected result: %s", createUser.Result)
42+
}
43+
}
44+
3245
func TestMux_HandlerNotFound(t *testing.T) {
3346
defer func() {
3447
if r := recover(); r == nil {

0 commit comments

Comments
 (0)