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

New unit: TorquePerLength and fix for array serialization & deserialization #712

Merged
merged 6 commits into from
Oct 29, 2019
Merged

Conversation

dschuermans
Copy link
Contributor

@dschuermans dschuermans commented Oct 17, 2019

Added a new unit: TorquePerLength
This unit is used in the structural analysis field when expressing the rotational stiffness resistance around an axis

Also added support for serialization & deserialization of an array of units in JSON

@codecov-io
Copy link

Codecov Report

❗ No coverage uploaded for pull request base (master@c447451). Click here to learn what that means.
The diff coverage is 67.18%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #712   +/-   ##
=========================================
  Coverage          ?   58.44%           
=========================================
  Files             ?      168           
  Lines             ?    38304           
  Branches          ?        0           
=========================================
  Hits              ?    22386           
  Misses            ?    15918           
  Partials          ?        0
Impacted Files Coverage Δ
UnitsNet/GeneratedCode/UnitAbbreviationsCache.g.cs 100% <ø> (ø)
UnitsNet/GeneratedCode/UnitConverter.g.cs 100% <100%> (ø)
...sNet/GeneratedCode/Quantities/TorquePerLength.g.cs 59.9% <59.9%> (ø)
UnitsNet/GeneratedCode/Quantity.g.cs 81.48% <80%> (ø)
...Net.Serialization.JsonNet/UnitsNetJsonConverter.cs 89.28% <98.07%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c447451...92fc6dd. Read the comment docs.

@dschuermans
Copy link
Contributor Author

@angularsen ping
If you don't want to merge this, is there any way i can create a pre-release package and host it on our own nuget?

@dschuermans
Copy link
Contributor Author

Okay, I added the TorquePerLength on a copy from old/v3, bumped minor & suffix (3.112.0-2) and uploaded the nuget package to our nuget feed.
So i'm all set for now 👍

@angularsen
Copy link
Owner

angularsen commented Oct 21, 2019 via email

Copy link
Owner

@angularsen angularsen left a comment

Choose a reason for hiding this comment

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

Looks good. Good job fixing the compatibility tests 👍
For next time, it would be better to create two separate pull requests for adding a quantity and fixing JSON serialization. I'll look at addressing the syntax suggestions after merging this PR.

@@ -41,9 +42,36 @@ public class UnitsNetJsonConverter : JsonConverter

object obj = TryDeserializeIComparable(reader, serializer);
// A null System.Nullable value or a comparable type was deserialized so return this
if (!(obj is ValueUnit vu))
if (!(obj is ValueUnit) && !(obj is Array))
Copy link
Owner

Choose a reason for hiding this comment

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

Minor, but this could benefit from being rewritten to if (obj is ValueUnit valueUnit) {} else if (obj is Array array) { } instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now that I think about it, "obj is Array" might be cutting it a bit short too?
What if it's an array of strings? or any other type?
Not entirely sure if we can even end up in that scenario though...

Copy link
Owner

Choose a reason for hiding this comment

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

is Array should cover string[] I believe. It's the base type all arrays implement if I understand it right.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, but shouldn't we only be expecting an array of Units there?
When it's an array of e.g. strings, we should return that array as is and not try to parse them as units?

As per the comment there: '// A null System.Nullable value or a comparable type was deserialized so return this'
I'd imagine it applies to a "array of system.nullable values or an array of comparable types"

Just thinking out loud here :)

Copy link
Owner

Choose a reason for hiding this comment

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

Good questions, I don't really recall how this all works anymore and I didn't write the implementation. I would have to read some more unit tests and dig a bit more into how this is actually used.

For instance, does this JSON deserialize properly or does it trip up on the other stuff?

{
"somestring": "foo",
"somenumber": 5,
"some_quantity": { "Value": 5, "Unit": "LengthUnit.Meter" },
"some_quantity_array": [ { "Value": 5, "Unit": "LengthUnit.Meter" } ],
"some_other_array": [ 5, "abc", ["def" ] ]
}


List<object> results = new List<object>();

foreach (var value in values)
Copy link
Owner

Choose a reason for hiding this comment

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

This foreach can be changed to LINQ.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, i copied it from my initial attempt and it was laid out like this for easier debugging while developing.

}

Type unitType = objectType.GetElementType();
Array typedArray = Array.CreateInstance(unitType, results.Count);
Copy link
Owner

Choose a reason for hiding this comment

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

Is this all actually necessary? I think if you replace the foreach and this below with a LINQ like this, I believe it should work:

var results = values.Select(value => ParseValueUnit(value as ValueUnit)).ToArray();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you'll run into runtime exceptions, saying it can't convert object to array or whatever...
This is how I've always converted an "object" into an type specific array (eg string[] or double[] or whatever)

Copy link
Owner

Choose a reason for hiding this comment

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

You were absolutely right, it wasn't quite as simple as I first imagined. I did make some simplifications in #715 if you want to take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, like how you handled it there :)

protected override double PoundForceInchesPerFootInOneNewtonMeterPerMeter => 2.69770722235;
protected override double TonneForceCentimetersPerMeterInOneNewtonMeterPerMeter => 1.01971621298e-2;
protected override double TonneForceMetersPerMeterInOneNewtonMeterPerMeter => 1.01971621298e-4;
protected override double TonneForceMillimetersPerMeterInOneNewtonMeterPerMeter => 1.01971621298e-1;
Copy link
Owner

Choose a reason for hiding this comment

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

Verified these.

@angularsen angularsen merged commit 3694c5f into angularsen:master Oct 29, 2019
angularsen added a commit that referenced this pull request Oct 29, 2019
Found during PR review #712. Addressed separately.
angularsen added a commit that referenced this pull request Oct 29, 2019
Found during PR review #712. Addressed separately.
@dschuermans
Copy link
Contributor Author

Looks good. Good job fixing the compatibility tests 👍
For next time, it would be better to create two separate pull requests for adding a quantity and fixing JSON serialization. I'll look at addressing the syntax suggestions after merging this PR.

Cheers.

Yea, having to do the merge from v3 to v4 it was easier to also incorporate my fixes for the JSON stuff...
Sorry about that one!

@angularsen
Copy link
Owner

I seem to have forgot to release the nuget. Here it is.

Release UnitsNet/4.40.0 · angularsen/UnitsNet

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.

3 participants