Skip to content

Commit 8eb0630

Browse files
committed
more tests and fixes
1 parent c331846 commit 8eb0630

29 files changed

+1965
-284
lines changed

Discord.Net.ComponentDesigner.sln.DotSettings.user

Lines changed: 12 additions & 78 deletions
Large diffs are not rendered by default.

Sandbox/Program.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,3 @@
33
using Sandbox.Examples.Spyfall;
44
using static Discord.ComponentDesigner;
55

6-
var pack = new Pack(
7-
Guid.NewGuid(),
8-
"Example",
9-
"Description",
10-
null,
11-
[
12-
new Location(
13-
Guid.NewGuid(),
14-
"Location1",
15-
null,
16-
[
17-
new Role(Guid.NewGuid(), "Role1", 100, null),
18-
new Role(Guid.NewGuid(), "Role2", 100, null),
19-
],
20-
100
21-
)
22-
],
23-
new(Guid.NewGuid(), "author", 123),
24-
DateTimeOffset.Now
25-
);
26-
27-
var x = PackExample.CreatePackInfo(pack, 0);
28-
29-
Console.WriteLine(x);

src/Discord.Net.ComponentDesigner.Generator/Constants.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public static class Constants
1313

1414
public const int MAX_ACTION_ROW_COMPONENTS = 5;
1515

16-
public const int SELECT_MIN_VALUES = 0;
17-
public const int SELECT_MAX_VALUES = 25;
16+
public const int STRING_SELECT_MIN_VALUES = 0;
17+
public const int STRING_SELECT_MAX_VALUES = 25;
1818

1919
public const int MAX_MEDIA_ITEMS = 10;
2020
public const int MAX_MEDIA_ITEM_DESCRIPTION_LENGTH = 1024;

src/Discord.Net.ComponentDesigner.Generator/Diagnostics.cs

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static Diagnostic CreateParsingDiagnostic(CXDiagnostic diagnostic, Locati
3939
public static readonly DiagnosticDescriptor OutOfRange = new(
4040
"DC0003",
4141
"Type mismatch",
42-
"'{0}' must be {1} in length",
42+
"'{0}' must be {1}",
4343
"Components",
4444
DiagnosticSeverity.Error,
4545
true
@@ -397,11 +397,92 @@ public static Diagnostic CreateParsingDiagnostic(CXDiagnostic diagnostic, Locati
397397
);
398398

399399
public static readonly DiagnosticDescriptor TooManyItemsInMediaGallery = new(
400-
"DC0043",
400+
"DC0044",
401401
"Too many items in media gallery",
402402
$"A media gallery can have at most {Constants.MAX_MEDIA_ITEMS} 'media-gallery-item's",
403403
"Components",
404404
DiagnosticSeverity.Error,
405405
true
406406
);
407+
408+
public static readonly DiagnosticDescriptor InvalidRange = new(
409+
"DC0045",
410+
"Invalid range",
411+
"'{0}' must be less than or equal to '{1}'",
412+
"Components",
413+
DiagnosticSeverity.Error,
414+
true
415+
);
416+
417+
public static readonly DiagnosticDescriptor InvalidSelectMenuDefaultKind = new(
418+
"DC0046",
419+
"Invalid select menu default kind",
420+
"'{0}' is not a valid default kind, valid kinds are: 'user', 'role', and 'channel'",
421+
"Components",
422+
DiagnosticSeverity.Error,
423+
true
424+
);
425+
426+
public static readonly DiagnosticDescriptor MissingSelectMenuDefaultValue = new(
427+
"DC0047",
428+
"Missing value for default option",
429+
"A value is required for a default select menu option",
430+
"Components",
431+
DiagnosticSeverity.Error,
432+
true
433+
);
434+
435+
public static readonly DiagnosticDescriptor TooManyValuesInSelectMenuDefault = new(
436+
"DC0048",
437+
"Too many values in default option",
438+
"At most 1 value is allowed for a select menu default option",
439+
"Components",
440+
DiagnosticSeverity.Error,
441+
true
442+
);
443+
444+
public static readonly DiagnosticDescriptor InvalidSelectMenuDefaultChild = new(
445+
"DC0049",
446+
"Invalid child of select menu default option",
447+
"'{0}' is not a valid value, expected a scalar or interpolation",
448+
"Components",
449+
DiagnosticSeverity.Error,
450+
true
451+
);
452+
453+
public static readonly DiagnosticDescriptor InvalidSelectMenuDefaultKindInCurrentMenu = new(
454+
"DC0050",
455+
"Invalid default value kind",
456+
"'{0}' is not a valid default kind for the menu '{1}'",
457+
"Components",
458+
DiagnosticSeverity.Error,
459+
true
460+
);
461+
462+
public static readonly DiagnosticDescriptor EmptyStringSelectMenu = new(
463+
"DC0051",
464+
"A string select menu requires at least one option",
465+
"A string select menu requires at least one option",
466+
"Components",
467+
DiagnosticSeverity.Error,
468+
true
469+
);
470+
471+
public static readonly DiagnosticDescriptor InvalidStringSelectChild = new(
472+
"DC0052",
473+
"Invalid child of string select menu",
474+
"'{0}' is not a valid child of a string select menu; valid children are: 'select-menu-option' or <interpolation>",
475+
"Components",
476+
DiagnosticSeverity.Error,
477+
true
478+
);
479+
480+
public static readonly DiagnosticDescriptor TooManyStringSelectMenuChildren = new(
481+
"DC0053",
482+
"Too many string select children",
483+
$"A string select menu must contain at most {Constants.STRING_SELECT_MAX_VALUES} options",
484+
"Components",
485+
DiagnosticSeverity.Error,
486+
true
487+
);
407488
}

src/Discord.Net.ComponentDesigner.Generator/Nodes/ComponentProperty.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public sealed class ComponentProperty
2020

2121
public bool IsOptional { get; }
2222
public bool RequiresValue { get; }
23+
public bool Synthetic { get; }
2324
public string DotnetPropertyName { get; }
2425
public string DotnetParameterName { get; }
2526
public PropertyRenderer Renderer { get; }
@@ -34,13 +35,15 @@ public ComponentProperty(
3435
IEnumerable<PropertyValidator>? validators = null,
3536
PropertyRenderer? renderer = null,
3637
string? dotnetParameterName = null,
37-
string? dotnetPropertyName = null
38+
string? dotnetPropertyName = null,
39+
bool synthetic = false
3840
)
3941
{
4042
Name = name;
4143
Aliases = [..aliases ?? []];
4244
IsOptional = isOptional;
4345
RequiresValue = requiresValue;
46+
Synthetic = synthetic;
4447
DotnetPropertyName = dotnetPropertyName ?? name;
4548
DotnetParameterName = dotnetParameterName ?? name;
4649
Renderer = renderer ?? Renderers.CreateDefault(this);

src/Discord.Net.ComponentDesigner.Generator/Nodes/ComponentPropertyValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public CXValue? Value
2222

2323
public bool HasValue => Value is not null;
2424

25-
public bool CanOmitFromSource => Property.IsOptional && !IsSpecified;
25+
public bool CanOmitFromSource => Property.Synthetic || ( Property.IsOptional && !IsSpecified);
2626

2727
public bool TryGetLiteralValue(out string value)
2828
{

src/Discord.Net.ComponentDesigner.Generator/Nodes/Components/ButtonComponentNode.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public ButtonComponentNode()
5252
Label = new ComponentProperty(
5353
"label",
5454
isOptional: true,
55-
validators: [Validators.Range(upper: Constants.BUTTON_MAX_LABEL_LENGTH)],
55+
validators: [Validators.StringRange(upper: Constants.BUTTON_MAX_LABEL_LENGTH)],
5656
renderer: Renderers.String
5757
),
5858
Emoji = new ComponentProperty(
@@ -65,7 +65,7 @@ public ButtonComponentNode()
6565
CustomId = new(
6666
"customId",
6767
isOptional: true,
68-
validators: [Validators.Range(upper: Constants.CUSTOM_ID_MAX_LENGTH)],
68+
validators: [Validators.StringRange(upper: Constants.CUSTOM_ID_MAX_LENGTH)],
6969
renderer: Renderers.String
7070
),
7171
SkuId = new(
@@ -78,7 +78,7 @@ public ButtonComponentNode()
7878
Url = new(
7979
"url",
8080
isOptional: true,
81-
validators: [Validators.Range(upper: Constants.BUTTON_URL_MAX_LENGTH)],
81+
validators: [Validators.StringRange(upper: Constants.BUTTON_URL_MAX_LENGTH)],
8282
renderer: Renderers.String
8383
),
8484
Disabled = new(

src/Discord.Net.ComponentDesigner.Generator/Nodes/Components/FileUploadComponentNode.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public FileUploadComponentNode()
1818
new(
1919
"customId",
2020
renderer: Renderers.String,
21-
validators: [Validators.Range(upper: Constants.CUSTOM_ID_MAX_LENGTH)]
21+
validators: [Validators.StringRange(upper: Constants.CUSTOM_ID_MAX_LENGTH)]
2222
),
2323
new(
2424
"min",
2525
isOptional: true,
2626
aliases: ["minValues"],
2727
renderer: Renderers.Integer,
2828
validators: [
29-
Validators.Range(Constants.FILE_UPLOAD_MIN_VALUES_LOWER, Constants.FILE_UPLOAD_MIN_VALUES_UPPER)
29+
Validators.IntRange(Constants.FILE_UPLOAD_MIN_VALUES_LOWER, Constants.FILE_UPLOAD_MIN_VALUES_UPPER)
3030
]
3131
),
3232
new(
@@ -35,7 +35,7 @@ public FileUploadComponentNode()
3535
aliases: ["maxValues"],
3636
renderer: Renderers.Integer,
3737
validators: [
38-
Validators.Range(Constants.FILE_UPLOAD_MAX_VALUES_LOWER, Constants.FILE_UPLOAD_MAX_VALUES_UPPER)
38+
Validators.IntRange(Constants.FILE_UPLOAD_MAX_VALUES_LOWER, Constants.FILE_UPLOAD_MAX_VALUES_UPPER)
3939
]
4040
),
4141
new(

0 commit comments

Comments
 (0)