Skip to content

Commit

Permalink
ref - Refactored GetPartType() to specify componen...
Browse files Browse the repository at this point in the history
...t once

---

Type: ref
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Oct 9, 2024
1 parent 50dc8b4 commit ffb6962
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 95 deletions.
4 changes: 2 additions & 2 deletions VisualCard.Calendar/Parsers/VCalendarParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public Parts.Calendar Parse()

// Get the type and its properties
Type calendarType = subPart is not null ? subPart.GetType() : calendar.GetType();
var partType = VCalendarParserTools.GetPartType(info.Prefix, VCalendarParserTools.GetObjectTypeFromType(calendarType), CalendarVersion, calendarType);
var partType = VCalendarParserTools.GetPartType(info.Prefix, CalendarVersion, calendarType);

// Check the type for allowed types
string[] elementTypes = VcardCommonTools.GetTypes(info.Arguments, partType.defaultType);
Expand Down Expand Up @@ -350,7 +350,7 @@ private bool HasComponent<TComponent>(string expectedFieldName, TComponent compo
where TComponent : Parts.Calendar
{
// Requirement checks
var partType = VCalendarParserTools.GetPartType(expectedFieldName, VCalendarParserTools.GetObjectTypeFromComponent(component), CalendarVersion, component.GetType());
var partType = VCalendarParserTools.GetPartType(expectedFieldName, CalendarVersion, component.GetType());
bool exists = false;
switch (partType.type)
{
Expand Down
36 changes: 8 additions & 28 deletions VisualCard.Calendar/Parsers/VCalendarParserTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ internal static string GetPrefixFromPartsArrayEnum(CalendarPartsArrayEnum partsA
_ => ""
};

internal static CalendarPartsArrayEnum GetPartsArrayEnumFromType(Type? partsArrayType, string objectType, Version cardVersion, Type componentType)
internal static CalendarPartsArrayEnum GetPartsArrayEnumFromType(Type? partsArrayType, Version cardVersion, Type componentType)
{
if (partsArrayType is null)
throw new NotImplementedException("Type is not provided.");
Expand All @@ -122,21 +122,21 @@ internal static CalendarPartsArrayEnum GetPartsArrayEnumFromType(Type? partsArra
foreach (CalendarPartsArrayEnum part in enums)
{
string prefix = GetPrefixFromPartsArrayEnum(part);
var type = GetPartType(prefix, objectType, cardVersion, componentType);
var type = GetPartType(prefix, cardVersion, componentType);
if (type.enumType == partsArrayType)
return part;
}
return CalendarPartsArrayEnum.IanaNames;
}

internal static VCalendarPartType GetPartType(string prefix, string objectType, Version calendarVersion, Type componentType)
internal static VCalendarPartType GetPartType(string prefix, Version calendarVersion, Type componentType)
{
string[] allowedStatuses =
objectType == VCalendarConstants._objectVEventSpecifier && calendarVersion.Major == 2 ? ["TENTATIVE", "CONFIRMED", "CANCELLED"] :
objectType == VCalendarConstants._objectVEventSpecifier && calendarVersion.Major == 1 ? ["NEEDS ACTION", "SENT", "TENTATIVE", "CONFIRMED", "DECLINED", "DELEGATED"] :
objectType == VCalendarConstants._objectVTodoSpecifier && calendarVersion.Major == 2 ? ["NEEDS-ACTION", "COMPLETED", "IN-PROCESS", "CANCELLED"] :
objectType == VCalendarConstants._objectVTodoSpecifier && calendarVersion.Major == 1 ? ["NEEDS ACTION", "SENT", "ACCEPTED", "COMPLETED", "DECLINED", "DELEGATED"] :
objectType == VCalendarConstants._objectVJournalSpecifier ? ["DRAFT", "FINAL", "CANCELLED"] :
componentType == typeof(CalendarEvent) && calendarVersion.Major == 2 ? ["TENTATIVE", "CONFIRMED", "CANCELLED"] :
componentType == typeof(CalendarEvent) && calendarVersion.Major == 1 ? ["NEEDS ACTION", "SENT", "TENTATIVE", "CONFIRMED", "DECLINED", "DELEGATED"] :
componentType == typeof(CalendarTodo) && calendarVersion.Major == 2 ? ["NEEDS-ACTION", "COMPLETED", "IN-PROCESS", "CANCELLED"] :
componentType == typeof(CalendarTodo) && calendarVersion.Major == 1 ? ["NEEDS ACTION", "SENT", "ACCEPTED", "COMPLETED", "DECLINED", "DELEGATED"] :
componentType == typeof(CalendarJournal) ? ["DRAFT", "FINAL", "CANCELLED"] :
[];
return prefix switch
{
Expand Down Expand Up @@ -201,26 +201,6 @@ internal static VCalendarPartType GetPartType(string prefix, string objectType,
};
}

internal static string GetObjectTypeFromComponent<TComponent>(TComponent component)
where TComponent : Parts.Calendar =>
GetObjectTypeFromType(component.GetType(), component is CalendarOtherComponent other ? other.ComponentName : "");

internal static string GetObjectTypeFromType(Type type, string specifier = "") =>
type.Name switch
{
nameof(Parts.Calendar) => VCalendarConstants._objectVCalendarSpecifier,
nameof(CalendarEvent) => VCalendarConstants._objectVEventSpecifier,
nameof(CalendarTodo) => VCalendarConstants._objectVTodoSpecifier,
nameof(CalendarJournal) => VCalendarConstants._objectVJournalSpecifier,
nameof(CalendarFreeBusy) => VCalendarConstants._objectVFreeBusySpecifier,
nameof(CalendarTimeZone) => VCalendarConstants._objectVTimeZoneSpecifier,
nameof(CalendarStandard) => VCalendarConstants._objectVStandardSpecifier,
nameof(CalendarDaylight) => VCalendarConstants._objectVDaylightSpecifier,
nameof(CalendarAlarm) => VCalendarConstants._objectVAlarmSpecifier,
nameof(CalendarOtherComponent) => specifier,
_ => throw new InvalidDataException("Type is not a valid component"),
};

private static bool TypeMatch(Type componentType, params Type[] expectedTypes) =>
expectedTypes.Contains(componentType);
}
Expand Down
40 changes: 20 additions & 20 deletions VisualCard.Calendar/Parts/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public class Calendar : IEquatable<Calendar>
public virtual TPart[] GetPartsArray<TPart>() where TPart : BaseCalendarPartInfo
{
// Get the parts enumeration according to the type
var key = VCalendarParserTools.GetPartsArrayEnumFromType(typeof(TPart), VCalendarConstants._objectVCalendarSpecifier, CalendarVersion, GetType());
var key = VCalendarParserTools.GetPartsArrayEnumFromType(typeof(TPart), CalendarVersion, GetType());

// Now, return the value
return GetPartsArray<TPart>(key);
Expand All @@ -149,7 +149,7 @@ public virtual BaseCalendarPartInfo[] GetPartsArray(Type partType)
throw new InvalidOperationException($"Base type is not BaseCalendarPartInfo [{partType.BaseType.Name}] and the part type is [{partType.Name}] that doesn't represent calendar part.");

// Get the parts enumeration according to the type
var key = VCalendarParserTools.GetPartsArrayEnumFromType(partType, VCalendarConstants._objectVCalendarSpecifier, CalendarVersion, GetType());
var key = VCalendarParserTools.GetPartsArrayEnumFromType(partType, CalendarVersion, GetType());

// Now, return the value
return GetPartsArray(partType, key);
Expand Down Expand Up @@ -178,7 +178,7 @@ public virtual BaseCalendarPartInfo[] GetPartsArray(Type partType, CalendarParts
public virtual BaseCalendarPartInfo[] GetPartsArray(CalendarPartsArrayEnum key)
{
string prefix = VCalendarParserTools.GetPrefixFromPartsArrayEnum(key);
var partType = VCalendarParserTools.GetPartType(prefix, "", CalendarVersion, typeof(Calendar));
var partType = VCalendarParserTools.GetPartType(prefix, CalendarVersion, typeof(Calendar));
if (partType.enumType is null)
throw new ArgumentException($"Enumeration type is not found for {key}");
return GetPartsArray(partType.enumType, key, CalendarVersion, partsArray);
Expand All @@ -188,7 +188,7 @@ internal TPart[] GetPartsArray<TPart>(Version version, Dictionary<CalendarPartsA
where TPart : BaseCalendarPartInfo
{
// Get the parts enumeration according to the type
var key = VCalendarParserTools.GetPartsArrayEnumFromType(typeof(TPart), VCalendarConstants._objectVCalendarSpecifier, version, GetType());
var key = VCalendarParserTools.GetPartsArrayEnumFromType(typeof(TPart), version, GetType());

// Now, return the value
return GetPartsArray<TPart>(key, version, partsArray);
Expand All @@ -201,7 +201,7 @@ internal TPart[] GetPartsArray<TPart>(CalendarPartsArrayEnum key, Version versio
internal BaseCalendarPartInfo[] GetPartsArray(Type partType, Version version, Dictionary<CalendarPartsArrayEnum, List<BaseCalendarPartInfo>> partsArray)
{
// Get the parts enumeration according to the type
var key = VCalendarParserTools.GetPartsArrayEnumFromType(typeof(BaseCalendarPartInfo), VCalendarConstants._objectVCalendarSpecifier, version, GetType());
var key = VCalendarParserTools.GetPartsArrayEnumFromType(typeof(BaseCalendarPartInfo), version, GetType());

// Now, return the value
return GetPartsArray(partType, key, version, partsArray);
Expand All @@ -211,7 +211,7 @@ internal BaseCalendarPartInfo[] GetPartsArray(Type partType, CalendarPartsArrayE
{
// Check for version support
string prefix = VCalendarParserTools.GetPrefixFromPartsArrayEnum(key);
var type = VCalendarParserTools.GetPartType(prefix, "", version, GetType());
var type = VCalendarParserTools.GetPartType(prefix, version, GetType());
if (!type.minimumVersionCondition(version))
return [];

Expand Down Expand Up @@ -252,7 +252,7 @@ internal CalendarValueInfo<string>[] GetString(CalendarStringsEnum key, Version
{
// Check for version support
string prefix = VCalendarParserTools.GetPrefixFromStringsEnum(key);
var partType = VCalendarParserTools.GetPartType(prefix, "", version, GetType());
var partType = VCalendarParserTools.GetPartType(prefix, version, GetType());
if (!partType.minimumVersionCondition(version))
return [];

Expand All @@ -277,7 +277,7 @@ internal CalendarValueInfo<double>[] GetInteger(CalendarIntegersEnum key, Versio
{
// Check for version support
string prefix = VCalendarParserTools.GetPrefixFromIntegersEnum(key);
var partType = VCalendarParserTools.GetPartType(prefix, "", version, GetType());
var partType = VCalendarParserTools.GetPartType(prefix, version, GetType());
if (!partType.minimumVersionCondition(version))
return [];

Expand Down Expand Up @@ -316,7 +316,7 @@ internal string SaveToString(Version version, Dictionary<CalendarPartsArrayEnum,

// Get the prefix
string prefix = VCalendarParserTools.GetPrefixFromStringsEnum(stringEnum);
var type = VCalendarParserTools.GetPartType(prefix, objectType, version, GetType());
var type = VCalendarParserTools.GetPartType(prefix, version, GetType());
string defaultType = type.defaultType;
string defaultValueType = type.defaultValueType;

Expand Down Expand Up @@ -346,7 +346,7 @@ internal string SaveToString(Version version, Dictionary<CalendarPartsArrayEnum,

// Get the prefix
string prefix = VCalendarParserTools.GetPrefixFromIntegersEnum(integerEnum);
var type = VCalendarParserTools.GetPartType(prefix, objectType, version, GetType());
var type = VCalendarParserTools.GetPartType(prefix, version, GetType());
string defaultType = type.defaultType;
string defaultValueType = type.defaultValueType;

Expand Down Expand Up @@ -376,7 +376,7 @@ internal string SaveToString(Version version, Dictionary<CalendarPartsArrayEnum,

// Get the prefix
string prefix = VCalendarParserTools.GetPrefixFromPartsArrayEnum(partsArrayEnum);
var type = VCalendarParserTools.GetPartType(prefix, objectType, version, GetType());
var type = VCalendarParserTools.GetPartType(prefix, version, GetType());
string defaultType = type.defaultType;
string defaultValueType = type.defaultValueType;

Expand Down Expand Up @@ -516,16 +516,16 @@ public override int GetHashCode()
=> !a.Equals(b);

internal virtual void AddPartToArray(CalendarPartsArrayEnum key, BaseCalendarPartInfo value) =>
AddPartToArray(key, value, version, partsArray, VCalendarConstants._objectVCalendarSpecifier);
AddPartToArray(key, value, version, partsArray);

internal virtual void AddPartToArray(CalendarPartsArrayEnum key, BaseCalendarPartInfo value, Version version, Dictionary<CalendarPartsArrayEnum, List<BaseCalendarPartInfo>> partsArray, string objectType)
internal virtual void AddPartToArray(CalendarPartsArrayEnum key, BaseCalendarPartInfo value, Version version, Dictionary<CalendarPartsArrayEnum, List<BaseCalendarPartInfo>> partsArray)
{
if (value is null)
return;

// Get the appropriate type and check it
string prefix = VCalendarParserTools.GetPrefixFromPartsArrayEnum(key);
var partType = VCalendarParserTools.GetPartType(prefix, objectType, version, GetType());
var partType = VCalendarParserTools.GetPartType(prefix, version, GetType());
var enumType = partType.enumType;
if (value.GetType() != enumType)
return;
Expand All @@ -547,16 +547,16 @@ internal virtual void AddPartToArray(CalendarPartsArrayEnum key, BaseCalendarPar
}

internal virtual void AddString(CalendarStringsEnum key, CalendarValueInfo<string> value) =>
AddString(key, value, version, strings, VCalendarConstants._objectVCalendarSpecifier);
AddString(key, value, version, strings);

internal virtual void AddString(CalendarStringsEnum key, CalendarValueInfo<string> value, Version version, Dictionary<CalendarStringsEnum, List<CalendarValueInfo<string>>> strings, string objectType)
internal virtual void AddString(CalendarStringsEnum key, CalendarValueInfo<string> value, Version version, Dictionary<CalendarStringsEnum, List<CalendarValueInfo<string>>> strings)
{
if (value is null || string.IsNullOrEmpty(value.Value))
return;

// Get the appropriate type
string prefix = VCalendarParserTools.GetPrefixFromStringsEnum(key);
var partType = VCalendarParserTools.GetPartType(prefix, objectType, version, GetType());
var partType = VCalendarParserTools.GetPartType(prefix, version, GetType());

// If we don't have this key yet, add it.
if (!strings.ContainsKey(key))
Expand All @@ -575,16 +575,16 @@ internal virtual void AddString(CalendarStringsEnum key, CalendarValueInfo<strin
}

internal virtual void AddInteger(CalendarIntegersEnum key, CalendarValueInfo<double> value) =>
AddInteger(key, value, version, integers, VCalendarConstants._objectVCalendarSpecifier);
AddInteger(key, value, version, integers);

internal virtual void AddInteger(CalendarIntegersEnum key, CalendarValueInfo<double> value, Version version, Dictionary<CalendarIntegersEnum, List<CalendarValueInfo<double>>> integers, string objectType)
internal virtual void AddInteger(CalendarIntegersEnum key, CalendarValueInfo<double> value, Version version, Dictionary<CalendarIntegersEnum, List<CalendarValueInfo<double>>> integers)
{
if (value is null || value.Value < 0)
return;

// Get the appropriate type
string prefix = VCalendarParserTools.GetPrefixFromIntegersEnum(key);
var partType = VCalendarParserTools.GetPartType(prefix, objectType, version, GetType());
var partType = VCalendarParserTools.GetPartType(prefix, version, GetType());

// If we don't have this key yet, add it.
if (!integers.ContainsKey(key))
Expand Down
10 changes: 5 additions & 5 deletions VisualCard.Calendar/Parts/CalendarAlarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class CalendarAlarm : Calendar, IEquatable<CalendarAlarm>
public override TPart[] GetPartsArray<TPart>()
{
// Get the parts enumeration according to the type
var key = VCalendarParserTools.GetPartsArrayEnumFromType(typeof(TPart), VCalendarConstants._objectVAlarmSpecifier, CalendarVersion, GetType());
var key = VCalendarParserTools.GetPartsArrayEnumFromType(typeof(TPart), CalendarVersion, GetType());

// Now, return the value
return GetPartsArray<TPart>(key, CalendarVersion, partsArray);
Expand All @@ -85,7 +85,7 @@ public override TPart[] GetPartsArray<TPart>(CalendarPartsArrayEnum key) =>
public override BaseCalendarPartInfo[] GetPartsArray(CalendarPartsArrayEnum key)
{
string prefix = VCalendarParserTools.GetPrefixFromPartsArrayEnum(key);
var partType = VCalendarParserTools.GetPartType(prefix, "", CalendarVersion, typeof(CalendarAlarm));
var partType = VCalendarParserTools.GetPartType(prefix, CalendarVersion, typeof(CalendarAlarm));
if (partType.enumType is null)
throw new ArgumentException($"Enumeration type is not found for {key}");
return GetPartsArray(partType.enumType, key, CalendarVersion, partsArray);
Expand Down Expand Up @@ -171,13 +171,13 @@ public override int GetHashCode()
=> !a.Equals(b);

internal override void AddPartToArray(CalendarPartsArrayEnum key, BaseCalendarPartInfo value) =>
AddPartToArray(key, value, CalendarVersion, partsArray, VCalendarConstants._objectVAlarmSpecifier);
AddPartToArray(key, value, CalendarVersion, partsArray);

internal override void AddString(CalendarStringsEnum key, CalendarValueInfo<string> value) =>
AddString(key, value, CalendarVersion, strings, VCalendarConstants._objectVAlarmSpecifier);
AddString(key, value, CalendarVersion, strings);

internal override void AddInteger(CalendarIntegersEnum key, CalendarValueInfo<double> value) =>
AddInteger(key, value, CalendarVersion, integers, VCalendarConstants._objectVAlarmSpecifier);
AddInteger(key, value, CalendarVersion, integers);

internal CalendarAlarm(Version version) :
base(version)
Expand Down
Loading

0 comments on commit ffb6962

Please sign in to comment.