@@ -167,48 +167,24 @@ func (mx *mux) updateHandler(m middlewareType) {
167
167
}
168
168
169
169
// 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 ()
196
179
}
197
180
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 ())
212
188
}
213
189
}
214
190
}
0 commit comments