Skip to content

Commit e20e7ce

Browse files
ClaudeQuahu
andauthored
Deserialize activity party size as string array with on-demand parsing (#140)
* Initial plan * Deserialize party size as string array and convert on-demand with overflow handling Co-authored-by: Quahu <24848652+Quahu@users.noreply.github.com> * Rename ParseInt to ParseSizeInt and remove self-explanatory comments Co-authored-by: Quahu <24848652+Quahu@users.noreply.github.com> --------- Co-authored-by: anthropic-code-agent[bot] <242468646+Claude@users.noreply.github.com> Co-authored-by: Quahu <24848652+Quahu@users.noreply.github.com>
1 parent 3a5d045 commit e20e7ce

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/Disqord.Gateway.Api/Models/Activity/ActivityPartyJsonModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ public class ActivityPartyJsonModel : JsonModel
99
public Optional<string> Id;
1010

1111
[JsonProperty("size")]
12-
public Optional<int[]> Size;
12+
public Optional<string[]> Size;
1313
}
Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Disqord.Gateway.Api.Models;
1+
using System;
2+
using Disqord.Gateway.Api.Models;
23
using Qommon;
34

45
namespace Disqord.Gateway;
@@ -9,12 +10,42 @@ public class TransientRichActivityParty : TransientEntity<ActivityPartyJsonModel
910
public string? Id => Model.Id.GetValueOrDefault();
1011

1112
/// <inheritdoc/>
12-
public int? Size => Model.Size.GetValueOrDefault()?[0];
13+
public int? Size => ParseSizeInt(Model.Size.GetValueOrDefault(), 0);
1314

1415
/// <inheritdoc/>
15-
public int? MaximumSize => Model.Size.GetValueOrDefault()?[1];
16+
public int? MaximumSize => ParseSizeInt(Model.Size.GetValueOrDefault(), 1);
1617

1718
public TransientRichActivityParty(ActivityPartyJsonModel model)
1819
: base(model)
1920
{ }
21+
22+
private static int? ParseSizeInt(string[]? array, int index)
23+
{
24+
if (array == null || index >= array.Length)
25+
return null;
26+
27+
var stringValue = array[index];
28+
if (string.IsNullOrEmpty(stringValue))
29+
return null;
30+
31+
if (long.TryParse(stringValue, out var longValue))
32+
{
33+
if (longValue > int.MaxValue)
34+
return int.MaxValue;
35+
if (longValue < int.MinValue)
36+
return int.MinValue;
37+
return (int)longValue;
38+
}
39+
40+
if (double.TryParse(stringValue, out var doubleValue))
41+
{
42+
if (doubleValue > int.MaxValue)
43+
return int.MaxValue;
44+
if (doubleValue < int.MinValue)
45+
return int.MinValue;
46+
return (int)doubleValue;
47+
}
48+
49+
return null;
50+
}
2051
}

0 commit comments

Comments
 (0)