77 "github.com/go-simpl/simplapi/errors"
88 "github.com/go-simpl/simplapi/pkg/context"
99 "github.com/go-simpl/simplapi/pkg/framework"
10- "github.com/go-simpl/simplapi/pkg/reflection"
11- "github.com/go-simpl/simplapi/types"
1210)
1311
1412func WrapHandler (handler interface {}, next framework.FrameworkHandler ) framework.FrameworkHandler {
@@ -30,26 +28,13 @@ func WrapHandler(handler interface{}, next framework.FrameworkHandler) framework
3028 panic ("handler must return an error at the last position" )
3129 }
3230
33- createParams := func (req framework.FrameworkRequest , ctx * context.Context ) ([]reflect.Value , error ) {
34- numInputs := handlerType .NumIn ()
35- inputs := make ([]reflect.Value , numInputs )
36- for i := 0 ; i < numInputs ; i ++ {
37- if handlerType .In (i ) == reflect .TypeOf (ctx ) {
38- inputs [i ] = reflect .ValueOf (ctx )
39- continue
40- }
41-
42- inputs [i ] = reflect .New (handlerType .In (i )).Elem ()
43- err := reflection .PopulateValueFromTypeUsingContext (req , handlerType .In (i ), inputs [i ])
44- if err != nil {
45- return nil , err
46- }
47- }
48- return inputs , nil
31+ handlerInputTypes := make ([]reflect.Type , handlerType .NumIn ())
32+ for i := 0 ; i < handlerType .NumIn (); i ++ {
33+ handlerInputTypes [i ] = handlerType .In (i )
4934 }
5035
5136 return func (req framework.FrameworkRequest , res framework.FrameworkResponse , ctx * context.Context ) error {
52- inputs , err := createParams (req , ctx )
37+ inputs , err := constructParams (req , ctx , handlerInputTypes )
5338 if err != nil {
5439 if typeErr , ok := err .(errors.TypeError ); ok {
5540 res .SetStatusCode (http .StatusUnprocessableEntity )
@@ -68,19 +53,15 @@ func WrapHandler(handler interface{}, next framework.FrameworkHandler) framework
6853 // handle response
6954 for _ , result := range results {
7055 if ! result .IsNil () {
71- // check for custom response types
72- if response , ok := result .Interface ().(* types.HTMLResponse ); ok {
73- res .SetStatusCode (http .StatusOK )
74- res .SetHeader ("Content-Type" , "text/html" )
75- return res .SendString (response .HTML )
56+ bodyDone , err := transferToResponse (res , result )
57+ if err != nil {
58+ return err
7659 }
7760
78- if response , ok := result .Interface ().(types.APIResponse ); ok {
79- res .SetStatusCode (response .GetStatusCode ())
80- return res .SendJSON (result .Interface ())
81- } else {
82- return res .SendJSON (result .Interface ())
61+ if bodyDone {
62+ return nil
8363 }
64+
8465 }
8566 }
8667
0 commit comments