tick

package
v1.2.0-rc2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2017 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

TICKscript is a simple invocation chaining DSL.

See the examples for how its used and example syntax of the DSL.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrEmptyStack = errors.New("stack is empty")

Functions

func Evaluate

func Evaluate(script string, scope *stateful.Scope, predefinedVars map[string]Var, ignoreMissingVars bool) (_ map[string]Var, err error)

Parse and evaluate a given script for the scope. Returns a set of default vars. If a set of predefined vars is provided, they may effect the default var values.

Example
package main

import (
	"fmt"

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

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 := stateful.NewScope()
	parent := &Process{}
	scope.Set("parent", parent)

	_, err := Evaluate(script, scope, nil, false)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(parent)
}
Output:

{"parent" [{"child1" [{"grandchild" []}]} {"child2" []}]}

func Format added in v0.12.0

func Format(script string) (string, error)

Formats a TICKscript according to the standard.

func SetLogger added in v0.12.0

func SetLogger(l *log.Logger)

Types

type PartialDescriber added in v0.13.0

type PartialDescriber interface {
	ChainMethods() map[string]reflect.Value
}

PartialDescriber can provide a description of its chain methods that hide embedded property methods.

type ReflectionDescriber added in v0.10.0

type ReflectionDescriber struct {
	// contains filtered or unexported fields
}

Wraps any object as a SelfDescriber using reflection.

Uses tags on fields to determine if a method is really a PropertyMethod Can disambiguate property fields and chain methods of the same name but from different composed anonymous fields. Cannot disambiguate property methods and chain methods of the same name. See NewReflectionDescriber for providing explicit chain methods in this case.

Example:

type MyType struct {
    UseX `tick:"X"`
}
func (m *MyType) X() *MyType{
    m.UseX = true
    return m
}

UseX will be ignored as a property and the method X will become a property method.

Expects that all callable methods are pointer receiver methods.

func NewReflectionDescriber added in v0.10.0

func NewReflectionDescriber(obj interface{}, chainMethods map[string]reflect.Value) (*ReflectionDescriber, error)

Create a NewReflectionDescriber from an object. The object must be a pointer type. Use the chainMethods parameter to provide a set of explicit methods that should be considered chain methods even if an embedded type declares them as property methods

Example:

type MyType struct {
    UseX `tick:"X"`
}
func (m *MyType) X() *MyType{
    m.UseX = true
    return m
}

type AnotherType struct {
    MyType
}
func (a *AnotherType) X() *YetAnotherType {
    // do chain method work here...
}

// Now create NewReflectionDescriber with X as a chain method and property method
at := new(AnotherType)
rd := NewReflectionDescriber(at, map[string]reflect.Value{
    "X": reflect.ValueOf(at.X),
})
rd.HasProperty("x") // true
rd.HasChainMethod("x") // true

func (*ReflectionDescriber) CallChainMethod added in v0.12.0

func (r *ReflectionDescriber) CallChainMethod(name string, args ...interface{}) (interface{}, error)

func (*ReflectionDescriber) Desc added in v0.10.0

func (r *ReflectionDescriber) Desc() string

func (*ReflectionDescriber) HasChainMethod added in v0.12.0

func (r *ReflectionDescriber) HasChainMethod(name string) bool

Using reflection check if the object has the method or field. A field is a valid method because we can set it via reflection too.

func (*ReflectionDescriber) HasProperty added in v0.10.0

func (r *ReflectionDescriber) HasProperty(name string) bool

Using reflection check if the object has a field with the property name.

func (*ReflectionDescriber) Property added in v0.10.0

func (r *ReflectionDescriber) Property(name string) interface{}

func (*ReflectionDescriber) SetProperty added in v0.10.0

func (r *ReflectionDescriber) SetProperty(name string, values ...interface{}) (interface{}, error)

type SelfDescriber added in v0.10.0

type SelfDescriber interface {
	//A description the object
	Desc() string

	HasChainMethod(name string) bool
	CallChainMethod(name string, args ...interface{}) (interface{}, error)

	HasProperty(name string) bool
	Property(name string) interface{}
	SetProperty(name string, args ...interface{}) (interface{}, error)
}

Interface for interacting with objects. If an object does not self describe via this interface than a reflection based implemenation will be used.

type Var added in v1.0.0

type Var struct {
	Value       interface{}
	Type        ast.ValueType
	Description string
}

Directories

Path Synopsis
cmd
tickdoc
Tickdoc is a simple utility similar to godoc that generates documentation from comments.
Tickdoc is a simple utility similar to godoc that generates documentation from comments.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL