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

🚸Describe why Parse fails due to case-insensitive ambiguity #1484

Merged
merged 2 commits into from
Jan 2, 2025

Conversation

angularsen
Copy link
Owner

@angularsen angularsen commented Dec 30, 2024

Fixes #1423

UnitParser.Parse<LengthUnit>("MM") fails due to matching both Megameter and Millimeter in case-insensitive matching, but matches neither of them in the case-sensitive fallback. It was confusing to get UnitsNotFoundException in this case, since case-insensitive usually works for most units.

Changes

  • Handle this case and throw AmbiguousUnitParseException instead of UnitNotFoundException
  • Describe the case-insensitive units that matched
  • Fix existing test Parse_WithMultipleCaseInsensitiveMatchesButNoExactMatches_ThrowsUnitNotFoundException
  • Skip retrying with fallback culture if no specific formatProvider was given

Fixes #1423

`UnitParser.Parse<LengthUnit>("MM")` fails due to matching both `Megameter` and `Millimeter` in case-insensitive matching, but matches neither of them in the case-sensitive fallback. It was confusing to get `UnitsNotFoundException` in this case, since case-insensitive usually works for most untis.

### Changes
- Handle this case and throw `AmbiguousUnitParseException` instead of `UnitNotFoundException`
- Describe the case-insensitive units that matched
@angularsen angularsen requested a review from lipchev December 30, 2024 21:44
@angularsen
Copy link
Owner Author

@lipchev relevant to your exception helpers PR, the exception message can be improved further to include information about the quantity and unit enum type, which I believe you already did there.

@lipchev
Copy link
Collaborator

lipchev commented Dec 30, 2024

@lipchev relevant to your exception helpers PR, the exception message can be improved further to include information about the quantity and unit enum type, which I believe you already did there.

Yes, I was also going to bring this is up (eventually) - if we want to go wild we could even include extra fields like in these examples:

               throw new UnitNotFoundException($"No unit was found for quantity '{quantityName}' with the name: '{unitName}'.")
               {
                   Data = { ["quantityName"] = quantityName, ["unitName"] = unitName }
               };
        throw new UnitNotFoundException($"No quantity was found with the specified unit type: '{unitType}'.") { Data = { ["unitType"] = unitType.Name } };

@angularsen
Copy link
Owner Author

Copy link

codecov bot commented Dec 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84%. Comparing base (bb1420e) to head (def3211).
Report is 1 commits behind head on master.

Additional details and impacted files
@@          Coverage Diff           @@
##           master   #1484   +/-   ##
======================================
  Coverage      84%     84%           
======================================
  Files         368     368           
  Lines       35601   35606    +5     
======================================
+ Hits        30000   30005    +5     
  Misses       5601    5601           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@angularsen
Copy link
Owner Author

@lipchev Are you still reviewing this or can I merge?

@lipchev
Copy link
Collaborator

lipchev commented Jan 1, 2025

@lipchev Are you still reviewing this or can I merge?

Well, my earlier comment wasn't about the test that broke the build - it was a bit more subtle than that..
Nevertheless, I would say "merge it" as fixing that very unlikely case would be an unpleasant endeavor (and testing it would probably require setting up custom abbreviation mappings etc.), so I think it would be easier if we just add it like it is and try to fix it better in v6.

@angularsen
Copy link
Owner Author

Gotcha, I'll merge this then.

@angularsen angularsen merged commit f814884 into master Jan 2, 2025
3 checks passed
@angularsen angularsen deleted the agl/ambiguous-ex branch January 2, 2025 10:50
lipchev pushed a commit to lipchev/UnitsNet that referenced this pull request Jan 4, 2025
…en#1484)

Fixes angularsen#1423

`UnitParser.Parse<LengthUnit>("MM")` fails due to matching both
`Megameter` and `Millimeter` in case-insensitive matching, but matches
neither of them in the case-sensitive fallback. It was confusing to get
`UnitsNotFoundException` in this case, since case-insensitive usually
works for most units.

### Changes
- Handle this case and throw `AmbiguousUnitParseException` instead of
`UnitNotFoundException`
- Describe the case-insensitive units that matched
- Fix existing test
`Parse_WithMultipleCaseInsensitiveMatchesButNoExactMatches_ThrowsUnitNotFoundException`
- Skip retrying with fallback culture if no specific `formatProvider`
was given
angularsen added a commit that referenced this pull request Jan 4, 2025
… UnitNotFoundException (#1484) (#1494)

Fixes #1423 for `release-v6`
Carry over from #1484

`UnitParser.Parse<LengthUnit>("MM")` fails due to matching both
`Megameter` and `Millimeter` in case-insensitive matching, but matches
neither of them in the case-sensitive fallback. It was confusing to get
`UnitsNotFoundException` in this case, since case-insensitive usually
works for most units.

### Changes
- Handle this case and throw `AmbiguousUnitParseException` instead of
`UnitNotFoundException`
- Describe the case-insensitive units that matched
- Fix existing test
`Parse_WithMultipleCaseInsensitiveMatchesButNoExactMatches_ThrowsUnitNotFoundException`
- Skip retrying with fallback culture if no specific `formatProvider`
was given
- Avoid some of the unnecessary array allocations

Co-authored-by: Andreas Gullberg Larsen <[email protected]>
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.

Quantity.TryParse won't parse "10 MM" because "MM" is capitalized. Other abbreviations work fine.
2 participants