Skip to content

Commit

Permalink
Add list support for Pipeline to AST
Browse files Browse the repository at this point in the history
  • Loading branch information
goller committed Oct 18, 2017
1 parent 062a5db commit fdb98c8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
25 changes: 24 additions & 1 deletion pipeline/tick/ast.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
package tick

import (
"bytes"

"github.com/influxdata/kapacitor/tick/ast"
)

// AST converts a pipeline into an AST
type AST struct {
Node ast.Node
}

// TICKScript produces a TICKScript from the AST
func (a *AST) TICKScript() string {
var buf bytes.Buffer
a.Node.Format(&buf, "", false)
return buf.String()
}

// PipeFunction produces an ast.FunctionNode within a Pipe Chain. May return
// the left node if all args evaluate to the zero value
func PipeFunction(left ast.Node, name string, args ...interface{}) (ast.Node, error) {
Expand Down Expand Up @@ -87,9 +101,10 @@ func Function(name string, args ...interface{}) (ast.Node, error) {
astArgs := []ast.Node{}
for _, arg := range args {
// Skip zero values as they don't need to be rendered
if arg == ast.ZeroValue(ast.TypeOf(arg)) {
if IsZero(arg) {
continue
}

lit, err := Literal(arg)
if err != nil {
return nil, err
Expand All @@ -108,6 +123,14 @@ func Function(name string, args ...interface{}) (ast.Node, error) {
}, nil
}

func IsZero(arg interface{}) bool {
typeOf := ast.TypeOf(arg)
if typeOf == ast.TList {
return len(arg.([]interface{})) == 0
}
return arg == ast.ZeroValue(typeOf)
}

// Literal produces an ast Literal (NumberNode, etc).
func Literal(lit interface{}) (ast.Node, error) {
return ast.ValueToLiteralNode(&NullPosition{}, lit)
Expand Down
15 changes: 0 additions & 15 deletions pipeline/tick/window.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
package tick

import (
"bytes"

"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/tick/ast"
)

// AST converts a pipeline into an AST
type AST struct {
Node ast.Node
}

// TICKScript produces a TICKScript from the AST
func (a *AST) TICKScript() string {
var buf bytes.Buffer
a.Node.Format(&buf, "", false)
return buf.String()
}

// Window converts the window pipeline node into the TICKScript AST
func (a *AST) Window(w *pipeline.WindowNode) *AST {
// TODO: Handle the err
Expand Down
4 changes: 4 additions & 0 deletions tick/ast/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ func ValueToLiteralNode(pos Position, v interface{}) (Node, error) {
position: p,
Expression: e,
}, nil
case *StarNode:
return &StarNode{
position: p,
}, nil
case []interface{}:
nodes := make([]Node, len(value))
var err error
Expand Down

0 comments on commit fdb98c8

Please sign in to comment.