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 OpenRPC method for evaluating a when clause #4165

Merged
merged 4 commits into from
Jul 30, 2024
Merged

Conversation

juliasilge
Copy link
Contributor

Goes along with posit-dev/ark#449

I was thinking about how to address #2697 and have a proposed approach in this PR plus the accompanying PR for ark. I looked at how various extensions (like the git extension, etc) check for whether we are in a git repo in the workspace and all that checking is typically based on context keys. In an extension specifically, you can check those keys with a "when" clause and I realized that approach (i.e. a string) could work well as an OpenRPC method and would be flexible for other context keys we will need to check when we are not in the main thread.

Alternatively, we could add methods to return more specific info (are we in a git repo? etc etc etc) but I like the idea of using "when" clauses for this, like an extension could directly for commands, etc.

QA Notes

Evaluating code like this in R should show the correct results for your situation:

.ps.ui.evaluateWhenClause("isLinux || isWindows")
.ps.ui.evaluateWhenClause("isMac")
.ps.ui.evaluateWhenClause("gitOpenRepositoryCount >= 1")

@juliasilge juliasilge marked this pull request as ready for review July 26, 2024 21:26
@juliasilge juliasilge requested a review from jmcphers July 26, 2024 21:58
@jmcphers
Copy link
Collaborator

This is very clever and I think it'll be useful in lots more places!

I do think some error handling needs to happen so that an invalid when_clause generates an RPC error that gets propagated to an R error.

image

@juliasilge
Copy link
Contributor Author

Notice the behavior here:

const result = (rules ? rules.evaluate(context) : true);

If you do something like "logLevel == debug", then ContextKeyExpr.deserialize() gives you something like:

{
  key: "logLevel",
  value: "debug",
  negated: null,
  type: 4,
}

If you do something like "no one should eat mashed potatoes", then ContextKeyExpr.deserialize() gives you undefined and then contextKeyService.contextMatchesRules() evaluates to true. In 6939877 I caught all undefined deserialized context keys and added an error.

Screenshot 2024-07-28 at 12 54 41 PM

@juliasilge juliasilge merged commit fd367e4 into main Jul 30, 2024
22 checks passed
@juliasilge juliasilge deleted the openrpc-when-clause branch July 30, 2024 00:29
@github-actions github-actions bot locked and limited conversation to collaborators Jul 30, 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.

2 participants