Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support variable #73

Merged
merged 6 commits into from
Oct 16, 2023
Merged

Support variable #73

merged 6 commits into from
Oct 16, 2023

Conversation

dugenkui03
Copy link
Contributor

@dugenkui03 dugenkui03 commented Oct 13, 2023

background

Sometimes we want to pass some context, for example min price for commodity, and minPrice may be variable instead of static value. Introduce variables into expression will be more convenient.

proposal

enable evaluates the value with the given env.

demo

func TestExprWithEnv(t *testing.T) {
	var cases = []struct {
		expr string
		val  interface{}
	}{
		// env: a = 10, b = "string value",
		{expr: "a", val: 10.0},
		{expr: "b", val: "string value"},
		{expr: "a>10", val: false},
		{expr: "a<11", val: true},
		{expr: "a+1", val: 11.0},
		{expr: "a==10", val: true},
	}

	for _, c := range cases {
		t.Log(c.expr)
		vm, err := parseExpr(c.expr)
		if err != nil {
			t.Fatal(err)
		}
		val := vm.runWithEnv("", nil, map[string]interface{}{"a": 10, "b": "string value"})
		if !reflect.DeepEqual(val, c.val) {
			if f, ok := c.val.(float64); ok && math.IsNaN(f) && math.IsNaN(val.(float64)) {
				continue
			}
			t.Fatalf("expr: %q, got: %v, expect: %v", c.expr, val, c.val)
		}
	}
}

@andeya andeya merged commit 7015f02 into bytedance:master Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants