Skip to content

Commit

Permalink
add - doc - Added getting part by just type
Browse files Browse the repository at this point in the history
---

We've added getting part of a card by just type

---

Type: add
Breaking: False
Doc Required: True
Part: 1/1
  • Loading branch information
AptiviCEO committed Apr 1, 2024
1 parent 787b748 commit 5d9b9bb
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 28 deletions.
34 changes: 17 additions & 17 deletions VisualCard.ShowContacts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,25 @@ 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(PartsEnum.Revision));
TextWriterColor.WriteColor("Revision: {0}", ConsoleColors.Green, Contact.GetPart<RevisionInfo>());

// List names
foreach (NameInfo name in Contact.GetPartsArray(PartsArrayEnum.Names))
foreach (var name in Contact.GetPartsArray<NameInfo>())
{
TextWriterColor.Write("First name: {0}", name.ContactFirstName);
TextWriterColor.Write("Last name: {0}", name.ContactLastName);
TextWriterColor.Write("ALTID: {0}", name.AltId);
}

// List titles
foreach (TitleInfo title in Contact.GetPartsArray(PartsArrayEnum.Titles))
foreach (var title in Contact.GetPartsArray<TitleInfo>())
{
TextWriterColor.Write("Title or Job: {0}", title.ContactTitle);
TextWriterColor.Write("ALTID: {0}", title.AltId);
}

// List addresses
foreach (AddressInfo Address in Contact.GetPartsArray(PartsArrayEnum.Addresses))
foreach (var Address in Contact.GetPartsArray<AddressInfo>())
{
TextWriterColor.Write("P.O. Box: {0}", Address.PostOfficeBox);
TextWriterColor.Write("Extended Address: {0}", Address.ExtendedAddress);
Expand All @@ -117,27 +117,27 @@ static void Main(string[] args)
}

// List e-mails
foreach (EmailInfo Email in Contact.GetPartsArray(PartsArrayEnum.Mails))
foreach (var Email in Contact.GetPartsArray<EmailInfo>())
{
TextWriterColor.Write("Email address: {0}", Email.ContactEmailAddress);
}

// List organizations
foreach (OrganizationInfo Organization in Contact.GetPartsArray(PartsArrayEnum.Organizations))
foreach (var Organization in Contact.GetPartsArray<OrganizationInfo>())
{
TextWriterColor.Write("Organization Name: {0}", Organization.Name);
TextWriterColor.Write("Organization Unit: {0}", Organization.Unit);
TextWriterColor.Write("Organization Unit Role: {0}", Organization.Role);
}

// List telephones
foreach (TelephoneInfo Telephone in Contact.GetPartsArray(PartsArrayEnum.Telephones))
foreach (var Telephone in Contact.GetPartsArray<TelephoneInfo>())
{
TextWriterColor.Write("Phone number: {0}", Telephone.ContactPhoneNumber);
}

// List photos
foreach (PhotoInfo Photo in Contact.GetPartsArray(PartsArrayEnum.Photos))
foreach (var Photo in Contact.GetPartsArray<PhotoInfo>())
{
TextWriterColor.Write("Photo encoding: {0}", Photo.Encoding);
TextWriterColor.Write("Photo value type: {0}", Photo.ValueType);
Expand All @@ -146,21 +146,21 @@ static void Main(string[] args)
}

// List roles
foreach (RoleInfo Role in Contact.GetPartsArray(PartsArrayEnum.Roles))
foreach (var Role in Contact.GetPartsArray<RoleInfo>())
{
TextWriterColor.Write("Role: {0}", Role.ContactRole);
TextWriterColor.Write("ALTID: {0}", Role.AltId);
}

// List remaining
var birth = Contact.GetPart(PartsEnum.Birthdate);
var wed = Contact.GetPart(PartsEnum.Anniversary);
var gnd = Contact.GetPart(PartsEnum.Gender);
if (birth is BirthDateInfo bday)
TextWriterColor.Write("Contact birthdate: {0}", bday.BirthDate);
if (wed is AnniversaryInfo adate)
TextWriterColor.Write("Contact wedding date: {0}", adate.Anniversary);
if (gnd is GenderInfo gender)
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);
TextWriterColor.Write("Contact mailer: {0}", Contact.GetString(StringsEnum.Mailer));
TextWriterColor.Write("Contact URL: {0}", Contact.GetString(StringsEnum.Url));
Expand Down
2 changes: 1 addition & 1 deletion VisualCard/Parsers/VcardParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ internal void ValidateCard(Card card)
// Requirement checks
if (expectedFields.Contains(VcardConstants._nameSpecifier))
{
var names = card.GetPartsArray(PartsArrayEnum.Names);
var names = card.GetPartsArray<BaseCardPartInfo>(PartsArrayEnum.Names);
bool exists = names is not null && names.Length > 0;
if (exists)
actualFields.Add(VcardConstants._nameSpecifier);
Expand Down
66 changes: 66 additions & 0 deletions VisualCard/Parsers/VcardParserTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,72 @@ internal static string GetPrefixFromPartsArrayEnum(PartsArrayEnum partsArrayEnum
throw new NotImplementedException($"String enumeration {partsArrayEnum} is not implemented.")
};

internal static PartsEnum GetPartsEnumFromType(Type partsType)
{
if (partsType == null)
throw new NotImplementedException("Type is not provided.");

// Now, iterate through every type
if (partsType == typeof(RevisionInfo))
return PartsEnum.Revision;
else if (partsType == typeof(BirthDateInfo))
return PartsEnum.Birthdate;
else if (partsType == typeof(AnniversaryInfo))
return PartsEnum.Anniversary;
else if (partsType == typeof(GenderInfo))
return PartsEnum.Gender;
throw new NotImplementedException($"Type {partsType.Name} doesn't represent any part.");
}

internal static PartsArrayEnum GetPartsArrayEnumFromType(Type partsArrayType)
{
if (partsArrayType == null)
throw new NotImplementedException("Type is not provided.");

// Now, iterate through every type
if (partsArrayType == typeof(NameInfo))
return PartsArrayEnum.Names;
else if (partsArrayType == typeof(TelephoneInfo))
return PartsArrayEnum.Telephones;
else if (partsArrayType == typeof(AddressInfo))
return PartsArrayEnum.Addresses;
else if (partsArrayType == typeof(LabelAddressInfo))
return PartsArrayEnum.Labels;
else if (partsArrayType == typeof(AgentInfo))
return PartsArrayEnum.Agents;
else if (partsArrayType == typeof(EmailInfo))
return PartsArrayEnum.Mails;
else if (partsArrayType == typeof(OrganizationInfo))
return PartsArrayEnum.Organizations;
else if (partsArrayType == typeof(TitleInfo))
return PartsArrayEnum.Titles;
else if (partsArrayType == typeof(PhotoInfo))
return PartsArrayEnum.Photos;
else if (partsArrayType == typeof(NicknameInfo))
return PartsArrayEnum.Nicknames;
else if (partsArrayType == typeof(RoleInfo))
return PartsArrayEnum.Roles;
else if (partsArrayType == typeof(LogoInfo))
return PartsArrayEnum.Logos;
else if (partsArrayType == typeof(TimeDateZoneInfo))
return PartsArrayEnum.TimeZone;
else if (partsArrayType == typeof(GeoInfo))
return PartsArrayEnum.Geo;
else if (partsArrayType == typeof(SoundInfo))
return PartsArrayEnum.Sounds;
else if (partsArrayType == typeof(ImppInfo))
return PartsArrayEnum.Impps;
else if (partsArrayType == typeof(CategoryInfo))
return PartsArrayEnum.Categories;
else if (partsArrayType == typeof(LangInfo))
return PartsArrayEnum.Langs;
else if (partsArrayType == typeof(XmlInfo))
return PartsArrayEnum.Xml;
else if (partsArrayType == typeof(XNameInfo))
return PartsArrayEnum.NonstandardNames;
throw new NotImplementedException($"Type {partsArrayType.Name} doesn't represent any part array.");
}

internal static (PartType type, object enumeration, Type enumType, Func<string, string[], int, string[], string, Version, BaseCardPartInfo> fromStringFunc, string defaultType, string defaultValue) GetPartType(string prefix) =>
prefix switch
{
Expand Down
74 changes: 64 additions & 10 deletions VisualCard/Parts/Card.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.IO;
using System.Linq;
using System.Text;
using Textify.General;
using VisualCard.Parsers;
Expand All @@ -47,50 +48,103 @@ public class Card : IEquatable<Card>
public Version CardVersion =>
version;

/// <summary>
/// Gets a part array from a specified key
/// </summary>
/// <returns>An array of values or an empty part array []</returns>
public TPart[] GetPartsArray<TPart>() where TPart : BaseCardPartInfo
{
// Get the parts enumeration according to the type
var key = VcardParserTools.GetPartsArrayEnumFromType(typeof(TPart));

// Now, return the value
return GetPartsArray<TPart>(key);
}

/// <summary>
/// Gets a part array from a specified key
/// </summary>
/// <param name="key">A key to use</param>
/// <returns>An array of values or an empty part array []</returns>
public BaseCardPartInfo[] GetPartsArray(PartsArrayEnum key)
public TPart[] GetPartsArray<TPart>(PartsArrayEnum key) where TPart : BaseCardPartInfo
{
// Check for version support
if (!VcardParserTools.EnumArrayTypeSupported(key, CardVersion))
return null;

// Get the parts enumeration according to the type
var type = typeof(TPart);
if (type != typeof(BaseCardPartInfo))
{
// We don't need the base, but a derivative of it. Check it.
var partsArrayEnum = VcardParserTools.GetPartsArrayEnumFromType(typeof(TPart));
if (key != partsArrayEnum)
throw new InvalidOperationException($"Parts array enumeration [{key}] is different from the expected one [{partsArrayEnum}] according to type {typeof(TPart).Name}.");
}

// Get the fallback value
BaseCardPartInfo[] fallback = [];
TPart[] fallback = [];

// Check to see if the partarray has a value or not
bool hasValue = partsArray.TryGetValue(key, out List<BaseCardPartInfo> value);
bool hasValue = partsArray.ContainsKey(key);
if (!hasValue)
return fallback;

// Cast the values
var value = partsArray[key];
TPart[] parts = value.Cast<TPart>().ToArray();

// Now, return the value
return [.. value];
return parts;
}

/// <summary>
/// Gets a part from a specified key
/// </summary>
/// <returns>A value or an empty part if any other type doesn't exist</returns>
public TPart GetPart<TPart>() where TPart : BaseCardPartInfo
{
// Get the parts enumeration according to the type
var key = VcardParserTools.GetPartsEnumFromType(typeof(TPart));

// Now, return the value
return GetPart<TPart>(key);
}

/// <summary>
/// Gets a part from a specified key
/// </summary>
/// <param name="key">A key to use</param>
/// <returns>A value or an empty part if any other type doesn't exist</returns>
public BaseCardPartInfo GetPart(PartsEnum key)
public TPart GetPart<TPart>(PartsEnum key) where TPart : BaseCardPartInfo
{
// Check for version support
if (!VcardParserTools.EnumTypeSupported(key, CardVersion))
return null;

// Get the parts enumeration according to the type
var type = typeof(TPart);
if (type != typeof(BaseCardPartInfo))
{
// We don't need the base, but a derivative of it. Check it.
var partsEnum = VcardParserTools.GetPartsEnumFromType(typeof(TPart));
if (key != partsEnum)
throw new InvalidOperationException($"Part enumeration [{key}] is different from the expected one [{partsEnum}] according to type {typeof(TPart).Name}.");
}

// Get the fallback value
BaseCardPartInfo fallback = default;
TPart fallback = default;

// Check to see if the part has a value or not
bool hasValue = parts.TryGetValue(key, out BaseCardPartInfo value);
bool hasValue = parts.ContainsKey(key);
if (!hasValue)
return fallback;

// Cast the values
TPart part = (TPart)parts[key];

// Now, return the value
return value;
return part;
}

/// <summary>
Expand Down Expand Up @@ -153,7 +207,7 @@ public string SaveToString()
foreach (PartsArrayEnum partsArrayEnum in partsArrayEnums)
{
// Get the array value
var array = GetPartsArray(partsArrayEnum);
var array = GetPartsArray<BaseCardPartInfo>(partsArrayEnum);
if (array is null || array.Length == 0)
continue;

Expand Down Expand Up @@ -182,7 +236,7 @@ public string SaveToString()
foreach (PartsEnum partsEnum in partsEnums)
{
// Get the part value
var part = GetPart(partsEnum);
var part = GetPart<BaseCardPartInfo>(partsEnum);
if (part is null)
continue;

Expand Down

0 comments on commit 5d9b9bb

Please sign in to comment.