• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang stateful.NewScope函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/influxdata/kapacitor/tick/stateful.NewScope函数的典型用法代码示例。如果您正苦于以下问题:Golang NewScope函数的具体用法?Golang NewScope怎么用?Golang NewScope使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewScope函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
	}
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:34,代码来源:eval_function_node_test.go


示例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)
	}
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:25,代码来源:eval_function_node_test.go


示例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)
	}
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:25,代码来源:eval_function_node_test.go


示例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")
	}
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:27,代码来源:eval_test.go


示例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)
	}
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:60,代码来源:eval_test.go


示例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,
			},
		},
	})
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:31,代码来源:expr_benchmarks_test.go


示例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" []}]}
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:32,代码来源:example_eval_test.go


示例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)
	}
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:25,代码来源:eval_unary_node_test.go


示例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)
	}
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:25,代码来源:eval_unary_node_test.go


示例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
}
开发者ID:influxdata,项目名称:kapacitor,代码行数:29,代码来源:task_master.go


示例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,
		},
	})
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:10,代码来源:expr_benchmarks_test.go


示例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",
		},
	})
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:12,代码来源:expr_benchmarks_test.go


示例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")
	}
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:13,代码来源:eval_test.go


示例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)
	}
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:39,代码来源:eval_function_node_test.go


示例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
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:51,代码来源:expr_benchmarks_test.go


示例16: TestEvaluate_DynamicMethod

func TestEvaluate_DynamicMethod(t *testing.T) {
	script := `var x = [email protected](1,'str', 10s).sad(FALSE)`

	scope := stateful.NewScope()
	a := &structA{}
	scope.Set("a", a)

	dm := func(self interface{}, args ...interface{}) (interface{}, error) {
		a, ok := self.(*structA)
		if !ok {
			return nil, fmt.Errorf("cannot call dynamicMethod on %T", self)
		}
		o := &orphan{
			parent: a,
			Sad:    true,
			args:   args,
		}
		return o, nil
	}
	scope.SetDynamicMethod("dynamicMethod", dm)

	_, err := tick.Evaluate(script, scope, nil, false)
	if err != nil {
		t.Fatal(err)
	}

	xI, err := scope.Get("x")
	if err != nil {
		t.Fatal(err)
	}
	x, ok := xI.(*orphan)
	if !ok {
		t.Fatalf("expected x to be an *orphan, got %T", xI)
	}
	if x.Sad {
		t.Errorf("expected x to not be sad")
	}

	if got, exp := len(x.args), 3; exp != got {
		t.Fatalf("unexpected number of args: got %d exp %d", got, exp)
	}
	if got, exp := x.args[0], int64(1); exp != got {
		t.Errorf("unexpected x.args[0]: got %v exp %d", got, exp)
	}
	if got, exp := x.args[1], "str"; exp != got {
		t.Errorf("unexpected x.args[1]: got %v exp %s", got, exp)
	}
	if got, exp := x.args[2], time.Second*10; exp != got {
		t.Errorf("unexpected x.args[1]: got %v exp %v", got, exp)
	}
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:51,代码来源:eval_test.go


示例17: BenchmarkEvalBool_OneOperator_NumberInt64_NumberInt64

func BenchmarkEvalBool_OneOperator_NumberInt64_NumberInt64(b *testing.B) {
	emptyScope := stateful.NewScope()
	benchmarkEvalBool(b, emptyScope, &ast.BinaryNode{
		Operator: ast.TokenGreater,
		Left: &ast.NumberNode{
			IsInt: true,
			Int64: int64(20),
		},
		Right: &ast.NumberNode{
			IsInt: true,
			Int64: int64(10),
		},
	})
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:14,代码来源:expr_benchmarks_test.go


示例18: BenchmarkEvalBool_OneOperator_ReferenceNodeFloat64_ReferenceNodeFloat64

func BenchmarkEvalBool_OneOperator_ReferenceNodeFloat64_ReferenceNodeFloat64(b *testing.B) {

	scope := stateful.NewScope()
	scope.Set("l", float64(20))
	scope.Set("r", float64(10))
	benchmarkEvalBool(b, scope, &ast.BinaryNode{
		Operator: ast.TokenGreater,
		Left: &ast.ReferenceNode{
			Reference: "l",
		},
		Right: &ast.ReferenceNode{
			Reference: "r",
		},
	})
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:15,代码来源:expr_benchmarks_test.go


示例19: TestStrictEvaluate

// Test that using the wrong chain operator fails
func TestStrictEvaluate(t *testing.T) {
	script := `
var s2 = a.structB()
			.field1('f1')
			.field2(42)
`

	scope := stateful.NewScope()
	a := &structA{}
	scope.Set("a", a)

	_, err := tick.Evaluate(script, scope, nil, false)
	if err == nil {
		t.Fatal("expected error from Evaluate")
	}
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:17,代码来源:eval_test.go


示例20: BenchmarkEvalBool_OneOperator_ReferenceNodeFloat64_NumberInt64

func BenchmarkEvalBool_OneOperator_ReferenceNodeFloat64_NumberInt64(b *testing.B) {

	scope := stateful.NewScope()
	scope.Set("value", float64(20))

	benchmarkEvalBool(b, scope, &ast.BinaryNode{
		Operator: ast.TokenGreater,
		Left: &ast.ReferenceNode{
			Reference: "value",
		},
		Right: &ast.NumberNode{
			IsInt: true,
			Int64: int64(10),
		},
	})
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:16,代码来源:expr_benchmarks_test.go



注:本文中的github.com/influxdata/kapacitor/tick/stateful.NewScope函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang stateful.Scope类代码示例发布时间:2022-05-28
下一篇:
Golang logging.Interface类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap