Skip to content

Commit

Permalink
ref - Allowed individual field checking
Browse files Browse the repository at this point in the history
---

We've refactored the component validation code so that it calls a function that focuses on just one component.

---

Type: ref
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Sep 11, 2024
1 parent a95cd08 commit c4e7a5b
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions VisualCard.Calendar/Parsers/VCalendarParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,43 +399,47 @@ private bool ValidateComponent<TComponent>(ref string[] expectedFields, out stri
// Requirement checks
foreach (string expectedFieldName in expectedFields)
{
var (type, enumeration, enumType, _, _, _, _) = VCalendarParserTools.GetPartType(expectedFieldName);
switch (type)
{
case PartType.Strings:
{
string value = component.GetString((CalendarStringsEnum)enumeration);
bool exists = !string.IsNullOrEmpty(value);
if (exists)
actualFieldList.Add(expectedFieldName);
}
break;
case PartType.PartsArray:
{
if (enumType is null)
continue;
var values = component.GetPartsArray(enumType, (CalendarPartsArrayEnum)enumeration);
bool exists = values.Length > 0;
if (exists)
actualFieldList.Add(expectedFieldName);
}
break;
case PartType.Integers:
{
int value = component.GetInteger((CalendarIntegersEnum)enumeration);
bool exists = value != -1;
if (exists)
actualFieldList.Add(expectedFieldName);
}
break;
}
if (HasComponent(expectedFieldName, component))
actualFieldList.Add(expectedFieldName);
}
Array.Sort(expectedFields);
actualFieldList.Sort();
actualFields = [.. actualFieldList];
return actualFields.SequenceEqual(expectedFields);
}

private bool HasComponent<TComponent>(string expectedFieldName, TComponent component)
where TComponent : Parts.Calendar
{
// Requirement checks
var (type, enumeration, enumType, _, _, _, _) = VCalendarParserTools.GetPartType(expectedFieldName);
bool exists = false;
switch (type)
{
case PartType.Strings:
{
string value = component.GetString((CalendarStringsEnum)enumeration);
exists = !string.IsNullOrEmpty(value);
}
break;
case PartType.PartsArray:
{
if (enumType is null)
return false;
var values = component.GetPartsArray(enumType, (CalendarPartsArrayEnum)enumeration);
exists = values.Length > 0;
}
break;
case PartType.Integers:
{
int value = component.GetInteger((CalendarIntegersEnum)enumeration);
exists = value != -1;
}
break;
}
return exists;
}

private void ValidateAlarm(CalendarAlarm alarmInfo)
{
string[] expectedAlarmFields = [VCalendarConstants._actionSpecifier, VCalendarConstants._triggerSpecifier];
Expand Down

0 comments on commit c4e7a5b

Please sign in to comment.