From 30e2ce1811fdc01ca8350c03bf052c358df302ca Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Fri, 29 Jul 2022 17:31:12 +0530 Subject: [PATCH] Allow nil arguments for function It uses reflect value for interface when argument is resolved as nil. --- functions.go | 4 +++- functions_test.go | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/functions.go b/functions.go index 4b4a3a0..10cf3cb 100644 --- a/functions.go +++ b/functions.go @@ -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) } diff --git a/functions_test.go b/functions_test.go index 7bcf07c..c12b1a7 100644 --- a/functions_test.go +++ b/functions_test.go @@ -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) {