Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion UnitsNet.Serialization.JsonNet/UnitsNetJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
object unit = Enum.Parse(reflectedUnitEnumType, unitEnumValue);

// Mass.From() method, assume no overloads exist
MethodInfo fromMethod = reflectedUnitType.GetMethod("From");
MethodInfo fromMethod = (from m in reflectedUnitType.GetMethods()
where m.Name.Equals("From", StringComparison.InvariantCulture) &&
!m.ReturnType.IsGenericType // we want the non nullable type
Copy link
Owner

Choose a reason for hiding this comment

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

I just noticed this. It probably works already, but I found a post on how to properly test for nullable.

http://stackoverflow.com/a/374663/134761

Do you think we should use that method 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.

The tests captured this, hence the change (I would never have discovered it without the tests). Since there is a test that checks that this work I would leave it as is. We could add m.ReturnType.IsValueType && Nullable.GetUnderlyingType(m.ReturnType) == null but what's there right now works and it seems unlikely that more functions named From are added.

Copy link
Owner

Choose a reason for hiding this comment

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

OK, fair enough. We will leave it as-is.

select m).Single();

// Ex: Mass.From(55, MassUnit.Gram)
// TODO: there is a possible loss of precision if base value requires higher precision than double can represent.
Expand Down
90 changes: 90 additions & 0 deletions UnitsNet.Tests/NullableConstructors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright(c) 2007 Andreas Gullberg Larsen
// https://github.com/anjdreas/UnitsNet
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using NUnit.Framework;
using UnitsNet.Units;

namespace UnitsNet.Tests
{
public class NullableConstructors
{
[Test]
public void StaticConstructorWithNullReturnsNullWhenBackingTypeIsDouble()
{
Length? meter = Length.FromMeters(null);
Assert.IsFalse(meter.HasValue);
}

[Test]
public void StaticConstructorWithNullAndEnumReturnsNullWhenBackingTypeIsDouble()
{
Length? meter = Length.From(null, LengthUnit.Meter);
Assert.IsFalse(meter.HasValue);
}

[Test]
public void StaticConstructorWithNullAndEnumArgumentReturnsValueWhenInputArgumentHasValueWhenBackingTypeIsDouble()
{
double? value = 1.0;
Length? meter = Length.From(value, LengthUnit.Meter);
Assert.IsTrue(meter.HasValue);
}

[Test]
public void StaticConstructorWithNullArgumentReturnsValueWhenInputArgumentHasValueWhenBackingTypeIsDouble()
{
double? value = 1.0;
Length? meter = Length.FromMeters(value);
Assert.IsTrue(meter.HasValue);
}

[Test]
public void StaticConstructorWithNullReturnsNullWhenBackingTypeIsDecimal()
{
Information? meter = Information.FromBytes(null);
Assert.IsFalse(meter.HasValue);
}

[Test]
public void StaticConstructorWithNullAndEnumReturnsNullWhenBackingTypeIsDecimal()
{
Information? meter = Information.From(null, InformationUnit.Byte);
Assert.IsFalse(meter.HasValue);
}

[Test]
public void StaticConstructorWithNullAndEnumArgumentReturnsValueWhenInputArgumentHasValueWhenBackingTypeIsDecimal()
{
double? value = 1.0;
Information? meter = Information.From(value, InformationUnit.Byte);
Assert.IsTrue(meter.HasValue);
}

[Test]
public void StaticConstructorWithNullArgumentReturnsValueWhenInputArgumentHasValueWhenBackingTypeIsDecimal()
{
double? value = 1.0;
Information? meter = Information.FromBytes(value);
Assert.IsTrue(meter.HasValue);
}
}
}
152 changes: 146 additions & 6 deletions UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,112 @@ public static Acceleration FromNanometerPerSecondSquared(double nanometerperseco
}


/// <summary>
/// Get nullable Acceleration from nullable CentimeterPerSecondSquared.
/// </summary>
public static Acceleration? FromCentimeterPerSecondSquared(double? centimeterpersecondsquared)
{
if (centimeterpersecondsquared.HasValue)
{
return FromCentimeterPerSecondSquared(centimeterpersecondsquared.Value);
}
else
{
return null;
}
}

/// <summary>
/// Get nullable Acceleration from nullable DecimeterPerSecondSquared.
/// </summary>
public static Acceleration? FromDecimeterPerSecondSquared(double? decimeterpersecondsquared)
{
if (decimeterpersecondsquared.HasValue)
{
return FromDecimeterPerSecondSquared(decimeterpersecondsquared.Value);
}
else
{
return null;
}
}

/// <summary>
/// Get nullable Acceleration from nullable KilometerPerSecondSquared.
/// </summary>
public static Acceleration? FromKilometerPerSecondSquared(double? kilometerpersecondsquared)
{
if (kilometerpersecondsquared.HasValue)
{
return FromKilometerPerSecondSquared(kilometerpersecondsquared.Value);
}
else
{
return null;
}
}

/// <summary>
/// Get nullable Acceleration from nullable MeterPerSecondSquared.
/// </summary>
public static Acceleration? FromMeterPerSecondSquared(double? meterpersecondsquared)
{
if (meterpersecondsquared.HasValue)
{
return FromMeterPerSecondSquared(meterpersecondsquared.Value);
}
else
{
return null;
}
}

/// <summary>
/// Get nullable Acceleration from nullable MicrometerPerSecondSquared.
/// </summary>
public static Acceleration? FromMicrometerPerSecondSquared(double? micrometerpersecondsquared)
{
if (micrometerpersecondsquared.HasValue)
{
return FromMicrometerPerSecondSquared(micrometerpersecondsquared.Value);
}
else
{
return null;
}
}

/// <summary>
/// Get nullable Acceleration from nullable MillimeterPerSecondSquared.
/// </summary>
public static Acceleration? FromMillimeterPerSecondSquared(double? millimeterpersecondsquared)
{
if (millimeterpersecondsquared.HasValue)
{
return FromMillimeterPerSecondSquared(millimeterpersecondsquared.Value);
}
else
{
return null;
}
}

/// <summary>
/// Get nullable Acceleration from nullable NanometerPerSecondSquared.
/// </summary>
public static Acceleration? FromNanometerPerSecondSquared(double? nanometerpersecondsquared)
{
if (nanometerpersecondsquared.HasValue)
{
return FromNanometerPerSecondSquared(nanometerpersecondsquared.Value);
}
else
{
return null;
}
}


/// <summary>
/// Dynamically convert from value and unit enum <see cref="AccelerationUnit" /> to <see cref="Acceleration" />.
/// </summary>
Expand Down Expand Up @@ -206,6 +312,40 @@ public static Acceleration From(double value, AccelerationUnit fromUnit)
}
}

/// <summary>
/// Dynamically convert from value and unit enum <see cref="AccelerationUnit" /> to <see cref="Acceleration" />.
/// </summary>
/// <param name="value">Value to convert from.</param>
/// <param name="fromUnit">Unit to convert from.</param>
/// <returns>Acceleration unit value.</returns>
public static Acceleration? From(double? value, AccelerationUnit fromUnit)
{
if (!value.HasValue)
{
return null;
}
switch (fromUnit)
{
case AccelerationUnit.CentimeterPerSecondSquared:
return FromCentimeterPerSecondSquared(value.Value);
case AccelerationUnit.DecimeterPerSecondSquared:
return FromDecimeterPerSecondSquared(value.Value);
case AccelerationUnit.KilometerPerSecondSquared:
return FromKilometerPerSecondSquared(value.Value);
case AccelerationUnit.MeterPerSecondSquared:
return FromMeterPerSecondSquared(value.Value);
case AccelerationUnit.MicrometerPerSecondSquared:
return FromMicrometerPerSecondSquared(value.Value);
case AccelerationUnit.MillimeterPerSecondSquared:
return FromMillimeterPerSecondSquared(value.Value);
case AccelerationUnit.NanometerPerSecondSquared:
return FromNanometerPerSecondSquared(value.Value);

default:
throw new NotImplementedException("fromUnit: " + fromUnit);
}
}

/// <summary>
/// Get unit abbreviation string.
/// </summary>
Expand Down Expand Up @@ -371,14 +511,14 @@ public double As(AccelerationUnit unit)
/// "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
/// </exception>
/// <exception cref="AmbiguousUnitParseException">
/// More than one unit is represented by the specified unit abbreviation.
/// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
/// More than one unit is represented by the specified unit abbreviation.
/// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
/// </exception>
/// <exception cref="UnitsNetException">
/// If anything else goes wrong, typically due to a bug or unhandled case.
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
/// Units.NET exceptions from other exceptions.
/// If anything else goes wrong, typically due to a bug or unhandled case.
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
/// Units.NET exceptions from other exceptions.
/// </exception>
public static Acceleration Parse(string str, IFormatProvider formatProvider = null)
{
Expand Down
84 changes: 78 additions & 6 deletions UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,52 @@ public static AmplitudeRatio FromDecibelVolts(double decibelvolts)
}


/// <summary>
/// Get nullable AmplitudeRatio from nullable DecibelMicrovolts.
/// </summary>
public static AmplitudeRatio? FromDecibelMicrovolts(double? decibelmicrovolts)
{
if (decibelmicrovolts.HasValue)
{
return FromDecibelMicrovolts(decibelmicrovolts.Value);
}
else
{
return null;
}
}

/// <summary>
/// Get nullable AmplitudeRatio from nullable DecibelMillivolts.
/// </summary>
public static AmplitudeRatio? FromDecibelMillivolts(double? decibelmillivolts)
{
if (decibelmillivolts.HasValue)
{
return FromDecibelMillivolts(decibelmillivolts.Value);
}
else
{
return null;
}
}

/// <summary>
/// Get nullable AmplitudeRatio from nullable DecibelVolts.
/// </summary>
public static AmplitudeRatio? FromDecibelVolts(double? decibelvolts)
{
if (decibelvolts.HasValue)
{
return FromDecibelVolts(decibelvolts.Value);
}
else
{
return null;
}
}


/// <summary>
/// Dynamically convert from value and unit enum <see cref="AmplitudeRatioUnit" /> to <see cref="AmplitudeRatio" />.
/// </summary>
Expand All @@ -134,6 +180,32 @@ public static AmplitudeRatio From(double value, AmplitudeRatioUnit fromUnit)
}
}

/// <summary>
/// Dynamically convert from value and unit enum <see cref="AmplitudeRatioUnit" /> to <see cref="AmplitudeRatio" />.
/// </summary>
/// <param name="value">Value to convert from.</param>
/// <param name="fromUnit">Unit to convert from.</param>
/// <returns>AmplitudeRatio unit value.</returns>
public static AmplitudeRatio? From(double? value, AmplitudeRatioUnit fromUnit)
{
if (!value.HasValue)
{
return null;
}
switch (fromUnit)
{
case AmplitudeRatioUnit.DecibelMicrovolt:
return FromDecibelMicrovolts(value.Value);
case AmplitudeRatioUnit.DecibelMillivolt:
return FromDecibelMillivolts(value.Value);
case AmplitudeRatioUnit.DecibelVolt:
return FromDecibelVolts(value.Value);

default:
throw new NotImplementedException("fromUnit: " + fromUnit);
}
}

/// <summary>
/// Get unit abbreviation string.
/// </summary>
Expand Down Expand Up @@ -299,14 +371,14 @@ public double As(AmplitudeRatioUnit unit)
/// "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
/// </exception>
/// <exception cref="AmbiguousUnitParseException">
/// More than one unit is represented by the specified unit abbreviation.
/// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
/// More than one unit is represented by the specified unit abbreviation.
/// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
/// </exception>
/// <exception cref="UnitsNetException">
/// If anything else goes wrong, typically due to a bug or unhandled case.
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
/// Units.NET exceptions from other exceptions.
/// If anything else goes wrong, typically due to a bug or unhandled case.
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
/// Units.NET exceptions from other exceptions.
/// </exception>
public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = null)
{
Expand Down
Loading