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

Closed
wants to merge 1 commit into 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: `'""'`.
@ModProg ModProg force-pushed the safe_access_empty_string branch from 742a783 to 4f52999 Compare November 27, 2022 11:05
@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

w-lfchen added a commit to w-lfchen/eww that referenced this pull request Aug 27, 2024
Co-authored-by: Roland Fredenhagen <[email protected]>
w-lfchen added a commit to w-lfchen/eww that referenced this pull request Aug 28, 2024
Co-authored-by: Roland Fredenhagen <[email protected]>
Copy link
Contributor

@w-lfchen w-lfchen left a comment

Choose a reason for hiding this comment

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

i've adopted this pr in #1176 (i want to finally get this merged), this pr can therefore be closed.
thank you for your work!

w-lfchen added a commit to w-lfchen/eww that referenced this pull request Aug 31, 2024
Co-authored-by: Roland Fredenhagen <[email protected]>
w-lfchen added a commit to w-lfchen/eww that referenced this pull request Sep 3, 2024
Co-authored-by: Roland Fredenhagen <[email protected]>
w-lfchen added a commit to w-lfchen/eww that referenced this pull request Oct 10, 2024
Co-authored-by: Roland Fredenhagen <[email protected]>
w-lfchen added a commit to w-lfchen/eww that referenced this pull request Oct 11, 2024
Co-authored-by: Roland Fredenhagen <[email protected]>
elkowar pushed a commit that referenced this pull request Oct 12, 2024
* chore: apply pr #629

Co-authored-by: Roland Fredenhagen <[email protected]>

* style: early return

* feat: err on empty json string

* docs: update docs

* test: update test in accordance with decision

* chore: attribution

* docs: improve wording

* docs: add breaking change notice to changelog

* fix(changelog): fix pr link

---------

Co-authored-by: Roland Fredenhagen <[email protected]>
@elkowar
Copy link
Owner

elkowar commented Oct 12, 2024

Closing, as this has been merged as part of #1176. Thanks a ton for your work!

@elkowar elkowar closed this Oct 12, 2024
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.

4 participants