Skip to content

Commit

Permalink
imp - Upgraded some strings to part arrays
Browse files Browse the repository at this point in the history
---

We've upgraded some strings to parts array.

---

Type: imp
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Apr 3, 2024
1 parent 1074b5a commit 5ff308d
Show file tree
Hide file tree
Showing 17 changed files with 922 additions and 93 deletions.
16 changes: 13 additions & 3 deletions VisualCard.ShowContacts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ static void Main(string[] args)
foreach (Card Contact in contacts)
{
TextWriterColor.WriteColor("----------------------------", ConsoleColors.Green);
TextWriterColor.WriteColor("Name: {0}", ConsoleColors.Green, Contact.GetString(StringsEnum.FullName));

// List names
foreach (var fullName in Contact.GetPartsArray<FullNameInfo>())
{
TextWriterColor.Write("Name: {0}", fullName.FullName);
TextWriterColor.Write("ALTID: {0}", fullName.AltId);
}

// List names
foreach (var name in Contact.GetPartsArray<NameInfo>())
Expand Down Expand Up @@ -155,15 +161,19 @@ static void Main(string[] args)
var birth = Contact.GetPartsArray<BirthDateInfo>();
var wedding = Contact.GetPartsArray<AnniversaryInfo>();
var gender = Contact.GetPartsArray<GenderInfo>();
var url = Contact.GetPartsArray<UrlInfo>();
var note = Contact.GetPartsArray<NoteInfo>();
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);
if (url.Length > 0)
TextWriterColor.Write("Contact URL: {0}", url[0].Url);
if (note.Length > 0)
TextWriterColor.Write("Contact Note: {0}", note[0].Note);
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));

// Print VCard
string raw = Contact.SaveToString();
Expand Down
10 changes: 5 additions & 5 deletions VisualCard.Tests/ContactData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public static class ContactData
"""
BEGIN:VCARD
VERSION:3.0
FN:Rick Hood
N:Hood;Rick;;;
FN:Rick Hood
END:VCARD
"""
Expand Down Expand Up @@ -141,12 +141,12 @@ public static class ContactData
"""
BEGIN:VCARD
VERSION:3.0
FN:John Sanders
NOTE:Note test for VisualCard
N:Sanders;John;;;
TEL:495-522-3560
ADR:;;Los Angeles;;;;USA
EMAIL:[email protected]
FN:John Sanders
NOTE:Note test for VisualCard
END:VCARD
"""
Expand All @@ -156,14 +156,14 @@ public static class ContactData
"""
BEGIN:VCARD
VERSION:3.0
FN:John Sanders
NOTE:Note test for VisualCard
N:Sanders;John;;;
TEL:495-522-3560
TEL;TYPE=VIDEO:495-522-3550
ADR:;;Los Angeles;;;;USA
EMAIL:[email protected]
X-VISUALCARD-KANA:Saunders;John
FN:John Sanders
NOTE:Note test for VisualCard
END:VCARD
"""
Expand Down
21 changes: 12 additions & 9 deletions VisualCard/Converters/MeCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,27 +153,27 @@ public static string SaveCardToMeCardString(Card card, bool compatibility = fals

// Now, get all the values in the below order
var names = card.GetPartsArray<NameInfo>();
var fullName = card.GetString(StringsEnum.FullName);
var fullNames = card.GetPartsArray<FullNameInfo>();
var xNames = card.GetPartsArray<XNameInfo>();
var telephones = card.GetPartsArray<TelephoneInfo>();
var emails = card.GetPartsArray<EmailInfo>();
var note = card.GetString(StringsEnum.Notes);
var notes = card.GetPartsArray<NoteInfo>();
var birthdays = card.GetPartsArray<BirthDateInfo>();
var addresses = card.GetPartsArray<AddressInfo>();
var url = card.GetString(StringsEnum.Url);
var urls = card.GetPartsArray<UrlInfo>();
var nicknames = card.GetPartsArray<NicknameInfo>();

// Check them for existence
bool hasNames = names.Length > 0;
bool hasFullName = !string.IsNullOrEmpty(fullName);
bool hasFullName = fullNames.Length > 0;
bool hasReading = xNames.Any((xName) => xName.XKeyName == _meCardXNameKanaSpecifier);
bool hasTelephone = telephones.Length > 0 && telephones.Any((tel) => !tel.HasType("video"));
bool hasVideophone = telephones.Length > 0 && telephones.Any((tel) => tel.HasType("video")) && !compatibility;
bool hasEmails = emails.Length > 0;
bool hasNote = !string.IsNullOrEmpty(note) && !compatibility;
bool hasNote = notes.Length > 0 && !compatibility;
bool hasBirthday = birthdays.Length > 0;
bool hasAddresses = addresses.Length > 0;
bool hasUrl = !string.IsNullOrEmpty(url) && !compatibility;
bool hasUrl = urls.Length > 0 && !compatibility;
bool hasNicknames = nicknames.Length > 0 && !compatibility;
if (!hasNames && !hasFullName)
throw new InvalidDataException("Can't build a MeCard string from a vCard containing an empty name or an empty full name.");
Expand All @@ -192,7 +192,8 @@ public static string SaveCardToMeCardString(Card card, bool compatibility = fals
else if (hasFullName)
{
StringBuilder builder = new();
string[] splitFullName = fullName.Split([" "], StringSplitOptions.RemoveEmptyEntries);
var fullName = fullNames[0];
string[] splitFullName = fullName.FullName.Split([" "], StringSplitOptions.RemoveEmptyEntries);
builder.Append(_meCardNameSpecifier + _meCardArgumentDelimiter);
builder.Append(string.Join(_meCardValueDelimiter.ToString(), splitFullName));
properties.Add(builder.ToString());
Expand Down Expand Up @@ -232,8 +233,9 @@ public static string SaveCardToMeCardString(Card card, bool compatibility = fals
if (hasNote)
{
StringBuilder builder = new();
var note = notes[0];
builder.Append(_meCardNoteSpecifier + _meCardArgumentDelimiter);
builder.Append(note);
builder.Append(note.Note);
properties.Add(builder.ToString());
}
if (hasBirthday)
Expand Down Expand Up @@ -261,8 +263,9 @@ public static string SaveCardToMeCardString(Card card, bool compatibility = fals
if (hasUrl)
{
StringBuilder builder = new();
var url = urls[0];
builder.Append(_meCardUrlSpecifier + _meCardArgumentDelimiter);
builder.Append(url);
builder.Append(url.Url);
properties.Add(builder.ToString());
}
if (hasNicknames)
Expand Down
25 changes: 8 additions & 17 deletions VisualCard/Parsers/VcardParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ public Card Parse()
splitArgs.Where((arg) =>
arg.StartsWith(VcardConstants._altIdArgumentSpecifier) ||
arg.StartsWith(VcardConstants._valueArgumentSpecifier) ||
arg.StartsWith(VcardConstants._typeArgumentSpecifier)
arg.StartsWith(VcardConstants._typeArgumentSpecifier) ||
(CardVersion.Major == 2 && !arg.Contains(VcardConstants._argumentValueDelimiter))
)
));
}
Expand All @@ -160,25 +161,13 @@ public Card Parse()
// Now, handle each type individually
switch (stringType)
{
case StringsEnum.FullName:
case StringsEnum.Notes:
case StringsEnum.Mailer:
case StringsEnum.ProductId:
case StringsEnum.SortString:
case StringsEnum.AccessClassification:
// Unescape the value
finalValue = Regex.Unescape(value);
break;
case StringsEnum.Url:
case StringsEnum.Source:
case StringsEnum.FreeBusyUrl:
case StringsEnum.CalendarUrl:
case StringsEnum.CalendarSchedulingRequestUrl:
// Try to parse the URL to ensure that it conforms to IETF RFC 1738: Uniform Resource Locators
if (!Uri.TryCreate(value, UriKind.Absolute, out Uri uri))
throw new InvalidDataException($"URL {value} is invalid");
finalValue = uri.ToString();
break;
case StringsEnum.Kind:
// Get the kind
if (!string.IsNullOrEmpty(value))
Expand Down Expand Up @@ -231,9 +220,11 @@ internal void ValidateCard(Card card)
// Track the required fields
List<string> expectedFields = [];
List<string> actualFields = [];
if (VcardParserTools.GetPartsArrayEnumFromType(typeof(NameInfo), CardVersion).Item2 == PartCardinality.ShouldBeOne)
var nameCardinality = VcardParserTools.GetPartsArrayEnumFromType(typeof(NameInfo), CardVersion).Item2;
var fullNameCardinality = VcardParserTools.GetPartsArrayEnumFromType(typeof(FullNameInfo), CardVersion).Item2;
if (nameCardinality == PartCardinality.ShouldBeOne)
expectedFields.Add(VcardConstants._nameSpecifier);
if (CardVersion.Major >= 3)
if (fullNameCardinality == PartCardinality.AtLeastOne)
expectedFields.Add(VcardConstants._fullNameSpecifier);

// Requirement checks
Expand All @@ -246,8 +237,8 @@ internal void ValidateCard(Card card)
}
if (expectedFields.Contains(VcardConstants._fullNameSpecifier))
{
string fullName = card.GetString(StringsEnum.FullName);
bool exists = !string.IsNullOrEmpty(fullName);
var fullNames = card.GetPartsArray<FullNameInfo>(PartsArrayEnum.FullName);
bool exists = fullNames is not null && fullNames.Length > 0;
if (exists)
actualFields.Add(VcardConstants._fullNameSpecifier);
}
Expand Down
Loading

0 comments on commit 5ff308d

Please sign in to comment.