Skip to content

Commit

Permalink
Allow nil arguments for function
Browse files Browse the repository at this point in the history
It uses reflect value for interface when argument is resolved as nil.
  • Loading branch information
vicpatel committed Aug 31, 2022
1 parent 8fe7bad commit 30e2ce1
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 @@ -104,7 +104,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 30e2ce1

Please sign in to comment.