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

Add support for method of map. #19

Merged
merged 3 commits into from
Jan 19, 2019
Merged

Add support for method of map. #19

merged 3 commits into from
Jan 19, 2019

Conversation

szyhf
Copy link
Contributor

@szyhf szyhf commented Jan 19, 2019

Hi, I find out that while calling getFunc(...), when the kind of val is Map, will only assume the method is provide by k-v. But even a map can have it is own method, and it will help a lot.
For example:

type ExprParams map[string]interface{}

func (e ExprParams) Max(a, b float64) float64 {
	return math.Max(a, b)
}

type User struct{
    Age int
}

func demo(){
    expr.Eval("Max(Teacher.Age, Student.Age)",ExprParams{
		"Teacher":User{Age:28},
		"Student":Student{Age:8},
	})
}

In this cases, ExprParams can provide default method ( like Max) easily.

And the implement is very simple, I hope it can improve the lib.

Thanks a lot.

PS: another question
I find that the only to calling for getFunc(val interface{}, i interface{}), the param i is both string, why not just change the getFunc(...) to getFunc(val interface{}, funcName string) ? That will save the cost of reflect.ValueOf(i).String()

@antonmedv
Copy link
Member

Awesome! Can you add tests for this case?

@szyhf
Copy link
Contributor Author

szyhf commented Jan 19, 2019

Sure, but where may I put the test case ?

@antonmedv antonmedv merged commit 674ce63 into expr-lang:master Jan 19, 2019
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.

2 participants