Compare commits

...

2 Commits

Author SHA1 Message Date
Elara e518b68d8c Return error if expected argument not provided 2022-06-02 02:09:07 -07:00
Elara 771c8c136e fix time.Ticker leak 2022-05-28 14:58:39 -07:00
2 changed files with 13 additions and 0 deletions

View File

@ -149,6 +149,7 @@ func (Channel) Time(ctx *server.Context, interval time.Duration) error {
case t := <-tick.C:
ch <- t
case <-ctx.Done():
tick.Stop()
close(ch)
return
}

View File

@ -42,6 +42,7 @@ var (
ErrNoSuchMethod = errors.New("no such method was found")
ErrInvalidMethod = errors.New("method invalid for lrpc call")
ErrUnexpectedArgument = errors.New("argument provided but the function does not accept any arguments")
ErrArgNotProvided = errors.New("method expected an argument, but none was provided")
)
// Server is an lrpc server
@ -152,6 +153,9 @@ func (s *Server) execute(pCtx context.Context, typ string, name string, arg any,
switch mtdType.NumOut() {
case 0: // If method has no return values
if mtdType.NumIn() == 2 {
if arg == nil {
return nil, nil, ErrArgNotProvided
}
// Call method with arg, ignore returned value
mtd.Call([]reflect.Value{ctxVal, reflect.ValueOf(arg)})
} else {
@ -160,6 +164,10 @@ func (s *Server) execute(pCtx context.Context, typ string, name string, arg any,
}
case 1: // If method has one return value
if mtdType.NumIn() == 2 {
if arg == nil {
return nil, nil, ErrArgNotProvided
}
// Call method with arg, get returned values
out := mtd.Call([]reflect.Value{ctxVal, reflect.ValueOf(arg)})
@ -194,6 +202,10 @@ func (s *Server) execute(pCtx context.Context, typ string, name string, arg any,
}
case 2: // If method has two return values
if mtdType.NumIn() == 2 {
if arg == nil {
return nil, nil, ErrArgNotProvided
}
// Call method with arg and get returned values
out := mtd.Call([]reflect.Value{ctxVal, reflect.ValueOf(arg)})