@@ -535,6 +535,57 @@ func (s *scheduler) NewJob(jobDefinition JobDefinition, task Task, options ...Jo
535
535
return s .addOrUpdateJob (uuid .Nil , jobDefinition , task , options )
536
536
}
537
537
538
+ func (s * scheduler ) verifyVariadic (taskFunc reflect.Value , tsk task , variadicStart int ) error {
539
+ if err := s .verifyNonVariadic (taskFunc , tsk , variadicStart ); err != nil {
540
+ return err
541
+ }
542
+ parameterType := taskFunc .Type ().In (variadicStart ).Elem ().Kind ()
543
+ if parameterType == reflect .Interface || parameterType == reflect .Pointer {
544
+ parameterType = reflect .Indirect (reflect .ValueOf (taskFunc .Type ().In (variadicStart ))).Kind ()
545
+ }
546
+
547
+ for i := variadicStart ; i < len (tsk .parameters ); i ++ {
548
+ argumentType := reflect .TypeOf (tsk .parameters [i ]).Kind ()
549
+ if argumentType == reflect .Interface || argumentType == reflect .Pointer {
550
+ argumentType = reflect .TypeOf (tsk .parameters [i ]).Elem ().Kind ()
551
+ }
552
+ if argumentType != parameterType {
553
+ return ErrNewJobWrongTypeOfParameters
554
+ }
555
+ }
556
+ return nil
557
+ }
558
+
559
+ func (s * scheduler ) verifyNonVariadic (taskFunc reflect.Value , tsk task , length int ) error {
560
+ for i := 0 ; i < length ; i ++ {
561
+ t1 := reflect .TypeOf (tsk .parameters [i ]).Kind ()
562
+ if t1 == reflect .Interface || t1 == reflect .Pointer {
563
+ t1 = reflect .TypeOf (tsk .parameters [i ]).Elem ().Kind ()
564
+ }
565
+ t2 := reflect .New (taskFunc .Type ().In (i )).Elem ().Kind ()
566
+ if t2 == reflect .Interface || t2 == reflect .Pointer {
567
+ t2 = reflect .Indirect (reflect .ValueOf (taskFunc .Type ().In (i ))).Kind ()
568
+ }
569
+ if t1 != t2 {
570
+ return ErrNewJobWrongTypeOfParameters
571
+ }
572
+ }
573
+ return nil
574
+ }
575
+
576
+ func (s * scheduler ) verifyParameterType (taskFunc reflect.Value , tsk task ) error {
577
+ isVariadic := taskFunc .Type ().IsVariadic ()
578
+ if isVariadic {
579
+ variadicStart := taskFunc .Type ().NumIn () - 1
580
+ return s .verifyVariadic (taskFunc , tsk , variadicStart )
581
+ }
582
+ expectedParameterLength := taskFunc .Type ().NumIn ()
583
+ if len (tsk .parameters ) != expectedParameterLength {
584
+ return ErrNewJobWrongNumberOfParameters
585
+ }
586
+ return s .verifyNonVariadic (taskFunc , tsk , expectedParameterLength )
587
+ }
588
+
538
589
func (s * scheduler ) addOrUpdateJob (id uuid.UUID , definition JobDefinition , taskWrapper Task , options []JobOption ) (Job , error ) {
539
590
j := internalJob {}
540
591
if id == uuid .Nil {
@@ -569,23 +620,8 @@ func (s *scheduler) addOrUpdateJob(id uuid.UUID, definition JobDefinition, taskW
569
620
return nil , ErrNewJobTaskNotFunc
570
621
}
571
622
572
- expectedParameterLength := taskFunc .Type ().NumIn ()
573
- if len (tsk .parameters ) != expectedParameterLength {
574
- return nil , ErrNewJobWrongNumberOfParameters
575
- }
576
-
577
- for i := 0 ; i < expectedParameterLength ; i ++ {
578
- t1 := reflect .TypeOf (tsk .parameters [i ]).Kind ()
579
- if t1 == reflect .Interface || t1 == reflect .Pointer {
580
- t1 = reflect .TypeOf (tsk .parameters [i ]).Elem ().Kind ()
581
- }
582
- t2 := reflect .New (taskFunc .Type ().In (i )).Elem ().Kind ()
583
- if t2 == reflect .Interface || t2 == reflect .Pointer {
584
- t2 = reflect .Indirect (reflect .ValueOf (taskFunc .Type ().In (i ))).Kind ()
585
- }
586
- if t1 != t2 {
587
- return nil , ErrNewJobWrongTypeOfParameters
588
- }
623
+ if err := s .verifyParameterType (taskFunc , tsk ); err != nil {
624
+ return nil , err
589
625
}
590
626
591
627
j .name = runtime .FuncForPC (taskFunc .Pointer ()).Name ()
0 commit comments