Skip to content

Commit dccbc73

Browse files
committed
Simplifying the logic for determining SerializationSettings
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.
1 parent 1819c56 commit dccbc73

File tree

2 files changed

+57
-41
lines changed

2 files changed

+57
-41
lines changed

AutoRest/AutoRest.Core/Utilities/Extensions.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public static string EscapeXmlComment(this string comment)
244244
}
245245

246246
/// <summary>
247-
/// Returns true is the type is a PrimaryType with KnownPrimaryType matching typeToMatch.
247+
/// Returns true if the type is a PrimaryType with KnownPrimaryType matching typeToMatch.
248248
/// </summary>
249249
/// <param name="type"></param>
250250
/// <param name="typeToMatch"></param>
@@ -263,5 +263,54 @@ public static bool IsPrimaryType(this IType type, KnownPrimaryType typeToMatch)
263263
}
264264
return false;
265265
}
266+
267+
/// <summary>
268+
/// Returns true if the <paramref name="type"/> is a PrimaryType with KnownPrimaryType matching <paramref name="typeToMatch"/>
269+
/// or a DictionaryType with ValueType matching <paramref name="typeToMatch"/> or a SequenceType matching <paramref name="typeToMatch"/>
270+
/// </summary>
271+
/// <param name="type"></param>
272+
/// <param name="typeToMatch"></param>
273+
/// <returns></returns>
274+
public static bool IsOrContainsPrimaryType(this IType type, KnownPrimaryType typeToMatch)
275+
{
276+
if (type == null)
277+
{
278+
return false;
279+
}
280+
281+
if (type.IsPrimaryType(typeToMatch) ||
282+
type.IsDictionaryContainingType(typeToMatch) ||
283+
type.IsSequenceContainingType(typeToMatch))
284+
{
285+
return true;
286+
}
287+
return false;
288+
}
289+
290+
/// <summary>
291+
/// Returns true if the <paramref name="type"/> is a DictionaryType with ValueType matching <paramref name="typeToMatch"/>
292+
/// </summary>
293+
/// <param name="type"></param>
294+
/// <param name="typeToMatch"></param>
295+
/// <returns></returns>
296+
public static bool IsDictionaryContainingType(this IType type, KnownPrimaryType typeToMatch)
297+
{
298+
DictionaryType dictionaryType = type as DictionaryType;
299+
PrimaryType dictionaryPrimaryType = dictionaryType?.ValueType as PrimaryType;
300+
return dictionaryPrimaryType != null && dictionaryPrimaryType.IsPrimaryType(typeToMatch);
301+
}
302+
303+
/// <summary>
304+
/// Returns true if the <paramref name="type"/>is a SequenceType matching <paramref name="typeToMatch"/>
305+
/// </summary>
306+
/// <param name="type"></param>
307+
/// <param name="typeToMatch"></param>
308+
/// <returns></returns>
309+
public static bool IsSequenceContainingType(this IType type, KnownPrimaryType typeToMatch)
310+
{
311+
SequenceType sequenceType = type as SequenceType;
312+
PrimaryType sequencePrimaryType = sequenceType?.ElementType as PrimaryType;
313+
return sequencePrimaryType != null && sequencePrimaryType.IsPrimaryType(typeToMatch);
314+
}
266315
}
267316
}

AutoRest/Generators/CSharp/CSharp/TemplateModels/MethodTemplateModel.cs

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -325,37 +325,19 @@ public string ClientReference
325325
/// <returns></returns>
326326
public string GetSerializationSettingsReference(IType serializationType)
327327
{
328-
SequenceType sequenceType = serializationType as SequenceType;
329-
DictionaryType dictionaryType = serializationType as DictionaryType;
330-
if (serializationType.IsPrimaryType(KnownPrimaryType.Date) ||
331-
(sequenceType != null && sequenceType.ElementType is PrimaryType
332-
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.Date) ||
333-
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
334-
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.Date))
328+
if (serializationType.IsOrContainsPrimaryType(KnownPrimaryType.Date))
335329
{
336330
return "new DateJsonConverter()";
337331
}
338-
else if (serializationType.IsPrimaryType(KnownPrimaryType.DateTimeRfc1123) ||
339-
(sequenceType != null && sequenceType.ElementType is PrimaryType
340-
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.DateTimeRfc1123) ||
341-
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
342-
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.DateTimeRfc1123))
332+
else if (serializationType.IsOrContainsPrimaryType(KnownPrimaryType.DateTimeRfc1123))
343333
{
344334
return "new DateTimeRfc1123JsonConverter()";
345335
}
346-
else if (serializationType.IsPrimaryType(KnownPrimaryType.Base64Url) ||
347-
(sequenceType != null && sequenceType.ElementType is PrimaryType
348-
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.Base64Url) ||
349-
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
350-
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.Base64Url))
336+
else if (serializationType.IsOrContainsPrimaryType(KnownPrimaryType.Base64Url))
351337
{
352338
return "new Base64UrlJsonConverter()";
353339
}
354-
else if (serializationType.IsPrimaryType(KnownPrimaryType.UnixTime) ||
355-
(sequenceType != null && sequenceType.ElementType is PrimaryType
356-
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.UnixTime) ||
357-
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
358-
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.UnixTime))
340+
else if (serializationType.IsOrContainsPrimaryType(KnownPrimaryType.UnixTime))
359341
{
360342
return "new UnixTimeJsonConverter()";
361343
}
@@ -369,33 +351,18 @@ public string GetSerializationSettingsReference(IType serializationType)
369351
/// <returns></returns>
370352
public string GetDeserializationSettingsReference(IType deserializationType)
371353
{
372-
SequenceType sequenceType = deserializationType as SequenceType;
373-
DictionaryType dictionaryType = deserializationType as DictionaryType;
374-
if (deserializationType.IsPrimaryType(KnownPrimaryType.Date) ||
375-
(sequenceType != null && sequenceType.ElementType is PrimaryType
376-
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.Date) ||
377-
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
378-
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.Date))
354+
if (deserializationType.IsOrContainsPrimaryType(KnownPrimaryType.Date))
379355
{
380356
return "new DateJsonConverter()";
381357
}
382-
else if (deserializationType.IsPrimaryType(KnownPrimaryType.Base64Url) ||
383-
(sequenceType != null && sequenceType.ElementType is PrimaryType
384-
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.Base64Url) ||
385-
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
386-
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.Base64Url))
358+
else if (deserializationType.IsOrContainsPrimaryType(KnownPrimaryType.Base64Url))
387359
{
388360
return "new Base64UrlJsonConverter()";
389361
}
390-
else if (deserializationType.IsPrimaryType(KnownPrimaryType.UnixTime) ||
391-
(sequenceType != null && sequenceType.ElementType is PrimaryType
392-
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.UnixTime) ||
393-
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
394-
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.UnixTime))
362+
else if (deserializationType.IsOrContainsPrimaryType(KnownPrimaryType.UnixTime))
395363
{
396364
return "new UnixTimeJsonConverter()";
397365
}
398-
399366
return ClientReference + ".DeserializationSettings";
400367
}
401368

0 commit comments

Comments
 (0)