Add a default fallback value to paths #531
Merged
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.
In some query languages and for some formats, when a path is not found, it is fine to output nothing. For instance for XML, an empty sequence of nodes is ok.
However in some cases, a default value has to be output. This is the case in the JSON query language. When the query builds an object whose value is a sub-query, then the value cannot be empty, even if the sub-query finds no element. Moreover, the query must return exactly one result. If it return zero or more than one element, then the generated JSON data is invalid.
But in the JSON query lanuage, generating no data is still valid, and happens when the path is iterated over, for instance.
This change allows for the query to parameterize for each path whether it should return a (unique) default value, or not. If not, then the previous behavior is kept, otherwise in case of no-match, then the default value is output.