Skip to content

Commit

Permalink
add - doc - Implemented ArgumentInfo for arguments
Browse files Browse the repository at this point in the history
---

We've added ArgumentInfo for better property argument parsing.

---

Type: add
Breaking: False
Doc Required: True
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Oct 2, 2024
1 parent da66b85 commit 724ebd6
Show file tree
Hide file tree
Showing 78 changed files with 445 additions and 281 deletions.
13 changes: 10 additions & 3 deletions VisualCard.Calendar/Parsers/VCalendarParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using VisualCard.Calendar.Exceptions;
using VisualCard.Parts.Enums;
using VisualCard.Parsers;
using VisualCard.Parsers.Arguments;

namespace VisualCard.Calendar.Parsers
{
Expand Down Expand Up @@ -112,7 +113,7 @@ public Parts.Calendar Parse()
string[] splitArgs = args.Split([VCalendarConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
string[] splitValues = value.Split([VCalendarConstants._fieldDelimiter], StringSplitOptions.RemoveEmptyEntries);
bool isWithType = splitArgs.Length > 0;
List<string> finalArgs = [];
List<ArgumentInfo> finalArgs = [];

// Check to see if we have a BEGIN or an END prefix
if (prefix == VCalendarConstants._beginSpecifier)
Expand Down Expand Up @@ -149,13 +150,19 @@ public Parts.Calendar Parse()
if (isWithType)
{
// Finalize the arguments
finalArgs.AddRange(splitArgs.Except(
var argsStr = splitArgs.Except(
splitArgs.Where((arg) =>
arg.StartsWith(VCalendarConstants._valueArgumentSpecifier) ||
arg.StartsWith(VCalendarConstants._typeArgumentSpecifier) ||
CalendarVersion.Major == 2 && !arg.Contains(VCalendarConstants._argumentValueDelimiter)
)
));
);
foreach (string arg in argsStr)
{
string keyStr = arg.Substring(0, arg.IndexOf(VCalendarConstants._argumentValueDelimiter));
string valueStr = arg.RemovePrefix($"{keyStr}{VCalendarConstants._argumentValueDelimiter}");
finalArgs.Add(new(keyStr, valueStr));
}
}

// Check the type for allowed types
Expand Down
3 changes: 2 additions & 1 deletion VisualCard.Calendar/Parsers/VCalendarParserTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System.Text.RegularExpressions;
using System.IO;
using VisualCard.Calendar.Parts.Implementations.FreeBusy;
using VisualCard.Parsers.Arguments;

namespace VisualCard.Calendar.Parsers
{
Expand Down Expand Up @@ -251,7 +252,7 @@ internal static (CalendarPartsArrayEnum, PartCardinality) GetPartsArrayEnumFromT
return (CalendarPartsArrayEnum.IanaNames, PartCardinality.Any);
}

internal static (PartType type, object enumeration, Type? enumType, Func<string, string[], string[], string, Version, BaseCalendarPartInfo>? fromStringFunc, string defaultType, string defaultValue, string defaultValueType, string[] allowedExtraTypes, string[] allowedValues) GetPartType(string prefix, string objectType, Version calendarVersion)
internal static (PartType type, object enumeration, Type? enumType, Func<string, ArgumentInfo[], string[], string, Version, BaseCalendarPartInfo>? fromStringFunc, string defaultType, string defaultValue, string defaultValueType, string[] allowedExtraTypes, string[] allowedValues) GetPartType(string prefix, string objectType, Version calendarVersion)
{
string[] allowedStatuses =
objectType == VCalendarConstants._objectVEventSpecifier && calendarVersion.Major == 2 ? ["TENTATIVE", "CONFIRMED", "CANCELLED"] :
Expand Down
34 changes: 5 additions & 29 deletions VisualCard.Calendar/Parts/BaseCalendarPartInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Diagnostics;
using System.Linq;
using VisualCard.Calendar.Parsers;
using VisualCard.Parsers.Arguments;

namespace VisualCard.Calendar.Parts
{
Expand All @@ -35,7 +36,7 @@ public abstract class BaseCalendarPartInfo : IEquatable<BaseCalendarPartInfo>
/// <summary>
/// Final arguments
/// </summary>
public virtual string[]? Arguments { get; internal set; }
public virtual ArgumentInfo[]? Arguments { get; internal set; }

/// <summary>
/// Card element type (home, work, ...)
Expand All @@ -53,31 +54,6 @@ public abstract class BaseCalendarPartInfo : IEquatable<BaseCalendarPartInfo>
public bool IsPreferred =>
HasType("PREF");

/// <summary>
/// Arguments in key/value pair format
/// </summary>
public ReadOnlyDictionary<string, string> ArgumentValues
{
get
{
// Check to see if we have an empty list of arguments
Dictionary<string, string> values = [];
if (Arguments is null || Arguments.Length == 0)
return new(values);

// Now, separate a key from a value
foreach (var arg in Arguments)
{
string key = arg.Substring(0, arg.IndexOf(VCalendarConstants._argumentValueDelimiter));
string value = arg.Substring(arg.IndexOf(VCalendarConstants._argumentValueDelimiter) + 1);
values.Add(key, value);
}

// Now, return a read-only dictionary
return new(values);
}
}

/// <summary>
/// Checks to see if this part has a specific type
/// </summary>
Expand Down Expand Up @@ -133,7 +109,7 @@ 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<ArgumentInfo[]?>.Default.GetHashCode(Arguments);
hashCode = hashCode * -1521134295 + EqualityComparer<string[]?>.Default.GetHashCode(ElementTypes);
hashCode = hashCode * -1521134295 + EqualityComparer<string?>.Default.GetHashCode(ValueType);
return hashCode;
Expand All @@ -150,14 +126,14 @@ public override int GetHashCode()
internal virtual bool EqualsInternal(BaseCalendarPartInfo source, BaseCalendarPartInfo target) =>
true;

internal abstract BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version calendarVersion);
internal abstract BaseCalendarPartInfo FromStringVcalendarInternal(string value, ArgumentInfo[] finalArgs, string[] elementTypes, string valueType, Version calendarVersion);

internal abstract string ToStringVcalendarInternal(Version calendarVersion);

internal BaseCalendarPartInfo()
{ }

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

Expand Down Expand Up @@ -80,7 +80,9 @@ 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 = valueArguments.Where((arg) => arg.Contains(VCalendarConstants._argumentValueDelimiter)).ToArray();
string[] finalArguments = partInfo.Arguments
.Where((arg) => !string.IsNullOrWhiteSpace(arg.Value))
.Select((arg) => $"{arg.Key}={(arg.CaseSensitive ? $"\"{arg.Value}\"" : arg.Value)}").ToArray();
argumentsBuilder.Append(string.Join(VCalendarConstants._fieldDelimiter.ToString(), finalArguments));
}

Expand Down
7 changes: 4 additions & 3 deletions VisualCard.Calendar/Parts/Implementations/AttachInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.IO;
using VisualCard.Calendar.Parsers;
using VisualCard.Parsers;
using VisualCard.Parsers.Arguments;

namespace VisualCard.Calendar.Parts.Implementations
{
Expand All @@ -46,13 +47,13 @@ public class AttachInfo : BaseCalendarPartInfo, IEquatable<AttachInfo>
public bool IsBlob =>
VcardCommonTools.IsEncodingBlob(Arguments, AttachEncoded);

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

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

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version calendarVersion)
internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, ArgumentInfo[] finalArgs, string[] elementTypes, string valueType, Version calendarVersion)
{
// Check to see if the value is prepended by the ENCODING= argument
string attachEncoding = VcardCommonTools.GetValuesString(finalArgs, "b", VCalendarConstants._encodingArgumentSpecifier);
Expand Down Expand Up @@ -130,7 +131,7 @@ internal override bool EqualsInternal(BaseCalendarPartInfo source, BaseCalendarP

internal AttachInfo() { }

internal AttachInfo(string[] arguments, string[] elementTypes, string valueType, string encoding, string attachEncoded) :
internal AttachInfo(ArgumentInfo[] arguments, string[] elementTypes, string valueType, string encoding, string attachEncoded) :
base(arguments, elementTypes, valueType)
{
Encoding = encoding;
Expand Down
7 changes: 4 additions & 3 deletions VisualCard.Calendar/Parts/Implementations/AttendeeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using VisualCard.Parsers.Arguments;

namespace VisualCard.Calendar.Parts.Implementations
{
Expand All @@ -35,13 +36,13 @@ public class AttendeeInfo : BaseCalendarPartInfo, IEquatable<AttendeeInfo>
/// </summary>
public string? Attendee { get; }

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

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

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, ArgumentInfo[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
{
// Populate the fields
var attendee = Regex.Unescape(value);
Expand Down Expand Up @@ -103,7 +104,7 @@ internal override bool EqualsInternal(BaseCalendarPartInfo source, BaseCalendarP

internal AttendeeInfo() { }

internal AttendeeInfo(string[] arguments, string[] elementTypes, string valueType, string attendee) :
internal AttendeeInfo(ArgumentInfo[] arguments, string[] elementTypes, string valueType, string attendee) :
base(arguments, elementTypes, valueType)
{
Attendee = attendee;
Expand Down
7 changes: 4 additions & 3 deletions VisualCard.Calendar/Parts/Implementations/CommentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using VisualCard.Parsers.Arguments;

namespace VisualCard.Calendar.Parts.Implementations
{
Expand All @@ -35,13 +36,13 @@ public class CommentInfo : BaseCalendarPartInfo, IEquatable<CommentInfo>
/// </summary>
public string? Comment { get; }

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

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

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, ArgumentInfo[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
{
// Populate the fields
var comment = Regex.Unescape(value);
Expand Down Expand Up @@ -103,7 +104,7 @@ internal override bool EqualsInternal(BaseCalendarPartInfo source, BaseCalendarP

internal CommentInfo() { }

internal CommentInfo(string[] arguments, string[] elementTypes, string valueType, string comment) :
internal CommentInfo(ArgumentInfo[] arguments, string[] elementTypes, string valueType, string comment) :
base(arguments, elementTypes, valueType)
{
Comment = comment;
Expand Down
7 changes: 4 additions & 3 deletions VisualCard.Calendar/Parts/Implementations/ContactInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using VisualCard.Parsers.Arguments;

namespace VisualCard.Calendar.Parts.Implementations
{
Expand All @@ -35,13 +36,13 @@ public class ContactInfo : BaseCalendarPartInfo, IEquatable<ContactInfo>
/// </summary>
public string? Contact { get; }

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

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

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, ArgumentInfo[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
{
// Populate the fields
var contact = Regex.Unescape(value);
Expand Down Expand Up @@ -103,7 +104,7 @@ internal override bool EqualsInternal(BaseCalendarPartInfo source, BaseCalendarP

internal ContactInfo() { }

internal ContactInfo(string[] arguments, string[] elementTypes, string valueType, string contact) :
internal ContactInfo(ArgumentInfo[] arguments, string[] elementTypes, string valueType, string contact) :
base(arguments, elementTypes, valueType)
{
Contact = contact;
Expand Down
7 changes: 4 additions & 3 deletions VisualCard.Calendar/Parts/Implementations/DateCreatedInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System;
using System.Diagnostics;
using VisualCard.Parsers;
using VisualCard.Parsers.Arguments;

namespace VisualCard.Calendar.Parts.Implementations
{
Expand All @@ -34,13 +35,13 @@ public class DateCreatedInfo : BaseCalendarPartInfo, IEquatable<DateCreatedInfo>
/// </summary>
public DateTimeOffset DateCreated { get; }

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

internal override string ToStringVcalendarInternal(Version cardVersion) =>
$"{VcardCommonTools.SavePosixDate(DateCreated)}";

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, ArgumentInfo[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
{
// Populate the fields
DateTimeOffset created = VcardCommonTools.ParsePosixDateTime(value);
Expand Down Expand Up @@ -102,7 +103,7 @@ internal override bool EqualsInternal(BaseCalendarPartInfo source, BaseCalendarP

internal DateCreatedInfo() { }

internal DateCreatedInfo(string[] arguments, string[] elementTypes, string valueType, DateTimeOffset rev) :
internal DateCreatedInfo(ArgumentInfo[] arguments, string[] elementTypes, string valueType, DateTimeOffset rev) :
base(arguments, elementTypes, valueType)
{
DateCreated = rev;
Expand Down
7 changes: 4 additions & 3 deletions VisualCard.Calendar/Parts/Implementations/DurationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Diagnostics;
using System.Text.RegularExpressions;
using VisualCard.Parsers;
using VisualCard.Parsers.Arguments;

namespace VisualCard.Calendar.Parts.Implementations
{
Expand All @@ -48,13 +49,13 @@ public class DurationInfo : BaseCalendarPartInfo, IEquatable<DurationInfo>
public DateTimeOffset DurationResult =>
VcardCommonTools.GetDurationSpan(Duration ?? "").result;

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

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

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, ArgumentInfo[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
{
// Populate the fields
string duration = Regex.Unescape(value);
Expand Down Expand Up @@ -116,7 +117,7 @@ internal override bool EqualsInternal(BaseCalendarPartInfo source, BaseCalendarP

internal DurationInfo() { }

internal DurationInfo(string[] arguments, string[] elementTypes, string valueType, string duration) :
internal DurationInfo(ArgumentInfo[] arguments, string[] elementTypes, string valueType, string duration) :
base(arguments, elementTypes, valueType)
{
Duration = duration;
Expand Down
Loading

0 comments on commit 724ebd6

Please sign in to comment.