-
-
Notifications
You must be signed in to change notification settings - Fork 471
Add ability to return errors from custom functions. #159
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
Merged
antonmedv
merged 1 commit into
expr-lang:master
from
ffenix113:feature/return-function-errors
Aug 1, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| # Custom functions | ||
|
|
||
| User can provide custom functions in environment. | ||
| This functions can either be defined as functions or as methods. | ||
|
|
||
| Functions can be typed, in which case if any of the arguments passed to such function will not match required types - error will be returned. | ||
|
|
||
| By default, function need to return at least one value. | ||
| Other return values will be silently skipped. | ||
|
|
||
| Only exception is if second return value is `error`, in which case returned error will be returned if it is non-nil. | ||
| Read about returning errors below. | ||
|
|
||
| ## Functions | ||
|
|
||
| Simple example of custom functions would be to define function in map which will be used as environment: | ||
|
|
||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "github.com/antonmedv/expr" | ||
| ) | ||
|
|
||
| func main() { | ||
| env := map[string]interface{}{ | ||
| "foo": 1, | ||
| "double": func(i int) int { return i * 2 }, | ||
| } | ||
|
|
||
| out, err := expr.Eval("double(foo)", env) | ||
|
|
||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| fmt.Print(out) | ||
| } | ||
| ``` | ||
|
|
||
| ## Methods | ||
|
|
||
| Methods can be defined on type that is provided as environment. | ||
|
|
||
| Methods MUST be exported in order to be callable. | ||
|
|
||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/antonmedv/expr" | ||
| ) | ||
|
|
||
| type Env struct { | ||
| Tweets []Tweet | ||
| } | ||
|
|
||
| // Methods defined on such struct will be functions. | ||
| func (Env) Format(t time.Time) string { return t.Format(time.RFC822) } | ||
|
|
||
| type Tweet struct { | ||
| Text string | ||
| Date time.Time | ||
| } | ||
|
|
||
| func main() { | ||
| code := `map(filter(Tweets, {len(.Text) > 0}), {.Text + Format(.Date)})` | ||
|
|
||
| // We can use an empty instance of the struct as an environment. | ||
| program, err := expr.Compile(code, expr.Env(Env{})) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| env := Env{ | ||
| Tweets: []Tweet{{"Oh My God!", time.Now()}, {"How you doin?", time.Now()}, {"Could I be wearing any more clothes?", time.Now()}}, | ||
| } | ||
|
|
||
| output, err := expr.Run(program, env) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| fmt.Print(output) | ||
| } | ||
| ``` | ||
|
|
||
| ## Fast functions | ||
|
|
||
| Fast functions are functions that don't use reflection for calling them. | ||
| This improves performance but drops ability to have typed arguments. | ||
|
|
||
| Such functions have strict signatures for them: | ||
| ```go | ||
| func(...interface{}) interface{} | ||
| ``` | ||
| or | ||
| ```go | ||
| func(...interface{}) (interface{}, error) | ||
| ``` | ||
|
|
||
| Methods can also be used as fast functions if they will have signature specified above. | ||
|
|
||
| Example: | ||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "github.com/antonmedv/expr" | ||
| ) | ||
|
|
||
| type Env map[string]interface{} | ||
|
|
||
| func (Env) FastMethod(...interface{}) interface{} { | ||
| return "Hello, " | ||
| } | ||
|
|
||
| func main() { | ||
| env := Env{ | ||
| "fast_func": func(...interface{}) interface{} { return "world" }, | ||
| } | ||
|
|
||
| out, err := expr.Eval("FastMethod() + fast_func()", env) | ||
|
|
||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| fmt.Print(out) | ||
| } | ||
| ``` | ||
|
|
||
| ## Returning errors | ||
|
|
||
| Both normal and fast functions can return `error`s as second return value. | ||
| In this case if function will return any value and non-nil error - such error will be returned to the caller. | ||
|
|
||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "github.com/antonmedv/expr" | ||
| ) | ||
|
|
||
| func main() { | ||
| env := map[string]interface{}{ | ||
| "foo": -1, | ||
| "double": func(i int) (int, error) { | ||
| if i < 0 { | ||
| return 0, errors.New("value cannot be less than zero") | ||
| } | ||
| return i * 2 | ||
| }, | ||
| } | ||
|
|
||
| out, err := expr.Eval("double(foo)", env) | ||
|
|
||
| // This `err` will be the one returned from `double` function. | ||
| // err.Error() == "value cannot be less than zero" | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| fmt.Print(out) | ||
| } | ||
| ``` | ||
|
|
||
| * [Contents](README.md) | ||
| * Next: [Operator Override](Operator-Override.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CallFast is a special case. We don't want to change the signature of call fast methods. The idea for call fast funcs is what they are created manually for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't change it here, but rather allowing to add a new one, that also returns an error.
So both variants will work.