Skip to content

Commit

Permalink
Fix reflection pointers stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Jan 28, 2021
1 parent fa5b765 commit 289ea86
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 6 additions & 2 deletions router/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func getEndpoint(m reflect.Method) *endpoint {
return nil
}

if m.Type.In(2).Kind() != reflect.Ptr || m.Type.In(3).Kind() != reflect.Ptr {
return nil
}

// Functions must return an error value
if m.Type.NumOut() != 1 {
return nil
Expand All @@ -71,7 +75,7 @@ func getEndpoint(m reflect.Method) *endpoint {
return &endpoint{
Name: m.Name,
HandlerFunc: m.Func,
In: m.Type.In(2),
Out: m.Type.In(3),
In: m.Type.In(2).Elem(),
Out: m.Type.In(3).Elem(),
}
}
12 changes: 4 additions & 8 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *router) Handle(path string, data []byte) ([]byte, error) {
ret := method.HandlerFunc.Call([]reflect.Value{
reflect.ValueOf(handler.Instance),
reflect.ValueOf(context.Background()),
*in,
in,
respValue,
})

Expand All @@ -98,17 +98,13 @@ func (s *router) Handle(path string, data []byte) ([]byte, error) {
return outdata, nil
}

func (s *router) decode(t reflect.Type, d []byte) (*reflect.Value, error) {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}

func (s *router) decode(t reflect.Type, d []byte) (reflect.Value, error) {
val := reflect.New(t)
intf := val.Interface()

if err := s.codec.Unmarshal(d, intf); err != nil {
return nil, err
return reflect.Value{}, err
}

return &val, nil
return val, nil
}

0 comments on commit 289ea86

Please sign in to comment.