Documentation
¶
Overview ¶
TICKscript is a simple invocation chaining DSL. See the examples for how its used and example syntax of the DSL.
A reflection based evaluation of an AST.
Index ¶
- Variables
- func Evaluate(script string, scope *Scope) (err error)
- type BinaryNode
- type BoolNode
- type DurationNode
- type Func
- type Funcs
- type FunctionNode
- type IdentifierNode
- type LambdaNode
- type ListNode
- type Node
- type NumberNode
- type ReferenceNode
- type RegexNode
- type Scope
- type StarNode
- type StatefulExpr
- type StringNode
- type UnaryNode
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyStack = errors.New("stack is empty")
var ErrInvalidExpr = errors.New("expression is invalid, could not evaluate")
var ErrNotFloat = errors.New("value is not a float")
Functions ¶
func Evaluate ¶
Parse and evaluate a given script for the scope. This evaluation method uses reflection to call methods on objects within the scope.
Example ¶
package main
import (
"fmt"
"github.com/influxdb/kapacitor/tick"
)
type Process struct {
Name string
Children []*Process
}
func (p *Process) Spawn() *Process {
child := &Process{}
p.Children = append(p.Children, child)
return child
}
func (p *Process) String() string {
return fmt.Sprintf("{%q %s}", p.Name, p.Children)
}
func main() {
//Run a test that evaluates the DSL against the Process struct.
script := `
//Name the parent
parent.name('parent')
// Spawn a first child
var child1 = parent.spawn()
// Name the first child
child1.name('child1')
//Spawn a grandchild and name it
child1.spawn().name('grandchild')
//Spawn a second child and name it
parent.spawn().name('child2')
`
scope := tick.NewScope()
parent := &Process{}
scope.Set("parent", parent)
err := tick.Evaluate(script, scope)
if err != nil {
fmt.Println(err)
}
fmt.Println(parent)
}
Output: {"parent" [{"child1" [{"grandchild" []}]} {"child2" []}]}
Types ¶
type BinaryNode ¶
type BinaryNode struct {
Left Node
Right Node
Operator tokenType
// contains filtered or unexported fields
}
binaryNode holds two arguments and an operator.
func (*BinaryNode) String ¶
func (b *BinaryNode) String() string
type BoolNode ¶
type BoolNode struct {
Bool bool
// contains filtered or unexported fields
}
boolNode holds one argument and an operator.
type DurationNode ¶
type DurationNode struct {
Dur time.Duration //the duration
// contains filtered or unexported fields
}
durationNode holds a number: signed or unsigned integer or float. The value is parsed and stored under all the types that can represent the value. This simulates in a small amount of code the behavior of Go's ideal constants.
func (*DurationNode) String ¶
func (d *DurationNode) String() string
type Func ¶
type Func interface {
Reset()
Call(...interface{}) (interface{}, error)
}
A callable function from within the expression
type FunctionNode ¶
type FunctionNode struct {
Func string // The identifier
Args []Node
// contains filtered or unexported fields
}
Holds the a function call with its args
func (*FunctionNode) String ¶
func (f *FunctionNode) String() string
type IdentifierNode ¶
type IdentifierNode struct {
Ident string // The identifier
// contains filtered or unexported fields
}
Holds the textual representation of an identifier
func (*IdentifierNode) String ¶
func (i *IdentifierNode) String() string
type LambdaNode ¶
type LambdaNode struct {
Node Node
// contains filtered or unexported fields
}
Represents the begining of a lambda expression
func (*LambdaNode) String ¶
func (l *LambdaNode) String() string
type ListNode ¶
type ListNode struct {
Nodes []Node
// contains filtered or unexported fields
}
Holds a function call with its args
type NumberNode ¶
type NumberNode struct {
IsInt bool // Number has an integral value.
IsFloat bool // Number has a floating-point value.
Int64 int64 // The integer value.
Float64 float64 // The floating-point value.
// contains filtered or unexported fields
}
numberNode holds a number: signed or unsigned integer or float. The value is parsed and stored under all the types that can represent the value. This simulates in a small amount of code the behavior of Go's ideal constants.
func (*NumberNode) String ¶
func (n *NumberNode) String() string
type ReferenceNode ¶
type ReferenceNode struct {
Reference string // The field reference
// contains filtered or unexported fields
}
Holds the textual representation of an identifier
func (*ReferenceNode) String ¶
func (r *ReferenceNode) String() string
type Scope ¶
type Scope struct {
// contains filtered or unexported fields
}
Contains a set of variables references and their values.
type StarNode ¶
type StarNode struct {
// contains filtered or unexported fields
}
Represents a standalone '*' token.
type StatefulExpr ¶
Expression functions are stateful. Their state is updated with each call to the function. A StatefulExpr is a Node and its associated function state.
func NewStatefulExpr ¶
func NewStatefulExpr(n Node) *StatefulExpr
type StringNode ¶
type StringNode struct {
Literal string // The string literal
// contains filtered or unexported fields
}
Holds the textual representation of a string literal
func (*StringNode) String ¶
func (s *StringNode) String() string