Skip to content

Commit

Permalink
imp - Check for cardinality in strings and ints
Browse files Browse the repository at this point in the history
---

For adding strings and/or integers, we need to check for cardinality.

---

Type: imp
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Oct 2, 2024
1 parent da1888c commit 2a6a528
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions VisualCard.Calendar/Parts/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,16 @@ internal virtual void AddString(CalendarStringsEnum key, CalendarValueInfo<strin
if (!strings.ContainsKey(key))
strings.Add(key, [value]);
else
{
// We need to check the cardinality.
var cardinality = VCalendarParserTools.GetStringsEnumFromType(key);
bool onlyOne =
cardinality == PartCardinality.ShouldBeOne ||
cardinality == PartCardinality.MayBeOne;
if (onlyOne)
throw new InvalidOperationException($"Can't add part array {key}, because cardinality is {cardinality}.");
strings[key].Add(value);
}
}

internal virtual void AddInteger(CalendarIntegersEnum key, CalendarValueInfo<double> value) =>
Expand All @@ -550,7 +559,16 @@ internal virtual void AddInteger(CalendarIntegersEnum key, CalendarValueInfo<dou
if (!integers.ContainsKey(key))
integers.Add(key, [value]);
else
{
// We need to check the cardinality.
var cardinality = VCalendarParserTools.GetIntegersEnumFromType(key);
bool onlyOne =
cardinality == PartCardinality.ShouldBeOne ||
cardinality == PartCardinality.MayBeOne;
if (onlyOne)
throw new InvalidOperationException($"Can't add part array {key}, because cardinality is {cardinality}.");
integers[key].Add(value);
}
}

internal Calendar(Version version) =>
Expand Down
15 changes: 15 additions & 0 deletions VisualCard/Parts/Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,22 @@ internal void AddString(StringsEnum key, CardValueInfo<string> value)
if (!strings.ContainsKey(key))
strings.Add(key, [value]);
else
{
// We need to check the cardinality.
var cardinality = VcardParserTools.GetStringsEnumFromType(key, CardVersion);
int actualAltId = strings[key][0].AltId;
bool onlyOne =
cardinality == PartCardinality.ShouldBeOne ||
cardinality == PartCardinality.MayBeOne;
bool onlyOneNoAltId =
cardinality == PartCardinality.ShouldBeOneNoAltId ||
cardinality == PartCardinality.MayBeOneNoAltId;
if (onlyOne && actualAltId != value.AltId)
throw new InvalidOperationException($"Can't overwrite string {key} with AltID {value.AltId}, because cardinality is {cardinality} and expected AltID is {actualAltId}.");
if (onlyOneNoAltId)
throw new InvalidOperationException($"Can never overwrite string {key} with AltID {value.AltId}, because cardinality is {cardinality}, even though the expected AltID is {actualAltId}.");
strings[key].Add(value);
}
}

internal Card(Version version) =>
Expand Down

0 comments on commit 2a6a528

Please sign in to comment.