Skip to content

Commit

Permalink
Removed expression<func> from readxml, no need or it in generated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Oct 20, 2014
1 parent c1a9835 commit c62b774
Show file tree
Hide file tree
Showing 31 changed files with 105 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Gu.Units.Generator/Templates/Quantity.tt
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ namespace <#= Settings.Namespace #>
}

/// <summary>
/// Compares this instance to a specified <see cref="T:MathNet.Spatial.Units.<#= quantity.ClassName #>"/> object and returns an integer that indicates whether this <see cref="instance"/> is shorter than, equal to, or longer than the <see cref="T:MathNet.Spatial.Units.<#= quantity.ClassName #>"/> object.
/// Compares this instance to a specified <see cref="T:MathNet.Spatial.Units.<#= quantity.ClassName #>"/> object and returns an integer that indicates whether this <see cref="quantity"/> is smaller than, equal to, or greater than the <see cref="T:MathNet.Spatial.Units.<#= quantity.ClassName #>"/> object.
/// </summary>
/// <returns>
/// A signed number indicating the relative quantitys of this instance and <paramref name="quantity"/>.
Expand Down Expand Up @@ -432,7 +432,7 @@ namespace <#= Settings.Namespace #>
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.<#= quantity.Unit.ClassName #>, reader, "Value");
XmlExt.SetReadonlyField(ref this, "<#= quantity.Unit.ClassName #>", reader, "Value");
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Gu.Units.Generator/Templates/Quantity.txt
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
}

/// <summary>
/// Compares this instance to a specified <see cref="T:MathNet.Spatial.Units.Length"/> object and returns an integer that indicates whether this <see cref="instance"/> is shorter than, equal to, or longer than the <see cref="T:MathNet.Spatial.Units.Length"/> object.
/// Compares this instance to a specified <see cref="T:MathNet.Spatial.Units.Length"/> object and returns an integer that indicates whether this <see cref="quantity"/> is smaller than, equal to, or greater than the <see cref="T:MathNet.Spatial.Units.Length"/> object.
/// </summary>
/// <returns>
/// A signed number indicating the relative quantitys of this instance and <paramref name="quantity"/>.
Expand Down Expand Up @@ -403,7 +403,7 @@
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Metres, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Metres", reader, "Value");
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Gu.Units.Tests/Gu.Units.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Helpers\UnitTypeProvider.cs" />
<Compile Include="Sandbox.cs" />
<Compile Include="SerializationTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
54 changes: 54 additions & 0 deletions Gu.Units.Tests/SerializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace Gu.Units.Tests
{
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using NUnit.Framework;

public class SerializationTests
{
[Test]
public void RoundtripXml()
{
var length = new Length(1.2, LengthUnit.Metres);
var roundtrip = Roundtrip(length);
Assert.AreEqual(length, roundtrip);
var dummy = new Dummy(length);
var roundtripDummy = Roundtrip(dummy);
Assert.AreEqual(length, roundtripDummy.Length);
}

private T Roundtrip<T>(T value)
{
var serializer = new XmlSerializer(typeof(T));
var builder = new StringBuilder();
string xml;
using (var writer = new StringWriter(builder))
{
serializer.Serialize(writer, value);
xml = builder.ToString();
//Assert.AreEqual(@"<Length Value=""1.2"" />",);
}
using (var reader = new StringReader(xml))
{
var deserialize = (T)serializer.Deserialize(reader);
return deserialize;
}
}

public class Dummy
{
private Dummy()
{
}

public Dummy(Length length)
{
Length = length;
}

public Length Length { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion Gu.Units/Acceleration.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.MetresPerSecondSquared, reader, "Value");
XmlExt.SetReadonlyField(ref this, "MetresPerSecondSquared", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Angle.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Radians, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Radians", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/AngularSpeed.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.RadiansPerSecond, reader, "Value");
XmlExt.SetReadonlyField(ref this, "RadiansPerSecond", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Area.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.SquareMetres, reader, "Value");
XmlExt.SetReadonlyField(ref this, "SquareMetres", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Current.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Amperes, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Amperes", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Density.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.KilogramsPerCubicMetre, reader, "Value");
XmlExt.SetReadonlyField(ref this, "KilogramsPerCubicMetre", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/ElectricCharge.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Coulombs, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Coulombs", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Energy.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Joules, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Joules", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Force.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Newtons, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Newtons", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Fraction.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Fractions, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Fractions", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Frequency.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Hertz, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Hertz", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Length.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Metres, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Metres", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/LuminousIntensity.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Candelas, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Candelas", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Mass.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Kilograms, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Kilograms", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Power.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Watts, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Watts", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Pressure.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Pascals, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Pascals", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Resistance.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Ohm, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Ohm", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/SpecificEnergy.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.JoulesPerKilogram, reader, "Value");
XmlExt.SetReadonlyField(ref this, "JoulesPerKilogram", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Speed.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.MetresPerSecond, reader, "Value");
XmlExt.SetReadonlyField(ref this, "MetresPerSecond", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Stiffness.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.NewtonsPerMetre, reader, "Value");
XmlExt.SetReadonlyField(ref this, "NewtonsPerMetre", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Temperature.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Kelvin, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Kelvin", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Time.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Seconds, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Seconds", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Torque.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.NewtonMetres, reader, "Value");
XmlExt.SetReadonlyField(ref this, "NewtonMetres", reader, "Value");
}

/// <summary>
Expand Down
31 changes: 19 additions & 12 deletions Gu.Units/Utils/XmlExt.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
namespace Gu.Units
{
using System;
using System.Linq.Expressions;
using System.Xml;
using System.Xml.Linq;

public class XmlExt
{
public static void SetReadonlyField<T>(ref T force, Func<T, object> func, double toDouble)
internal static void SetReadonlyField<T>(ref T self, string fieldName, XmlReader reader, string attributeName)
where T : IQuantity
{
throw new NotImplementedException();
reader.MoveToContent();
var d = XmlConvert.ToDouble(reader.GetAttribute(attributeName));
reader.ReadStartElement();
SetReadonlyField(ref self, fieldName, d);
}

public static string ReadAttributeOrElementOrDefault(XElement xElement, string value)
private static void SetReadonlyField<T>(ref T self, string fieldName, double value)
where T : IQuantity
{
throw new NotImplementedException();
var fieldInfo = self.GetType()
.GetField(fieldName);
object boxed = self;
fieldInfo.SetValue(boxed, value);
self = (T)boxed;
}

public static void WriteAttribute(XmlWriter writer, string value, double newtons)
{
throw new NotImplementedException();
}
public static void SetReadonlyField<T>(ref T force, Func<T, double> func, XmlReader reader, string value)
where T : IQuantity

internal static void WriteAttribute(XmlWriter writer, string name, double value)
{
reader.MoveToContent();
writer.WriteStartAttribute(name);
writer.WriteValue(value);
writer.WriteEndAttribute();
}
}
}
5 changes: 2 additions & 3 deletions Gu.Units/Voltage.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Globalization;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.Serialization;

Expand Down Expand Up @@ -368,7 +367,7 @@ public string ToString(string format, IFormatProvider formatProvider, VoltageUni
}

/// <summary>
/// Compares this instance to a specified <see cref="T:MathNet.Spatial.Units.Voltage"/> object and returns an integer that indicates whether this <see cref="instance"/> is shorter than, equal to, or longer than the <see cref="T:MathNet.Spatial.Units.Voltage"/> object.
/// Compares this instance to a specified <see cref="T:MathNet.Spatial.Units.Voltage"/> object and returns an integer that indicates whether this <see cref="quantity"/> is smaller than, equal to, or greater than the <see cref="T:MathNet.Spatial.Units.Voltage"/> object.
/// </summary>
/// <returns>
/// A signed number indicating the relative quantitys of this instance and <paramref name="quantity"/>.
Expand Down Expand Up @@ -458,7 +457,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.Volts, reader, "Value");
XmlExt.SetReadonlyField(ref this, "Volts", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/Volume.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.CubicMetres, reader, "Value");
XmlExt.SetReadonlyField(ref this, "CubicMetres", reader, "Value");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gu.Units/VolumetricFlow.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public XmlSchema GetSchema()
public void ReadXml(XmlReader reader)
{
// Hacking set readonly fields here, can't think of a cleaner workaround
XmlExt.SetReadonlyField(ref this, x => x.CubicMetresPerSecond, reader, "Value");
XmlExt.SetReadonlyField(ref this, "CubicMetresPerSecond", reader, "Value");
}

/// <summary>
Expand Down

0 comments on commit c62b774

Please sign in to comment.