Skip to content

Commit

Permalink
Simplifying the logic for determining SerializationSettings
Browse files Browse the repository at this point in the history
Removing duplicated and hard-to-read code that determined the C#
serialization/deserialization settings to be used for an IType. Changes
are needed because adding new checks for known primary types was failing
builds based cyclomatic complexity > 25.

This change adds extension methods that determine if an IType is either
a PrimaryType for a given KnownPrimaryType, or a
DictionaryType/SequenceType that contains values of that known type.
  • Loading branch information
tbombach committed Apr 21, 2016
1 parent 1819c56 commit dccbc73
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 41 deletions.
51 changes: 50 additions & 1 deletion AutoRest/AutoRest.Core/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public static string EscapeXmlComment(this string comment)
}

/// <summary>
/// Returns true is the type is a PrimaryType with KnownPrimaryType matching typeToMatch.
/// Returns true if the type is a PrimaryType with KnownPrimaryType matching typeToMatch.
/// </summary>
/// <param name="type"></param>
/// <param name="typeToMatch"></param>
Expand All @@ -263,5 +263,54 @@ public static bool IsPrimaryType(this IType type, KnownPrimaryType typeToMatch)
}
return false;
}

/// <summary>
/// Returns true if the <paramref name="type"/> is a PrimaryType with KnownPrimaryType matching <paramref name="typeToMatch"/>
/// or a DictionaryType with ValueType matching <paramref name="typeToMatch"/> or a SequenceType matching <paramref name="typeToMatch"/>
/// </summary>
/// <param name="type"></param>
/// <param name="typeToMatch"></param>
/// <returns></returns>
public static bool IsOrContainsPrimaryType(this IType type, KnownPrimaryType typeToMatch)
{
if (type == null)
{
return false;
}

if (type.IsPrimaryType(typeToMatch) ||
type.IsDictionaryContainingType(typeToMatch) ||
type.IsSequenceContainingType(typeToMatch))
{
return true;
}
return false;
}

/// <summary>
/// Returns true if the <paramref name="type"/> is a DictionaryType with ValueType matching <paramref name="typeToMatch"/>
/// </summary>
/// <param name="type"></param>
/// <param name="typeToMatch"></param>
/// <returns></returns>
public static bool IsDictionaryContainingType(this IType type, KnownPrimaryType typeToMatch)
{
DictionaryType dictionaryType = type as DictionaryType;
PrimaryType dictionaryPrimaryType = dictionaryType?.ValueType as PrimaryType;
return dictionaryPrimaryType != null && dictionaryPrimaryType.IsPrimaryType(typeToMatch);
}

/// <summary>
/// Returns true if the <paramref name="type"/>is a SequenceType matching <paramref name="typeToMatch"/>
/// </summary>
/// <param name="type"></param>
/// <param name="typeToMatch"></param>
/// <returns></returns>
public static bool IsSequenceContainingType(this IType type, KnownPrimaryType typeToMatch)
{
SequenceType sequenceType = type as SequenceType;
PrimaryType sequencePrimaryType = sequenceType?.ElementType as PrimaryType;
return sequencePrimaryType != null && sequencePrimaryType.IsPrimaryType(typeToMatch);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,37 +325,19 @@ public string ClientReference
/// <returns></returns>
public string GetSerializationSettingsReference(IType serializationType)
{
SequenceType sequenceType = serializationType as SequenceType;
DictionaryType dictionaryType = serializationType as DictionaryType;
if (serializationType.IsPrimaryType(KnownPrimaryType.Date) ||
(sequenceType != null && sequenceType.ElementType is PrimaryType
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.Date) ||
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.Date))
if (serializationType.IsOrContainsPrimaryType(KnownPrimaryType.Date))
{
return "new DateJsonConverter()";
}
else if (serializationType.IsPrimaryType(KnownPrimaryType.DateTimeRfc1123) ||
(sequenceType != null && sequenceType.ElementType is PrimaryType
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.DateTimeRfc1123) ||
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.DateTimeRfc1123))
else if (serializationType.IsOrContainsPrimaryType(KnownPrimaryType.DateTimeRfc1123))
{
return "new DateTimeRfc1123JsonConverter()";
}
else if (serializationType.IsPrimaryType(KnownPrimaryType.Base64Url) ||
(sequenceType != null && sequenceType.ElementType is PrimaryType
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.Base64Url) ||
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.Base64Url))
else if (serializationType.IsOrContainsPrimaryType(KnownPrimaryType.Base64Url))
{
return "new Base64UrlJsonConverter()";
}
else if (serializationType.IsPrimaryType(KnownPrimaryType.UnixTime) ||
(sequenceType != null && sequenceType.ElementType is PrimaryType
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.UnixTime) ||
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.UnixTime))
else if (serializationType.IsOrContainsPrimaryType(KnownPrimaryType.UnixTime))
{
return "new UnixTimeJsonConverter()";
}
Expand All @@ -369,33 +351,18 @@ public string GetSerializationSettingsReference(IType serializationType)
/// <returns></returns>
public string GetDeserializationSettingsReference(IType deserializationType)
{
SequenceType sequenceType = deserializationType as SequenceType;
DictionaryType dictionaryType = deserializationType as DictionaryType;
if (deserializationType.IsPrimaryType(KnownPrimaryType.Date) ||
(sequenceType != null && sequenceType.ElementType is PrimaryType
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.Date) ||
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.Date))
if (deserializationType.IsOrContainsPrimaryType(KnownPrimaryType.Date))
{
return "new DateJsonConverter()";
}
else if (deserializationType.IsPrimaryType(KnownPrimaryType.Base64Url) ||
(sequenceType != null && sequenceType.ElementType is PrimaryType
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.Base64Url) ||
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.Base64Url))
else if (deserializationType.IsOrContainsPrimaryType(KnownPrimaryType.Base64Url))
{
return "new Base64UrlJsonConverter()";
}
else if (deserializationType.IsPrimaryType(KnownPrimaryType.UnixTime) ||
(sequenceType != null && sequenceType.ElementType is PrimaryType
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.UnixTime) ||
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.UnixTime))
else if (deserializationType.IsOrContainsPrimaryType(KnownPrimaryType.UnixTime))
{
return "new UnixTimeJsonConverter()";
}

return ClientReference + ".DeserializationSettings";
}

Expand Down

0 comments on commit dccbc73

Please sign in to comment.