Skip to content

Commit

Permalink
fix: nil pointer
Browse files Browse the repository at this point in the history
Signed-off-by: Eray Ates <eates23@gmail.com>
  • Loading branch information
rytsh committed Mar 20, 2023
1 parent 2ca51ba commit e277133
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![License](https://img.shields.io/github/license/rytsh/call?color=red&style=flat-square)](https://raw.githubusercontent.com/rytsh/call/main/LICENSE)
[![Sonar Coverage](https://img.shields.io/sonar/coverage/rytsh_call?logo=sonarcloud&server=https%3A%2F%2Fsonarcloud.io&style=flat-square)](https://sonarcloud.io/summary/overall?id=rytsh_call)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/rytsh/call/Test?logo=github&style=flat-square&label=ci)](https://github.com/rytsh/call/actions)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/rytsh/call/test.yml?branch=main&logo=github&style=flat-square&label=ci)](https://github.com/rytsh/call/actions)
[![Go Report Card](https://goreportcard.com/badge/github.com/rytsh/call?style=flat-square)](https://goreportcard.com/report/github.com/rytsh/call)
[![Go PKG](https://raw.githubusercontent.com/rytsh/call/pages/assets/reference.svg)](https://pkg.go.dev/github.com/rytsh/call)
[![Web](https://img.shields.io/badge/web-document-blueviolet?style=flat-square)](https://rytsh.github.io/call/)
Expand Down
20 changes: 14 additions & 6 deletions call.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,24 @@ func (r *Reg) CallWithArgs(name string, args ...string) ([]any, error) {
if f.Fn.Type().IsVariadic() {
fnArgType := f.Fn.Type().In(f.Fn.Type().NumIn() - 1).Elem()
for i := f.Fn.Type().NumIn() - 1; i < len(fnArgs); i++ {
argType := fnArgs[i].Type()
if !argType.AssignableTo(fnArgType) {
return nil, fmt.Errorf("variadic function: index %d argument %s type mismatch with function %s type", i, argType, fnArgType)
if fnArgs[i].IsValid() {
argType := fnArgs[i].Type()
if !argType.AssignableTo(fnArgType) {
return nil, fmt.Errorf("variadic function: index %d argument %s type mismatch with function %s type", i, argType, fnArgType)
}
} else {
fnArgs[i] = reflect.Zero(fnArgType)
}
}
} else if f.Fn.Type().NumIn() > 0 {
fnArgType := f.Fn.Type().In(f.Fn.Type().NumIn() - 1)
argType := fnArgs[len(fnArgs)-1].Type()
if !argType.AssignableTo(fnArgType) {
return nil, fmt.Errorf("function: index %d argument %s type mismatch with function %s type", len(fnArgs)-1, argType, fnArgType)
if fnArgs[len(fnArgs)-1].IsValid() {
argType := fnArgs[len(fnArgs)-1].Type()
if !argType.AssignableTo(fnArgType) {
return nil, fmt.Errorf("function: index %d argument %s type mismatch with function %s type", len(fnArgs)-1, argType, fnArgType)
}
} else {
fnArgs[len(fnArgs)-1] = reflect.Zero(fnArgType)
}
}

Expand Down
42 changes: 42 additions & 0 deletions call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,48 @@ func TestReg_CallWithArgs(t *testing.T) {
wantErr: true,
wantErrStr: "function: index 2 argument int type mismatch with function string type",
},
{
name: "argument nil",
modify: func(r *Reg) {
r.AddFunction("test", func(v string, _ interface{}) string { return v })
r.AddArgument("test-1", "test-1").AddArgument("test-2", nil)
},
args: args{
name: "test",
args: []string{"test-1", "test-2"},
},
want: []any{
"test-1",
},
},
{
name: "variadic functions nil",
modify: func(r *Reg) {
r.AddFunction("test", func(v string, _ ...interface{}) string { return v })
r.AddArgument("test-1", "test-1").AddArgument("test-2", nil)
},
args: args{
name: "test",
args: []string{"test-1", "test-2"},
},
want: []any{
"test-1",
},
},
{
name: "variadic functions nil without value",
modify: func(r *Reg) {
r.AddFunction("test", func(v string, _ ...interface{}) string { return v })
r.AddArgument("test-1", "test-1")
},
args: args{
name: "test",
args: []string{"test-1"},
},
want: []any{
"test-1",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit e277133

Please sign in to comment.