Documentation ¶
Overview ¶
TICKscript is a simple invocation chaining DSL.
See the examples for how its used and example syntax of the DSL.
Index ¶
- Variables
- func Evaluate(script string, scope *stateful.Scope, predefinedVars map[string]Var, ...) (_ map[string]Var, err error)
- func Format(script string) (string, error)
- func SetLogger(l *log.Logger)
- type PartialDescriber
- type ReflectionDescriber
- func (r *ReflectionDescriber) CallChainMethod(name string, args ...interface{}) (interface{}, error)
- func (r *ReflectionDescriber) Desc() string
- func (r *ReflectionDescriber) HasChainMethod(name string) bool
- func (r *ReflectionDescriber) HasProperty(name string) bool
- func (r *ReflectionDescriber) Property(name string) interface{}
- func (r *ReflectionDescriber) SetProperty(name string, values ...interface{}) (interface{}, error)
- type SelfDescriber
- type Var
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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" []}]}
Types ¶
type PartialDescriber ¶ added in v0.13.0
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.