Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions src/libraries/Microsoft.Extensions.Options/gen/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,12 @@ private void GenValidationAttributesClasses()
OutCloseBrace();
}

private void GenModelSelfValidationIfNecessary(ValidatedModel modelToValidate, string modelName)
private void GenModelSelfValidationIfNecessary(ValidatedModel modelToValidate)
{
if (modelToValidate.SelfValidates)
{
OutLn($"context.MemberName = \"Validate\";");
OutLn($"context.DisplayName = string.IsNullOrEmpty(name) ? \"{modelName}.Validate\" : $\"{{name}}.Validate\";");
OutLn($"context.DisplayName = string.IsNullOrEmpty(name) ? \"Validate\" : $\"{{name}}.Validate\";");
OutLn($"(builder ??= new()).AddResults(((global::System.ComponentModel.DataAnnotations.IValidatableObject)options).Validate(context));");
OutLn();
}
Expand Down Expand Up @@ -693,8 +693,7 @@ private void GenModelValidationMethod(
OutOpenBrace();
OutLn($"global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;");
OutLn("#if NET10_0_OR_GREATER");
OutLn($"string displayName = string.IsNullOrEmpty(name) ? \"{modelToValidate.SimpleName}.Validate\" : $\"{{name}}.Validate\";");
OutLn($"var context = new {StaticValidationContextType}(options, displayName, null, null);");
OutLn($"var context = new {StaticValidationContextType}(options, \"{modelToValidate.SimpleName}\", null, null);");
OutLn("#else");
OutLn($"var context = new {StaticValidationContextType}(options);");
OutLn("#endif");
Expand All @@ -712,33 +711,33 @@ private void GenModelValidationMethod(
{
if (vm.ValidationAttributes.Count > 0)
{
GenMemberValidation(vm, modelToValidate.SimpleName, ref staticValidationAttributesDict, cleanListsBeforeUse);
GenMemberValidation(vm, ref staticValidationAttributesDict, cleanListsBeforeUse);
cleanListsBeforeUse = true;
OutLn();
}

if (vm.TransValidatorType is not null)
{
GenTransitiveValidation(vm, modelToValidate.SimpleName, ref staticValidatorsDict);
GenTransitiveValidation(vm, ref staticValidatorsDict);
OutLn();
}

if (vm.EnumerationValidatorType is not null)
{
GenEnumerationValidation(vm, modelToValidate.SimpleName, ref staticValidatorsDict);
GenEnumerationValidation(vm, ref staticValidatorsDict);
OutLn();
}
}

GenModelSelfValidationIfNecessary(modelToValidate, modelToValidate.SimpleName);
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)
private void GenMemberValidation(ValidatedMember vm, ref Dictionary<string, StaticFieldInfo> staticValidationAttributesDict, bool cleanListsBeforeUse)
{
OutLn($"context.MemberName = \"{vm.Name}\";");
OutLn($"context.DisplayName = string.IsNullOrEmpty(name) ? \"{modelName}.{vm.Name}\" : $\"{{name}}.{vm.Name}\";");
OutLn($"context.DisplayName = string.IsNullOrEmpty(name) ? \"{vm.Name}\" : $\"{{name}}.{vm.Name}\";");

if (cleanListsBeforeUse)
{
Expand Down Expand Up @@ -818,7 +817,7 @@ private StaticFieldInfo GetOrAddStaticValidationAttribute(ref Dictionary<string,
return staticValidationAttributeInstance;
}

private void GenTransitiveValidation(ValidatedMember vm, string modelName, ref Dictionary<string, StaticFieldInfo> staticValidatorsDict)
private void GenTransitiveValidation(ValidatedMember vm, ref Dictionary<string, StaticFieldInfo> staticValidatorsDict)
{
string callSequence;
if (vm.TransValidateTypeIsSynthetic)
Expand All @@ -834,7 +833,7 @@ private void GenTransitiveValidation(ValidatedMember vm, string modelName, ref D

var valueAccess = (vm.IsNullable && vm.IsValueType) ? ".Value" : string.Empty;

var baseName = $"string.IsNullOrEmpty(name) ? \"{modelName}.{vm.Name}\" : $\"{{name}}.{vm.Name}\"";
var baseName = $"string.IsNullOrEmpty(name) ? \"{vm.Name}\" : $\"{{name}}.{vm.Name}\"";

if (vm.IsNullable)
{
Expand All @@ -849,7 +848,7 @@ private void GenTransitiveValidation(ValidatedMember vm, string modelName, ref D
}
}

private void GenEnumerationValidation(ValidatedMember vm, string modelName, ref Dictionary<string, StaticFieldInfo> staticValidatorsDict)
private void GenEnumerationValidation(ValidatedMember vm, ref Dictionary<string, StaticFieldInfo> staticValidatorsDict)
{
var valueAccess = (vm.IsValueType && vm.IsNullable) ? ".Value" : string.Empty;
var enumeratedValueAccess = (vm.EnumeratedIsNullable && vm.EnumeratedIsValueType) ? ".Value" : string.Empty;
Expand Down Expand Up @@ -880,15 +879,15 @@ private void GenEnumerationValidation(ValidatedMember vm, string modelName, ref
{
OutLn($"if (o is not null)");
OutOpenBrace();
var propertyName = $"string.IsNullOrEmpty(name) ? $\"{modelName}.{vm.Name}[{{count}}]\" : $\"{{name}}.{vm.Name}[{{count}}]\"";
var propertyName = $"string.IsNullOrEmpty(name) ? $\"{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\"";
var error = $"string.IsNullOrEmpty(name) ? $\"{vm.Name}[{{count}}] is null\" : $\"{{name}}.{vm.Name}[{{count}}] is null\"";
OutLn($"(builder ??= new()).AddError({error});");
OutCloseBrace();
}
Expand All @@ -897,7 +896,7 @@ private void GenEnumerationValidation(ValidatedMember vm, string modelName, ref
}
else
{
var propertyName = $"string.IsNullOrEmpty(name) ? $\"{modelName}.{vm.Name}[{{count++}}] is null\" : $\"{{name}}.{vm.Name}[{{count++}}] is null\"";
var propertyName = $"string.IsNullOrEmpty(name) ? $\"{vm.Name}[{{count++}}]\" : $\"{{name}}.{vm.Name}[{{count++}}]\"";
OutLn($"(builder ??= new()).AddResult({callSequence}.Validate({propertyName}, o{enumeratedValueAccess}));");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@ partial class MyOptionsValidator
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "MyOptions.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, "MyOptions", null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

context.MemberName = "P1";
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.P1" : $"{name}.P1";
context.DisplayName = string.IsNullOrEmpty(name) ? "P1" : $"{name}.P1";
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A1);
if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.P1, context, validationResults, validationAttributes))
{
(builder ??= new()).AddResults(validationResults);
}

context.MemberName = "P2";
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.P2" : $"{name}.P2";
context.DisplayName = string.IsNullOrEmpty(name) ? "P2" : $"{name}.P2";
validationResults.Clear();
validationAttributes.Clear();
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A2);
Expand All @@ -48,7 +47,7 @@ partial class MyOptionsValidator
}

context.MemberName = "P3";
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.P3" : $"{name}.P3";
context.DisplayName = string.IsNullOrEmpty(name) ? "P3" : $"{name}.P3";
validationResults.Clear();
validationAttributes.Clear();
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A3);
Expand All @@ -58,7 +57,7 @@ partial class MyOptionsValidator
}

context.MemberName = "P4";
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.P4" : $"{name}.P4";
context.DisplayName = string.IsNullOrEmpty(name) ? "P4" : $"{name}.P4";
validationResults.Clear();
validationAttributes.Clear();
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@ partial struct MyOptionsValidator
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "MyOptions.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, "MyOptions", null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

context.MemberName = "Val1";
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.Val1" : $"{name}.Val1";
context.DisplayName = string.IsNullOrEmpty(name) ? "Val1" : $"{name}.Val1";
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A1);
if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.Val1, context, validationResults, validationAttributes))
{
(builder ??= new()).AddResults(validationResults);
}

context.MemberName = "Val2";
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.Val2" : $"{name}.Val2";
context.DisplayName = string.IsNullOrEmpty(name) ? "Val2" : $"{name}.Val2";
validationResults.Clear();
validationAttributes.Clear();
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,23 @@ partial struct MyOptionsValidator
{
global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;
#if NET10_0_OR_GREATER
string displayName = string.IsNullOrEmpty(name) ? "MyOptions.Validate" : $"{name}.Validate";
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, displayName, null, null);
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options, "MyOptions", null, null);
#else
var context = new global::System.ComponentModel.DataAnnotations.ValidationContext(options);
#endif
var validationResults = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationResult>();
var validationAttributes = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(1);

context.MemberName = "Val1";
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.Val1" : $"{name}.Val1";
context.DisplayName = string.IsNullOrEmpty(name) ? "Val1" : $"{name}.Val1";
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A1);
if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.Val1, context, validationResults, validationAttributes))
{
(builder ??= new()).AddResults(validationResults);
}

context.MemberName = "Val2";
context.DisplayName = string.IsNullOrEmpty(name) ? "MyOptions.Val2" : $"{name}.Val2";
context.DisplayName = string.IsNullOrEmpty(name) ? "Val2" : $"{name}.Val2";
validationResults.Clear();
validationAttributes.Clear();
validationAttributes.Add(global::__OptionValidationStaticInstances.__Attributes.A2);
Expand Down
Loading
Loading