Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Dec 23, 2025. It is now read-only.

Support variable#73

Merged
andeya merged 6 commits into
bytedance:masterfrom
dugenkui03:support_variable
Oct 16, 2023
Merged

Support variable#73
andeya merged 6 commits into
bytedance:masterfrom
dugenkui03:support_variable

Conversation

@dugenkui03
Copy link
Copy Markdown
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 subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants