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

Support empty string for safe access operator Currently only an empty json string, i.e. a string in a string was supported: '""'. #629

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ModProg
Copy link
Contributor

@ModProg ModProg commented Nov 27, 2022

Please follow this template, if applicable.

Description

Fixes ?. for empty strings

Usage

{""?.test == "null"}

Additional Notes

TBD: should a json string i.e. '""' be considered null?

Checklist

Please make sure you can check all the boxes that apply to this PR.

  • I added my changes to CHANGELOG.md, if appropriate.
  • I used cargo fmt to automatically format all code before committing

Currently only an empty json string, i.e. a string in a string was supported: `'""'`.
@oldwomanjosiah
Copy link
Contributor

What was the error you were seeing when indexing an empty string?

match val.as_json_value()? {
serde_json::Value::Array(val) => {
let index = index.as_i32()?;
let indexed_value = val.get(index as usize).unwrap_or(&serde_json::Value::Null);
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't for this PR, but should we be throwing an error here? It doesn't seem like we'd want to silently ignore out of bounds accesses

@ModProg
Copy link
Contributor Author

ModProg commented Dec 12, 2022

What was the error you were seeing when indexing an empty string?

@oldwomanjosiah

When I add the test to master I get:

thread 'eval::tests::safe_access_to_empty' panicked at 'Output was not Ok(_): ConversionError(ConversionError { value: "", target_type: "json-value", source: Some(Error("EOF while parsing a value", line: 1, column: 0)) })', crates/simplexpr/src/eval.rs:392:5

@elkowar
Copy link
Owner

elkowar commented Feb 25, 2023

I'm not sure if this is actually desirable behavior, as empty strings will be valid values in many situations -- having a differentiation between empty strings and null does seem quite useful. However, I guess it also depends on how empty strings are treated elsewhere, which to be honest, I'm not 100% sure at this point -- and it miiiiiight be useful to then provide some easier way to convert empty strings into null... easier than (foo == "") ? "null" : foo

@ModProg
Copy link
Contributor Author

ModProg commented Feb 25, 2023

I'm not sure if this is actually desirable behavior, as empty strings will be valid values in many situations

yes, but ""?.test would be an error otherwise so this is not breaking anything.

the only situation where behavior wouldn't be unambiguous would be ""?:.

@ModProg
Copy link
Contributor Author

ModProg commented Feb 25, 2023

I think we should only make an empty string "" and not an empty json string '""' null.

If you use a ? operator you actively decide to use the value as json, so as long as it is documented it is fine to add this behavior.

@elkowar
Copy link
Owner

elkowar commented Feb 25, 2023

That i'd be more fine with, sure! Although this behavior should probably be documented very well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants