Skip to content

Commit

Permalink
fix issue where function params could not be expressions (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel Cook authored Jun 18, 2016
1 parent 0086059 commit 82755a1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 9 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## v1.0.0-beta2 [unreleased]

### Release Notes

### Features

### Bugfixes

- [#656](https://github.com/influxdata/kapacitor/pull/656): Fix issues where an expression could not be passed as a function parameter in TICKscript.

## v1.0.0-beta2 [2016-06-17]

### Release Notes
Expand Down
10 changes: 1 addition & 9 deletions tick/ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,7 @@ func (p *parser) parameters() (args []Node) {
}

func (p *parser) parameter() (n Node) {
switch p.peek().typ {
case TokenIdent:
n = p.expression()
case TokenLambda:
n = p.lambda()
default:
n = p.primary()
}
return
return p.expression()
}

//parse a string list
Expand Down
74 changes: 74 additions & 0 deletions tick/ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,80 @@ func TestParseStatements(t *testing.T) {
},
},
},
{
script: `f('first ' + string(5m) + 'third')`,
Root: &ProgramNode{
position: position{
pos: 0,
line: 1,
char: 1,
},
Nodes: []Node{
&FunctionNode{
position: position{
pos: 0,
line: 1,
char: 1,
},
Type: GlobalFunc,
Func: "f",
Args: []Node{
&BinaryNode{
position: position{
pos: 24,
line: 1,
char: 25,
},
Operator: TokenPlus,
Left: &BinaryNode{
position: position{
pos: 11,
line: 1,
char: 12,
},
Operator: TokenPlus,
Left: &StringNode{
position: position{
pos: 2,
line: 1,
char: 3,
},
Literal: "first ",
},
Right: &FunctionNode{
position: position{
pos: 13,
line: 1,
char: 14,
},
Type: GlobalFunc,
Func: "string",
Args: []Node{
&DurationNode{
position: position{
pos: 20,
line: 1,
char: 21,
},
Dur: 5 * time.Minute,
},
},
},
},
Right: &StringNode{
position: position{
pos: 26,
line: 1,
char: 27,
},
Literal: "third",
},
},
},
},
},
},
},
{
script: `var x = 5
var y = x * 2`,
Expand Down
19 changes: 19 additions & 0 deletions tick/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,25 @@ f("asdf")
}
}

func TestEvaluate_Func_Expression_Parameter(t *testing.T) {
script := `
f('asdf' + string(10) + 'qwerty')
`

f := func(got string) (interface{}, error) {
if exp := "asdf10qwerty"; got != exp {
t.Errorf("unexpected arg to function: got %s exp %s", got, exp)
}
return nil, nil
}
scope := stateful.NewScope()
scope.Set("f", f)

if _, err := tick.Evaluate(script, scope, nil, false); err != nil {
t.Fatal(err)
}
}

//------------------------------------
// Types for TestReflectionDescriber
//
Expand Down

0 comments on commit 82755a1

Please sign in to comment.