Skip to content

Commit

Permalink
issue-756: fix interface variadic parameters verifying (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
apocelipes authored Jul 17, 2024
1 parent 2c08083 commit f37e274
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
15 changes: 14 additions & 1 deletion scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,12 +535,25 @@ func (s *scheduler) NewJob(jobDefinition JobDefinition, task Task, options ...Jo
return s.addOrUpdateJob(uuid.Nil, jobDefinition, task, options)
}

func (s *scheduler) verifyInterfaceVariadic(taskFunc reflect.Value, tsk task, variadicStart int) error {
ifaceType := taskFunc.Type().In(variadicStart).Elem()
for i := variadicStart; i < len(tsk.parameters); i++ {
if !reflect.TypeOf(tsk.parameters[i]).Implements(ifaceType) {
return ErrNewJobWrongTypeOfParameters
}
}
return nil
}

func (s *scheduler) verifyVariadic(taskFunc reflect.Value, tsk task, variadicStart int) error {
if err := s.verifyNonVariadic(taskFunc, tsk, variadicStart); err != nil {
return err
}
parameterType := taskFunc.Type().In(variadicStart).Elem().Kind()
if parameterType == reflect.Interface || parameterType == reflect.Pointer {
if parameterType == reflect.Interface {
return s.verifyInterfaceVariadic(taskFunc, tsk, variadicStart)
}
if parameterType == reflect.Pointer {
parameterType = reflect.Indirect(reflect.ValueOf(taskFunc.Type().In(variadicStart))).Kind()
}

Expand Down
11 changes: 11 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -924,6 +925,16 @@ func TestScheduler_NewJobTask(t *testing.T) {
NewTask(func(args ...interface{}) {}),
nil,
},
{
"all good - interface variadic, int, string",
NewTask(func(args ...interface{}) {}, 1, "2", 3.0),
nil,
},
{
"parameter type does not match - different argument types against interface variadic parameters",
NewTask(func(args ...io.Reader) {}, os.Stdout, any(3.0)),
ErrNewJobWrongTypeOfParameters,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit f37e274

Please sign in to comment.