Skip to content

Commit

Permalink
ref - Moved AltID handler
Browse files Browse the repository at this point in the history
---

Type: ref
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Oct 3, 2024
1 parent af6d14d commit 30a26f8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 37 deletions.
42 changes: 42 additions & 0 deletions VisualCard/Parsers/VcardCommonTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -732,5 +732,47 @@ internal static string ConstructBlocks((int, string)[] cardContent, ref int i)
i = idx;
return valueBuilder.ToString();
}

internal static int GetAltIdFromArgs(Version version, ArgumentInfo[] arguments, Type? classType, PartType type, object enumeration)
{
int altId = -1;
if (arguments.Length > 0)
{
// If we have more than one argument, check for ALTID
if (version.Major >= 4)
{
var cardinality =
type == PartType.Strings ? VcardParserTools.GetStringsEnumFromType((StringsEnum)enumeration, version) :
VcardParserTools.GetPartsArrayEnumFromType(classType, version).Item2;
bool supportsAltId =
cardinality != PartCardinality.MayBeOneNoAltId && cardinality != PartCardinality.ShouldBeOneNoAltId &&
cardinality != PartCardinality.AtLeastOneNoAltId && cardinality != PartCardinality.AnyNoAltId;
var altIdArg = arguments.SingleOrDefault((arg) => arg.Key == VcardConstants._altIdArgumentSpecifier);
if (supportsAltId)
{
// The type supports ALTID.
if (arguments[0].Key == VcardConstants._altIdArgumentSpecifier)
{
// We need ALTID to be numeric
if (!int.TryParse(altIdArg.Values[0].value, out altId))
throw new InvalidDataException("ALTID must be numeric");

// We need ALTID to be positive
if (altId < 0)
throw new InvalidDataException("ALTID must be positive");

// Here, we require arguments for ALTID
if (arguments.Length <= 1)
throw new InvalidDataException("ALTID must have one or more arguments to specify why this instance is an alternative");
}
else if (altIdArg is not null)
throw new InvalidDataException("ALTID must be exactly in the first position of the argument, because arguments that follow it are required to be specified");
}
else if (altIdArg is not null)
throw new InvalidDataException($"ALTID must not be specified in the {enumeration} type that expects a cardinality of {cardinality}.");
}
}
return altId;
}
}
}
39 changes: 2 additions & 37 deletions VisualCard/Parsers/VcardParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,43 +92,8 @@ public Card Parse()
var info = new PropertyInfo(_value);
var (type, enumeration, classType, fromString, defaultType, defaultValue, defaultValueType, extraAllowedTypes, allowedValues) = VcardParserTools.GetPartType(info.Prefix);

// Handle arguments
int altId = -1;
if (info.Arguments.Length > 0)
{
// If we have more than one argument, check for ALTID
if (CardVersion.Major >= 4 && type == PartType.PartsArray)
{
var tuple = VcardParserTools.GetPartsArrayEnumFromType(classType, CardVersion);
var cardinality = tuple.Item2;
bool supportsAltId =
cardinality != PartCardinality.MayBeOneNoAltId && cardinality != PartCardinality.ShouldBeOneNoAltId &&
cardinality != PartCardinality.AtLeastOneNoAltId && cardinality != PartCardinality.AnyNoAltId;
var altIdArg = info.Arguments.SingleOrDefault((arg) => arg.Key == VcardConstants._altIdArgumentSpecifier);
if (supportsAltId)
{
// The type supports ALTID.
if (info.Arguments[0].Key == VcardConstants._altIdArgumentSpecifier)
{
// We need ALTID to be numeric
if (!int.TryParse(altIdArg.Values[0].value, out altId))
throw new InvalidDataException("ALTID must be numeric");

// We need ALTID to be positive
if (altId < 0)
throw new InvalidDataException("ALTID must be positive");

// Here, we require arguments for ALTID
if (info.Arguments.Length <= 1)
throw new InvalidDataException("ALTID must have one or more arguments to specify why this instance is an alternative");
}
else if (altIdArg is not null)
throw new InvalidDataException("ALTID must be exactly in the first position of the argument, because arguments that follow it are required to be specified");
}
else if (altIdArg is not null)
throw new InvalidDataException($"ALTID must not be specified in the {tuple.Item1} type that expects a cardinality of {cardinality}.");
}
}
// Handle AltID
int altId = VcardCommonTools.GetAltIdFromArgs(CardVersion, info.Arguments, classType, type, enumeration);

// Check the type for allowed types
bool specifierRequired = CardVersion.Major >= 3;
Expand Down
8 changes: 8 additions & 0 deletions VisualCard/Parsers/VcardParserTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ internal static PartCardinality GetStringsEnumFromType(StringsEnum strings, Vers
// Now, iterate through every type
return strings switch
{
StringsEnum.AccessClassification => PartCardinality.MayBeOneNoAltId,
StringsEnum.Kind => PartCardinality.MayBeOneNoAltId,
StringsEnum.Mailer => PartCardinality.MayBeOneNoAltId,
StringsEnum.ProductId => PartCardinality.MayBeOneNoAltId,
StringsEnum.SortString => PartCardinality.MayBeOneNoAltId,
StringsEnum.Uid => PartCardinality.MayBeOneNoAltId,
StringsEnum.SourceName => PartCardinality.Any,
StringsEnum.Profile => PartCardinality.MayBeOneNoAltId,
StringsEnum.Telephones => PartCardinality.Any,
StringsEnum.Labels => PartCardinality.Any,
StringsEnum.Mails => PartCardinality.Any,
Expand Down

0 comments on commit 30a26f8

Please sign in to comment.