-
Notifications
You must be signed in to change notification settings - Fork 5
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
proposal to add 'error' function #27
Open
sebastien-rosset
wants to merge
7
commits into
jmespath:main
Choose a base branch
from
CiscoM31:error-function
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5cca88b
proposal to add 'error' function
sebastien-rosset f1542ad
Add concrete use case
sebastien-rosset 32ceff8
Add concrete use case
sebastien-rosset 02c603b
Add concrete use case
sebastien-rosset 37bdd2b
Add concrete use case
sebastien-rosset d942137
Add concrete use case
sebastien-rosset 806d974
Add concrete use case
sebastien-rosset 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 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,57 @@ | ||
# Add 'error' function | ||
|
||
||| | ||
|---|--- | ||
| **JEP** | 20 | ||
| **Author** | Sebastien Rosset | ||
| **Created**| 22-March-2023 | ||
| **[SemVer](https://semver.org/spec/v2.0.0.html#summary)** | MINOR | ||
|
||
## Abstract | ||
|
||
This JEP proposes a new function `error()` that raises a runtime error. | ||
This could be used to detect and raise an error when input JSON documents have unexpected data. | ||
|
||
|
||
## Motivation | ||
|
||
AFAIK, there is no dedicated function to raise a runtime error within a JMESpath expression. | ||
|
||
|
||
## Specification | ||
|
||
Add a new `error` function that takes a string expression. | ||
If the `error` function is evaluated, an error is raised with the specified string message. | ||
|
||
## Rationale | ||
|
||
Suppose we have the following JSON documents: | ||
```json | ||
{ "id": "eth0", "status": "up" } | ||
{ "id": "eth1", "status": "down" } | ||
``` | ||
|
||
In this example, the values of `status` are either `up` or `down`. | ||
Suppose the JMESpath author wants to: | ||
1. Map the values of `status` to `0` and `1`. | ||
1. Raise an error when an expected `status` value is encountered. For example, when the value of `status` is set to `degraded`. | ||
|
||
``` | ||
((status == 'up') && `1`) || ((status == 'down') && `0`) || error(join(" ", ["invalid value", status])) | ||
``` | ||
|
||
### Other options that have been considered | ||
|
||
1. When appropriate, use a JSON schema to validate the input. However, this may not always be the right tool: | ||
1. The JSON schema may not be available. | ||
1. Constructing a JSON schema may be difficult when the input data is generated by a 3rd-party component. | ||
1. JSON schema validation may have a significant runtime cost, especially for large documents with a complex schema. | ||
1. Leverage the fact that some JMESpath expressions are invalid, e.g., `to_number('bad')` would raise an error. However, this is a kludgy workaround. | ||
|
||
``` | ||
((status == 'up') && `1`) || ((status == 'down') && `0`) || to_number('bad') | ||
``` | ||
|
||
## Implementation Notes | ||
|
||
Are there any details that implementers should try to follow? |
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.
The motivation section should go into more detail as to
why
JMESPath expressions need the ability to raise a runtime error. The existing errors are intended for cases where the user has made a mistake in their expression. JMESPath is primarily a language for filtering/extracting/reshaping JSON data, and not really about schema validation. This section should make a convincing case for that.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.
I have added a section with concrete use cases and improved the motivation section.
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.
@jamesls , does this address your concerns?