Skip to content

Commit

Permalink
imp - doc - PartsArray and Strings are the only types
Browse files Browse the repository at this point in the history
---

We've changed how we categorize items so that we use the Cardinality instead of relying on three types to reduce complexity.

---

Type: imp
Breaking: False
Doc Required: True
Part: 1/1
  • Loading branch information
AptiviCEO committed Apr 3, 2024
1 parent 74afa8f commit 1074b5a
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 291 deletions.
19 changes: 9 additions & 10 deletions VisualCard.ShowContacts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ static void Main(string[] args)
{
TextWriterColor.WriteColor("----------------------------", ConsoleColors.Green);
TextWriterColor.WriteColor("Name: {0}", ConsoleColors.Green, Contact.GetString(StringsEnum.FullName));
TextWriterColor.WriteColor("Revision: {0}", ConsoleColors.Green, Contact.GetPart<RevisionInfo>());

// List names
foreach (var name in Contact.GetPartsArray<NameInfo>())
Expand Down Expand Up @@ -153,15 +152,15 @@ static void Main(string[] args)
}

// List remaining
var birth = Contact.GetPart<BirthDateInfo>();
var wedding = Contact.GetPart<AnniversaryInfo>();
var gender = Contact.GetPart<GenderInfo>();
if (birth is not null)
TextWriterColor.Write("Contact birthdate: {0}", birth.BirthDate);
if (wedding is not null)
TextWriterColor.Write("Contact wedding date: {0}", wedding.Anniversary);
if (gender is not null)
TextWriterColor.Write("Contact gender {0} [{1}]", gender.Gender.ToString(), gender.GenderDescription);
var birth = Contact.GetPartsArray<BirthDateInfo>();
var wedding = Contact.GetPartsArray<AnniversaryInfo>();
var gender = Contact.GetPartsArray<GenderInfo>();
if (birth.Length > 0)
TextWriterColor.Write("Contact birthdate: {0}", birth[0].BirthDate);
if (wedding.Length > 0)
TextWriterColor.Write("Contact wedding date: {0}", wedding[0].Anniversary);
if (gender.Length > 0)
TextWriterColor.Write("Contact gender {0} [{1}]", gender[0].Gender.ToString(), gender[0].GenderDescription);
TextWriterColor.Write("Contact mailer: {0}", Contact.GetString(StringsEnum.Mailer));
TextWriterColor.Write("Contact URL: {0}", Contact.GetString(StringsEnum.Url));
TextWriterColor.Write("Contact Note: {0}", Contact.GetString(StringsEnum.Notes));
Expand Down
4 changes: 2 additions & 2 deletions VisualCard.Tests/ContactData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public static class ContactData
IMPP;TYPE=HOME:aim:IM
IMPP;TYPE=HOME:msn:Windows LIVE
IMPP;TYPE=HOME:ymsgr:Yahoo
N:Navasquillo;Neville;Neville\,Nevile;Mr.;Jr.
N;ALTID=0;LANGUAGE=en:Navasquillo;Neville;Neville\,Nevile;Mr.;Jr.
N;ALTID=0;LANGUAGE=de:NAVASQUILLO;Neville;Neville\,Nevile;Mr.;Jr.
NOTE:Notes
ORG:Organization
Expand Down Expand Up @@ -467,7 +467,7 @@ public static class ContactData
IMPP;TYPE=HOME:aim:IM
IMPP;TYPE=HOME:msn:Windows LIVE
IMPP;TYPE=HOME:ymsgr:Yahoo
N:Navasquillo;Neville;Neville\,Nevile;Mr.;Jr.
N;ALTID=0;LANGUAGE=en:Navasquillo;Neville;Neville\,Nevile;Mr.;Jr.
N;ALTID=0;LANGUAGE=de:NAVASQUILLO;Neville;Neville\,Nevile;Mr.;Jr.
NOTE:Notes
ORG:Organization
Expand Down
5 changes: 3 additions & 2 deletions VisualCard/Converters/MeCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static string SaveCardToMeCardString(Card card, bool compatibility = fals
var telephones = card.GetPartsArray<TelephoneInfo>();
var emails = card.GetPartsArray<EmailInfo>();
var note = card.GetString(StringsEnum.Notes);
var birthday = card.GetPart<BirthDateInfo>();
var birthdays = card.GetPartsArray<BirthDateInfo>();
var addresses = card.GetPartsArray<AddressInfo>();
var url = card.GetString(StringsEnum.Url);
var nicknames = card.GetPartsArray<NicknameInfo>();
Expand All @@ -171,7 +171,7 @@ public static string SaveCardToMeCardString(Card card, bool compatibility = fals
bool hasVideophone = telephones.Length > 0 && telephones.Any((tel) => tel.HasType("video")) && !compatibility;
bool hasEmails = emails.Length > 0;
bool hasNote = !string.IsNullOrEmpty(note) && !compatibility;
bool hasBirthday = birthday is not null;
bool hasBirthday = birthdays.Length > 0;
bool hasAddresses = addresses.Length > 0;
bool hasUrl = !string.IsNullOrEmpty(url) && !compatibility;
bool hasNicknames = nicknames.Length > 0 && !compatibility;
Expand Down Expand Up @@ -239,6 +239,7 @@ public static string SaveCardToMeCardString(Card card, bool compatibility = fals
if (hasBirthday)
{
StringBuilder builder = new();
var birthday = birthdays[0];
builder.Append(_meCardBirthdaySpecifier + _meCardArgumentDelimiter);
builder.Append($"{birthday.BirthDate:yyyyMMdd}");
properties.Add(builder.ToString());
Expand Down
52 changes: 12 additions & 40 deletions VisualCard/Parsers/VcardParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,16 @@ public Card Parse()
// Now, parse a line
try
{
// Get the part type
bool xNonstandard = prefix.StartsWith(VcardConstants._xSpecifier);
bool specifierRequired = CardVersion.Major >= 3;
var (type, enumeration, classType, fromString, defaultType, defaultValue) = VcardParserTools.GetPartType(xNonstandard ? VcardConstants._xSpecifier : prefix);

// Handle arguments
if (isWithType)
{
// If we have more than one argument, check for ALTID
if (CardVersion.Major >= 4)
if (CardVersion.Major >= 4 && type == PartType.PartsArray)
{
if (splitArgs[0].StartsWith(VcardConstants._altIdArgumentSpecifier))
{
Expand Down Expand Up @@ -141,10 +147,7 @@ public Card Parse()
));
}

// Get the part type and handle it
bool xNonstandard = prefix.StartsWith(VcardConstants._xSpecifier);
bool specifierRequired = CardVersion.Major >= 3;
var (type, enumeration, classType, fromString, defaultType, defaultValue) = VcardParserTools.GetPartType(xNonstandard ? VcardConstants._xSpecifier : prefix);
// Handle the part type
string[] elementTypes = VcardParserTools.GetTypes(splitArgs, defaultType, specifierRequired);
string values = VcardParserTools.GetValuesString(splitArgs, defaultValue, VcardConstants._valueArgumentSpecifier);
switch (type)
Expand Down Expand Up @@ -194,19 +197,6 @@ public Card Parse()
card.SetString(stringType, finalValue);
}
break;
case PartType.Parts:
{
PartsEnum partsType = (PartsEnum)enumeration;
Type partsClass = classType;
bool supported = VcardParserTools.EnumTypeSupported(partsType, CardVersion);
if (!supported)
continue;

// Now, get the part info
var partInfo = fromString(value, [.. finalArgs], altId, elementTypes, values, CardVersion);
card.SetPart(partsType, partInfo);
}
break;
case PartType.PartsArray:
{
PartsArrayEnum partsArrayType = (PartsArrayEnum)enumeration;
Expand Down Expand Up @@ -241,20 +231,10 @@ internal void ValidateCard(Card card)
// Track the required fields
List<string> expectedFields = [];
List<string> actualFields = [];
switch (CardVersion.ToString(2))
{
case "2.1":
expectedFields.Add(VcardConstants._nameSpecifier);
break;
case "4.0":
expectedFields.Add(VcardConstants._fullNameSpecifier);
break;
case "3.0":
case "5.0":
expectedFields.Add(VcardConstants._nameSpecifier);
expectedFields.Add(VcardConstants._fullNameSpecifier);
break;
}
if (VcardParserTools.GetPartsArrayEnumFromType(typeof(NameInfo), CardVersion).Item2 == PartCardinality.ShouldBeOne)
expectedFields.Add(VcardConstants._nameSpecifier);
if (CardVersion.Major >= 3)
expectedFields.Add(VcardConstants._fullNameSpecifier);

// Requirement checks
if (expectedFields.Contains(VcardConstants._nameSpecifier))
Expand All @@ -275,14 +255,6 @@ internal void ValidateCard(Card card)
actualFields.Sort();
if (!actualFields.SequenceEqual(expectedFields))
throw new InvalidDataException($"The following keys [{string.Join(", ", expectedFields)}] are required. Got [{string.Join(", ", actualFields)}].");

// Check the cardinality of properties
CheckCardinality(card);
}

internal void CheckCardinality(Card card)
{
// TODO: Scaffolding. Actually check the cardinality.
}

internal VcardParser(string[] cardContent, Version cardVersion)
Expand Down
Loading

0 comments on commit 1074b5a

Please sign in to comment.