Skip to content

Commit d4fa675

Browse files
authored
add support for pointer parameters in functions (#22)
1 parent 1abd257 commit d4fa675

4 files changed

Lines changed: 142 additions & 4 deletions

File tree

core/runner.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/siherrmann/queuer/helper"
1212
"github.com/siherrmann/queuer/model"
13+
vh "github.com/siherrmann/validator/helper"
1314
)
1415

1516
type Runner struct {
@@ -35,12 +36,11 @@ func NewRunner(options *model.Options, task interface{}, parameters ...interface
3536
}
3637

3738
for i, param := range parameters {
38-
// Convert json float to int if the parameter is int
39-
if taskInputParameters[i].Kind() == reflect.Int && reflect.TypeOf(param).Kind() == reflect.Float64 {
40-
parameters[i] = int(param.(float64))
41-
} else if taskInputParameters[i].Kind() != reflect.TypeOf(param).Kind() {
39+
paramConverted, err := vh.AnyToType(param, taskInputParameters[i])
40+
if err != nil {
4241
return nil, fmt.Errorf("parameter %d of task must be of type %s, got %s", i, taskInputParameters[i].Kind(), reflect.TypeOf(param).Kind())
4342
}
43+
parameters[i] = paramConverted
4444
}
4545

4646
err = helper.CheckValidTaskWithParameters(task, parameters...)

core/runner_test.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,138 @@ outerLoop:
425425
}
426426
}
427427
}
428+
429+
// Test structs for struct parameter tests
430+
type TestConfig struct {
431+
Name string
432+
Value int
433+
}
434+
435+
type TestData struct {
436+
ID int
437+
Message string
438+
Active bool
439+
}
440+
441+
// Task function that accepts a struct and pointer to struct
442+
func taskWithStructParams(config TestConfig, data *TestData) (string, error) {
443+
if data == nil {
444+
return "", fmt.Errorf("data cannot be nil")
445+
}
446+
return fmt.Sprintf("%s-%d: %s (ID: %d, Active: %t)", config.Name, config.Value, data.Message, data.ID, data.Active), nil
447+
}
448+
449+
// TestNewRunnerWithStructParameters tests the runner with struct and pointer-to-struct parameters
450+
func TestNewRunnerWithStructParameters(t *testing.T) {
451+
tests := []struct {
452+
name string
453+
param1 interface{}
454+
param2 interface{}
455+
wantErr bool
456+
wantResult string
457+
}{
458+
{
459+
name: "Valid Struct and Pointer",
460+
param1: TestConfig{
461+
Name: "config",
462+
Value: 42,
463+
},
464+
param2: &TestData{
465+
ID: 1,
466+
Message: "test message",
467+
Active: true,
468+
},
469+
wantErr: false,
470+
wantResult: "config-42: test message (ID: 1, Active: true)",
471+
},
472+
{
473+
name: "Map to Struct and Map to Pointer Conversion",
474+
param1: map[string]interface{}{
475+
"Name": "fromMap",
476+
"Value": 99,
477+
},
478+
param2: map[string]interface{}{
479+
"ID": 2,
480+
"Message": "converted from map",
481+
"Active": false,
482+
},
483+
wantErr: false,
484+
wantResult: "fromMap-99: converted from map (ID: 2, Active: false)",
485+
},
486+
{
487+
name: "Struct Value and Map to Pointer",
488+
param1: TestConfig{
489+
Name: "direct",
490+
Value: 10,
491+
},
492+
param2: map[string]interface{}{
493+
"ID": 3,
494+
"Message": "mixed params",
495+
"Active": true,
496+
},
497+
wantErr: false,
498+
wantResult: "direct-10: mixed params (ID: 3, Active: true)",
499+
},
500+
{
501+
name: "Map to Struct with Type Conversion",
502+
param1: map[string]interface{}{
503+
"Name": "typeConv",
504+
"Value": 25.0, // float64 should convert to int
505+
},
506+
param2: map[string]interface{}{
507+
"ID": 4.0, // float64 should convert to int
508+
"Message": "type conversion test",
509+
"Active": true,
510+
},
511+
wantErr: false,
512+
wantResult: "typeConv-25: type conversion test (ID: 4, Active: true)",
513+
},
514+
}
515+
516+
for _, test := range tests {
517+
t.Run(test.name, func(t *testing.T) {
518+
mockTask, err := model.NewTask(taskWithStructParams)
519+
require.NoError(t, err, "Failed to create mock task")
520+
521+
job := &model.Job{
522+
TaskName: mockTask.Name,
523+
Parameters: []interface{}{
524+
test.param1,
525+
test.param2,
526+
},
527+
Options: &model.Options{
528+
OnError: &model.OnError{
529+
Timeout: 5.0,
530+
},
531+
},
532+
}
533+
534+
runner, err := NewRunnerFromJob(mockTask, job)
535+
if test.wantErr {
536+
assert.Error(t, err, "NewRunnerFromJob should return an error")
537+
return
538+
}
539+
540+
require.NoError(t, err, "NewRunnerFromJob should not return an error")
541+
assert.NotNil(t, runner)
542+
543+
go runner.Run(context.Background())
544+
545+
outerLoop:
546+
for {
547+
select {
548+
case err := <-runner.ErrorChannel:
549+
assert.NoError(t, err, "Runner should not return an error")
550+
break outerLoop
551+
case results := <-runner.ResultsChannel:
552+
assert.NotNil(t, results)
553+
assert.Len(t, results, 1)
554+
assert.Equal(t, test.wantResult, results[0])
555+
break outerLoop
556+
case <-time.After(2 * time.Second):
557+
t.Fatal("Test timed out")
558+
}
559+
}
560+
})
561+
}
562+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ require (
6868
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
6969
github.com/shirou/gopsutil/v4 v4.25.10 // indirect
7070
github.com/siherrmann/queuerSql v0.0.0-20251130135331-9ed23b19fae5
71+
github.com/siherrmann/validator v0.11.0
7172
github.com/sirupsen/logrus v1.9.3 // indirect
7273
github.com/spf13/cobra v1.10.1
7374
github.com/tklauser/go-sysconf v0.3.16 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7
127127
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
128128
github.com/shirou/gopsutil/v4 v4.25.10 h1:at8lk/5T1OgtuCp+AwrDofFRjnvosn0nkN2OLQ6g8tA=
129129
github.com/shirou/gopsutil/v4 v4.25.10/go.mod h1:+kSwyC8DRUD9XXEHCAFjK+0nuArFJM0lva+StQAcskM=
130+
github.com/siherrmann/validator v0.11.0 h1:DTLzGvwZUJwhr842xZkX/m7ed4DMFKJ9AKoYDk4+cNs=
131+
github.com/siherrmann/validator v0.11.0/go.mod h1:LqNNViIg5YKzO4SX4zMdafyffL6xCWuRqSyt1szpolo=
130132
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
131133
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
132134
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=

0 commit comments

Comments
 (0)