From c089b0e48744a5e1e4e6388e3235fc40fdbc2550 Mon Sep 17 00:00:00 2001 From: Aptivi CEO Date: Tue, 1 Oct 2024 15:04:16 +0300 Subject: [PATCH] imp - Improved support for integers --- Integers are now more compatible, because there can be floating point values. --- Type: imp Breaking: False Doc Required: False Backport Required: False Part: 1/1 --- .../Parsers/VCalendarParser.cs | 14 +- .../Parsers/VCalendarParserTools.cs | 198 +++++------------- VisualCard.Calendar/Parts/Calendar.cs | 18 +- VisualCard.Calendar/Parts/CalendarAlarm.cs | 6 +- VisualCard.Calendar/Parts/CalendarDaylight.cs | 6 +- VisualCard.Calendar/Parts/CalendarEvent.cs | 6 +- VisualCard.Calendar/Parts/CalendarFreeBusy.cs | 6 +- VisualCard.Calendar/Parts/CalendarJournal.cs | 6 +- VisualCard.Calendar/Parts/CalendarStandard.cs | 6 +- VisualCard.Calendar/Parts/CalendarTimeZone.cs | 6 +- VisualCard.Calendar/Parts/CalendarTodo.cs | 6 +- .../Parts/Comparers/PartComparison.cs | 6 +- VisualCard/Parsers/VcardParserTools.cs | 23 +- 13 files changed, 108 insertions(+), 199 deletions(-) diff --git a/VisualCard.Calendar/Parsers/VCalendarParser.cs b/VisualCard.Calendar/Parsers/VCalendarParser.cs index e1b96051..70119ab2 100644 --- a/VisualCard.Calendar/Parsers/VCalendarParser.cs +++ b/VisualCard.Calendar/Parsers/VCalendarParser.cs @@ -142,7 +142,7 @@ public Parts.Calendar Parse() // Get the part type bool xNonstandard = prefix.StartsWith(VCalendarConstants._xSpecifier); bool specifierRequired = CalendarVersion.Major >= 3; - var (type, enumeration, classType, fromString, defaultType, defaultValue, extraAllowedTypes) = VCalendarParserTools.GetPartType(xNonstandard ? VCalendarConstants._xSpecifier : prefix); + var (type, enumeration, classType, fromString, defaultType, defaultValue, defaultValueType, extraAllowedTypes) = VCalendarParserTools.GetPartType(xNonstandard ? VCalendarConstants._xSpecifier : prefix); // Handle arguments if (isWithType) @@ -168,7 +168,7 @@ public Parts.Calendar Parse() // Handle the part type Type calendarType = subPart is not null ? subPart.GetType() : calendar.GetType(); - string values = VcardCommonTools.GetValuesString(splitArgs, defaultValue, VCalendarConstants._valueArgumentSpecifier); + string valueType = VcardCommonTools.GetFirstValue(splitArgs, defaultValue, VCalendarConstants._valueArgumentSpecifier); switch (type) { case PartType.Strings: @@ -179,7 +179,7 @@ public Parts.Calendar Parse() continue; // Get the final value - string finalValue = VCalendarParserTools.ProcessStringValue(value, values, stringType, calendarType, CalendarVersion); + string finalValue = VcardParserTools.ProcessStringValue(value, valueType); // Set the string for real if (subPart is not null) @@ -196,7 +196,7 @@ public Parts.Calendar Parse() continue; // Get the final value - int finalValue = VCalendarParserTools.ProcessIntegerValue(value, integerType); + double finalValue = VcardParserTools.ProcessIntegerValue(value, valueType); // Set the integer for real if (subPart is not null) @@ -217,7 +217,7 @@ public Parts.Calendar Parse() // Now, get the part info string finalValue = partsArrayType is CalendarPartsArrayEnum.IanaNames or CalendarPartsArrayEnum.NonstandardNames ? _value : value; - var partInfo = fromString(finalValue, [.. finalArgs], elementTypes, values, CalendarVersion); + var partInfo = fromString(finalValue, [.. finalArgs], elementTypes, valueType, CalendarVersion); // Set the array for real if (subPart is not null) @@ -326,7 +326,7 @@ private bool HasComponent(string expectedFieldName, TComponent compo where TComponent : Parts.Calendar { // Requirement checks - var (type, enumeration, enumType, _, _, _, _) = VCalendarParserTools.GetPartType(expectedFieldName); + var (type, enumeration, enumType, _, _, _, _, _) = VCalendarParserTools.GetPartType(expectedFieldName); bool exists = false; switch (type) { @@ -346,7 +346,7 @@ private bool HasComponent(string expectedFieldName, TComponent compo break; case PartType.Integers: { - int value = component.GetInteger((CalendarIntegersEnum)enumeration); + double value = component.GetInteger((CalendarIntegersEnum)enumeration); exists = value != -1; } break; diff --git a/VisualCard.Calendar/Parsers/VCalendarParserTools.cs b/VisualCard.Calendar/Parsers/VCalendarParserTools.cs index 57badb44..db610fec 100644 --- a/VisualCard.Calendar/Parsers/VCalendarParserTools.cs +++ b/VisualCard.Calendar/Parsers/VCalendarParserTools.cs @@ -227,162 +227,60 @@ internal static (CalendarPartsArrayEnum, PartCardinality) GetPartsArrayEnumFromT return (CalendarPartsArrayEnum.IanaNames, PartCardinality.Any); } - internal static (PartType type, object enumeration, Type? enumType, Func? fromStringFunc, string defaultType, string defaultValue, string[] allowedExtraTypes) GetPartType(string prefix) => + internal static (PartType type, object enumeration, Type? enumType, Func? fromStringFunc, string defaultType, string defaultValue, string defaultValueType, string[] allowedExtraTypes) GetPartType(string prefix) => prefix switch { - VCalendarConstants._attachSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Attach, typeof(AttachInfo), AttachInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._categoriesSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Categories, typeof(CategoriesInfo), CategoriesInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._commentSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Comment, typeof(CommentInfo), CommentInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._geoSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Geography, typeof(GeoInfo), GeoInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._locationSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Location, typeof(LocationInfo), LocationInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._resourcesSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Resources, typeof(ResourcesInfo), ResourcesInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._attendeeSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Attendee, typeof(AttendeeInfo), AttendeeInfo.FromStringVcalendarStatic, "", "", ["VCARD"]), - VCalendarConstants._createdSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DateCreated, typeof(DateCreatedInfo), DateCreatedInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._created1Specifier => (PartType.PartsArray, CalendarPartsArrayEnum.DateCreatedAlt, typeof(DateCreatedInfo), DateCreatedInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._dateStartSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DateStart, typeof(DateStartInfo), DateStartInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._dateEndSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DateEnd, typeof(DateEndInfo), DateEndInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._dateCompletedSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DateCompleted, typeof(DateCompletedInfo), DateCompletedInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._dueDateSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DueDate, typeof(DueDateInfo), DueDateInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._dateStampSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DateStamp, typeof(DateStampInfo), DateStampInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._tzNameSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.TimeZoneName, typeof(TimeZoneNameInfo), TimeZoneNameInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._tzOffsetFromSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.TimeZoneOffsetFrom, typeof(TimeZoneOffsetFromInfo), TimeZoneOffsetFromInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._tzOffsetToSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.TimeZoneOffsetTo, typeof(TimeZoneOffsetToInfo), TimeZoneOffsetToInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._recDateSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.RecDate, typeof(RecDateInfo), RecDateInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._exDateSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.ExDate, typeof(ExDateInfo), ExDateInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._daylightSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Daylight, typeof(DaylightInfo), DaylightInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._aAlarmSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.AudioAlarm, typeof(AudioAlarmInfo), AudioAlarmInfo.FromStringVcalendarStatic, "PCM", "", ["PCM", "WAVE", "AIFF"]), - VCalendarConstants._dAlarmSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DisplayAlarm, typeof(DisplayAlarmInfo), DisplayAlarmInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._mAlarmSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.MailAlarm, typeof(MailAlarmInfo), MailAlarmInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._pAlarmSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.ProcedureAlarm, typeof(ProcedureAlarmInfo), ProcedureAlarmInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._relationshipSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.RelatedTo, typeof(RelatedToInfo), RelatedToInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._lastModSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.LastModified, typeof(LastModifiedInfo), LastModifiedInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._freeBusySpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.FreeBusy, typeof(CalendarFreeBusyInfo), CalendarFreeBusyInfo.FromStringVcalendarStatic, "", "", []), - VCalendarConstants._productIdSpecifier => (PartType.Strings, CalendarStringsEnum.ProductId, null, null, "", "", []), - VCalendarConstants._calScaleSpecifier => (PartType.Strings, CalendarStringsEnum.CalScale, null, null, "", "", []), - VCalendarConstants._methodSpecifier => (PartType.Strings, CalendarStringsEnum.Method, null, null, "", "", []), - VCalendarConstants._classSpecifier => (PartType.Strings, CalendarStringsEnum.Class, null, null, "", "", []), - VCalendarConstants._uidSpecifier => (PartType.Strings, CalendarStringsEnum.Uid, null, null, "", "", []), - VCalendarConstants._organizerSpecifier => (PartType.Strings, CalendarStringsEnum.Organizer, null, null, "", "", []), - VCalendarConstants._statusSpecifier => (PartType.Strings, CalendarStringsEnum.Status, null, null, "", "", []), - VCalendarConstants._summarySpecifier => (PartType.Strings, CalendarStringsEnum.Summary, null, null, "", "", []), - VCalendarConstants._descriptionSpecifier => (PartType.Strings, CalendarStringsEnum.Description, null, null, "", "", []), - VCalendarConstants._transparencySpecifier => (PartType.Strings, CalendarStringsEnum.Transparency, null, null, "", "", []), - VCalendarConstants._actionSpecifier => (PartType.Strings, CalendarStringsEnum.Action, null, null, "", "", []), - VCalendarConstants._triggerSpecifier => (PartType.Strings, CalendarStringsEnum.Trigger, null, null, "", "", []), - VCalendarConstants._tzidSpecifier => (PartType.Strings, CalendarStringsEnum.TimeZoneId, null, null, "", "", []), - VCalendarConstants._tzUrlSpecifier => (PartType.Strings, CalendarStringsEnum.TimeZoneUrl, null, null, "", "", []), - VCalendarConstants._recurseSpecifier => (PartType.Strings, CalendarStringsEnum.Recursion, null, null, "", "", []), - VCalendarConstants._prioritySpecifier => (PartType.Integers, CalendarIntegersEnum.Priority, null, null, "", "", []), - VCalendarConstants._sequenceSpecifier => (PartType.Integers, CalendarIntegersEnum.Sequence, null, null, "", "", []), - VCalendarConstants._percentCompletionSpecifier => (PartType.Integers, CalendarIntegersEnum.PercentComplete, null, null, "", "", []), + VCalendarConstants._attachSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Attach, typeof(AttachInfo), AttachInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._categoriesSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Categories, typeof(CategoriesInfo), CategoriesInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._commentSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Comment, typeof(CommentInfo), CommentInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._geoSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Geography, typeof(GeoInfo), GeoInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._locationSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Location, typeof(LocationInfo), LocationInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._resourcesSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Resources, typeof(ResourcesInfo), ResourcesInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._attendeeSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Attendee, typeof(AttendeeInfo), AttendeeInfo.FromStringVcalendarStatic, "", "", "", ["VCARD"]), + VCalendarConstants._createdSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DateCreated, typeof(DateCreatedInfo), DateCreatedInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._created1Specifier => (PartType.PartsArray, CalendarPartsArrayEnum.DateCreatedAlt, typeof(DateCreatedInfo), DateCreatedInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._dateStartSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DateStart, typeof(DateStartInfo), DateStartInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._dateEndSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DateEnd, typeof(DateEndInfo), DateEndInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._dateCompletedSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DateCompleted, typeof(DateCompletedInfo), DateCompletedInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._dueDateSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DueDate, typeof(DueDateInfo), DueDateInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._dateStampSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DateStamp, typeof(DateStampInfo), DateStampInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._tzNameSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.TimeZoneName, typeof(TimeZoneNameInfo), TimeZoneNameInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._tzOffsetFromSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.TimeZoneOffsetFrom, typeof(TimeZoneOffsetFromInfo), TimeZoneOffsetFromInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._tzOffsetToSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.TimeZoneOffsetTo, typeof(TimeZoneOffsetToInfo), TimeZoneOffsetToInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._recDateSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.RecDate, typeof(RecDateInfo), RecDateInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._exDateSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.ExDate, typeof(ExDateInfo), ExDateInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._daylightSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Daylight, typeof(DaylightInfo), DaylightInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._aAlarmSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.AudioAlarm, typeof(AudioAlarmInfo), AudioAlarmInfo.FromStringVcalendarStatic, "PCM", "", "", ["PCM", "WAVE", "AIFF"]), + VCalendarConstants._dAlarmSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.DisplayAlarm, typeof(DisplayAlarmInfo), DisplayAlarmInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._mAlarmSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.MailAlarm, typeof(MailAlarmInfo), MailAlarmInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._pAlarmSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.ProcedureAlarm, typeof(ProcedureAlarmInfo), ProcedureAlarmInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._relationshipSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.RelatedTo, typeof(RelatedToInfo), RelatedToInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._lastModSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.LastModified, typeof(LastModifiedInfo), LastModifiedInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._freeBusySpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.FreeBusy, typeof(CalendarFreeBusyInfo), CalendarFreeBusyInfo.FromStringVcalendarStatic, "", "", "", []), + VCalendarConstants._productIdSpecifier => (PartType.Strings, CalendarStringsEnum.ProductId, null, null, "", "", "", []), + VCalendarConstants._calScaleSpecifier => (PartType.Strings, CalendarStringsEnum.CalScale, null, null, "", "", "", []), + VCalendarConstants._methodSpecifier => (PartType.Strings, CalendarStringsEnum.Method, null, null, "", "", "", []), + VCalendarConstants._classSpecifier => (PartType.Strings, CalendarStringsEnum.Class, null, null, "", "", "", []), + VCalendarConstants._uidSpecifier => (PartType.Strings, CalendarStringsEnum.Uid, null, null, "", "", "", []), + VCalendarConstants._organizerSpecifier => (PartType.Strings, CalendarStringsEnum.Organizer, null, null, "", "", "", []), + VCalendarConstants._statusSpecifier => (PartType.Strings, CalendarStringsEnum.Status, null, null, "", "", "", []), + VCalendarConstants._summarySpecifier => (PartType.Strings, CalendarStringsEnum.Summary, null, null, "", "", "", []), + VCalendarConstants._descriptionSpecifier => (PartType.Strings, CalendarStringsEnum.Description, null, null, "", "", "", []), + VCalendarConstants._transparencySpecifier => (PartType.Strings, CalendarStringsEnum.Transparency, null, null, "", "", "", []), + VCalendarConstants._actionSpecifier => (PartType.Strings, CalendarStringsEnum.Action, null, null, "", "", "", []), + VCalendarConstants._triggerSpecifier => (PartType.Strings, CalendarStringsEnum.Trigger, null, null, "", "", "", []), + VCalendarConstants._tzidSpecifier => (PartType.Strings, CalendarStringsEnum.TimeZoneId, null, null, "", "", "", []), + VCalendarConstants._tzUrlSpecifier => (PartType.Strings, CalendarStringsEnum.TimeZoneUrl, null, null, "", "", "", []), + VCalendarConstants._recurseSpecifier => (PartType.Strings, CalendarStringsEnum.Recursion, null, null, "", "", "", []), + VCalendarConstants._prioritySpecifier => (PartType.Integers, CalendarIntegersEnum.Priority, null, null, "", "", "", []), + VCalendarConstants._sequenceSpecifier => (PartType.Integers, CalendarIntegersEnum.Sequence, null, null, "", "", "", []), + VCalendarConstants._percentCompletionSpecifier => (PartType.Integers, CalendarIntegersEnum.PercentComplete, null, null, "", "", "", []), // Extensions are allowed - VCalendarConstants._xSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.NonstandardNames, typeof(XNameInfo), XNameInfo.FromStringVcalendarStatic, "", "", []), - _ => (PartType.PartsArray, CalendarPartsArrayEnum.IanaNames, typeof(ExtraInfo), ExtraInfo.FromStringVcalendarStatic, "", "", []), + VCalendarConstants._xSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.NonstandardNames, typeof(XNameInfo), XNameInfo.FromStringVcalendarStatic, "", "", "", []), + _ => (PartType.PartsArray, CalendarPartsArrayEnum.IanaNames, typeof(ExtraInfo), ExtraInfo.FromStringVcalendarStatic, "", "", "", []), }; - internal static string ProcessStringValue(string value, string values, CalendarStringsEnum stringType, Type calendarType, Version calendarVersion) - { - string[] allowedStatuses = - calendarType == typeof(CalendarEvent) && calendarVersion.Major == 2 ? ["TENTATIVE", "CONFIRMED", "CANCELLED"] : - calendarType == typeof(CalendarEvent) && calendarVersion.Major == 1 ? ["NEEDS ACTION", "SENT", "TENTATIVE", "CONFIRMED", "DECLINED", "DELEGATED"] : - calendarType == typeof(CalendarTodo) && calendarVersion.Major == 2 ? ["NEEDS-ACTION", "COMPLETED", "IN-PROCESS", "CANCELLED"] : - calendarType == typeof(CalendarTodo) && calendarVersion.Major == 1 ? ["NEEDS ACTION", "SENT", "ACCEPTED", "COMPLETED", "DECLINED", "DELEGATED"] : - calendarType == typeof(CalendarJournal) ? ["DRAFT", "FINAL", "CANCELLED"] : - []; - - // Now, handle each type individually - string finalValue; - switch (stringType) - { - case CalendarStringsEnum.ProductId: - case CalendarStringsEnum.Organizer: - case CalendarStringsEnum.Summary: - case CalendarStringsEnum.Description: - case CalendarStringsEnum.CalScale: - case CalendarStringsEnum.Method: - case CalendarStringsEnum.Class: - case CalendarStringsEnum.Trigger: - case CalendarStringsEnum.TimeZoneId: - case CalendarStringsEnum.Recursion: - // Unescape the value - finalValue = Regex.Unescape(value); - break; - case CalendarStringsEnum.Action: - // Unescape the value - finalValue = Regex.Unescape(value); - if (finalValue != "AUDIO" && finalValue != "DISPLAY" && finalValue != "EMAIL") - throw new ArgumentException($"Invalid action {finalValue}"); - break; - case CalendarStringsEnum.Status: - // Unescape the value - finalValue = Regex.Unescape(value); - if (!allowedStatuses.Contains(finalValue)) - throw new ArgumentException($"Invalid status {finalValue}"); - break; - case CalendarStringsEnum.Transparency: - // Unescape the value - finalValue = Regex.Unescape(value); - if (calendarVersion.Major == 1 && !int.TryParse(finalValue, out _)) - throw new ArgumentException($"Invalid transparency number {finalValue}"); - else if (calendarVersion.Major == 2 && finalValue != "TRANSPARENT" && finalValue != "OPAQUE") - throw new ArgumentException($"Invalid transparency {finalValue}"); - break; - case CalendarStringsEnum.Uid: - // Unescape the value - finalValue = Regex.Unescape(value); - if (!Uri.TryCreate(finalValue, UriKind.Absolute, out Uri uri) && values.Equals("uri", StringComparison.OrdinalIgnoreCase)) - throw new InvalidDataException($"URL {finalValue} is invalid"); - finalValue = uri is not null ? uri.ToString() : finalValue; - break; - case CalendarStringsEnum.TimeZoneUrl: - // Unescape the value - finalValue = Regex.Unescape(value); - if (!Uri.TryCreate(finalValue, UriKind.Absolute, out Uri zoneUri)) - throw new InvalidDataException($"Time zone URL {finalValue} is invalid"); - finalValue = zoneUri.ToString(); - break; - default: - throw new InvalidDataException($"The string enum type {stringType} is invalid. Are you sure that you've specified the correct type in your vCalendar representation?"); - } - - // Return the result - return finalValue; - } - - internal static int ProcessIntegerValue(string value, CalendarIntegersEnum integerType) - { - // Now, handle each type individually - int primaryValue = int.Parse(value); - int finalValue; - switch (integerType) - { - case CalendarIntegersEnum.Priority: - // Check the range - if (primaryValue < 0 || primaryValue > 9) - throw new ArgumentOutOfRangeException(nameof(primaryValue), primaryValue, "Priority may not be less than zero or greater than 9"); - finalValue = primaryValue; - break; - case CalendarIntegersEnum.Sequence: - // Check the range - if (primaryValue < 0) - throw new ArgumentOutOfRangeException(nameof(primaryValue), primaryValue, "Sequence may not be less than zero"); - finalValue = primaryValue; - break; - case CalendarIntegersEnum.PercentComplete: - // Check the range - if (primaryValue < 0 || primaryValue > 100) - throw new ArgumentOutOfRangeException(nameof(primaryValue), primaryValue, "Percent completion may not be less than zero or greater than 100"); - finalValue = primaryValue; - break; - default: - throw new InvalidDataException($"The integer enum type {integerType} is invalid. Are you sure that you've specified the correct type in your vCalendar representation?"); - } - - // Return the result - return finalValue; - } - private static bool TypeMatch(Type componentType, params Type[] expectedTypes) => expectedTypes.Contains(componentType); } diff --git a/VisualCard.Calendar/Parts/Calendar.cs b/VisualCard.Calendar/Parts/Calendar.cs index 0ce5c05f..d2a25256 100644 --- a/VisualCard.Calendar/Parts/Calendar.cs +++ b/VisualCard.Calendar/Parts/Calendar.cs @@ -46,7 +46,7 @@ public class Calendar : IEquatable private readonly Version version; private readonly Dictionary> partsArray = []; private readonly Dictionary strings = []; - private readonly Dictionary integers = []; + private readonly Dictionary integers = []; /// /// The vCalendar version @@ -230,10 +230,10 @@ internal string GetString(CalendarStringsEnum key, Version version, Dictionary /// A key to use /// A value or -1 if any other type either doesn't exist or the type is not supported by the card version - public int GetInteger(CalendarIntegersEnum key) => + public double GetInteger(CalendarIntegersEnum key) => GetInteger(key, integers); - internal int GetInteger(CalendarIntegersEnum key, Dictionary integers) + internal double GetInteger(CalendarIntegersEnum key, Dictionary integers) { // Check for version support if (!VCalendarParserTools.IntegerSupported(key, GetType())) @@ -243,7 +243,7 @@ internal int GetInteger(CalendarIntegersEnum key, Dictionary SaveToString(version, partsArray, strings, integers, VCalendarConstants._objectVCalendarSpecifier); - internal string SaveToString(Version version, Dictionary> partsArray, Dictionary strings, Dictionary integers, string objectType) + internal string SaveToString(Version version, Dictionary> partsArray, Dictionary strings, Dictionary integers, string objectType) { // Initialize the card builder var cardBuilder = new StringBuilder(); @@ -285,7 +285,7 @@ internal string SaveToString(Version version, Dictionary>.Default.GetHashCode(timeZones); hashCode = hashCode * -1521134295 + EqualityComparer>>.Default.GetHashCode(partsArray); hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(strings); - hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); + hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); return hashCode; } @@ -482,10 +482,10 @@ internal void SetString(CalendarStringsEnum key, string value, Dictionary + internal void SetInteger(CalendarIntegersEnum key, double value) => SetInteger(key, value, integers); - internal void SetInteger(CalendarIntegersEnum key, int value, Dictionary integers) + internal void SetInteger(CalendarIntegersEnum key, double value, Dictionary integers) { if (value == -1) return; diff --git a/VisualCard.Calendar/Parts/CalendarAlarm.cs b/VisualCard.Calendar/Parts/CalendarAlarm.cs index 5815d880..5b5a8db2 100644 --- a/VisualCard.Calendar/Parts/CalendarAlarm.cs +++ b/VisualCard.Calendar/Parts/CalendarAlarm.cs @@ -34,7 +34,7 @@ public class CalendarAlarm : Calendar, IEquatable { private readonly Dictionary> partsArray = []; private readonly Dictionary strings = []; - private readonly Dictionary integers = []; + private readonly Dictionary integers = []; /// /// Gets a part array from a specified key @@ -116,7 +116,7 @@ public override int GetHashCode() hashCode = hashCode * -1521134295 + base.GetHashCode(); hashCode = hashCode * -1521134295 + EqualityComparer>>.Default.GetHashCode(partsArray); hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(strings); - hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); + hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); return hashCode; } @@ -134,7 +134,7 @@ public override int GetHashCode() internal new void SetString(CalendarStringsEnum key, string value) => SetString(key, value, strings); - internal new void SetInteger(CalendarIntegersEnum key, int value) => + internal new void SetInteger(CalendarIntegersEnum key, double value) => SetInteger(key, value, integers); internal CalendarAlarm(Version version) : diff --git a/VisualCard.Calendar/Parts/CalendarDaylight.cs b/VisualCard.Calendar/Parts/CalendarDaylight.cs index 07d035c6..37546cdb 100644 --- a/VisualCard.Calendar/Parts/CalendarDaylight.cs +++ b/VisualCard.Calendar/Parts/CalendarDaylight.cs @@ -34,7 +34,7 @@ public class CalendarDaylight : Calendar, IEquatable { private readonly Dictionary> partsArray = []; private readonly Dictionary strings = []; - private readonly Dictionary integers = []; + private readonly Dictionary integers = []; /// /// Gets a part array from a specified key @@ -116,7 +116,7 @@ public override int GetHashCode() hashCode = hashCode * -1521134295 + base.GetHashCode(); hashCode = hashCode * -1521134295 + EqualityComparer>>.Default.GetHashCode(partsArray); hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(strings); - hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); + hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); return hashCode; } @@ -134,7 +134,7 @@ public override int GetHashCode() internal new void SetString(CalendarStringsEnum key, string value) => SetString(key, value, strings); - internal new void SetInteger(CalendarIntegersEnum key, int value) => + internal new void SetInteger(CalendarIntegersEnum key, double value) => SetInteger(key, value, integers); internal CalendarDaylight(Version version) : diff --git a/VisualCard.Calendar/Parts/CalendarEvent.cs b/VisualCard.Calendar/Parts/CalendarEvent.cs index 27f0fa78..a28170b1 100644 --- a/VisualCard.Calendar/Parts/CalendarEvent.cs +++ b/VisualCard.Calendar/Parts/CalendarEvent.cs @@ -35,7 +35,7 @@ public class CalendarEvent : Calendar, IEquatable internal readonly List alarms = []; private readonly Dictionary> partsArray = []; private readonly Dictionary strings = []; - private readonly Dictionary integers = []; + private readonly Dictionary integers = []; /// /// Alarm list @@ -125,7 +125,7 @@ public override int GetHashCode() hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(alarms); hashCode = hashCode * -1521134295 + EqualityComparer>>.Default.GetHashCode(partsArray); hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(strings); - hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); + hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); return hashCode; } @@ -143,7 +143,7 @@ public override int GetHashCode() internal new void SetString(CalendarStringsEnum key, string value) => SetString(key, value, strings); - internal new void SetInteger(CalendarIntegersEnum key, int value) => + internal new void SetInteger(CalendarIntegersEnum key, double value) => SetInteger(key, value, integers); internal CalendarEvent(Version version) : diff --git a/VisualCard.Calendar/Parts/CalendarFreeBusy.cs b/VisualCard.Calendar/Parts/CalendarFreeBusy.cs index 42b2bf0f..caba7024 100644 --- a/VisualCard.Calendar/Parts/CalendarFreeBusy.cs +++ b/VisualCard.Calendar/Parts/CalendarFreeBusy.cs @@ -34,7 +34,7 @@ public class CalendarFreeBusy : Calendar, IEquatable { private readonly Dictionary> partsArray = []; private readonly Dictionary strings = []; - private readonly Dictionary integers = []; + private readonly Dictionary integers = []; /// /// Gets a part array from a specified key @@ -116,7 +116,7 @@ public override int GetHashCode() hashCode = hashCode * -1521134295 + base.GetHashCode(); hashCode = hashCode * -1521134295 + EqualityComparer>>.Default.GetHashCode(partsArray); hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(strings); - hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); + hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); return hashCode; } @@ -134,7 +134,7 @@ public override int GetHashCode() internal new void SetString(CalendarStringsEnum key, string value) => SetString(key, value, strings); - internal new void SetInteger(CalendarIntegersEnum key, int value) => + internal new void SetInteger(CalendarIntegersEnum key, double value) => SetInteger(key, value, integers); internal CalendarFreeBusy(Version version) : diff --git a/VisualCard.Calendar/Parts/CalendarJournal.cs b/VisualCard.Calendar/Parts/CalendarJournal.cs index 72150e1b..d2427e3b 100644 --- a/VisualCard.Calendar/Parts/CalendarJournal.cs +++ b/VisualCard.Calendar/Parts/CalendarJournal.cs @@ -34,7 +34,7 @@ public class CalendarJournal : Calendar, IEquatable { private readonly Dictionary> partsArray = []; private readonly Dictionary strings = []; - private readonly Dictionary integers = []; + private readonly Dictionary integers = []; /// /// Gets a part array from a specified key @@ -116,7 +116,7 @@ public override int GetHashCode() hashCode = hashCode * -1521134295 + base.GetHashCode(); hashCode = hashCode * -1521134295 + EqualityComparer>>.Default.GetHashCode(partsArray); hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(strings); - hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); + hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); return hashCode; } @@ -134,7 +134,7 @@ public override int GetHashCode() internal new void SetString(CalendarStringsEnum key, string value) => SetString(key, value, strings); - internal new void SetInteger(CalendarIntegersEnum key, int value) => + internal new void SetInteger(CalendarIntegersEnum key, double value) => SetInteger(key, value, integers); internal CalendarJournal(Version version) : diff --git a/VisualCard.Calendar/Parts/CalendarStandard.cs b/VisualCard.Calendar/Parts/CalendarStandard.cs index bf673589..6768aa7a 100644 --- a/VisualCard.Calendar/Parts/CalendarStandard.cs +++ b/VisualCard.Calendar/Parts/CalendarStandard.cs @@ -34,7 +34,7 @@ public class CalendarStandard : Calendar, IEquatable { private readonly Dictionary> partsArray = []; private readonly Dictionary strings = []; - private readonly Dictionary integers = []; + private readonly Dictionary integers = []; /// /// Gets a part array from a specified key @@ -116,7 +116,7 @@ public override int GetHashCode() hashCode = hashCode * -1521134295 + base.GetHashCode(); hashCode = hashCode * -1521134295 + EqualityComparer>>.Default.GetHashCode(partsArray); hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(strings); - hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); + hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); return hashCode; } @@ -134,7 +134,7 @@ public override int GetHashCode() internal new void SetString(CalendarStringsEnum key, string value) => SetString(key, value, strings); - internal new void SetInteger(CalendarIntegersEnum key, int value) => + internal new void SetInteger(CalendarIntegersEnum key, double value) => SetInteger(key, value, integers); internal CalendarStandard(Version version) : diff --git a/VisualCard.Calendar/Parts/CalendarTimeZone.cs b/VisualCard.Calendar/Parts/CalendarTimeZone.cs index 86be3231..52a6f9d9 100644 --- a/VisualCard.Calendar/Parts/CalendarTimeZone.cs +++ b/VisualCard.Calendar/Parts/CalendarTimeZone.cs @@ -36,7 +36,7 @@ public class CalendarTimeZone : Calendar, IEquatable internal readonly List daylights = []; private readonly Dictionary> partsArray = []; private readonly Dictionary strings = []; - private readonly Dictionary integers = []; + private readonly Dictionary integers = []; /// /// Standard time list @@ -134,7 +134,7 @@ public override int GetHashCode() hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(daylights); hashCode = hashCode * -1521134295 + EqualityComparer>>.Default.GetHashCode(partsArray); hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(strings); - hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); + hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); return hashCode; } @@ -152,7 +152,7 @@ public override int GetHashCode() internal new void SetString(CalendarStringsEnum key, string value) => SetString(key, value, strings); - internal new void SetInteger(CalendarIntegersEnum key, int value) => + internal new void SetInteger(CalendarIntegersEnum key, double value) => SetInteger(key, value, integers); internal CalendarTimeZone(Version version) : diff --git a/VisualCard.Calendar/Parts/CalendarTodo.cs b/VisualCard.Calendar/Parts/CalendarTodo.cs index 268d6034..c3d70be6 100644 --- a/VisualCard.Calendar/Parts/CalendarTodo.cs +++ b/VisualCard.Calendar/Parts/CalendarTodo.cs @@ -35,7 +35,7 @@ public class CalendarTodo : Calendar, IEquatable internal readonly List alarms = []; private readonly Dictionary> partsArray = []; private readonly Dictionary strings = []; - private readonly Dictionary integers = []; + private readonly Dictionary integers = []; /// /// Alarm list @@ -125,7 +125,7 @@ public override int GetHashCode() hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(alarms); hashCode = hashCode * -1521134295 + EqualityComparer>>.Default.GetHashCode(partsArray); hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(strings); - hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); + hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(integers); return hashCode; } @@ -143,7 +143,7 @@ public override int GetHashCode() internal new void SetString(CalendarStringsEnum key, string value) => SetString(key, value, strings); - internal new void SetInteger(CalendarIntegersEnum key, int value) => + internal new void SetInteger(CalendarIntegersEnum key, double value) => SetInteger(key, value, integers); internal CalendarTodo(Version version) : diff --git a/VisualCard.Calendar/Parts/Comparers/PartComparison.cs b/VisualCard.Calendar/Parts/Comparers/PartComparison.cs index 43e497f6..c92b806c 100644 --- a/VisualCard.Calendar/Parts/Comparers/PartComparison.cs +++ b/VisualCard.Calendar/Parts/Comparers/PartComparison.cs @@ -85,8 +85,8 @@ internal static bool StringsEqual( } internal static bool IntegersEqual( - IDictionary source, - IDictionary target) + IDictionary source, + IDictionary target) { // Verify the dictionaries if (!VerifyDicts(source, target)) @@ -99,7 +99,7 @@ internal static bool IntegersEqual( // Now, test the equality bool equal = source.All(kvp => { - bool exists = target.TryGetValue(kvp.Key, out int part); + bool exists = target.TryGetValue(kvp.Key, out double part); bool partsEqual = kvp.Value == part; return exists && partsEqual; }); diff --git a/VisualCard/Parsers/VcardParserTools.cs b/VisualCard/Parsers/VcardParserTools.cs index 8d27b038..7152e071 100644 --- a/VisualCard/Parsers/VcardParserTools.cs +++ b/VisualCard/Parsers/VcardParserTools.cs @@ -257,11 +257,6 @@ internal static string ProcessStringValue(string value, string valueType) string finalValue; switch (valueType.ToUpper()) { - case "": - case "TEXT": - // Unescape the value - finalValue = Regex.Unescape(value); - break; case "URI": // Unescape the value finalValue = Regex.Unescape(value); @@ -270,11 +265,27 @@ internal static string ProcessStringValue(string value, string valueType) finalValue = uri is not null ? uri.ToString() : finalValue; break; default: - throw new InvalidDataException($"The string value type {valueType} is invalid. Are you sure that you've specified the correct type in your vCard representation?"); + // Unescape the value + finalValue = Regex.Unescape(value); + break; } // Return the result return finalValue; } + + internal static double ProcessIntegerValue(string value, string valueType) + { + // Now, handle each type individually + double primaryValue = double.Parse(value); + var finalValue = valueType switch + { + "INTEGER" => (int)primaryValue, + _ => primaryValue, + }; + + // Return the result + return finalValue; + } } }