Skip to content

Commit 460cca1

Browse files
committed
Address feedback
1 parent 444125d commit 460cca1

File tree

3 files changed

+49
-53
lines changed

3 files changed

+49
-53
lines changed

src/Validation/gen/Parsers/ValidationsGenerator.AttributeParser.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
3-
4-
using System;
53
using System.Collections.Generic;
64
using System.Collections.Immutable;
75
using System.Threading;
@@ -13,12 +11,12 @@ namespace Microsoft.Extensions.Validation;
1311

1412
public sealed partial class ValidationsGenerator : IIncrementalGenerator
1513
{
16-
internal static bool ShouldTransformSymbolWithAttribute(SyntaxNode syntaxNode, CancellationToken _)
14+
internal static bool ShouldTransformSymbolWithAttribute(SyntaxNode syntaxNode, CancellationToken cancellationToken)
1715
{
1816
return syntaxNode is ClassDeclarationSyntax or RecordDeclarationSyntax;
1917
}
2018

21-
internal ImmutableArray<ValidatableType> TransformValidatableTypeWithAttribute(GeneratorAttributeSyntaxContext context, CancellationToken _)
19+
internal ImmutableArray<ValidatableType> TransformValidatableTypeWithAttribute(GeneratorAttributeSyntaxContext context, CancellationToken cancellationToken)
2220
{
2321
var validatableTypes = new HashSet<ValidatableType>(ValidatableTypeComparer.Instance);
2422
List<ITypeSymbol> visitedTypes = [];

src/Validation/gen/ValidationsGenerator.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3535
);
3636

3737
// Combine both sources of validatable types
38-
var validatableTypesWithAttribute = frameworkValidatableTypes
39-
.Collect()
40-
.Combine(generatedValidatableTypes.Collect())
41-
.SelectMany((pair, _) => pair.Left.Concat(pair.Right).ToImmutableArray());
38+
var validatableTypesWithAttribute = frameworkValidatableTypes.Concat(generatedValidatableTypes);
4239

4340
// Extract all minimal API endpoints in the application.
4441
var endpoints = context.SyntaxProvider
@@ -52,8 +49,29 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
5249
.Select(ExtractValidatableEndpoint);
5350

5451
// Join all validatable types encountered in the type graph.
55-
var validatableTypes = validatableTypesWithAttribute
56-
.Concat(validatableTypesFromEndpoints)
52+
var allValidatableTypesProviders = validatableTypesWithAttribute
53+
.Collect()
54+
.Combine(validatableTypesFromEndpoints.Collect())
55+
.SelectMany(static (tuple, _) =>
56+
{
57+
var results = ImmutableArray.CreateBuilder<ValidatableType>();
58+
59+
// Add from attribute-based sources
60+
foreach (var array in tuple.Left)
61+
{
62+
results.AddRange(array);
63+
}
64+
65+
// Add from endpoint sources
66+
foreach (var array in tuple.Right)
67+
{
68+
results.AddRange(array);
69+
}
70+
71+
return results.DrainToImmutable();
72+
});
73+
74+
var validatableTypes = allValidatableTypesProviders
5775
.Distinct(ValidatableTypeComparer.Instance)
5876
.Collect();
5977

@@ -62,6 +80,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
6280

6381
// Emit the IValidatableInfo resolver injection and
6482
// ValidatableTypeInfo for all validatable types.
65-
context.RegisterSourceOutput(emitInputs, Emit);
83+
context.RegisterSourceOutput(emitInputs, (context, emitInputs) =>
84+
Emit(context, (emitInputs.Left, emitInputs.Right)));
6685
}
6786
}

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.AutoGeneratedAttribute.cs

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ namespace Microsoft.Extensions.Validation.GeneratorTests;
1010
[UsesVerify]
1111
public partial class ValidationsGeneratorTests : ValidationsGeneratorTestBase
1212
{
13+
private const string GeneratedAttributeSource = """
14+
// <auto-generated/>
15+
namespace Microsoft.CodeAnalysis
16+
{
17+
[global::System.AttributeUsage(global::System.AttributeTargets.All, AllowMultiple = true, Inherited = false)]
18+
internal sealed class EmbeddedAttribute : global::System.Attribute
19+
{
20+
}
21+
}
22+
23+
namespace Microsoft.Extensions.Validation.Embedded;
24+
25+
[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
26+
[global::System.AttributeUsage(global::System.AttributeTargets.Class)]
27+
internal sealed class ValidatableTypeAttribute : global::System.Attribute
28+
{
29+
}
30+
""";
31+
1332
[Fact]
1433
public async Task CanDiscoverGeneratedValidatableTypeAttribute()
1534
{
@@ -38,28 +57,8 @@ public class Customer
3857
app.Run();
3958
""";
4059

41-
// Simulate the Razor SDK generating the attribute
42-
var generatedAttributeSource = """
43-
// <auto-generated/>
44-
namespace Microsoft.CodeAnalysis
45-
{
46-
[global::System.AttributeUsage(global::System.AttributeTargets.All, AllowMultiple = true, Inherited = false)]
47-
internal sealed class EmbeddedAttribute : global::System.Attribute
48-
{
49-
}
50-
}
51-
52-
namespace Microsoft.Extensions.Validation.Embedded;
53-
54-
[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
55-
[global::System.AttributeUsage(global::System.AttributeTargets.Class)]
56-
internal sealed class ValidatableTypeAttribute : global::System.Attribute
57-
{
58-
}
59-
""";
60-
6160
// Combine the generated attribute with the user's source
62-
var combinedSource = generatedAttributeSource + "\n" + source;
61+
var combinedSource = GeneratedAttributeSource + "\n" + source;
6362

6463
await Verify(combinedSource, out var compilation);
6564
}
@@ -106,28 +105,8 @@ public class Product
106105
app.Run();
107106
""";
108107

109-
// Simulate the Razor SDK generating the attribute
110-
var generatedAttributeSource = """
111-
// <auto-generated/>
112-
namespace Microsoft.CodeAnalysis
113-
{
114-
[global::System.AttributeUsage(global::System.AttributeTargets.All, AllowMultiple = true, Inherited = false)]
115-
internal sealed class EmbeddedAttribute : global::System.Attribute
116-
{
117-
}
118-
}
119-
120-
namespace Microsoft.Extensions.Validation.Embedded;
121-
122-
[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
123-
[global::System.AttributeUsage(global::System.AttributeTargets.Class)]
124-
internal sealed class ValidatableTypeAttribute : global::System.Attribute
125-
{
126-
}
127-
""";
128-
129108
// Combine the generated attribute with the user's source
130-
var combinedSource = generatedAttributeSource + "\n" + source;
109+
var combinedSource = GeneratedAttributeSource + "\n" + source;
131110

132111
await Verify(combinedSource, out var compilation);
133112
}

0 commit comments

Comments
 (0)