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

Accept pre-parsed expressions in functions that evaluate expressions #171

Conversation

fedme
Copy link
Contributor

@fedme fedme commented Jan 16, 2024

Accept pre-parsed expressions (Expressions ASTs instead of strings) in functions that evaluate expressions.

This allows the library user to cache pre-parsed expressions when evaluating them rather than have this library parse the expression AST from an expression string at every evaluation.

Copy link

This pull request has been linked to Shortcut Story #26590: Cache Trigger Expressions for faster evaluation.

def parse!(expression) when is_number(expression), do: to_string(expression) |> parse!()

def parse!(expression_ast) when is_list(expression_ast), do: expression_ast
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better way to match Expressions ASTs?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right now there isn't :/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you can write a defguard function to make sure the given payload contains an :expression attribute and the expression payload is valid.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, it can be possible to just give a random list such as ["hola", "mundo"] which is not valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea @santiagocardo! Will do tomorrow!

# If the expression is a already provided as a parsed AST then it is simply
# returned as is
assert [expression: [attribute: [atom: "contact", atom: "age"]], text: " + 2"] ==
Expression.parse!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supplying AST (parsed structure) to a function called parse! seems a bit odd to be honest, what problem are we solving here?

Copy link
Contributor Author

@fedme fedme Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just because this function is called from inside every evaluate_* function, so it was easier to add this check here than adding it to every evaluate_* function, but I could also do that. I agree that it feels odd.

As per the PR description the goal here is to allow the library user to cache/store AST representations of commonly evaluated expressions rather than forcing the library user to supply the expression as a string (and waste time parsing it again) at every evaluation.

Copy link
Contributor

CLA Assistant Lite bot:
Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


1 out of 2 committers have signed the CLA.
@smn
@fedme
You can retrigger this bot by commenting recheck in this Pull Request

@smn
Copy link
Contributor

smn commented Sep 26, 2024

Closing for now due to inactivity, sorry @fedme !

@smn smn closed this Sep 26, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 26, 2024
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.

3 participants