Skip to content

Commit e642e94

Browse files
committed
fix argument evaluation when calling variadic functions
fixes #171
1 parent f67efbc commit e642e94

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

eval.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,8 +1278,7 @@ func (st *Runtime) evaluateArgs(fnType reflect.Type, args CallArgs, pipedArg *re
12781278
}
12791279
}
12801280

1281-
slot, i := 0, 0
1282-
var term reflect.Value
1281+
slot := 0 // index in argument values (evaluated expressions combined with piped argument if applicable)
12831282

12841283
if !args.HasPipeSlot && pipedArg != nil {
12851284
in := fnType.In(slot)
@@ -1290,8 +1289,11 @@ func (st *Runtime) evaluateArgs(fnType reflect.Type, args CallArgs, pipedArg *re
12901289
slot++
12911290
}
12921291

1293-
for i < len(args.Exprs) {
1292+
i := 0 // index in parsed argument expression list
1293+
1294+
for slot < numArgsRequired {
12941295
in := fnType.In(slot)
1296+
var term reflect.Value
12951297
if args.Exprs[i].Type() == NodeUnderscore {
12961298
term = *pipedArg
12971299
} else {
@@ -1308,6 +1310,7 @@ func (st *Runtime) evaluateArgs(fnType reflect.Type, args CallArgs, pipedArg *re
13081310
if isVariadic {
13091311
in := fnType.In(numArgsRequired).Elem()
13101312
for i < len(args.Exprs) {
1313+
var term reflect.Value
13111314
if args.Exprs[i].Type() == NodeUnderscore {
13121315
term = *pipedArg
13131316
} else {

eval_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,13 @@ func TestEvalActionNode(t *testing.T) {
162162
"José Santos", "[email protected]",
163163
})
164164

165+
data.Set("print", fmt.Sprint)
166+
data.Set("printf", fmt.Sprintf)
167+
165168
RunJetTest(t, nil, nil, "actionNode", `hello {{"world"}}`, `hello world`)
166169
RunJetTest(t, data, nil, "actionNode_func", `hello {{lower: "WORLD"}}`, `hello world`)
170+
RunJetTest(t, data, nil, "actionNode_func_variadic", `{{ print("hello world") }}`, `hello world`)
171+
RunJetTest(t, data, nil, "actionNode_func_variadic2", `{{ printf("hello %s", "world") }}`, `hello world`)
167172
RunJetTest(t, data, nil, "actionNode_funcPipe", `hello {{lower: "WORLD" |upper}}`, `hello WORLD`)
168173
RunJetTest(t, data, nil, "actionNode_funcPipe_parens", `{{ "foo" | repeat(2) }}`, `foofoo`)
169174
RunJetTest(t, data, nil, "actionNode_funcPipe_parens2", `hello {{lower ( "WORLD" ) |upper}}`, `hello WORLD`)

0 commit comments

Comments
 (0)