Skip to content

Commit

Permalink
Merge pull request PaesslerAG#84 from cortezaproject/fix-allowing-nil…
Browse files Browse the repository at this point in the history
…-as-argument

Allow nil arguments for function
  • Loading branch information
generikvault committed Sep 21, 2022
2 parents 02d92bf + 30e2ce1 commit ecdb989
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ func createCallArguments(ctx context.Context, t reflect.Type, args []interface{}
inType = t.In(numIn - 1).Elem()
}
argVal := reflect.ValueOf(arg)
if arg == nil || !argVal.Type().AssignableTo(inType) {
if arg == nil {
argVal = reflect.ValueOf(reflect.Interface)
} else if !argVal.Type().AssignableTo(inType) {
return nil, fmt.Errorf("expected type %s for parameter %d but got %T",
inType.String(), i, arg)
}
Expand Down
11 changes: 11 additions & 0 deletions functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ func Test_toFunc(t *testing.T) {
},
wantErr: context.DeadlineExceeded,
},
{
name: "nil arg",
function: func(a interface{}) bool {
if a != nil {
return true
}
return false
},
arguments: []interface{}{nil},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit ecdb989

Please sign in to comment.