本文整理匯總了Golang中github.com/influxdata/kapacitor/tick/stateful.NewScope函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewScope函數的具體用法?Golang NewScope怎麽用?Golang NewScope使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewScope函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestEvalFunctionNode_EvalInt64_KeepConsistentState
func TestEvalFunctionNode_EvalInt64_KeepConsistentState(t *testing.T) {
evaluator, err := stateful.NewEvalFunctionNode(&ast.FunctionNode{
Func: "count",
Args: []ast.Node{},
})
if err != nil {
t.Fatalf("Failed to create node evaluator: %v", err)
}
executionState := stateful.CreateExecutionState()
// first evaluation
result, err := evaluator.EvalInt(stateful.NewScope(), executionState)
if err != nil {
t.Errorf("first evaluation: Expected a result, but got error - %v", err)
return
}
if result != int64(1) {
t.Errorf("first evaluation: unexpected result: got: %T(%v), expected: int64(1)", result, result)
}
// second evaluation
result, err = evaluator.EvalInt(stateful.NewScope(), executionState)
if err != nil {
t.Errorf("second evaluation: Expected a result, but got error - %v", err)
return
}
if result != int64(2) {
t.Errorf("second evaluation: unexpected result: got: %T(%v), expected: int64(2)", result, result)
}
}
示例2: TestEvalFunctionNode_EvalFloat64_Sanity
func TestEvalFunctionNode_EvalFloat64_Sanity(t *testing.T) {
evaluator, err := stateful.NewEvalFunctionNode(&ast.FunctionNode{
Func: "abs",
Args: []ast.Node{
&ast.NumberNode{
IsFloat: true,
Float64: float64(-1),
},
},
})
if err != nil {
t.Fatalf("Failed to create node evaluator: %v", err)
}
result, err := evaluator.EvalFloat(stateful.NewScope(), stateful.CreateExecutionState())
if err != nil {
t.Errorf("Expected a result, but got error - %v", err)
return
}
if result != float64(1) {
t.Errorf("unexpected result: got: %T(%v), expected: float64(1)", result, result)
}
}
示例3: TestEvalFunctionNode_FailedToEvaluateArgumentNodes
func TestEvalFunctionNode_FailedToEvaluateArgumentNodes(t *testing.T) {
// bool("value"), where in our case "value" won't exist
evaluator, err := stateful.NewEvalFunctionNode(&ast.FunctionNode{
Func: "bool",
Args: []ast.Node{
&ast.ReferenceNode{Reference: "value"},
},
})
if err != nil {
t.Fatalf("Failed to create node evaluator: %v", err)
}
result, err := evaluator.EvalBool(stateful.NewScope(), stateful.CreateExecutionState())
expectedError := errors.New("Failed to handle 1 argument: name \"value\" is undefined. Names in scope:")
if err == nil {
t.Errorf("Expected an error, but got nil error and result (%t)", result)
return
}
if strings.TrimSpace(err.Error()) != expectedError.Error() {
t.Errorf("Got unexpected error:\ngot: %v\nexpected: %v\n", err, expectedError)
}
}
示例4: TestEvaluate_ListVars
func TestEvaluate_ListVars(t *testing.T) {
script := `
var strList = [ 'host', 'dc', 'service' ]
f(strList)
`
called := false
f := func(a, b, c string) interface{} {
called = true
got := []string{a, b, c}
exp := []string{"host", "dc", "service"}
if !reflect.DeepEqual(got, exp) {
t.Errorf("unexpected func args got %v exp %v", got, exp)
}
return nil
}
scope := stateful.NewScope()
scope.Set("f", f)
_, err := tick.Evaluate(script, scope, nil, false)
if err != nil {
t.Fatal(err)
}
if !called {
t.Fatal("expected function to be called")
}
}
示例5: TestEvaluate
func TestEvaluate(t *testing.T) {
//Run a test that evaluates the DSL against the above structures.
script := `
var s2 = a|structB()
.field1('f1')
.field2(42)
s2.field3(15m)
s2|structC()
.options('c', 21.5, 7h)
.aggFunc(influxql.agg.sum)
`
scope := stateful.NewScope()
a := &structA{}
scope.Set("a", a)
i := &influxql{
Agg: &agg{
Sum: aggSum,
},
}
scope.Set("influxql", i)
_, err := tick.Evaluate(script, scope, nil, false)
if err != nil {
t.Fatal(err)
}
s2I, err := scope.Get("s2")
if err != nil {
t.Fatal(err)
}
s2 := s2I.(*structB)
exp := structB{
Field1: "f1",
Field2: 42,
Field3: time.Minute * 15,
}
s3 := *s2.c
s2.c = nil
if !reflect.DeepEqual(*s2, exp) {
t.Errorf("unexpected s2 exp:%v got%v", exp, *s2)
}
c := structC{
field1: "c",
field2: 21.5,
field3: time.Hour * 7,
}
aggFunc := s3.AggFunc
s3.AggFunc = nil
if !reflect.DeepEqual(s3, c) {
t.Errorf("unexpected s3 exp:%v got%v", c, s3)
}
if exp, got := []float64{10.0}, aggFunc([]float64{5, 5}); !reflect.DeepEqual(exp, got) {
t.Errorf("unexpected s3.AggFunc exp:%v got%v", exp, got)
}
}
示例6: BenchmarkEvalBool_TwoLevelDeep
func BenchmarkEvalBool_TwoLevelDeep(b *testing.B) {
scope := stateful.NewScope()
scope.Set("a", float64(11))
scope.Set("b", float64(8))
benchmarkEvalBool(b, scope, &ast.BinaryNode{
Operator: ast.TokenAnd,
Left: &ast.BinaryNode{
Operator: ast.TokenGreater,
Left: &ast.ReferenceNode{
Reference: "a",
},
Right: &ast.NumberNode{
IsFloat: true,
Float64: 10,
},
},
Right: &ast.BinaryNode{
Operator: ast.TokenLess,
Left: &ast.ReferenceNode{
Reference: "b",
},
Right: &ast.NumberNode{
IsFloat: true,
Float64: 10,
},
},
})
}
示例7: ExampleEvaluate
func ExampleEvaluate() {
//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" []}]}
}
示例8: TestEvalUnaryNode_EvalFloat64_FailedToEvaluateNode
func TestEvalUnaryNode_EvalFloat64_FailedToEvaluateNode(t *testing.T) {
evaluator, err := stateful.NewEvalUnaryNode(&ast.UnaryNode{
Operator: ast.TokenMinus,
Node: &ast.ReferenceNode{
Reference: "value",
},
})
if err != nil {
t.Fatalf("Failed to compile unary node: %v", err)
}
result, err := evaluator.EvalFloat(stateful.NewScope(), stateful.CreateExecutionState())
expectedError := errors.New("name \"value\" is undefined. Names in scope:")
if err == nil && result != float64(0) {
t.Errorf("Expected an error, but got nil error and result: %v", result)
return
}
if strings.TrimSpace(err.Error()) != expectedError.Error() {
t.Errorf("Got unexpected error:\ngot: %v\nexpected: %v\n", err, expectedError)
}
}
示例9: TestEvalUnaryNode_EvalInt64_FailedToEvaluateNode
func TestEvalUnaryNode_EvalInt64_FailedToEvaluateNode(t *testing.T) {
evaluator, err := stateful.NewEvalUnaryNode(&ast.UnaryNode{
Operator: ast.TokenMinus,
Node: &ast.BoolNode{
Bool: false,
},
})
if err != nil {
t.Fatalf("Failed to compile unary node: %v", err)
}
result, err := evaluator.EvalInt(stateful.NewScope(), stateful.CreateExecutionState())
expectedError := errors.New("TypeGuard: expression returned unexpected type boolean, expected int")
if err == nil && result != int64(0) {
t.Errorf("Expected an error, but got nil error and result: %v", result)
return
}
if err.Error() != expectedError.Error() {
t.Errorf("Got unexpected error:\ngot: %v\nexpected: %v\n", err, expectedError)
}
}
示例10: CreateTICKScope
func (tm *TaskMaster) CreateTICKScope() *stateful.Scope {
scope := stateful.NewScope()
scope.Set("time", groupByTime)
// Add dynamic methods to the scope for UDFs
if tm.UDFService != nil {
for _, f := range tm.UDFService.List() {
f := f
info, _ := tm.UDFService.Info(f)
scope.SetDynamicMethod(
f,
stateful.DynamicMethod(func(self interface{}, args ...interface{}) (interface{}, error) {
parent, ok := self.(pipeline.Node)
if !ok {
return nil, fmt.Errorf("cannot call %s on %T", f, self)
}
udf := pipeline.NewUDF(
parent,
f,
info.Wants,
info.Provides,
info.Options,
)
return udf, nil
}),
)
}
}
return scope
}
示例11: BenchmarkEvalBool_OneOperator_UnaryNode_BoolNode
func BenchmarkEvalBool_OneOperator_UnaryNode_BoolNode(b *testing.B) {
emptyScope := stateful.NewScope()
benchmarkEvalBool(b, emptyScope, &ast.UnaryNode{
Operator: ast.TokenNot,
Node: &ast.BoolNode{
Bool: false,
},
})
}
示例12: BenchmarkEvalBool_OneOperator_UnaryNode_ReferenceNode
func BenchmarkEvalBool_OneOperator_UnaryNode_ReferenceNode(b *testing.B) {
scope := stateful.NewScope()
scope.Set("value", bool(false))
benchmarkEvalBool(b, scope, &ast.UnaryNode{
Operator: ast.TokenNot,
Node: &ast.ReferenceNode{
Reference: "value",
},
})
}
示例13: TestEvaluate_Vars_ErrorMissingValue
func TestEvaluate_Vars_ErrorMissingValue(t *testing.T) {
script := `
var x duration
`
scope := stateful.NewScope()
if _, err := tick.Evaluate(script, scope, nil, false); err == nil {
t.Fatal("expected error for missing var type")
}
if _, err := tick.Evaluate(script, scope, nil, true); err != nil {
t.Fatal("uexpected error missing var should be ignored")
}
}
示例14: TestEvalFunctionNode_EvalInt64_Reset
func TestEvalFunctionNode_EvalInt64_Reset(t *testing.T) {
evaluator, err := stateful.NewEvalFunctionNode(&ast.FunctionNode{
Func: "count",
Args: []ast.Node{},
})
if err != nil {
t.Fatalf("Failed to create node evaluator: %v", err)
}
executionState := stateful.CreateExecutionState()
// first evaluation
result, err := evaluator.EvalInt(stateful.NewScope(), executionState)
if err != nil {
t.Errorf("first evaluation: Expected a result, but got error - %v", err)
return
}
if result != int64(1) {
t.Errorf("first evaluation: unexpected result: got: %T(%v), expected: int64(1)", result, result)
}
// reset (we don't call ResetAll on ExecutionState in order to isolate the scope of this test)
for _, fnc := range executionState.Funcs {
fnc.Reset()
}
// second evaluation
result, err = evaluator.EvalInt(stateful.NewScope(), executionState)
if err != nil {
t.Errorf("second evaluation (after reset): Expected a result, but got error - %v", err)
return
}
if result != int64(1) {
t.Errorf("second evaluation (after reset): unexpected result: got: %T(%v), expected: int64(1)", result, result)
}
}
示例15: BenchmarkEvalBool_OneOperatorValueChanges_ReferenceNodeInt64_NumberInt64
func BenchmarkEvalBool_OneOperatorValueChanges_ReferenceNodeInt64_NumberInt64(b *testing.B) {
scope := stateful.NewScope()
initialValue := int64(20)
scope.Set("value", initialValue)
b.ReportAllocs()
b.ResetTimer()
se, err := stateful.NewExpression(&ast.BinaryNode{
Operator: ast.TokenGreater,
Left: &ast.ReferenceNode{
Reference: "value",
},
Right: &ast.NumberNode{
IsInt: true,
Int64: int64(10),
},
})
if err != nil {
b.Fatalf("Failed to compile the expression: %v", err)
}
// We have maximum value because we want to limit the maximum number in
// the reference node so we don't get too much big numbers and the benchmark suite will increase our iterations number (b.N)
currentValue := initialValue
maximumValue := int64(40)
var result bool
for i := 0; i < b.N; i++ {
b.StopTimer()
currentValue += int64(1)
if currentValue > maximumValue {
currentValue = initialValue
}
scope.Set("value", currentValue)
b.StartTimer()
result, err := se.EvalBool(scope)
if err != nil || !result {
v, _ := scope.Get("value")
b.Errorf("Failed to evaluate: error=%v, result=%t, value=%v, init=%v, maximum=%v", err, result, v, initialValue, maximumValue)
}
}
evalBoolResult = result
}