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

Export as_opt_<type> helper functions #5

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

rnag
Copy link
Owner

@rnag rnag commented Mar 17, 2023

Closes #4

Copy link

@corneliusroemer corneliusroemer left a comment

Choose a reason for hiding this comment

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

There seems to be some differing assumptions on what should be None and what shouldn't.

I find the examples section hard to read. Rather than having many examples, it may be better to have unit tests - and examples (doc tests) in src/de_impl_opt.rs doc strings.

Somewhere there should be a dense table/section where the behaviour can be seen at a glance - possibly in the doc string itself. I like the # Returns section, explaining behaviour, but this could be complemented with more comprehensive cases for each of the input types str, i64, f64, u64.

examples/demo.rs Show resolved Hide resolved
examples/optionals/as_bool.rs Outdated Show resolved Hide resolved
examples/optionals/as_bool.rs Outdated Show resolved Hide resolved

let m: Msg = serde_json::from_str(data).unwrap();
trace!(" {m:?}");
assert_eq!(m.timestamp, Some(1));

Choose a reason for hiding this comment

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

I think this could arguably be None. To round or not to round...

Copy link
Owner Author

@rnag rnag Mar 20, 2023

Choose a reason for hiding this comment

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

Agreed! When I searched online, I found that the rounding logic can be considerably more complex, as it relies on if a number to the left of the decimal point is even or odd. This makes rounding 0.5 ambiguous, as 0 is neither odd or even (or maybe both?).

For now, I wouldn't say that it's necessarily wrong to round 0.5 to 1. That is, it appears that the rounding logic is at least consistent, so that it is expected that 0.5 would round upwards to 1 (same with 3.5 or 4.5 for example).

However, I'm definitely open to exploring this scenario more in detail, and seeing what differing thoughts and opinions might arise on this issue with rounding numbers in this case.

src/de_impl_opt.rs Show resolved Hide resolved
src/de_impl_opt.rs Show resolved Hide resolved
@rnag
Copy link
Owner Author

rnag commented Mar 20, 2023

There seems to be some differing assumptions on what should be None and what shouldn't.

I find the examples section hard to read. Rather than having many examples, it may be better to have unit tests - and examples (doc tests) in src/de_impl_opt.rs doc strings.

Hi @corneliusroemer ! Thanks for bringing this up. I totally agree, that at some point it would be worth it to refactor it to move example cases into doc tests & unit tests more explicitly, that way it also runs automatically in the GitHub Actions workflow (CI) and also when deploying new versions.

I've created a new issue to track effort on this separately, as part of #7 .

For now, as and when when time allows, please take a look at recent changes and commits I have added as part of this PR, to confirm if it resolves or addresses all issues based on your notes. I will hold off on merging the changes until then.

Thanks, and have a great Monday 🎉 !

@josephwynn-sc
Copy link

I just stumbled upon this crate and the first thing I wanted was as_opt_* - is there a chance this PR would be merged in the near future?

@rnag
Copy link
Owner Author

rnag commented Nov 7, 2024

@josephwynn-sc Yes, totally! Sorry for the long wait, just coming back after a hiatus.

I'll look into it this weekend, run some tests with rust -- now that I've got a new laptop and got Rust set up -- and if everything looks good, I'll go ahead and merge this PR. Thanks!

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.

Provide extra functions to return Options when the input is null as Serde usually does
3 participants