/
Emitter.cs
917 lines (797 loc) · 42.3 KB
/
Emitter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.DotnetRuntime.Extensions;
namespace Microsoft.Extensions.Options.Generators
{
/// <summary>
/// Emits option validation.
/// </summary>
internal sealed class Emitter : EmitterBase
{
private const string StaticFieldHolderClassesNamespace = "__OptionValidationStaticInstances";
internal const string StaticGeneratedValidationAttributesClassesNamespace = "__OptionValidationGeneratedAttributes";
internal const string StaticAttributeClassNamePrefix = "__SourceGen_";
internal const string StaticGeneratedMaxLengthAttributeClassesName = "__SourceGen_MaxLengthAttribute";
private const string StaticListType = "global::System.Collections.Generic.List";
private const string StaticValidationResultType = "global::System.ComponentModel.DataAnnotations.ValidationResult";
private const string StaticValidationAttributeType = "global::System.ComponentModel.DataAnnotations.ValidationAttribute";
private const string StaticValidationContextType = "global::System.ComponentModel.DataAnnotations.ValidationContext";
private string _staticValidationAttributeHolderClassName = "__Attributes";
private string _staticValidatorHolderClassName = "__Validators";
private string _staticValidationAttributeHolderClassFQN;
private string _staticValidatorHolderClassFQN;
private string _TryGetValueNullableAnnotation;
private readonly SymbolHolder _symbolHolder;
private readonly OptionsSourceGenContext _optionsSourceGenContext;
private sealed record StaticFieldInfo(string FieldTypeFQN, int FieldOrder, string FieldName, IList<string> InstantiationLines);
public Emitter(Compilation compilation, SymbolHolder symbolHolder, OptionsSourceGenContext optionsSourceGenContext, bool emitPreamble = true) : base(emitPreamble)
{
_optionsSourceGenContext = optionsSourceGenContext;
if (!_optionsSourceGenContext.IsLangVersion11AndAbove)
{
_staticValidationAttributeHolderClassName += _optionsSourceGenContext.Suffix;
_staticValidatorHolderClassName += _optionsSourceGenContext.Suffix;
}
_staticValidationAttributeHolderClassFQN = $"global::{StaticFieldHolderClassesNamespace}.{_staticValidationAttributeHolderClassName}";
_staticValidatorHolderClassFQN = $"global::{StaticFieldHolderClassesNamespace}.{_staticValidatorHolderClassName}";
_TryGetValueNullableAnnotation = GetNullableAnnotationStringForTryValidateValueToUseInGeneratedCode(compilation);
_symbolHolder = symbolHolder;
}
public string Emit(
IEnumerable<ValidatorType> validatorTypes,
CancellationToken cancellationToken)
{
var staticValidationAttributesDict = new Dictionary<string, StaticFieldInfo>();
var staticValidatorsDict = new Dictionary<string, StaticFieldInfo>();
foreach (var vt in validatorTypes.OrderBy(static lt => lt.Namespace + "." + lt.Name))
{
cancellationToken.ThrowIfCancellationRequested();
GenValidatorType(vt, ref staticValidationAttributesDict, ref staticValidatorsDict);
}
GenStaticClassWithStaticReadonlyFields(staticValidationAttributesDict.Values, StaticFieldHolderClassesNamespace, _staticValidationAttributeHolderClassName);
GenStaticClassWithStaticReadonlyFields(staticValidatorsDict.Values, StaticFieldHolderClassesNamespace, _staticValidatorHolderClassName);
GenValidationAttributesClasses();
return Capture();
}
/// <summary>
/// Returns the nullable annotation string to use in the code generation according to the first parameter of
/// <see cref="System.ComponentModel.DataAnnotations.Validator.TryValidateValue(object, ValidationContext, ICollection{ValidationResult}, IEnumerable{ValidationAttribute})"/> is nullable annotated.
/// </summary>
/// <param name="compilation">The <see cref="Compilation"/> to consider for analysis.</param>
/// <returns>"!" if the first parameter is not nullable annotated, otherwise an empty string.</returns>
/// <remarks>
/// In .NET 8.0 we have changed the nullable annotation on first parameter of the method cref="System.ComponentModel.DataAnnotations.Validator.TryValidateValue(object, ValidationContext, ICollection{ValidationResult}, IEnumerable{ValidationAttribute})"/>
/// The source generator need to detect if we need to append "!" to the first parameter of the method call when running on down-level versions.
/// </remarks>
private static string GetNullableAnnotationStringForTryValidateValueToUseInGeneratedCode(Compilation compilation)
{
INamedTypeSymbol? validatorTypeSymbol = compilation.GetBestTypeByMetadataName("System.ComponentModel.DataAnnotations.Validator");
if (validatorTypeSymbol is not null)
{
ImmutableArray<ISymbol> members = validatorTypeSymbol.GetMembers("TryValidateValue");
if (members.Length == 1 && members[0] is IMethodSymbol tryValidateValueMethod)
{
return tryValidateValueMethod.Parameters[0].NullableAnnotation == NullableAnnotation.NotAnnotated ? "!" : string.Empty;
}
}
return "!";
}
private void GenValidatorType(ValidatorType vt, ref Dictionary<string, StaticFieldInfo> staticValidationAttributesDict, ref Dictionary<string, StaticFieldInfo> staticValidatorsDict)
{
if (vt.Namespace.Length > 0)
{
OutLn($"namespace {vt.Namespace}");
OutOpenBrace();
}
foreach (var p in vt.ParentTypes)
{
OutLn(p);
OutOpenBrace();
}
if (vt.IsSynthetic)
{
OutGeneratedCodeAttribute();
OutLn($"internal sealed partial {vt.DeclarationKeyword} {vt.Name}");
}
else
{
OutLn($"partial {vt.DeclarationKeyword} {vt.Name}");
}
OutOpenBrace();
for (var i = 0; i < vt.ModelsToValidate.Count; i++)
{
var modelToValidate = vt.ModelsToValidate[i];
GenModelValidationMethod(modelToValidate, vt.IsSynthetic, ref staticValidationAttributesDict, ref staticValidatorsDict);
}
OutCloseBrace();
foreach (var _ in vt.ParentTypes)
{
OutCloseBrace();
}
if (vt.Namespace.Length > 0)
{
OutCloseBrace();
}
}
private void GenStaticClassWithStaticReadonlyFields(IEnumerable<StaticFieldInfo> staticFields, string classNamespace, string className)
{
OutLn($"namespace {classNamespace}");
OutOpenBrace();
OutGeneratedCodeAttribute();
OutLn($"{_optionsSourceGenContext.ClassModifier} static class {className}");
OutOpenBrace();
var staticValidationAttributes = staticFields
.OrderBy(x => x.FieldOrder)
.ToArray();
for (var i = 0; i < staticValidationAttributes.Length; i++)
{
var attributeInstance = staticValidationAttributes[i];
OutIndent();
Out($"internal static readonly {attributeInstance.FieldTypeFQN} {attributeInstance.FieldName} = ");
for (var j = 0; j < attributeInstance.InstantiationLines.Count; j++)
{
var line = attributeInstance.InstantiationLines[j];
Out(line);
if (j != attributeInstance.InstantiationLines.Count - 1)
{
OutLn();
OutIndent();
}
else
{
Out(';');
}
}
OutLn();
if (i != staticValidationAttributes.Length - 1)
{
OutLn();
}
}
OutCloseBrace();
OutCloseBrace();
}
public void EmitMaxLengthAttribute(string modifier, string prefix, string className, string linesToInsert, string suffix)
{
OutGeneratedCodeAttribute();
string qualifiedClassName = $"{prefix}{suffix}_{className}";
OutLn($$"""
[global::System.AttributeUsage(global::System.AttributeTargets.Property | global::System.AttributeTargets.Field | global::System.AttributeTargets.Parameter, AllowMultiple = false)]
{{modifier}} class {{qualifiedClassName}} : {{StaticValidationAttributeType}}
{
private const int MaxAllowableLength = -1;
private static string DefaultErrorMessageString => "The field {0} must be a string or array type with a maximum length of '{1}'.";
public {{qualifiedClassName}}(int length) : base(() => DefaultErrorMessageString) { Length = length; }
public {{qualifiedClassName}}(): base(() => DefaultErrorMessageString) { Length = MaxAllowableLength; }
public int Length { get; }
public override string FormatErrorMessage(string name) => string.Format(global::System.Globalization.CultureInfo.CurrentCulture, ErrorMessageString, name, Length);
public override bool IsValid(object? value)
{
if (Length == 0 || Length < -1)
{
throw new global::System.InvalidOperationException("MaxLengthAttribute must have a Length value that is greater than zero. Use MaxLength() without parameters to indicate that the string or array can have the maximum allowable length.");
}
if (value == null || MaxAllowableLength == Length)
{
return true;
}
int length;
if (value is string stringValue)
{
length = stringValue.Length;
}
else if (value is System.Collections.ICollection collectionValue)
{
length = collectionValue.Count;
}
{{linesToInsert}}else
{
throw new global::System.InvalidCastException($"The field of type {value.GetType()} must be a string, array, or ICollection type.");
}
return length <= Length;
}
}
""");
}
public void EmitMinLengthAttribute(string modifier, string prefix, string className, string linesToInsert, string suffix)
{
OutGeneratedCodeAttribute();
string qualifiedClassName = $"{prefix}{suffix}_{className}";
OutLn($$"""
[global::System.AttributeUsage(global::System.AttributeTargets.Property | global::System.AttributeTargets.Field | global::System.AttributeTargets.Parameter, AllowMultiple = false)]
{{modifier}} class {{qualifiedClassName}} : {{StaticValidationAttributeType}}
{
private static string DefaultErrorMessageString => "The field {0} must be a string or array type with a minimum length of '{1}'.";
public {{qualifiedClassName}}(int length) : base(() => DefaultErrorMessageString) { Length = length; }
public int Length { get; }
public override bool IsValid(object? value)
{
if (Length < -1)
{
throw new global::System.InvalidOperationException("MinLengthAttribute must have a Length value that is zero or greater.");
}
if (value == null)
{
return true;
}
int length;
if (value is string stringValue)
{
length = stringValue.Length;
}
else if (value is System.Collections.ICollection collectionValue)
{
length = collectionValue.Count;
}
{{linesToInsert}}else
{
throw new global::System.InvalidCastException($"The field of type {value.GetType()} must be a string, array, or ICollection type.");
}
return length >= Length;
}
public override string FormatErrorMessage(string name) => string.Format(global::System.Globalization.CultureInfo.CurrentCulture, ErrorMessageString, name, Length);
}
""");
}
public void EmitLengthAttribute(string modifier, string prefix, string className, string linesToInsert, string suffix)
{
OutGeneratedCodeAttribute();
string qualifiedClassName = $"{prefix}{suffix}_{className}";
OutLn($$"""
[global::System.AttributeUsage(global::System.AttributeTargets.Property | global::System.AttributeTargets.Field | global::System.AttributeTargets.Parameter, AllowMultiple = false)]
{{modifier}} class {{qualifiedClassName}} : {{StaticValidationAttributeType}}
{
private static string DefaultErrorMessageString => "The field {0} must be a string or collection type with a minimum length of '{1}' and maximum length of '{2}'.";
public {{qualifiedClassName}}(int minimumLength, int maximumLength) : base(() => DefaultErrorMessageString) { MinimumLength = minimumLength; MaximumLength = maximumLength; }
public int MinimumLength { get; }
public int MaximumLength { get; }
public override bool IsValid(object? value)
{
if (MinimumLength < 0)
{
throw new global::System.InvalidOperationException("LengthAttribute must have a MinimumLength value that is zero or greater.");
}
if (MaximumLength < MinimumLength)
{
throw new global::System.InvalidOperationException("LengthAttribute must have a MaximumLength value that is greater than or equal to MinimumLength.");
}
if (value == null)
{
return true;
}
int length;
if (value is string stringValue)
{
length = stringValue.Length;
}
else if (value is System.Collections.ICollection collectionValue)
{
length = collectionValue.Count;
}
{{linesToInsert}}else
{
throw new global::System.InvalidCastException($"The field of type {value.GetType()} must be a string, array, or ICollection type.");
}
return (uint)(length - MinimumLength) <= (uint)(MaximumLength - MinimumLength);
}
public override string FormatErrorMessage(string name) => string.Format(global::System.Globalization.CultureInfo.CurrentCulture, ErrorMessageString, name, MinimumLength, MaximumLength);
}
""");
}
public void EmitCompareAttribute(string modifier, string prefix, string className, string linesToInsert, string suffix)
{
OutGeneratedCodeAttribute();
string qualifiedClassName = $"{prefix}{suffix}_{className}";
OutLn($$"""
[global::System.AttributeUsage(global::System.AttributeTargets.Property, AllowMultiple = false)]
{{modifier}} class {{qualifiedClassName}} : {{StaticValidationAttributeType}}
{
private static string DefaultErrorMessageString => "'{0}' and '{1}' do not match.";
public {{qualifiedClassName}}(string otherProperty) : base(() => DefaultErrorMessageString)
{
if (otherProperty == null)
{
throw new global::System.ArgumentNullException(nameof(otherProperty));
}
OtherProperty = otherProperty;
}
public string OtherProperty { get; }
public override bool RequiresValidationContext => true;
protected override {{StaticValidationResultType}}? IsValid(object? value, {{StaticValidationContextType}} validationContext)
{
bool result = true;
{{linesToInsert}}
if (!result)
{
string[]? memberNames = validationContext.MemberName is null ? null : new string[] { validationContext.MemberName };
return new {{StaticValidationResultType}}(FormatErrorMessage(validationContext.DisplayName), memberNames);
}
return null;
}
public override string FormatErrorMessage(string name) => string.Format(global::System.Globalization.CultureInfo.CurrentCulture, ErrorMessageString, name, OtherProperty);
}
""");
}
public void EmitRangeAttribute(string modifier, string prefix, string className, string suffix, bool emitTimeSpanSupport)
{
OutGeneratedCodeAttribute();
string qualifiedClassName = $"{prefix}{suffix}_{className}";
string initializationString = emitTimeSpanSupport ?
"""
if (OperandType == typeof(global::System.TimeSpan))
{
if (!global::System.TimeSpan.TryParse((string)Minimum, culture, out global::System.TimeSpan timeSpanMinimum) ||
!global::System.TimeSpan.TryParse((string)Maximum, culture, out global::System.TimeSpan timeSpanMaximum))
{
throw new global::System.InvalidOperationException(MinMaxError);
}
Minimum = timeSpanMinimum;
Maximum = timeSpanMaximum;
}
else
{
Minimum = ConvertValue(Minimum, culture) ?? throw new global::System.InvalidOperationException(MinMaxError);
Maximum = ConvertValue(Maximum, culture) ?? throw new global::System.InvalidOperationException(MinMaxError);
}
"""
:
"""
Minimum = ConvertValue(Minimum, culture) ?? throw new global::System.InvalidOperationException(MinMaxError);
Maximum = ConvertValue(Maximum, culture) ?? throw new global::System.InvalidOperationException(MinMaxError);
""";
string convertValue = emitTimeSpanSupport ?
"""
if (OperandType == typeof(global::System.TimeSpan))
{
if (value is global::System.TimeSpan)
{
convertedValue = value;
}
else if (value is string)
{
if (!global::System.TimeSpan.TryParse((string)value, formatProvider, out global::System.TimeSpan timeSpanValue))
{
return false;
}
convertedValue = timeSpanValue;
}
else
{
throw new global::System.InvalidOperationException($"A value type {value.GetType()} that is not a TimeSpan or a string has been given. This might indicate a problem with the source generator.");
}
}
else
{
try
{
convertedValue = ConvertValue(value, formatProvider);
}
catch (global::System.Exception e) when (e is global::System.FormatException or global::System.InvalidCastException or global::System.NotSupportedException)
{
return false;
}
}
"""
:
"""
try
{
convertedValue = ConvertValue(value, formatProvider);
}
catch (global::System.Exception e) when (e is global::System.FormatException or global::System.InvalidCastException or global::System.NotSupportedException)
{
return false;
}
""";
OutLn($$"""
[global::System.AttributeUsage(global::System.AttributeTargets.Property | global::System.AttributeTargets.Field | global::System.AttributeTargets.Parameter, AllowMultiple = false)]
{{modifier}} class {{qualifiedClassName}} : {{StaticValidationAttributeType}}
{
public {{qualifiedClassName}}(int minimum, int maximum) : base()
{
Minimum = minimum;
Maximum = maximum;
OperandType = typeof(int);
}
public {{qualifiedClassName}}(double minimum, double maximum) : base()
{
Minimum = minimum;
Maximum = maximum;
OperandType = typeof(double);
}
public {{qualifiedClassName}}(global::System.Type type, string minimum, string maximum) : base()
{
OperandType = type;
_needToConvertMinMax = true;
Minimum = minimum;
Maximum = maximum;
}
public object Minimum { get; private set; }
public object Maximum { get; private set; }
public bool MinimumIsExclusive { get; set; }
public bool MaximumIsExclusive { get; set; }
public global::System.Type OperandType { get; }
public bool ParseLimitsInInvariantCulture { get; set; }
public bool ConvertValueInInvariantCulture { get; set; }
public override string FormatErrorMessage(string name) =>
string.Format(global::System.Globalization.CultureInfo.CurrentCulture, GetValidationErrorMessage(), name, Minimum, Maximum);
private readonly bool _needToConvertMinMax;
private volatile bool _initialized;
private readonly object _lock = new();
private const string MinMaxError = "The minimum and maximum values must be set to valid values.";
public override bool IsValid(object? value)
{
if (!_initialized)
{
lock (_lock)
{
if (!_initialized)
{
if (Minimum is null || Maximum is null)
{
throw new global::System.InvalidOperationException(MinMaxError);
}
if (_needToConvertMinMax)
{
System.Globalization.CultureInfo culture = ParseLimitsInInvariantCulture ? global::System.Globalization.CultureInfo.InvariantCulture : global::System.Globalization.CultureInfo.CurrentCulture;
{{initializationString}}
}
int cmp = ((global::System.IComparable)Minimum).CompareTo((global::System.IComparable)Maximum);
if (cmp > 0)
{
throw new global::System.InvalidOperationException("The maximum value '{Maximum}' must be greater than or equal to the minimum value '{Minimum}'.");
}
else if (cmp == 0 && (MinimumIsExclusive || MaximumIsExclusive))
{
throw new global::System.InvalidOperationException("Cannot use exclusive bounds when the maximum value is equal to the minimum value.");
}
_initialized = true;
}
}
}
if (value is null or string { Length: 0 })
{
return true;
}
System.Globalization.CultureInfo formatProvider = ConvertValueInInvariantCulture ? global::System.Globalization.CultureInfo.InvariantCulture : global::System.Globalization.CultureInfo.CurrentCulture;
object? convertedValue;
{{convertValue}}
var min = (global::System.IComparable)Minimum;
var max = (global::System.IComparable)Maximum;
return
(MinimumIsExclusive ? min.CompareTo(convertedValue) < 0 : min.CompareTo(convertedValue) <= 0) &&
(MaximumIsExclusive ? max.CompareTo(convertedValue) > 0 : max.CompareTo(convertedValue) >= 0);
}
private string GetValidationErrorMessage()
{
return (MinimumIsExclusive, MaximumIsExclusive) switch
{
(false, false) => "The field {0} must be between {1} and {2}.",
(true, false) => "The field {0} must be between {1} exclusive and {2}.",
(false, true) => "The field {0} must be between {1} and {2} exclusive.",
(true, true) => "The field {0} must be between {1} exclusive and {2} exclusive.",
};
}
private object? ConvertValue(object? value, System.Globalization.CultureInfo formatProvider)
{
if (value is string stringValue)
{
value = global::System.Convert.ChangeType(stringValue, OperandType, formatProvider);
}
else
{
value = global::System.Convert.ChangeType(value, OperandType, formatProvider);
}
return value;
}
}
""");
}
private string GenerateStronglyTypedCodeForLengthAttributes(HashSet<object> data)
{
if (data.Count == 0)
{
return string.Empty;
}
StringBuilder sb = new();
string padding = GetPaddingString(3);
foreach (var type in data)
{
string typeName = (string)type;
sb.AppendLine($"else if (value is {typeName})");
sb.AppendLine($"{padding}{{");
sb.AppendLine($"{padding} length = (({typeName})value).Count;");
sb.AppendLine($"{padding}}}");
sb.Append($"{padding}");
}
return sb.ToString();
}
private string GenerateStronglyTypedCodeForCompareAttribute(HashSet<object>? data)
{
if (data is null || data.Count == 0)
{
return string.Empty;
}
StringBuilder sb = new();
string padding = GetPaddingString(3);
bool first = true;
foreach (var obj in data)
{
(string type, string property) = ((string, string))obj;
sb.Append(first ? $"if " : $"{padding}else if ");
sb.AppendLine($"(validationContext.ObjectInstance is {type} && OtherProperty == \"{property}\")");
sb.AppendLine($"{padding}{{");
sb.AppendLine($"{padding} result = Equals(value, (({type})validationContext.ObjectInstance).{property});");
sb.AppendLine($"{padding}}}");
first = false;
}
return sb.ToString();
}
private void GenValidationAttributesClasses()
{
if (_optionsSourceGenContext.AttributesToGenerate.Count == 0)
{
return;
}
var attributesData = _optionsSourceGenContext.AttributesToGenerate.OrderBy(static kvp => kvp.Key, StringComparer.Ordinal).ToArray();
OutLn($"namespace {StaticGeneratedValidationAttributesClassesNamespace}");
OutOpenBrace();
foreach (var attributeData in attributesData)
{
if (attributeData.Key == _symbolHolder.MaxLengthAttributeSymbol.Name)
{
string linesToInsert = attributeData.Value is not null ? GenerateStronglyTypedCodeForLengthAttributes((HashSet<object>)attributeData.Value) : string.Empty;
EmitMaxLengthAttribute(_optionsSourceGenContext.ClassModifier, Emitter.StaticAttributeClassNamePrefix, attributeData.Key, linesToInsert, _optionsSourceGenContext.Suffix);
}
else if (attributeData.Key == _symbolHolder.MinLengthAttributeSymbol.Name)
{
string linesToInsert = attributeData.Value is not null ? GenerateStronglyTypedCodeForLengthAttributes((HashSet<object>)attributeData.Value) : string.Empty;
EmitMinLengthAttribute(_optionsSourceGenContext.ClassModifier, Emitter.StaticAttributeClassNamePrefix, attributeData.Key, linesToInsert, _optionsSourceGenContext.Suffix);
}
else if (_symbolHolder.LengthAttributeSymbol is not null && attributeData.Key == _symbolHolder.LengthAttributeSymbol.Name)
{
string linesToInsert = attributeData.Value is not null ? GenerateStronglyTypedCodeForLengthAttributes((HashSet<object>)attributeData.Value) : string.Empty;
EmitLengthAttribute(_optionsSourceGenContext.ClassModifier, Emitter.StaticAttributeClassNamePrefix, attributeData.Key, linesToInsert, _optionsSourceGenContext.Suffix);
}
else if (attributeData.Key == _symbolHolder.CompareAttributeSymbol.Name && attributeData.Value is not null)
{
string linesToInsert = GenerateStronglyTypedCodeForCompareAttribute((HashSet<object>)attributeData.Value);
EmitCompareAttribute(_optionsSourceGenContext.ClassModifier, Emitter.StaticAttributeClassNamePrefix, attributeData.Key, linesToInsert: linesToInsert, _optionsSourceGenContext.Suffix);
}
else if (attributeData.Key == _symbolHolder.RangeAttributeSymbol.Name)
{
EmitRangeAttribute(_optionsSourceGenContext.ClassModifier, Emitter.StaticAttributeClassNamePrefix, attributeData.Key, _optionsSourceGenContext.Suffix, attributeData.Value is not null);
}
}
OutCloseBrace();
}
private void GenModelSelfValidationIfNecessary(ValidatedModel modelToValidate)
{
if (modelToValidate.SelfValidates)
{
OutLn($"(builder ??= new()).AddResults(((global::System.ComponentModel.DataAnnotations.IValidatableObject)options).Validate(context));");
OutLn();
}
}
private void GenModelValidationMethod(
ValidatedModel modelToValidate,
bool makeStatic,
ref Dictionary<string, StaticFieldInfo> staticValidationAttributesDict,
ref Dictionary<string, StaticFieldInfo> staticValidatorsDict)
{
OutLn($"/// <summary>");
OutLn($"/// Validates a specific named options instance (or all when <paramref name=\"name\"/> is <see langword=\"null\" />).");
OutLn($"/// </summary>");
OutLn($"/// <param name=\"name\">The name of the options instance being validated.</param>");
OutLn($"/// <param name=\"options\">The options instance.</param>");
OutLn($"/// <returns>Validation result.</returns>");
OutGeneratedCodeAttribute();
if (_symbolHolder.UnconditionalSuppressMessageAttributeSymbol is not null)
{
// We disable the warning on `new ValidationContext(object)` usage as we use it in a safe way that not require executing the reflection code.
// This is done by initializing the DisplayName in the context which is the part trigger reflection if it is not initialized.
OutLn($"[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2026:RequiresUnreferencedCode\",");
OutLn($" Justification = \"The created ValidationContext object is used in a way that never call reflection\")]");
}
OutLn($"public {(makeStatic ? "static " : string.Empty)}global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, {modelToValidate.Name} options)");
OutOpenBrace();
OutLn($"global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;");
OutLn($"var context = new {StaticValidationContextType}(options);");
int capacity = modelToValidate.MembersToValidate.Max(static vm => vm.ValidationAttributes.Count);
if (capacity > 0)
{
OutLn($"var validationResults = new {StaticListType}<{StaticValidationResultType}>();");
OutLn($"var validationAttributes = new {StaticListType}<{StaticValidationAttributeType}>({capacity});");
}
OutLn();
bool cleanListsBeforeUse = false;
foreach (var vm in modelToValidate.MembersToValidate)
{
if (vm.ValidationAttributes.Count > 0)
{
GenMemberValidation(vm, modelToValidate.SimpleName, ref staticValidationAttributesDict, cleanListsBeforeUse);
cleanListsBeforeUse = true;
OutLn();
}
if (vm.TransValidatorType is not null)
{
GenTransitiveValidation(vm, modelToValidate.SimpleName, ref staticValidatorsDict);
OutLn();
}
if (vm.EnumerationValidatorType is not null)
{
GenEnumerationValidation(vm, modelToValidate.SimpleName, ref staticValidatorsDict);
OutLn();
}
}
GenModelSelfValidationIfNecessary(modelToValidate);
OutLn($"return builder is null ? global::Microsoft.Extensions.Options.ValidateOptionsResult.Success : builder.Build();");
OutCloseBrace();
}
private void GenMemberValidation(ValidatedMember vm, string modelName, ref Dictionary<string, StaticFieldInfo> staticValidationAttributesDict, bool cleanListsBeforeUse)
{
OutLn($"context.MemberName = \"{vm.Name}\";");
OutLn($"context.DisplayName = string.IsNullOrEmpty(name) ? \"{modelName}.{vm.Name}\" : $\"{{name}}.{vm.Name}\";");
if (cleanListsBeforeUse)
{
OutLn($"validationResults.Clear();");
OutLn($"validationAttributes.Clear();");
}
foreach (var attr in vm.ValidationAttributes)
{
var staticValidationAttributeInstance = GetOrAddStaticValidationAttribute(ref staticValidationAttributesDict, attr);
OutLn($"validationAttributes.Add({_staticValidationAttributeHolderClassFQN}.{staticValidationAttributeInstance.FieldName});");
}
OutLn($"if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.{vm.Name}{_TryGetValueNullableAnnotation}, context, validationResults, validationAttributes))");
OutOpenBrace();
OutLn($"(builder ??= new()).AddResults(validationResults);");
OutCloseBrace();
}
private StaticFieldInfo GetOrAddStaticValidationAttribute(ref Dictionary<string, StaticFieldInfo> staticValidationAttributesDict, ValidationAttributeInfo attr)
{
var attrInstantiationStatementLines = new List<string>();
if (attr.ConstructorArguments.Count > 0)
{
attrInstantiationStatementLines.Add($"new {attr.AttributeName}(");
for (var i = 0; i < attr.ConstructorArguments.Count; i++)
{
if (i != attr.ConstructorArguments.Count - 1)
{
attrInstantiationStatementLines.Add($"{GetPaddingString(1)}{attr.ConstructorArguments[i]},");
}
else
{
attrInstantiationStatementLines.Add($"{GetPaddingString(1)}{attr.ConstructorArguments[i]})");
}
}
}
else
{
attrInstantiationStatementLines.Add($"new {attr.AttributeName}()");
}
if (attr.Properties.Count > 0)
{
attrInstantiationStatementLines.Add("{");
var propertiesOrderedByKey = attr.Properties
.OrderBy(p => p.Key)
.ToArray();
for (var i = 0; i < propertiesOrderedByKey.Length; i++)
{
var prop = propertiesOrderedByKey[i];
var notLast = i != propertiesOrderedByKey.Length - 1;
attrInstantiationStatementLines.Add($"{GetPaddingString(1)}{prop.Key} = {prop.Value}{(notLast ? "," : string.Empty)}");
}
attrInstantiationStatementLines.Add("}");
}
var instantiationStatement = string.Join(Environment.NewLine, attrInstantiationStatementLines);
if (!staticValidationAttributesDict.TryGetValue(instantiationStatement, out var staticValidationAttributeInstance))
{
var fieldNumber = staticValidationAttributesDict.Count + 1;
staticValidationAttributeInstance = new StaticFieldInfo(
FieldTypeFQN: attr.AttributeName,
FieldOrder: fieldNumber,
FieldName: $"A{fieldNumber}",
InstantiationLines: attrInstantiationStatementLines);
staticValidationAttributesDict.Add(instantiationStatement, staticValidationAttributeInstance);
}
return staticValidationAttributeInstance;
}
private void GenTransitiveValidation(ValidatedMember vm, string modelName, ref Dictionary<string, StaticFieldInfo> staticValidatorsDict)
{
string callSequence;
if (vm.TransValidateTypeIsSynthetic)
{
callSequence = vm.TransValidatorType!;
}
else
{
var staticValidatorInstance = GetOrAddStaticValidator(ref staticValidatorsDict, vm.TransValidatorType!);
callSequence = $"{_staticValidatorHolderClassFQN}.{staticValidatorInstance.FieldName}";
}
var valueAccess = (vm.IsNullable && vm.IsValueType) ? ".Value" : string.Empty;
var baseName = $"string.IsNullOrEmpty(name) ? \"{modelName}.{vm.Name}\" : $\"{{name}}.{vm.Name}\"";
if (vm.IsNullable)
{
OutLn($"if (options.{vm.Name} is not null)");
OutOpenBrace();
OutLn($"(builder ??= new()).AddResult({callSequence}.Validate({baseName}, options.{vm.Name}{valueAccess}));");
OutCloseBrace();
}
else
{
OutLn($"(builder ??= new()).AddResult({callSequence}.Validate({baseName}, options.{vm.Name}{valueAccess}));");
}
}
private void GenEnumerationValidation(ValidatedMember vm, string modelName, ref Dictionary<string, StaticFieldInfo> staticValidatorsDict)
{
var valueAccess = (vm.IsValueType && vm.IsNullable) ? ".Value" : string.Empty;
var enumeratedValueAccess = (vm.EnumeratedIsNullable && vm.EnumeratedIsValueType) ? ".Value" : string.Empty;
string callSequence;
if (vm.EnumerationValidatorTypeIsSynthetic)
{
callSequence = vm.EnumerationValidatorType!;
}
else
{
var staticValidatorInstance = GetOrAddStaticValidator(ref staticValidatorsDict, vm.EnumerationValidatorType!);
callSequence = $"{_staticValidatorHolderClassFQN}.{staticValidatorInstance.FieldName}";
}
if (vm.IsNullable)
{
OutLn($"if (options.{vm.Name} is not null)");
}
OutOpenBrace();
OutLn($"var count = 0;");
OutLn($"foreach (var o in options.{vm.Name}{valueAccess})");
OutOpenBrace();
if (vm.EnumeratedIsNullable)
{
OutLn($"if (o is not null)");
OutOpenBrace();
var propertyName = $"string.IsNullOrEmpty(name) ? $\"{modelName}.{vm.Name}[{{count}}]\" : $\"{{name}}.{vm.Name}[{{count}}]\"";
OutLn($"(builder ??= new()).AddResult({callSequence}.Validate({propertyName}, o{enumeratedValueAccess}));");
OutCloseBrace();
if (!vm.EnumeratedMayBeNull)
{
OutLn($"else");
OutOpenBrace();
var error = $"string.IsNullOrEmpty(name) ? $\"{modelName}.{vm.Name}[{{count}}] is null\" : $\"{{name}}.{vm.Name}[{{count}}] is null\"";
OutLn($"(builder ??= new()).AddError({error});");
OutCloseBrace();
}
OutLn($"count++;");
}
else
{
var propertyName = $"string.IsNullOrEmpty(name) ? $\"{modelName}.{vm.Name}[{{count++}}] is null\" : $\"{{name}}.{vm.Name}[{{count++}}] is null\"";
OutLn($"(builder ??= new()).AddResult({callSequence}.Validate({propertyName}, o{enumeratedValueAccess}));");
}
OutCloseBrace();
OutCloseBrace();
}
#pragma warning disable CA1822 // Mark members as static: static should come before non-static, but we want the method to be here
private StaticFieldInfo GetOrAddStaticValidator(ref Dictionary<string, StaticFieldInfo> staticValidatorsDict, string validatorTypeFQN)
#pragma warning restore CA1822
{
if (!staticValidatorsDict.TryGetValue(validatorTypeFQN, out var staticValidatorInstance))
{
var fieldNumber = staticValidatorsDict.Count + 1;
staticValidatorInstance = new StaticFieldInfo(
FieldTypeFQN: validatorTypeFQN,
FieldOrder: fieldNumber,
FieldName: $"V{fieldNumber}",
InstantiationLines: new[] { $"new {validatorTypeFQN}()" });
staticValidatorsDict.Add(validatorTypeFQN, staticValidatorInstance);
}
return staticValidatorInstance;
}
}
}