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 multiple abbreviations #658

Merged
merged 13 commits into from
Apr 22, 2019
Merged

Support multiple abbreviations #658

merged 13 commits into from
Apr 22, 2019

Conversation

lipchev
Copy link
Collaborator

@lipchev lipchev commented Apr 21, 2019

Fixes #344

Updated scripts to iterate over abbreviations

  • Types.psm1: type of AbbreviationsWithPrefixes changed to object[]
  • GenerateUnits.ps1: Add-PrefixUnits now iterates over all abbreviations (incl. with prefix)
  • Duration: added the cyrilic equivalent to "s": "sec" ("с", "сек") to both the main and the prefixed abbrviations
  • DurationTests: added a Theory with some of the main test cases for parsing seconds & milliseconds
  • MolarityTests: removed the Skip for the awaiting test

One little flaw I noticed just as I was committing was that the UnitAbbreviationsCache.g.cs generates separate rows instead of one string[]{..} but I guess that shouldn't be a problem since the way units are loaded here, just adds them in the next loop.
I won't need this fix anytime soon- so if you want you can just ignore this(I had the code stashed from before).
At the very least it brings an exhaustive test case with the addition to the duration abbreviation (I won't need those abbreviation at all, but you can keep them if you want- they are valid).

lipchev and others added 12 commits April 3, 2019 11:39
Added the following Concentration Units:
- MassConcentration: SI = kg/m3, typically mg/l
- VolumeConcentration : dimensionless, typically %
- MassFraction: SI = kg/kg, typically mg/kg

Modified the existing Molarity unit:
- Some operations that were originally based on the Density units now use the MassConcentration units instead (Note: despite the fact that they share the same measurement units- the Density is a distnct QuantyType from the MassConcentration)
- Removed all operators involving Molarity from the Density units

Defined some basic operations that were missing from the AmountOfSubstance/MolarMass/Mass units

Defined the triangular operations involving Mass/Molar/Volume concentrations (& the corresponding component's Density & MolarMass)

All unit tests included most were defined by actual chemists(which I AM NOT).
Note: one of the tests (QuantityIFormattableTests.VFormatEqualsValueToString)- was failing on my machine- it passes if I add CultureInfo.CurrentUICulture to the value.ToString() - as I presume was the intended behavior
- updated liter abbreviations for g/l, g/dl, g/ml & kg/l to uppercase 'L'  (TODO Density?)
- added base units to all units in MassConcentration & Molarity (TODO Density?)
- corrected the BaseUnits for MassConcentration
- marked the invalid methods from Molarity/Density as Obsolete (were previously omitted)
- some cosmetic changes to the Unit Tests
- MolesPerLiter: fixed the BaseUnits (default) to Deimeter/Mole
- Molar: removed in favor of using the alternative abbreviation 'M"
- MolarityTests - OneMilliMolarFromStringParsedCorrectly skipped while awaiting fix for #344
- added a KnownQuantities class with a few constants that were used in multiple tests
- replaced the usages in MassConcentrationTests MolarityTests * VolumeConcentrationTests
- converted two of the MassConcentration tests to using Theory + InlineData
- removed BaseUnits from GramPerDeciliter(not exact + overlap), kept it in GramPerLiter (as exact & non-overlapping), also kept it for GramPerMilliliter(exact + overlapping) because I thought it would be useful to have at least one such case for future testing
- moved the Mass/MolarMass operator to the Mass class (removing the MolarMass.extra)
- all tests refactored using Theory + Inline Data
- moved one or two tests to the appropriate .Test file
- removed a few redundant tests
- Types.psm1: type of AbbreviationsWithPrefixes changed to object[]
- GenerateUnits.ps1: Add-PrefixUnits now iterates over all abbreviations (incl. with prefix)
- Duration: added the cyrilic equivalent to  "s": "sec" ("с", "сек")  to both the main and the prefixed abbrviations
- DurationTests: added a Theory with some of the main test cases for parsing seconds & milliseconds
- MolarityTests: removed the Skip for the awaiting test
@@ -212,17 +212,17 @@ public sealed partial class UnitAbbreviationsCache
("en-US", typeof(DurationUnit), (int)DurationUnit.Hour, new string[]{"h", "hr", "hrs", "hour", "hours"}),
("ru-RU", typeof(DurationUnit), (int)DurationUnit.Hour, new string[]{"ч"}),
("en-US", typeof(DurationUnit), (int)DurationUnit.Microsecond, new string[]{"µs"}),
("ru-RU", typeof(DurationUnit), (int)DurationUnit.Microsecond, new string[]{"мкс"}),
("ru-RU", typeof(DurationUnit), (int)DurationUnit.Microsecond, new string[]{"мкс мксек"}),
Copy link
Owner

Choose a reason for hiding this comment

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

This doesn't seem right, it's a single string vs multiple strings.

I realize these are the Windows Runtime Component files. WRC is kind of deprecated and only maintained on the back-burner, so it has its own copy of the codegen files that are by now outdated and slightly different. However, it would currently break the existing abbreviations so we need to fix that.

Could you copy the script change to codegen scripts in UnitsNet.WindowsRuntimeComponent\Scripts folder and regenerate? I would prefer that C# files don't change as a result of this PR, but if it's easiest that WRC too receives the new abbreviations then that is fine too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm, this is strange. Why did the AppVeyor checks pass the first time? Shouldn't have my tests failed?
Code generated looks fine now, but I don't know- how do you really make sure..

Copy link
Owner

Choose a reason for hiding this comment

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

AppVeyor regenerates the code too, since often when merging multiple PRs the committed generated code is out-of-date.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, but I guess it doesn't run the same tests? That's a shame, cause it would have also caught this :)

Copy link
Owner

Choose a reason for hiding this comment

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

It should run the exact same tests as when running build.bat locally - which to my knowledge should run all the tests in the solution. Isn't that the case?

I didn't quite get what you meant by the linked "this" code snippet, what should it have caught there?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Erg, it won't let me attach the build log, but here is the highlight:

Restore completed in 26,38 ms for C:\Users\galin\Documents\GitHub\ConcentrationUnits\UnitsNet\UnitsNet.Serialization.JsonNet.Tests\UnitsNet.Serialization.JsonNet.Tests.csproj.
  Retrying 'FindPackagesByIdAsyncCore' for source 'https://camo.githubusercontent.com/da308986135cea0abf9a027dc6d147d397d5e383/68747470733a2f2f696d672e736869656c64732e696f2f6e756765742f767072652f6f72636865737472612e636f72652e737667/FindPackagesById()?id='UnitsNet.Serialization.JsonNet'&semVerLevel=2.0.0'.
  Response status code does not indicate success: 404 (Not Found).
  Retrying 'FindPackagesByIdAsyncCore' for source 'https://camo.githubusercontent.com/da308986135cea0abf9a027dc6d147d397d5e383/68747470733a2f2f696d672e736869656c64732e696f2f6e756765742f767072652f6f72636865737472612e636f72652e737667/FindPackagesById()?id='UnitsNet.Serialization.JsonNet'&semVerLevel=2.0.0'.
  Response status code does not indicate success: 404 (Not Found).
C:\Program Files\dotnet\sdk\2.1.505\NuGet.targets(114,5): error : Failed to retrieve information about 'UnitsNet.Serialization.JsonNet' from remote source 'https://camo.githubusercontent.com/da308986135cea0abf9a027dc6d147d397d5e383/68747470733a2f2f696d672e736869656c64732e696f2f6e756765742f767072652f6f72636865737472612e636f72652e737667/FindPackagesById()?id='UnitsNet.Serialization.JsonNet'&semVerLevel=2.0.0'. [C:\Users\galin\Documents\GitHub\ConcentrationUnits\UnitsNet\UnitsNet.sln]
C:\Program Files\dotnet\sdk\2.1.505\NuGet.targets(114,5): error :   Response status code does not indicate success: 404 (Not Found). [C:\Users\galin\Documents\GitHub\ConcentrationUnits\UnitsNet\UnitsNet.sln]

Build FAILED.

Copy link
Owner

Choose a reason for hiding this comment

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

Weird, I have never seen this before. I initially suspected something related to SourceLink, but googling that hostname gives me this:
https://help.github.com/en/articles/about-anonymized-image-urls

Googling some more on 404 and FindPackagesByIdAsyncCore, it seems that maybe you have multiple nuget sources configured on your machine? Maybe you have added a myget source or similar?

NuGet/Home#6140
https://github.com/dotnet/cli/issues/6680

Seems like a bug with dotnet restore that is not yet fixed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nice, disabling the alpha-channel source fixed the issue(I still got to keep the other sources so I guess it's kinda random). Anyway, I feel much better now. Thanks!

Copy link
Owner

Choose a reason for hiding this comment

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

I think the order matters, one of the links said it would only try the first source and ignore the others. Try moving the official nuget source as the first one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, saw that- but for me it was already the last source. Anyway, I don't need it anymore.

@@ -127,28 +130,40 @@ public partial class UnitAbbreviationsCache
("en-US", typeof(BitRateUnit), (int)BitRateUnit.BitPerSecond, new string[]{"bit/s", "bps"}),
("en-US", typeof(BitRateUnit), (int)BitRateUnit.BytePerSecond, new string[]{"B/s"}),
("en-US", typeof(BitRateUnit), (int)BitRateUnit.ExabitPerSecond, new string[]{"Ebit/s"}),
("en-US", typeof(BitRateUnit), (int)BitRateUnit.ExabitPerSecond, new string[]{"Ebps"}),
Copy link
Owner

Choose a reason for hiding this comment

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

Well this is awesome to see, how old unit definitions now get populated new abbreviations !
And as you say, extra row vs array makes no practical difference here.

Copy link
Collaborator Author

@lipchev lipchev Apr 22, 2019

Choose a reason for hiding this comment

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

Yeah, let's just hope people don't get surprised in a bad way as this opens the possibility for many new AmbiguousUnitParseExceptions..
Maybe there should be an explicit up-front list of those (shouldn't be difficult to auto-generate)..

Copy link
Owner

Choose a reason for hiding this comment

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

Good point, yeah that would actually be a nice list to have.

- same fix applied to  the WRC project
@angularsen angularsen merged commit fa048aa into angularsen:master Apr 22, 2019
@angularsen
Copy link
Owner

angularsen commented Apr 22, 2019

Great stuff @lipchev , I'm impressed with the quality and size of your contributions lately. Would you be interested in joining as a maintainer?

We are a handful, but only me and @tmilnthorp are active these days so we could definitely use the help. I don't ask much other than to help out answering issues and review PRs whenever you have the time and feel like it, no obligations. You seem like the guy to have some opinions on how this library can improve and what direction to go too, which is always nice.

Let me know what you think. I would have to run it by @tmilnthorp first too.

Best,
Andreas

@lipchev
Copy link
Collaborator Author

lipchev commented Apr 22, 2019

I'm happy, and flattered by your confidence in me and would agree on condition that I get the full noob treatment- I am in fact still quite new to the github workflow and would love a couple of tips (like regarding the VS integration- is there something for reviewing code from the IDE- maybe a 2019 feature?)
I do have to run for now, but if you like you can drop me a line lipchev@gmail.com

@angularsen
Copy link
Owner

I'm quite certain you are more than qualified to help out.
Besides, one of the benefits of projects like this is to learn from each other :-)

As for reviewing, I typically use github's web based review tools. If a PR is particularly big I might check out the branch locally and browse the code there or maybe debug some unit tests to better see what is going on. There are possibly some VS integrations to check out, but I haven't used any so I don't have any recommendations.

@angularsen
Copy link
Owner

As for git in general, I personally prefer SourceTree for reviewing local changes and staging commits.

Beyond that I typically use Git for Windows' bash command line tools for most things in Git and have created a fair share of aliases to reduce the typing needed. ConEmu is a nice tool to organize multiple console windows in tabs, if you are interested in things like that.

@angularsen
Copy link
Owner

angularsen added a commit that referenced this pull request May 1, 2019
Merge conflict had to be ported from PowerShell to C#.
UnitAbbreviationsCache now gets single entries with string arrays instead of multiple entries with single item arrays.
This is an improvement, but should be functionally equivalent.
angularsen added a commit that referenced this pull request May 1, 2019
* Initial project

* Add CodeGen to solution

* Add DragonFruit 0.2.0-alpha.19174.3

* Remove unused after.UnitsNet.sln.targets

* All codegen scripts recreated

* Codegen: Minor whitespace fixes

* README: Minor fix

* Remove PowerShell scripts

* Update build script

* WRC: Remove codegen for tests

It was stepping on the toes of the new codegen and WRC should not generate test code anyhow.

* Remove submodules from csproj

* Minor cleanup

* Add initial WRC codegen

* Don't call WRC PS1 scripts

* WRC StaticQuantityGenerator exact match

* WRC QuantityGenerator OK

* WRC QuantityTypeGenerator OK

* WRC UnitAbbreviationsCacheGenerator OK

* Restructure folders

* WRC: Remove all .ps1 scripts

* Update build message for codegen

* WIP

* Improve xmldoc of Main method

* Add option --skip-wrc

* Remove dead codegen code in WRC

* Re-implement Support multiple abbreviations (#658)

Merge conflict had to be ported from PowerShell to C#.
UnitAbbreviationsCache now gets single entries with string arrays instead of multiple entries with single item arrays.
This is an improvement, but should be functionally equivalent.
@angularsen
Copy link
Owner

@tmilnthorp In case you missed it, I'm hoping you can chime in on onboarding @lipchev as a collaborator. Please see #658 (comment).

@lipchev lipchev deleted the Support-multiple-abbreviations branch May 6, 2019 10:36
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.

Support multiple abbreviations for kilo, mega and other prefixes of units
2 participants