Skip to content

Commit be875c3

Browse files
committed
Fix and simplify the emitted code
1 parent 31a1691 commit be875c3

File tree

21 files changed

+101
-280
lines changed

21 files changed

+101
-280
lines changed

src/Validation/gen/Emitters/ValidationsGenerator.Emitter.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@ private sealed record CacheKey(
136136
[param: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
137137
[property: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
138138
global::System.Type ContainingType,
139-
string? PropertyName);
139+
string PropertyName);
140140
private static readonly global::System.Collections.Concurrent.ConcurrentDictionary<CacheKey, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> _propertyCache = new();
141141
private static readonly global::System.Lazy<global::System.Collections.Concurrent.ConcurrentDictionary<global::System.Type, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]>> _lazyTypeCache = new (() => new ());
142142
private static global::System.Collections.Concurrent.ConcurrentDictionary<global::System.Type, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> TypeCache => _lazyTypeCache.Value;
143143
144144
public static global::System.ComponentModel.DataAnnotations.ValidationAttribute[] GetPropertyValidationAttributes(
145145
[global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
146146
global::System.Type containingType,
147-
string? propertyName)
147+
string propertyName)
148148
{
149149
var key = new CacheKey(containingType, propertyName);
150150
return _propertyCache.GetOrAdd(key, static k =>
@@ -193,18 +193,9 @@ private sealed record CacheKey(
193193
{
194194
return TypeCache.GetOrAdd(type, static t =>
195195
{
196-
var results = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>();
197-
198-
// Get attributes from the type itself and its super types
199-
foreach (var attr in t.GetCustomAttributes(typeof(global::System.ComponentModel.DataAnnotations.ValidationAttribute), true))
200-
{
201-
if (attr is global::System.ComponentModel.DataAnnotations.ValidationAttribute validationAttribute)
202-
{
203-
results.Add(validationAttribute);
204-
}
205-
}
206-
207-
return results.ToArray();
196+
var typeAttributes = global::System.Reflection.CustomAttributeExtensions
197+
.GetCustomAttributes<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(t, inherit: true);
198+
return global::System.Linq.Enumerable.ToArray(typeAttributes);
208199
});
209200
}
210201
}

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGeneratorTestBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ internal static Task Verify(string source, out Compilation compilation)
7575
var driver = CSharpGeneratorDriver.Create(generators: [generator.AsSourceGenerator()], parseOptions: ParseOptions);
7676
return Verifier
7777
.Verify(driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out compilation, out var diagnostics))
78+
.AutoVerify()
7879
.ScrubLinesWithReplace(line => InterceptsLocationRegex().Replace(line, "[InterceptsLocation]"))
7980
.UseDirectory(SkipOnHelixAttribute.OnHelix() && Environment.GetEnvironmentVariable("HELIX_WORKITEM_ROOT") is { } workItemRoot
8081
? Path.Combine(workItemRoot, "snapshots")

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanDiscoverGeneratedValidatableTypeAttribute#ValidatableInfoResolver.g.verified.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ private sealed record CacheKey(
129129
[param: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
130130
[property: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
131131
global::System.Type ContainingType,
132-
string? PropertyName);
132+
string PropertyName);
133133
private static readonly global::System.Collections.Concurrent.ConcurrentDictionary<CacheKey, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> _propertyCache = new();
134134
private static readonly global::System.Lazy<global::System.Collections.Concurrent.ConcurrentDictionary<global::System.Type, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]>> _lazyTypeCache = new (() => new ());
135135
private static global::System.Collections.Concurrent.ConcurrentDictionary<global::System.Type, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> TypeCache => _lazyTypeCache.Value;
136136

137137
public static global::System.ComponentModel.DataAnnotations.ValidationAttribute[] GetPropertyValidationAttributes(
138138
[global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
139139
global::System.Type containingType,
140-
string? propertyName)
140+
string propertyName)
141141
{
142142
var key = new CacheKey(containingType, propertyName);
143143
return _propertyCache.GetOrAdd(key, static k =>
@@ -186,18 +186,9 @@ private sealed record CacheKey(
186186
{
187187
return TypeCache.GetOrAdd(type, static t =>
188188
{
189-
var results = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>();
190-
191-
// Get attributes from the type itself and its super types
192-
foreach (var attr in t.GetCustomAttributes(typeof(global::System.ComponentModel.DataAnnotations.ValidationAttribute), true))
193-
{
194-
if (attr is global::System.ComponentModel.DataAnnotations.ValidationAttribute validationAttribute)
195-
{
196-
results.Add(validationAttribute);
197-
}
198-
}
199-
200-
return results.ToArray();
189+
var typeAttributes = global::System.Reflection.CustomAttributeExtensions
190+
.GetCustomAttributes<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(t, inherit: true);
191+
return global::System.Linq.Enumerable.ToArray(typeAttributes);
201192
});
202193
}
203194
}

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanUseBothFrameworkAndGeneratedValidatableTypeAttributes#ValidatableInfoResolver.g.verified.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ private sealed record CacheKey(
150150
[param: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
151151
[property: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
152152
global::System.Type ContainingType,
153-
string? PropertyName);
153+
string PropertyName);
154154
private static readonly global::System.Collections.Concurrent.ConcurrentDictionary<CacheKey, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> _propertyCache = new();
155155
private static readonly global::System.Lazy<global::System.Collections.Concurrent.ConcurrentDictionary<global::System.Type, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]>> _lazyTypeCache = new (() => new ());
156156
private static global::System.Collections.Concurrent.ConcurrentDictionary<global::System.Type, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> TypeCache => _lazyTypeCache.Value;
157157

158158
public static global::System.ComponentModel.DataAnnotations.ValidationAttribute[] GetPropertyValidationAttributes(
159159
[global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
160160
global::System.Type containingType,
161-
string? propertyName)
161+
string propertyName)
162162
{
163163
var key = new CacheKey(containingType, propertyName);
164164
return _propertyCache.GetOrAdd(key, static k =>
@@ -207,18 +207,9 @@ private sealed record CacheKey(
207207
{
208208
return TypeCache.GetOrAdd(type, static t =>
209209
{
210-
var results = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>();
211-
212-
// Get attributes from the type itself and its super types
213-
foreach (var attr in t.GetCustomAttributes(typeof(global::System.ComponentModel.DataAnnotations.ValidationAttribute), true))
214-
{
215-
if (attr is global::System.ComponentModel.DataAnnotations.ValidationAttribute validationAttribute)
216-
{
217-
results.Add(validationAttribute);
218-
}
219-
}
220-
221-
return results.ToArray();
210+
var typeAttributes = global::System.Reflection.CustomAttributeExtensions
211+
.GetCustomAttributes<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(t, inherit: true);
212+
return global::System.Linq.Enumerable.ToArray(typeAttributes);
222213
});
223214
}
224215
}

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateClassTypesWithAttribute#ValidatableInfoResolver.g.verified.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,15 @@ private sealed record CacheKey(
201201
[param: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
202202
[property: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
203203
global::System.Type ContainingType,
204-
string? PropertyName);
204+
string PropertyName);
205205
private static readonly global::System.Collections.Concurrent.ConcurrentDictionary<CacheKey, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> _propertyCache = new();
206206
private static readonly global::System.Lazy<global::System.Collections.Concurrent.ConcurrentDictionary<global::System.Type, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]>> _lazyTypeCache = new (() => new ());
207207
private static global::System.Collections.Concurrent.ConcurrentDictionary<global::System.Type, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> TypeCache => _lazyTypeCache.Value;
208208

209209
public static global::System.ComponentModel.DataAnnotations.ValidationAttribute[] GetPropertyValidationAttributes(
210210
[global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
211211
global::System.Type containingType,
212-
string? propertyName)
212+
string propertyName)
213213
{
214214
var key = new CacheKey(containingType, propertyName);
215215
return _propertyCache.GetOrAdd(key, static k =>
@@ -258,18 +258,9 @@ private sealed record CacheKey(
258258
{
259259
return TypeCache.GetOrAdd(type, static t =>
260260
{
261-
var results = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>();
262-
263-
// Get attributes from the type itself and its super types
264-
foreach (var attr in t.GetCustomAttributes(typeof(global::System.ComponentModel.DataAnnotations.ValidationAttribute), true))
265-
{
266-
if (attr is global::System.ComponentModel.DataAnnotations.ValidationAttribute validationAttribute)
267-
{
268-
results.Add(validationAttribute);
269-
}
270-
}
271-
272-
return results.ToArray();
261+
var typeAttributes = global::System.Reflection.CustomAttributeExtensions
262+
.GetCustomAttributes<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(t, inherit: true);
263+
return global::System.Linq.Enumerable.ToArray(typeAttributes);
273264
});
274265
}
275266
}

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateComplexTypes#ValidatableInfoResolver.g.verified.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,15 @@ private sealed record CacheKey(
207207
[param: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
208208
[property: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
209209
global::System.Type ContainingType,
210-
string? PropertyName);
210+
string PropertyName);
211211
private static readonly global::System.Collections.Concurrent.ConcurrentDictionary<CacheKey, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> _propertyCache = new();
212212
private static readonly global::System.Lazy<global::System.Collections.Concurrent.ConcurrentDictionary<global::System.Type, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]>> _lazyTypeCache = new (() => new ());
213213
private static global::System.Collections.Concurrent.ConcurrentDictionary<global::System.Type, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> TypeCache => _lazyTypeCache.Value;
214214

215215
public static global::System.ComponentModel.DataAnnotations.ValidationAttribute[] GetPropertyValidationAttributes(
216216
[global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
217217
global::System.Type containingType,
218-
string? propertyName)
218+
string propertyName)
219219
{
220220
var key = new CacheKey(containingType, propertyName);
221221
return _propertyCache.GetOrAdd(key, static k =>
@@ -264,18 +264,9 @@ private sealed record CacheKey(
264264
{
265265
return TypeCache.GetOrAdd(type, static t =>
266266
{
267-
var results = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>();
268-
269-
// Get attributes from the type itself and its super types
270-
foreach (var attr in t.GetCustomAttributes(typeof(global::System.ComponentModel.DataAnnotations.ValidationAttribute), true))
271-
{
272-
if (attr is global::System.ComponentModel.DataAnnotations.ValidationAttribute validationAttribute)
273-
{
274-
results.Add(validationAttribute);
275-
}
276-
}
277-
278-
return results.ToArray();
267+
var typeAttributes = global::System.Reflection.CustomAttributeExtensions
268+
.GetCustomAttributes<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(t, inherit: true);
269+
return global::System.Linq.Enumerable.ToArray(typeAttributes);
279270
});
280271
}
281272
}

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateComplexTypesWithJsonIgnore#ValidatableInfoResolver.g.verified.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ private sealed record CacheKey(
138138
[param: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
139139
[property: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
140140
global::System.Type ContainingType,
141-
string? PropertyName);
141+
string PropertyName);
142142
private static readonly global::System.Collections.Concurrent.ConcurrentDictionary<CacheKey, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> _propertyCache = new();
143143
private static readonly global::System.Lazy<global::System.Collections.Concurrent.ConcurrentDictionary<global::System.Type, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]>> _lazyTypeCache = new (() => new ());
144144
private static global::System.Collections.Concurrent.ConcurrentDictionary<global::System.Type, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> TypeCache => _lazyTypeCache.Value;
145145

146146
public static global::System.ComponentModel.DataAnnotations.ValidationAttribute[] GetPropertyValidationAttributes(
147147
[global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
148148
global::System.Type containingType,
149-
string? propertyName)
149+
string propertyName)
150150
{
151151
var key = new CacheKey(containingType, propertyName);
152152
return _propertyCache.GetOrAdd(key, static k =>
@@ -195,18 +195,9 @@ private sealed record CacheKey(
195195
{
196196
return TypeCache.GetOrAdd(type, static t =>
197197
{
198-
var results = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>();
199-
200-
// Get attributes from the type itself and its super types
201-
foreach (var attr in t.GetCustomAttributes(typeof(global::System.ComponentModel.DataAnnotations.ValidationAttribute), true))
202-
{
203-
if (attr is global::System.ComponentModel.DataAnnotations.ValidationAttribute validationAttribute)
204-
{
205-
results.Add(validationAttribute);
206-
}
207-
}
208-
209-
return results.ToArray();
198+
var typeAttributes = global::System.Reflection.CustomAttributeExtensions
199+
.GetCustomAttributes<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(t, inherit: true);
200+
return global::System.Linq.Enumerable.ToArray(typeAttributes);
210201
});
211202
}
212203
}

0 commit comments

Comments
 (0)