Skip to content

Commit

Permalink
Merge branch 'issue-10'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauricio Klein committed Oct 25, 2019
2 parents 1bfbdbe + bfa75b6 commit 91bd0c2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
12 changes: 9 additions & 3 deletions chainable.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (lk *link) process(linkIndex int, args []Argument) ([]Argument, error) {

// call the function
out := []Argument{}
for _, o := range vfn.Call(reflectArgs(args)) {
for _, o := range vfn.Call(reflectArgs(vfnType, args)) {
out = append(out, o.Interface())
}

Expand Down Expand Up @@ -147,11 +147,17 @@ func validateArgs(linkIndex int, fn reflect.Type, args []Argument) error {

// reflectArgs transforms the args list in a list of
// reflect.Value, used to call a function using reflection
func reflectArgs(args []Argument) []reflect.Value {
func reflectArgs(fnType reflect.Type, args []Argument) []reflect.Value {
in := make([]reflect.Value, len(args))

for k, arg := range args {
in[k] = reflect.ValueOf(arg)
if arg == nil {
// Use the zero value of the function parameter type,
// since "reflect.Call" doesn't accept "nil" parameters
in[k] = reflect.New(fnType.In(k)).Elem()
} else {
in[k] = reflect.ValueOf(arg)
}
}

return in
Expand Down
33 changes: 26 additions & 7 deletions chainable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ func TestChain(t *testing.T) {
returnValue: []Argument{genericStruct{}},
err: nil,
},
{
desc: "With 'nil' value feeded to the chain",
from: []Argument{1, 2, nil},
funcs: []Function{
func(a, b int, e error) (int, int, error) { return a, b, e },
func(a, b int) (int, int) { return a, b },
},
returnValue: []Argument{1, 2},
err: nil,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -140,23 +150,22 @@ func TestChainDummy(t *testing.T) {
},
{
desc: "With cascading error",
from: []Argument{},
from: []Argument{errGeneric},
funcs: []Function{
func() error { return errGeneric },
func(e error) error { return e },
func(e error) error { return e },
},
returnValue: []Argument{errGeneric},
err: nil,
},
{
desc: "With cascading error",
from: []Argument{errGeneric},
desc: "Without argument feedback",
from: []Argument{},
funcs: []Function{
func(e error) error { return e },
func(e error) error { return e },
func() {},
func() {},
},
returnValue: []Argument{errGeneric},
returnValue: []Argument{},
err: nil,
},
{
Expand All @@ -179,6 +188,16 @@ func TestChainDummy(t *testing.T) {
returnValue: []Argument{genericStruct{}, errGeneric},
err: nil,
},
{
desc: "With 'nil' value feeded to the chain",
from: []Argument{1, 2, nil},
funcs: []Function{
func(a, b int, e error) (int, int, error) { return a, b, e },
func(a, b int, e error) (int, int, error) { return a, b, e },
},
returnValue: []Argument{1, 2, nil},
err: nil,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 91bd0c2

Please sign in to comment.