Skip to content

Commit

Permalink
add - Added full nullable support
Browse files Browse the repository at this point in the history
---

We've finally added full nullable support for VisualCard! We'll add more features soon.

---

Type: add
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Sep 10, 2024
1 parent 185b25b commit 127e201
Show file tree
Hide file tree
Showing 47 changed files with 212 additions and 197 deletions.
14 changes: 9 additions & 5 deletions VisualCard.Calendar/Parsers/VCalendarParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Parts.Calendar Parse()
continue;

// Take the last sub-part if available
Parts.Calendar subPart = null;
Parts.Calendar? subPart = null;
if (begins.Count > 0)
subPart = begins[begins.Count - 1].Item2;

Expand Down Expand Up @@ -166,7 +166,7 @@ public Parts.Calendar Parse()
{
string elementTypeUpper = elementType.ToUpper();
if (!allowedTypes.Contains(elementTypeUpper) && !extraAllowedTypes.Contains(elementTypeUpper) && !elementTypeUpper.StartsWith("X-"))
throw new InvalidDataException($"Part info type {classType.Name} doesn't support property type {elementTypeUpper} because the following base types are supported: [{string.Join(", ", allowedTypes)}] and the extra types are supported: [{string.Join(", ", extraAllowedTypes)}]");
throw new InvalidDataException($"Part info type {classType?.Name ?? "<null>"} doesn't support property type {elementTypeUpper} because the following base types are supported: [{string.Join(", ", allowedTypes)}] and the extra types are supported: [{string.Join(", ", extraAllowedTypes)}]");
}

// Handle the part type
Expand Down Expand Up @@ -258,18 +258,20 @@ public Parts.Calendar Parse()
case PartType.PartsArray:
{
CalendarPartsArrayEnum partsArrayType = (CalendarPartsArrayEnum)enumeration;
Type partsArrayClass = classType;
Type? partsArrayClass = classType;
bool supported = VCalendarParserTools.EnumArrayTypeSupported(partsArrayType, CalendarVersion);
if (!supported)
continue;
if (fromString is null)
continue;

// Now, get the part info
string finalValue = partsArrayType == CalendarPartsArrayEnum.NonstandardNames ? _value : value;
var partInfo = fromString(finalValue, [.. finalArgs], elementTypes, values, CalendarVersion);
if (subPart is not null)
subPart.AddPartToArray(partsArrayType, partInfo);
else
subPart.AddPartToArray(partsArrayType, partInfo);
calendar.AddPartToArray(partsArrayType, partInfo);
}
break;
default:
Expand Down Expand Up @@ -319,8 +321,10 @@ private Parts.Calendar GetCalendarInheritedInstance(string type)
};
}

private void SaveLastSubPart(Parts.Calendar subpart, ref Parts.Calendar part)
private void SaveLastSubPart(Parts.Calendar? subpart, ref Parts.Calendar part)
{
if (subpart is null)
return;
switch (part.GetType().Name)
{
case nameof(Calendar):
Expand Down
2 changes: 1 addition & 1 deletion VisualCard.Calendar/Parsers/VCalendarParserTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ internal static (CalendarPartsArrayEnum, PartCardinality) GetPartsArrayEnumFromT
throw new NotImplementedException($"Type {partsArrayType.Name} doesn't represent any part array.");
}

internal static (PartType type, object enumeration, Type enumType, Func<string, string[], string[], string, Version, BaseCalendarPartInfo> fromStringFunc, string defaultType, string defaultValue, string[] allowedExtraTypes) GetPartType(string prefix) =>
internal static (PartType type, object enumeration, Type? enumType, Func<string, string[], string[], string, Version, BaseCalendarPartInfo>? fromStringFunc, string defaultType, string defaultValue, string[] allowedExtraTypes) GetPartType(string prefix) =>
prefix switch
{
VCalendarConstants._attachSpecifier => (PartType.PartsArray, CalendarPartsArrayEnum.Attach, typeof(AttachInfo), AttachInfo.FromStringVcalendarStatic, "", "", []),
Expand Down
14 changes: 8 additions & 6 deletions VisualCard.Calendar/Parts/BaseCalendarPartInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ public abstract class BaseCalendarPartInfo : IEquatable<BaseCalendarPartInfo>
/// <summary>
/// Final arguments
/// </summary>
public virtual string[] Arguments { get; internal set; }
public virtual string[]? Arguments { get; internal set; }

/// <summary>
/// Card element type (home, work, ...)
/// </summary>
public virtual string[] ElementTypes { get; internal set; }
public virtual string[]? ElementTypes { get; internal set; }

/// <summary>
/// Value type (usually set by VALUE=)
/// </summary>
public virtual string ValueType { get; internal set; }
public virtual string? ValueType { get; internal set; }

/// <summary>
/// Is this part preferred?
Expand Down Expand Up @@ -85,6 +85,8 @@ public ReadOnlyDictionary<string, string> ArgumentValues
/// <returns>True if found; otherwise, false.</returns>
public bool HasType(string type)
{
if (ElementTypes is null)
return false;
bool found = false;
foreach (string elementType in ElementTypes)
{
Expand Down Expand Up @@ -131,9 +133,9 @@ public override bool Equals(object obj) =>
public override int GetHashCode()
{
int hashCode = -452519667;
hashCode = hashCode * -1521134295 + EqualityComparer<string[]>.Default.GetHashCode(Arguments);
hashCode = hashCode * -1521134295 + EqualityComparer<string[]>.Default.GetHashCode(ElementTypes);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ValueType);
hashCode = hashCode * -1521134295 + EqualityComparer<string[]?>.Default.GetHashCode(Arguments);
hashCode = hashCode * -1521134295 + EqualityComparer<string[]?>.Default.GetHashCode(ElementTypes);
hashCode = hashCode * -1521134295 + EqualityComparer<string?>.Default.GetHashCode(ValueType);
return hashCode;
}

Expand Down
12 changes: 7 additions & 5 deletions VisualCard.Calendar/Parts/CalendarBuilderTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ internal static class CalendarBuilderTools
internal static string BuildArguments(BaseCalendarPartInfo partInfo, string defaultType, string defaultValue)
{
// Filter the list of types and values first
string valueType = partInfo.ValueType ?? "";
string[] valueArguments = partInfo.Arguments ?? [];
string[] finalElementTypes = partInfo.ElementTypes.Where((type) => !type.Equals(defaultType, StringComparison.OrdinalIgnoreCase)).ToArray();
string finalValue = partInfo.ValueType.Equals(defaultValue, StringComparison.OrdinalIgnoreCase) ? "" : partInfo.ValueType;
string finalValue = valueType.Equals(defaultValue, StringComparison.OrdinalIgnoreCase) ? "" : valueType;

// Check to see if we've been provided arguments
bool noSemicolon = partInfo.Arguments.Length == 0 && finalElementTypes.Length == 0 && string.IsNullOrEmpty(finalValue);
string xNonstandardName = partInfo is XNameInfo xName ? xName.XKeyName : "";
bool noSemicolon = valueArguments.Length == 0 && finalElementTypes.Length == 0 && string.IsNullOrEmpty(finalValue);
string xNonstandardName = (partInfo is XNameInfo xName ? xName.XKeyName : "") ?? "";
if (noSemicolon)
return xNonstandardName + VCalendarConstants._argumentDelimiter.ToString();

// Now, initialize the argument builder
StringBuilder argumentsBuilder = new(xNonstandardName + VCalendarConstants._fieldDelimiter.ToString());
bool installArguments = partInfo.Arguments.Length > 0;
bool installArguments = valueArguments.Length > 0;
bool installElementTypes = finalElementTypes.Length > 0;
bool installValueType = !string.IsNullOrEmpty(finalValue);

Expand Down Expand Up @@ -76,7 +78,7 @@ internal static string BuildArguments(BaseCalendarPartInfo partInfo, string defa
// Finally, install the remaining arguments if they exist and contain keys and values
if (installArguments)
{
string[] finalArguments = partInfo.Arguments.Where((arg) => arg.Contains(VCalendarConstants._argumentValueDelimiter)).ToArray();
string[] finalArguments = valueArguments.Where((arg) => arg.Contains(VCalendarConstants._argumentValueDelimiter)).ToArray();
argumentsBuilder.Append(string.Join(VCalendarConstants._fieldDelimiter.ToString(), finalArguments));
}

Expand Down
10 changes: 5 additions & 5 deletions VisualCard.Calendar/Parts/Implementations/AttachInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public class AttachInfo : BaseCalendarPartInfo, IEquatable<AttachInfo>
/// <summary>
/// Attach encoding type
/// </summary>
public string Encoding { get; }
public string? Encoding { get; }
/// <summary>
/// Encoded attach
/// </summary>
public string AttachEncoded { get; }
public string? AttachEncoded { get; }
/// <summary>
/// Whether this attach is a blob or not
/// </summary>
Expand All @@ -50,7 +50,7 @@ internal static BaseCalendarPartInfo FromStringVcalendarStatic(string value, str
new AttachInfo().FromStringVcalendarInternal(value, finalArgs, elementTypes, valueType, calendarVersion);

internal override string ToStringVcalendarInternal(Version calendarVersion) =>
AttachEncoded;
AttachEncoded ?? "";

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version calendarVersion)
{
Expand Down Expand Up @@ -112,8 +112,8 @@ public override int GetHashCode()
{
int hashCode = -365738507;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Encoding);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(AttachEncoded);
hashCode = hashCode * -1521134295 + EqualityComparer<string?>.Default.GetHashCode(Encoding);
hashCode = hashCode * -1521134295 + EqualityComparer<string?>.Default.GetHashCode(AttachEncoded);
return hashCode;
}

Expand Down
6 changes: 3 additions & 3 deletions VisualCard.Calendar/Parts/Implementations/AttendeeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public class AttendeeInfo : BaseCalendarPartInfo, IEquatable<AttendeeInfo>
/// <summary>
/// The attendee address
/// </summary>
public string Attendee { get; }
public string? Attendee { get; }

internal static BaseCalendarPartInfo FromStringVcalendarStatic(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion) =>
new AttendeeInfo().FromStringVcalendarInternal(value, finalArgs, elementTypes, valueType, cardVersion);

internal override string ToStringVcalendarInternal(Version cardVersion) =>
Attendee;
Attendee ?? "";

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
{
Expand Down Expand Up @@ -87,7 +87,7 @@ public override int GetHashCode()
{
int hashCode = -1115589996;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Attendee);
hashCode = hashCode * -1521134295 + EqualityComparer<string?>.Default.GetHashCode(Attendee);
return hashCode;
}

Expand Down
6 changes: 3 additions & 3 deletions VisualCard.Calendar/Parts/Implementations/CommentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public class CommentInfo : BaseCalendarPartInfo, IEquatable<CommentInfo>
/// <summary>
/// The contact's comment
/// </summary>
public string Comment { get; }
public string? Comment { get; }

internal static BaseCalendarPartInfo FromStringVcalendarStatic(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion) =>
new CommentInfo().FromStringVcalendarInternal(value, finalArgs, elementTypes, valueType, cardVersion);

internal override string ToStringVcalendarInternal(Version cardVersion) =>
Comment;
Comment ?? "";

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
{
Expand Down Expand Up @@ -87,7 +87,7 @@ public override int GetHashCode()
{
int hashCode = -1115589996;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Comment);
hashCode = hashCode * -1521134295 + EqualityComparer<string?>.Default.GetHashCode(Comment);
return hashCode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class CategoriesInfo : BaseCalendarPartInfo, IEquatable<CategoriesInfo>
/// <summary>
/// The contact's categories
/// </summary>
public string[] Categories { get; }
public string[]? Categories { get; }

internal static BaseCalendarPartInfo FromStringVcalendarStatic(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion) =>
new CategoriesInfo().FromStringVcalendarInternal(value, finalArgs, elementTypes, valueType, cardVersion);
Expand Down Expand Up @@ -88,7 +88,7 @@ public override int GetHashCode()
{
int hashCode = -723142617;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string[]>.Default.GetHashCode(Categories);
hashCode = hashCode * -1521134295 + EqualityComparer<string[]?>.Default.GetHashCode(Categories);
return hashCode;
}

Expand Down
6 changes: 3 additions & 3 deletions VisualCard.Calendar/Parts/Implementations/LocationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public class LocationInfo : BaseCalendarPartInfo, IEquatable<LocationInfo>
/// <summary>
/// The contact's location
/// </summary>
public string Location { get; }
public string? Location { get; }

internal static BaseCalendarPartInfo FromStringVcalendarStatic(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion) =>
new LocationInfo().FromStringVcalendarInternal(value, finalArgs, elementTypes, valueType, cardVersion);

internal override string ToStringVcalendarInternal(Version cardVersion) =>
Location;
Location ?? "";

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
{
Expand Down Expand Up @@ -87,7 +87,7 @@ public override int GetHashCode()
{
int hashCode = -1115589996;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Location);
hashCode = hashCode * -1521134295 + EqualityComparer<string?>.Default.GetHashCode(Location);
return hashCode;
}

Expand Down
4 changes: 2 additions & 2 deletions VisualCard.Calendar/Parts/Implementations/ResourcesInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ResourcesInfo : BaseCalendarPartInfo, IEquatable<ResourcesInfo>
/// <summary>
/// The contact's resources
/// </summary>
public string[] Resources { get; }
public string[]? Resources { get; }

internal static BaseCalendarPartInfo FromStringVcalendarStatic(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion) =>
new ResourcesInfo().FromStringVcalendarInternal(value, finalArgs, elementTypes, valueType, cardVersion);
Expand Down Expand Up @@ -88,7 +88,7 @@ public override int GetHashCode()
{
int hashCode = -723142617;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string[]>.Default.GetHashCode(Resources);
hashCode = hashCode * -1521134295 + EqualityComparer<string[]?>.Default.GetHashCode(Resources);
return hashCode;
}

Expand Down
8 changes: 4 additions & 4 deletions VisualCard.Calendar/Parts/Implementations/XNameInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public class XNameInfo : BaseCalendarPartInfo, IEquatable<XNameInfo>
/// <summary>
/// X- key name
/// </summary>
public string XKeyName { get; }
public string? XKeyName { get; }
/// <summary>
/// X- values
/// </summary>
public string[] XValues { get; }
public string[]? XValues { get; }

internal static BaseCalendarPartInfo FromStringVcalendarStatic(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion) =>
new XNameInfo().FromStringVcalendarInternal(value, finalArgs, elementTypes, valueType, cardVersion);
Expand Down Expand Up @@ -98,8 +98,8 @@ public override int GetHashCode()
{
int hashCode = 390070728;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(XKeyName);
hashCode = hashCode * -1521134295 + EqualityComparer<string[]>.Default.GetHashCode(XValues);
hashCode = hashCode * -1521134295 + EqualityComparer<string?>.Default.GetHashCode(XKeyName);
hashCode = hashCode * -1521134295 + EqualityComparer<string[]?>.Default.GetHashCode(XValues);
return hashCode;
}

Expand Down
4 changes: 2 additions & 2 deletions VisualCard.ShowCalendars/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ static void Main(string[] args)
var starts = Calendar.GetPartsArray<DateStartInfo>();
var ends = Calendar.GetPartsArray<DateEndInfo>();
if (starts.Length > 0)
TextWriterColor.Write("Calendar start date: {0}", starts[0].DateStart);
TextWriterColor.Write("Calendar start date: {0}", starts[0].DateStart ?? new());
if (ends.Length > 0)
TextWriterColor.Write("Calendar end date: {0}", ends[0].DateEnd);
TextWriterColor.Write("Calendar end date: {0}", ends[0].DateEnd ?? new());
TextWriterColor.Write("Calendar product ID: {0}", Calendar.GetString(CalendarStringsEnum.ProductId));
TextWriterColor.Write("Calendar UUID: {0}", Calendar.UniqueId);

Expand Down
Loading

0 comments on commit 127e201

Please sign in to comment.