Skip to content

Commit

Permalink
+ parsable types are using now invariant culture to parse values
Browse files Browse the repository at this point in the history
  • Loading branch information
Suremaker committed Mar 7, 2013
1 parent 27fdc23 commit 606bdee
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 87 deletions.
121 changes: 61 additions & 60 deletions src/AutoGenerated/releaseNotification.cs

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions src/AutoGenerated/vehicles.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;
#pragma warning disable 660,661
Expand Down Expand Up @@ -37,13 +38,13 @@ internal class NullVehicles : Vehicles
public partial class Color
{
public virtual string Hue { get; set; }
public virtual decimal? Rgb { get; set; }
public virtual int? Rgb { get; set; }
public virtual ColorDescription Description { get; set; }

public Color(XElement element)
{
Hue = element.Elements().Where(e => e.Name == "hue").Select(e => e.Value).SingleOrDefault();
Rgb = element.Elements().Where(e => e.Name == "rgb").Select(e => string.IsNullOrEmpty(e.Value) ? (decimal?)null : decimal.Parse(e.Value)).SingleOrDefault();
Rgb = element.Elements().Where(e => e.Name == "rgb").Select(e => string.IsNullOrEmpty(e.Value) ? (int?)null : int.Parse(e.Value, CultureInfo.InvariantCulture)).SingleOrDefault();
Description = element.Elements().Where(e => e.Name == "description").Select(e => new ColorDescription(e)).SingleOrDefault() ?? new NullColorDescription();
}

Expand All @@ -64,7 +65,7 @@ public Color()
internal class NullColor : Color
{
public override string Hue { get { throw this.NullAccess("Hue"); } }
public override decimal? Rgb { get { throw this.NullAccess("Rgb"); } }
public override int? Rgb { get { throw this.NullAccess("Rgb"); } }
public override ColorDescription Description { get { throw this.NullAccess("Description"); } }
}

Expand All @@ -73,13 +74,15 @@ public partial class Car
public virtual string Brand { get; set; }
public virtual DateTime? ProductionDate { get; set; }
public virtual ManufacturerName Manufacturer { get; set; }
public virtual decimal? ModelVersion { get; set; }
public virtual Color Color { get; set; }

public Car(XElement element)
{
Brand = element.Elements().Where(e => e.Name == "brand").Select(e => e.Value).SingleOrDefault();
ProductionDate = element.Elements().Where(e => e.Name == "productionDate").Select(e => string.IsNullOrEmpty(e.Value) ? (DateTime?)null : DateTime.Parse(e.Value)).SingleOrDefault();
ProductionDate = element.Elements().Where(e => e.Name == "productionDate").Select(e => string.IsNullOrEmpty(e.Value) ? (DateTime?)null : DateTime.Parse(e.Value, CultureInfo.InvariantCulture)).SingleOrDefault();
Manufacturer = element.Elements().Where(e => e.Name == "manufacturer").Select(e => new ManufacturerName(e)).SingleOrDefault() ?? new NullManufacturerName();
ModelVersion = element.Elements().Where(e => e.Name == "modelVersion").Select(e => string.IsNullOrEmpty(e.Value) ? (decimal?)null : decimal.Parse(e.Value, CultureInfo.InvariantCulture)).SingleOrDefault();
Color = element.Elements().Where(e => e.Name == "color").Select(e => new Color(e)).SingleOrDefault() ?? new NullColor();
}

Expand All @@ -102,6 +105,7 @@ internal class NullCar : Car
public override string Brand { get { throw this.NullAccess("Brand"); } }
public override DateTime? ProductionDate { get { throw this.NullAccess("ProductionDate"); } }
public override ManufacturerName Manufacturer { get { throw this.NullAccess("Manufacturer"); } }
public override decimal? ModelVersion { get { throw this.NullAccess("ModelVersion"); } }
public override Color Color { get { throw this.NullAccess("Color"); } }
}

Expand Down
Expand Up @@ -34,11 +34,12 @@ public void ShouldHaveCar()
{
ClassInfo car = _classes.Single(c => c.XmlName == "car");

Assert.That(car.Elements.Count, Is.EqualTo(4));
Assert.That(car.Elements.Count, Is.EqualTo(5));
GeneratorAssertHelper.AssertStringProperty(car, "brand", false, "Brand");
GeneratorAssertHelper.AssertBindedProperty(car, "color", "color", false, "Color", "Color");
GeneratorAssertHelper.AssertBindedProperty(car, "manufacturer", "manufacturerName", false, "Manufacturer", "ManufacturerName");
GeneratorAssertHelper.AssertProperty(car, "productionDate", "date", false, "ProductionDate", "DateTime?", false);
GeneratorAssertHelper.AssertProperty(car, "modelVersion", "decimal", false, "ModelVersion", "decimal?", false);
}

[Test]
Expand All @@ -48,7 +49,7 @@ public void ShouldHaveColor()
Assert.That(color.Elements.Count, Is.EqualTo(3));

GeneratorAssertHelper.AssertStringProperty(color, "hue", false, "Hue");
GeneratorAssertHelper.AssertProperty(color, "rgb", "decimal", false, "Rgb", "decimal?", false);
GeneratorAssertHelper.AssertProperty(color, "rgb", "int", false, "Rgb", "int?", false);
GeneratorAssertHelper.AssertBindedProperty(color, "description", "colorDescription", false, "Description", "ColorDescription");
}

Expand Down
3 changes: 2 additions & 1 deletion src/SevenDigital.Parsing.XsdToObject.Tests/res/schema.xsd
Expand Up @@ -12,11 +12,12 @@
<xs:element name="brand" type="xs:string" minOccurs="0" />
<xs:element name="productionDate" type="xs:date" minOccurs="0" />
<xs:element name="manufacturer" type="manufacturerName" minOccurs="0" />
<xs:element name="modelVersion" type="xs:decimal" />
<xs:element name="color" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="hue" type="xs:string" minOccurs="0" />
<xs:element name="rgb" type="xs:decimal" minOccurs="0" />
<xs:element name="rgb" type="xs:int" minOccurs="0" />
<xs:element name="description" type="colorDescription" minOccurs="0" />
</xs:sequence>
</xs:complexType>
Expand Down
10 changes: 6 additions & 4 deletions src/SevenDigital.Parsing.XsdToObject.Tests/res/xml.xml
Expand Up @@ -2,21 +2,23 @@
<vehicles>
<car>
<brand>BMW</brand>
<productionDate>2008-10-25</productionDate>
<productionDate>2008-10-25</productionDate>
<color>
<hue>black</hue>
<description descriptionType="thisIsTheType">
<text>This is the text</text>
</description>
</description>
</color>
<manufacturer nameType="MajorType">BMW UK</manufacturer>
<manufacturer nameType="MajorType">BMW UK</manufacturer>
<modelVersion>2.1</modelVersion>
</car>
<car>
<brand>Honda</brand>
<color>
<hue>red</hue>
<rgb>16711680</rgb>
<rgb>16711680</rgb>
</color>
<manufacturer>UK Manufacturing Ltd</manufacturer>
<modelVersion>1.0</modelVersion>
</car>
</vehicles>
12 changes: 9 additions & 3 deletions src/SevenDigital.Parsing.XsdToObject/ClassWriter.cs
Expand Up @@ -45,6 +45,7 @@ private void WriteUsings()
{
_writer.WriteLine("using System;");
_writer.WriteLine("using System.Collections.Generic;");
_writer.WriteLine("using System.Globalization;");
_writer.WriteLine("using System.Linq;");
_writer.WriteLine("using System.Xml.Linq;");
_writer.WriteLine("#pragma warning disable 660,661");
Expand Down Expand Up @@ -168,7 +169,7 @@ private void WritePropertyInitialization(StreamWriter writer, PropertyInfo prope
private void WriteValuePropertyInitialization(StreamWriter writer, PropertyInfo property)
{
var instanceCreation = property.IsParsable
? GetParsedPropertyValue(property,"element")
? GetParsedPropertyValue(property, "element")
: "element.Value";

writer.WriteLine("\t\t\t{0} = {1};", property.GetCodeName(), instanceCreation);
Expand All @@ -178,7 +179,7 @@ private string GetPropertyValueRetriever(PropertyInfo property, string collectio
{
string instanceCreation = property.IsParsable
? GetParsedPropertyValue(property, "e")
: GetXElementToPropertyValue(property,"e");
: GetXElementToPropertyValue(property, "e");

return string.Format("element.{0}().Where(e => e.Name == \"{1}\").Select(e => {2})",
collectionName,
Expand All @@ -188,7 +189,12 @@ private string GetPropertyValueRetriever(PropertyInfo property, string collectio

private static string GetParsedPropertyValue(PropertyInfo property, string variableName)
{
return string.Format("string.IsNullOrEmpty({0}.Value) ? ({1})null : {2}.Parse({0}.Value)", variableName, property.GetCodeType(), property.GetCodeType().TrimEnd('?'));
var valueToParse = variableName + ".Value";

return string.Format("string.IsNullOrEmpty({0}) ? ({1})null : {2}",
valueToParse,
property.GetCodeType(),
TypeUtils.GetParsableType(property.XmlType).ConstructParseCall(valueToParse));
}

private string GetPropertyValueAccessorMethod(PropertyInfo property)
Expand Down
31 changes: 31 additions & 0 deletions src/SevenDigital.Parsing.XsdToObject/ParsableType.cs
@@ -0,0 +1,31 @@
using System;

namespace SevenDigital.Parsing.XsdToObject
{
public class ParsableType
{
private readonly string _parseCallFormat;

public ParsableType(string xmlTypeName, string netTypeName, Type type)
{
XmlTypeName = xmlTypeName;
NetTypeName = netTypeName;
_parseCallFormat = FormatParseCall(type);
}

private string FormatParseCall(Type type)
{
var formatParseCall = type.GetMethod("Parse", new[] { typeof(string), typeof(IFormatProvider) }) != null
? ".Parse({0}, CultureInfo.InvariantCulture)"
: ".Parse({0})";
return NetTypeName.TrimEnd('?') + formatParseCall;
}

public string XmlTypeName { get; private set; }
public string NetTypeName { get; private set; }
public string ConstructParseCall(string value)
{
return string.Format(_parseCallFormat, value);
}
}
}
2 changes: 1 addition & 1 deletion src/SevenDigital.Parsing.XsdToObject/PropertyInfo.cs
Expand Up @@ -26,7 +26,7 @@ public string GetCodeName()

public string GetCodeType()
{
string type = BindedType != null ? BindedType.GetCodeName() : TypeUtils.ToCodeType(XmlType);
string type = BindedType != null ? BindedType.GetCodeName() : TypeUtils.ToNetTypeName(XmlType);
return !IsList ? type : string.Format("IList<{0}>", type);
}

Expand Down
Expand Up @@ -43,6 +43,7 @@
<Compile Include="ClassParser.cs" />
<Compile Include="ClassWriter.cs" />
<Compile Include="NameUtils.cs" />
<Compile Include="ParsableType.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PropertyInfo.cs" />
Expand Down
37 changes: 25 additions & 12 deletions src/SevenDigital.Parsing.XsdToObject/TypeUtils.cs
@@ -1,30 +1,43 @@
using System;
using System.Collections.Generic;

namespace SevenDigital.Parsing.XsdToObject
{
public static class TypeUtils
{
private static readonly IDictionary<string, string> _parsableTypesMap = new Dictionary<string, string>
private static readonly IDictionary<string, ParsableType> _parsableTypesMap = new Dictionary<string, ParsableType>();

static TypeUtils()
{
AddType("boolean", "bool?", typeof(bool));
AddType("integer", "int?", typeof(int));
AddType("int", "int?", typeof(int));
AddType("decimal", "decimal?", typeof(decimal));
AddType("dateTime", "DateTime?", typeof(DateTime));
AddType("date", "DateTime?", typeof(DateTime));
}

private static void AddType(string xmlTypeName, string codeTypeName, Type netType)
{
{"boolean","bool?"},
{"integer","int?"},
{"int","int?"},
{"decimal","decimal?"},
{"dateTime","DateTime?"},
{"date","DateTime?"}
};
_parsableTypesMap.Add(xmlTypeName, new ParsableType(xmlTypeName, codeTypeName, netType));
}

public static bool IsParsable(string xmlType)
{
return _parsableTypesMap.ContainsKey(xmlType.ToLower());
}

public static string ToCodeType(string xmlType)
public static ParsableType GetParsableType(string xmlType)
{
return _parsableTypesMap[xmlType];
}

public static string ToNetTypeName(string xmlType)
{
string result;
ParsableType result;
return _parsableTypesMap.TryGetValue(xmlType.ToLower(), out result)
? result
: xmlType;
? result.NetTypeName
: xmlType;
}

public static bool IsSimpleType(string xmlType)
Expand Down

0 comments on commit 606bdee

Please sign in to comment.