Skip to content

Commit

Permalink
Use new server method definition
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Jan 21, 2021
1 parent 853ff49 commit b3215a7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
10 changes: 5 additions & 5 deletions router/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ func getEndpoint(m reflect.Method) *endpoint {
return nil
}

// Functions must have inputs like (c context.Context, d *data.Data), plus one input for the receiver
if m.Type.NumIn() != 3 {
// Functions must have inputs like (c context.Context, req *data.Request, resp *data.Response), plus one input for the receiver
if m.Type.NumIn() != 4 {
return nil
}

// Functions must return either 1 or 2 values
if m.Type.NumOut() == 0 || m.Type.NumOut() > 2 {
// Functions must return an error value
if m.Type.NumOut() != 1 {
return nil
}

return &endpoint{
Name: m.Name,
HandlerFunc: m.Func,
In: m.Type.In(2),
Out: m.Type.Out(0),
Out: m.Type.In(3),
}
}
14 changes: 6 additions & 8 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,24 @@ func (s *router) Handle(path string, data []byte) ([]byte, error) {
return nil, fmt.Errorf("decode request: %w", err)
}

respValue := reflect.New(method.Out)

ret := method.HandlerFunc.Call([]reflect.Value{
reflect.ValueOf(handler.Instance),
reflect.ValueOf(context.Background()),
*in,
respValue,
})

if len(ret) == 2 && !ret[1].IsNil() {
if !ret[0].IsNil() {
return nil, &HandlerError{
endpoint: method,
handler: handler,
err: ret[1].Interface().(error),
err: ret[0].Interface().(error),
}
}

retval := ret[0]
if len(ret) == 2 {
retval = ret[1]
}

outdata, err := s.codec.Marshal(retval.Interface())
outdata, err := s.codec.Marshal(respValue.Interface())
if err != nil {
return nil, fmt.Errorf("encode response: %w", err)
}
Expand Down

0 comments on commit b3215a7

Please sign in to comment.