From 5d638d3b159415ff99a78d9c11ea17877b20069e Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Mon, 11 Sep 2023 16:27:26 -0500 Subject: [PATCH 1/6] Make src gen for property setters consistent with reflection --- .../gen/Emitter/CoreBindingHelpers.cs | 32 ++++- .../gen/Specs/Types/SimpleTypeSpec.cs | 2 +- .../ConfigurationBinderTests.TestClasses.cs | 24 +++- .../tests/Common/ConfigurationBinderTests.cs | 13 +- .../ConfigurationBinder/Bind.generated.txt | 9 +- .../Bind_Instance.generated.txt | 9 +- .../Bind_Instance_BinderOptions.generated.txt | 9 +- .../Bind_Key_Instance.generated.txt | 9 +- .../ConfigurationBinder/Get.generated.txt | 13 +- .../ConfigurationBinder/Get_T.generated.txt | 9 +- .../Get_T_BinderOptions.generated.txt | 9 +- .../Get_TypeOf.generated.txt | 4 + .../Get_TypeOf_BinderOptions.generated.txt | 4 + .../BindConfiguration.generated.txt | 9 +- .../OptionsBuilder/Bind_T.generated.txt | 9 +- .../Bind_T_BinderOptions.generated.txt | 9 +- .../Baselines/Primitives.generated.txt | 118 +++++++++++++++++- .../Configure_T.generated.txt | 13 +- .../Configure_T_BinderOptions.generated.txt | 13 +- .../Configure_T_name.generated.txt | 13 +- ...nfigure_T_name_BinderOptions.generated.txt | 13 +- 21 files changed, 308 insertions(+), 35 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs index 7b698544c91514..e25b8696332a9a 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs @@ -97,6 +97,7 @@ private void EmitGetCoreMethod() Expression.sectionPath, writeOnSuccess: parsedValueExpr => _writer.WriteLine($"return {parsedValueExpr};"), checkForNullSectionValue: stringParsableType.StringParsableTypeKind is not StringParsableTypeKind.AssignFromSectionValue, + useDefaultValueIfSectionValueIsNull : false, useIncrementalStringValueIdentifier: false); } break; @@ -173,6 +174,7 @@ private void EmitGetValueCoreMethod() Expression.sectionPath, writeOnSuccess: (parsedValueExpr) => _writer.WriteLine($"return {parsedValueExpr};"), checkForNullSectionValue: false, + useDefaultValueIfSectionValueIsNull: false, useIncrementalStringValueIdentifier: false); EmitEndBlock(); @@ -307,6 +309,10 @@ private void EmitInitializeMethod(ObjectSpec type) { if (property.ShouldBindTo && property.MatchingCtorParam is null) { + // Currently properties don't have an 'else' failure case. Doing that may collide with the + // feature to set properties to their default values if they don't exist in the config section. + Debug.Assert(property.ErrorOnFailedBinding == false); + EmitBindImplForMember(property); } } @@ -661,6 +667,7 @@ private void EmitPopulationImplForEnumerableWithAdd(EnumerableSpec type) Expression.sectionPath, (parsedValueExpr) => _writer.WriteLine($"{addExpr}({parsedValueExpr});"), checkForNullSectionValue: true, + useDefaultValueIfSectionValueIsNull: false, useIncrementalStringValueIdentifier: false); } break; @@ -696,6 +703,7 @@ private void EmitBindCoreImplForDictionary(DictionarySpec type) Expression.sectionPath, Emit_BindAndAddLogic_ForElement, checkForNullSectionValue: false, + useDefaultValueIfSectionValueIsNull: false, useIncrementalStringValueIdentifier: false); void Emit_BindAndAddLogic_ForElement(string parsedKeyExpr) @@ -710,6 +718,7 @@ void Emit_BindAndAddLogic_ForElement(string parsedKeyExpr) Expression.sectionPath, writeOnSuccess: parsedValueExpr => _writer.WriteLine($"{instanceIdentifier}[{parsedKeyExpr}] = {parsedValueExpr};"), checkForNullSectionValue: true, + useDefaultValueIfSectionValueIsNull: false, useIncrementalStringValueIdentifier: false); } break; @@ -786,6 +795,7 @@ private bool EmitBindImplForMember( bool canSet) { TypeSpec effectiveMemberType = member.Type.EffectiveType; + string sectionParseExpr = GetSectionFromConfigurationExpression(member.ConfigurationKeyName); switch (effectiveMemberType) @@ -794,11 +804,11 @@ private bool EmitBindImplForMember( { if (canSet) { - bool checkForNullSectionValue = member is ParameterSpec - ? true - : stringParsableType.StringParsableTypeKind is not StringParsableTypeKind.AssignFromSectionValue; - - string nullBangExpr = checkForNullSectionValue ? string.Empty : "!"; + string nullBangExpr = member.Type.IsValueType ? string.Empty : "!"; + bool useDefaultValueIfSectionValueIsNull = member is PropertySpec && + //stringParsableType.StringParsableTypeKind is not StringParsableTypeKind.AssignFromSectionValue && + member.Type.IsValueType && + member.Type.SpecKind is not TypeSpecKind.Nullable; EmitBlankLineIfRequired(); EmitBindingLogic( @@ -806,7 +816,8 @@ private bool EmitBindImplForMember( $@"{Identifier.configuration}[""{member.ConfigurationKeyName}""]", sectionPathExpr, writeOnSuccess: parsedValueExpr => _writer.WriteLine($"{memberAccessExpr} = {parsedValueExpr}{nullBangExpr};"), - checkForNullSectionValue, + checkForNullSectionValue: true, + useDefaultValueIfSectionValueIsNull, useIncrementalStringValueIdentifier: true); } @@ -993,6 +1004,7 @@ private void EmitBindingLogic( string sectionPathExpr, Action? writeOnSuccess, bool checkForNullSectionValue, + bool useDefaultValueIfSectionValueIsNull, bool useIncrementalStringValueIdentifier) { StringParsableTypeKind typeKind = type.StringParsableTypeKind; @@ -1018,6 +1030,14 @@ private void EmitBindingLogic( EmitEndBlock(); } + if (useDefaultValueIfSectionValueIsNull) + { + parsedValueExpr = $"default"; + EmitStartBlock($"else"); + InvokeWriteOnSuccess(); + EmitEndBlock(); + } + void InvokeWriteOnSuccess() => writeOnSuccess?.Invoke(parsedValueExpr); } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Types/SimpleTypeSpec.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Types/SimpleTypeSpec.cs index 50e488008ad68d..2dfe08dc5f547a 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Types/SimpleTypeSpec.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Types/SimpleTypeSpec.cs @@ -54,7 +54,7 @@ internal enum StringParsableTypeKind None = 0, /// - /// Declared types that can be assigned directly from IConfigurationSection.Value, i.e. string and tyepof(object). + /// Declared types that can be assigned directly from IConfigurationSection.Value, i.e. string and typeof(object). /// AssignFromSectionValue = 1, Enum = 2, diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs index e92ed061808740..46a5d87245fb98 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs @@ -443,6 +443,9 @@ public record NestedConfig(string MyProp); public class OptionWithCollectionProperties { private int _otherCode; + private int _otherCodeNullable; + private object _otherCodeNull; + private Uri _otherCodeUri; private ICollection blacklist = new HashSet(); public ICollection Blacklist @@ -460,12 +463,31 @@ public ICollection Blacklist // ParsedBlacklist initialized using the setter of Blacklist. public ICollection ParsedBlacklist { get; private set; } = new HashSet(); - // This property not having any match in the configuration. Still the setter need to be called during the binding. + // This does not have a match in the configuration, however the setter should be called during the binding: public int OtherCode { get => _otherCode; set => _otherCode = value == 0 ? 2 : value; } + + // These do not have any match in the configuration, and the setters should not be called during the binding: + public int? OtherCodeNullable + { + get => _otherCodeNullable; + set => _otherCodeNullable = !value.HasValue ? 3 : value.Value; + } + + public object? OtherCodeNull + { + get => _otherCodeNull; + set => _otherCodeNull = value is null ? 4 : value; + } + + public Uri OtherCodeUri + { + get => _otherCodeUri; + set => _otherCodeUri = value is null ? new Uri("hello") : value; + } } public interface ISomeInterface diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs index 2a6006b38a2711..d755d02a1ce068 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs @@ -1712,13 +1712,12 @@ public void EnsureCallingThePropertySetter() Assert.Equal(2, options.ParsedBlacklist.Count); // should be initialized when calling the options.Blacklist setter. Assert.Equal(401, options.HttpStatusCode); // exists in configuration and properly sets the property -#if BUILDING_SOURCE_GENERATOR_TESTS - // Setter not called if there's no matching configuration value. - Assert.Equal(0, options.OtherCode); -#else - // doesn't exist in configuration. the setter sets default value '2' - Assert.Equal(2, options.OtherCode); -#endif + Assert.Equal(2, options.OtherCode); // doesn't exist in configuration. the setter sets default value '2' + + // These don't exist in configuration and setters are not called. + Assert.Equal(0, options.OtherCodeNullable); + Assert.Null(options.OtherCodeNull); + Assert.Null(options.OtherCodeUri); } [Fact] diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt index e5b37775cffd4b..0f57607077cdd1 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt @@ -126,12 +126,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value0) + { + instance.MyString = value0!; + } if (configuration["MyInt"] is string value1) { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section2) { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt index fc35d29e693349..efd58d05fa4e80 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt @@ -90,12 +90,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value0) + { + instance.MyString = value0!; + } if (configuration["MyInt"] is string value1) { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section2) { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt index 0d3eb884c966e2..a32a635d861679 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt @@ -90,12 +90,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value0) + { + instance.MyString = value0!; + } if (configuration["MyInt"] is string value1) { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section2) { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt index 392533daea462f..c0f0b9f997c378 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt @@ -90,12 +90,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value0) + { + instance.MyString = value0!; + } if (configuration["MyInt"] is string value1) { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section2) { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt index d0faaa8fc91513..e7af5356422c4f 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt @@ -116,12 +116,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value4) + { + instance.MyString = value4!; + } if (configuration["MyInt"] is string value5) { instance.MyInt = ParseInt(value5, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section6) { @@ -156,6 +163,10 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value15, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt index 0b7031b1da00ba..9af1984a85e490 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt @@ -97,12 +97,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value3) + { + instance.MyString = value3!; + } if (configuration["MyInt"] is string value4) { instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section5) { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt index 4c950a79266d11..c7557069602316 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt @@ -97,12 +97,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value3) + { + instance.MyString = value3!; + } if (configuration["MyInt"] is string value4) { instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section5) { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt index ca323c234b8485..290f0d5aad6c11 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt @@ -70,6 +70,10 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt index e4a376a0cb3257..768b66cc4be753 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt @@ -70,6 +70,10 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt index f3829683be2bb3..66bf324796a946 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt @@ -104,12 +104,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value1) + { + instance.MyString = value1!; + } if (configuration["MyInt"] is string value2) { instance.MyInt = ParseInt(value2, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section3) { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt index de6ea5a3b9e1c6..00e71ae3a18b20 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt @@ -114,12 +114,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value1) + { + instance.MyString = value1!; + } if (configuration["MyInt"] is string value2) { instance.MyInt = ParseInt(value2, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section3) { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt index 2c40ebf69c9b46..98df8b5d56bfec 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt @@ -108,12 +108,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value1) + { + instance.MyString = value1!; + } if (configuration["MyInt"] is string value2) { instance.MyInt = ParseInt(value2, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section3) { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt index 7d94145d42a511..7fe5faa50da1e0 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt @@ -60,150 +60,256 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop0 = ParseBool(value0, () => configuration.GetSection("Prop0").Path); } + else + { + instance.Prop0 = default; + } if (configuration["Prop1"] is string value1) { instance.Prop1 = ParseByte(value1, () => configuration.GetSection("Prop1").Path); } + else + { + instance.Prop1 = default; + } if (configuration["Prop2"] is string value2) { instance.Prop2 = ParseSbyte(value2, () => configuration.GetSection("Prop2").Path); } + else + { + instance.Prop2 = default; + } if (configuration["Prop3"] is string value3) { instance.Prop3 = ParseChar(value3, () => configuration.GetSection("Prop3").Path); } + else + { + instance.Prop3 = default; + } if (configuration["Prop4"] is string value4) { instance.Prop4 = ParseDouble(value4, () => configuration.GetSection("Prop4").Path); } + else + { + instance.Prop4 = default; + } - instance.Prop5 = configuration["Prop5"]!; + if (configuration["Prop5"] is string value5) + { + instance.Prop5 = value5!; + } if (configuration["Prop6"] is string value6) { instance.Prop6 = ParseInt(value6, () => configuration.GetSection("Prop6").Path); } + else + { + instance.Prop6 = default; + } if (configuration["Prop8"] is string value7) { instance.Prop8 = ParseShort(value7, () => configuration.GetSection("Prop8").Path); } + else + { + instance.Prop8 = default; + } if (configuration["Prop9"] is string value8) { instance.Prop9 = ParseLong(value8, () => configuration.GetSection("Prop9").Path); } + else + { + instance.Prop9 = default; + } if (configuration["Prop10"] is string value9) { instance.Prop10 = ParseFloat(value9, () => configuration.GetSection("Prop10").Path); } + else + { + instance.Prop10 = default; + } if (configuration["Prop13"] is string value10) { instance.Prop13 = ParseUshort(value10, () => configuration.GetSection("Prop13").Path); } + else + { + instance.Prop13 = default; + } if (configuration["Prop14"] is string value11) { instance.Prop14 = ParseUint(value11, () => configuration.GetSection("Prop14").Path); } + else + { + instance.Prop14 = default; + } if (configuration["Prop15"] is string value12) { instance.Prop15 = ParseUlong(value12, () => configuration.GetSection("Prop15").Path); } + else + { + instance.Prop15 = default; + } - instance.Prop16 = configuration["Prop16"]!; + if (configuration["Prop16"] is string value13) + { + instance.Prop16 = value13!; + } if (configuration["Prop17"] is string value14) { - instance.Prop17 = ParseCultureInfo(value14, () => configuration.GetSection("Prop17").Path); + instance.Prop17 = ParseCultureInfo(value14, () => configuration.GetSection("Prop17").Path)!; } if (configuration["Prop19"] is string value15) { instance.Prop19 = ParseDateTime(value15, () => configuration.GetSection("Prop19").Path); } + else + { + instance.Prop19 = default; + } if (configuration["Prop20"] is string value16) { instance.Prop20 = ParseDateTimeOffset(value16, () => configuration.GetSection("Prop20").Path); } + else + { + instance.Prop20 = default; + } if (configuration["Prop21"] is string value17) { instance.Prop21 = ParseDecimal(value17, () => configuration.GetSection("Prop21").Path); } + else + { + instance.Prop21 = default; + } if (configuration["Prop23"] is string value18) { instance.Prop23 = ParseTimeSpan(value18, () => configuration.GetSection("Prop23").Path); } + else + { + instance.Prop23 = default; + } if (configuration["Prop24"] is string value19) { instance.Prop24 = ParseGuid(value19, () => configuration.GetSection("Prop24").Path); } + else + { + instance.Prop24 = default; + } if (configuration["Prop25"] is string value20) { - instance.Prop25 = ParseUri(value20, () => configuration.GetSection("Prop25").Path); + instance.Prop25 = ParseUri(value20, () => configuration.GetSection("Prop25").Path)!; } if (configuration["Prop26"] is string value21) { - instance.Prop26 = ParseVersion(value21, () => configuration.GetSection("Prop26").Path); + instance.Prop26 = ParseVersion(value21, () => configuration.GetSection("Prop26").Path)!; } if (configuration["Prop27"] is string value22) { instance.Prop27 = ParseEnum(value22, () => configuration.GetSection("Prop27").Path); } + else + { + instance.Prop27 = default; + } if (configuration["Prop7"] is string value23) { instance.Prop7 = ParseInt128(value23, () => configuration.GetSection("Prop7").Path); } + else + { + instance.Prop7 = default; + } if (configuration["Prop11"] is string value24) { instance.Prop11 = ParseHalf(value24, () => configuration.GetSection("Prop11").Path); } + else + { + instance.Prop11 = default; + } if (configuration["Prop12"] is string value25) { instance.Prop12 = ParseUInt128(value25, () => configuration.GetSection("Prop12").Path); } + else + { + instance.Prop12 = default; + } if (configuration["Prop18"] is string value26) { instance.Prop18 = ParseDateOnly(value26, () => configuration.GetSection("Prop18").Path); } + else + { + instance.Prop18 = default; + } if (configuration["Prop22"] is string value27) { instance.Prop22 = ParseTimeOnly(value27, () => configuration.GetSection("Prop22").Path); } + else + { + instance.Prop22 = default; + } if (configuration["Prop28"] is string value28) { - instance.Prop28 = ParseByteArray(value28, () => configuration.GetSection("Prop28").Path); + instance.Prop28 = ParseByteArray(value28, () => configuration.GetSection("Prop28").Path)!; } if (configuration["Prop29"] is string value29) { instance.Prop29 = ParseInt(value29, () => configuration.GetSection("Prop29").Path); } + else + { + instance.Prop29 = default; + } if (configuration["Prop30"] is string value30) { instance.Prop30 = ParseDateTime(value30, () => configuration.GetSection("Prop30").Path); } + else + { + instance.Prop30 = default; + } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt index 973895e09fe55e..7c707ad1bf5bde 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt @@ -105,6 +105,10 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } } public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) @@ -132,12 +136,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value3) + { + instance.MyString = value3!; + } if (configuration["MyInt"] is string value4) { instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section5) { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt index 08f233aff5503b..7ca01519f6cc22 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt @@ -105,6 +105,10 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } } public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) @@ -132,12 +136,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value3) + { + instance.MyString = value3!; + } if (configuration["MyInt"] is string value4) { instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section5) { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt index cb41ea860980a9..0b1e1231725f5b 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt @@ -105,6 +105,10 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } } public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) @@ -132,12 +136,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value3) + { + instance.MyString = value3!; + } if (configuration["MyInt"] is string value4) { instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section5) { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt index 0a5e5a7ff6d26c..9239806d3e089e 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt @@ -99,6 +99,10 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } } public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) @@ -126,12 +130,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); - instance.MyString = configuration["MyString"]!; + if (configuration["MyString"] is string value3) + { + instance.MyString = value3!; + } if (configuration["MyInt"] is string value4) { instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path); } + else + { + instance.MyInt = default; + } if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section5) { From 9e3f3f797bf9a87f045d8cf21b8530134f989ada Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Tue, 12 Sep 2023 19:28:17 -0500 Subject: [PATCH 2/6] Don't default value during initialization --- .../gen/Emitter/ConfigurationBinder.cs | 2 +- .../gen/Emitter/CoreBindingHelpers.cs | 19 ++++--- .../ConfigurationBinderTests.TestClasses.cs | 24 ++++++++- .../tests/Common/ConfigurationBinderTests.cs | 40 +++++++++++++- .../Baselines/Collections.generated.txt | 28 +++++----- .../ConfigurationBinder/Bind.generated.txt | 22 ++++---- .../Bind_Instance.generated.txt | 18 +++---- .../Bind_Instance_BinderOptions.generated.txt | 18 +++---- .../Bind_Key_Instance.generated.txt | 18 +++---- .../ConfigurationBinder/Get.generated.txt | 26 ++++----- .../ConfigurationBinder/Get_T.generated.txt | 20 +++---- .../Get_T_BinderOptions.generated.txt | 20 +++---- .../Get_TypeOf.generated.txt | 6 +-- .../Get_TypeOf_BinderOptions.generated.txt | 6 +-- .../Baselines/EmptyConfigType.generated.txt | 4 +- .../BindConfiguration.generated.txt | 10 ++-- .../OptionsBuilder/Bind_T.generated.txt | 10 ++-- .../Bind_T_BinderOptions.generated.txt | 10 ++-- .../Baselines/Primitives.generated.txt | 54 +++++++++---------- .../Configure_T.generated.txt | 24 ++++----- .../Configure_T_BinderOptions.generated.txt | 24 ++++----- .../Configure_T_name.generated.txt | 24 ++++----- ...nfigure_T_name_BinderOptions.generated.txt | 24 ++++----- 23 files changed, 256 insertions(+), 195 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/ConfigurationBinder.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/ConfigurationBinder.cs index c4f128cffd6c1d..781bbc22a08b64 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/ConfigurationBinder.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/ConfigurationBinder.cs @@ -148,7 +148,7 @@ void EmitMethods(MethodsToGen_ConfigurationBinder method, string additionalParam EmitCheckForNullArgument_WithBlankLine(Identifier.instance, voidReturn: true); _writer.WriteLine($$""" var {{Identifier.typedObj}} = ({{type.EffectiveType.DisplayString}}){{Identifier.instance}}; - {{nameof(MethodsToGen_CoreBindingHelper.BindCore)}}({{configExpression}}, ref {{Identifier.typedObj}}, {{binderOptionsArg}}); + {{nameof(MethodsToGen_CoreBindingHelper.BindCore)}}({{configExpression}}, ref {{Identifier.typedObj}}, false, {{binderOptionsArg}}); """); } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs index e25b8696332a9a..caee2b17a11289 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs @@ -237,7 +237,7 @@ private void EmitBindCoreMethods() private void EmitBindCoreMethod(ComplexTypeSpec type) { string objParameterExpression = $"ref {type.DisplayString} {Identifier.instance}"; - EmitStartBlock(@$"public static void {nameof(MethodsToGen_CoreBindingHelper.BindCore)}({Identifier.IConfiguration} {Identifier.configuration}, {objParameterExpression}, {Identifier.BinderOptions}? {Identifier.binderOptions})"); + EmitStartBlock(@$"public static void {nameof(MethodsToGen_CoreBindingHelper.BindCore)}({Identifier.IConfiguration} {Identifier.configuration}, {objParameterExpression}, bool defaultValueIfNotFound, {Identifier.BinderOptions}? {Identifier.binderOptions})"); ComplexTypeSpec effectiveType = (ComplexTypeSpec)type.EffectiveType; if (effectiveType is EnumerableSpec enumerable) @@ -383,7 +383,8 @@ void EmitBindImplForMember(MemberSpec member) member, parsedMemberAssignmentLhsExpr, sectionPathExpr: GetSectionPathFromConfigurationExpression(configKeyName), - canSet: true); + canSet: true, + firstTimeInitialization: true); if (canBindToMember) { @@ -783,7 +784,8 @@ private void EmitBindCoreImplForObject(ObjectSpec type) property, memberAccessExpr: $"{containingTypeRef}.{property.Name}", GetSectionPathFromConfigurationExpression(property.ConfigurationKeyName), - canSet: property.CanSet); + canSet: property.CanSet, + firstTimeInitialization: false); } } } @@ -792,7 +794,8 @@ private bool EmitBindImplForMember( MemberSpec member, string memberAccessExpr, string sectionPathExpr, - bool canSet) + bool canSet, + bool firstTimeInitialization) { TypeSpec effectiveMemberType = member.Type.EffectiveType; @@ -805,8 +808,8 @@ private bool EmitBindImplForMember( if (canSet) { string nullBangExpr = member.Type.IsValueType ? string.Empty : "!"; - bool useDefaultValueIfSectionValueIsNull = member is PropertySpec && - //stringParsableType.StringParsableTypeKind is not StringParsableTypeKind.AssignFromSectionValue && + bool useDefaultValueIfSectionValueIsNull = !firstTimeInitialization && + member is PropertySpec && member.Type.IsValueType && member.Type.SpecKind is not TypeSpecKind.Nullable; @@ -963,7 +966,7 @@ private void EmitBindingLogic( void EmitBindingLogic(string instanceToBindExpr, InitializationKind initKind) { - string bindCoreCall = $@"{nameof(MethodsToGen_CoreBindingHelper.BindCore)}({configArgExpr}, ref {instanceToBindExpr}, {Identifier.binderOptions});"; + string bindCoreCall = $@"{nameof(MethodsToGen_CoreBindingHelper.BindCore)}({configArgExpr}, ref {instanceToBindExpr}, true, {Identifier.binderOptions});"; if (type.CanInstantiate) { @@ -1033,7 +1036,7 @@ private void EmitBindingLogic( if (useDefaultValueIfSectionValueIsNull) { parsedValueExpr = $"default"; - EmitStartBlock($"else"); + EmitStartBlock($"else if (defaultValueIfNotFound)"); InvokeWriteOnSuccess(); EmitEndBlock(); } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs index 46a5d87245fb98..4e06a9c5b03b4e 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs @@ -470,7 +470,7 @@ public int OtherCode set => _otherCode = value == 0 ? 2 : value; } - // These do not have any match in the configuration, and the setters should not be called during the binding: + // These do not have any match in the configuration, and the setters should not be called during the binding: public int? OtherCodeNullable { get => _otherCodeNullable; @@ -818,5 +818,27 @@ internal class ClassWith_DirectlyAssignable_CtorParams public ClassWith_DirectlyAssignable_CtorParams(IConfigurationSection mySection, object myObject, string myString) => (MySection, MyObject, MyString) = (mySection, myObject, myString); } + + public class SharedChildInstance_Class + { + public string? ConnectionString { get; set; } + } + + public class ClassThatThrowsOnSetters + { + private int _myIntProperty; + + public ClassThatThrowsOnSetters() + { + _myIntProperty = 42; + } + + public int MyIntProperty + { + get => _myIntProperty; + set => throw new InvalidOperationException("Not expected"); + } + } + } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs index d755d02a1ce068..376e9fd120a5d0 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs @@ -1712,14 +1712,30 @@ public void EnsureCallingThePropertySetter() Assert.Equal(2, options.ParsedBlacklist.Count); // should be initialized when calling the options.Blacklist setter. Assert.Equal(401, options.HttpStatusCode); // exists in configuration and properly sets the property - Assert.Equal(2, options.OtherCode); // doesn't exist in configuration. the setter sets default value '2' - // These don't exist in configuration and setters are not called. + // This doesn't exist in configuration but the setter should be called which defaults the to '2' from input of '0'. + Assert.Equal(2, options.OtherCode); + + // These don't exist in configuration and setters are not called since they are nullable. Assert.Equal(0, options.OtherCodeNullable); Assert.Null(options.OtherCodeNull); Assert.Null(options.OtherCodeUri); } + [Fact] + public void EnsureNotCallingThePropertySetterOnBindWhenNoOwningConfig() + { + var builder = new ConfigurationBuilder(); + builder.AddInMemoryCollection(new KeyValuePair[] { }); + var config = builder.Build(); + + ClassThatThrowsOnSetters instance = new(); + + // The setter for MyIntProperty throws. + config.GetSection("Dmy").Bind(instance); + Assert.Equal(42, instance.MyIntProperty); + } + [Fact] public void EnsureSuccessfullyBind() { @@ -2320,5 +2336,25 @@ public void IConfigurationSectionAsCtorParam() Assert.Equal("MyObject", obj.MyObject); Assert.Equal("MyString", obj.MyString); } + + [Fact] + public void SharedChildInstance() + { + var builder = new ConfigurationBuilder(); + builder.AddInMemoryCollection(new KeyValuePair[] + { + new("A:B:ConnectionString", "localhost"), + }); + + var config = builder.Build(); + + SharedChildInstance_Class instance = new(); + config.GetSection("A:B").Bind(instance); + Assert.Equal("localhost", instance.ConnectionString); + + // Binding to a new section should not set the value to null. + config.GetSection("A").Bind(instance); + Assert.Equal("localhost", instance.ConnectionString); + } } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt index 775d9052ded435..0cf1955ebc198a 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt @@ -56,14 +56,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClassWithCustomCollections)) { var instance = new Program.MyClassWithCustomCollections(); - BindCore(configuration, ref instance, binderOptions); + BindCore(configuration, ref instance, true, binderOptions); return instance; } throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input."); } - public static void BindCore(IConfiguration configuration, ref Program.CustomDictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.CustomDictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -74,7 +74,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.CustomList instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.CustomList instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -85,7 +85,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -96,7 +96,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref ICollection instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref ICollection instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -107,7 +107,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref IReadOnlyList instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref IReadOnlyList instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { if (instance is not ICollection temp) { @@ -123,7 +123,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -134,7 +134,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref IDictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref IDictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -145,7 +145,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref IReadOnlyDictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref IReadOnlyDictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { if (instance is not IDictionary temp) { @@ -161,7 +161,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClassWithCustomCollections instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClassWithCustomCollections instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClassWithCustomCollections), s_configKeys_ProgramMyClassWithCustomCollections, configuration, binderOptions); @@ -169,7 +169,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Program.CustomDictionary? temp3 = instance.CustomDictionary; temp3 ??= new Program.CustomDictionary(); - BindCore(section1, ref temp3, binderOptions); + BindCore(section1, ref temp3, true, binderOptions); instance.CustomDictionary = temp3; } @@ -177,7 +177,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Program.CustomList? temp6 = instance.CustomList; temp6 ??= new Program.CustomList(); - BindCore(section4, ref temp6, binderOptions); + BindCore(section4, ref temp6, true, binderOptions); instance.CustomList = temp6; } @@ -185,7 +185,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { IReadOnlyList? temp9 = instance.IReadOnlyList; temp9 = temp9 is null ? new List() : new List(temp9); - BindCore(section7, ref temp9, binderOptions); + BindCore(section7, ref temp9, true, binderOptions); instance.IReadOnlyList = temp9; } @@ -193,7 +193,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { IReadOnlyDictionary? temp12 = instance.IReadOnlyDictionary; temp12 = temp12 is null ? new Dictionary() : temp12.ToDictionary(pair => pair.Key, pair => pair.Value); - BindCore(section10, ref temp12, binderOptions); + BindCore(section10, ref temp12, true, binderOptions); instance.IReadOnlyDictionary = temp12; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt index 0f57607077cdd1..22665ee57e70b7 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt @@ -45,7 +45,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration, ref typedObj, binderOptions: null); + BindCore(configuration, ref typedObj, false, binderOptions: null); } /// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively. @@ -63,7 +63,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration, ref typedObj, GetBinderOptions(configureOptions)); + BindCore(configuration, ref typedObj, false, GetBinderOptions(configureOptions)); } /// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively. @@ -81,14 +81,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration.GetSection(key), ref typedObj, binderOptions: null); + BindCore(configuration.GetSection(key), ref typedObj, false, binderOptions: null); } #endregion IConfiguration extensions. #region Core binding extensions. private readonly static Lazy> s_configKeys_ProgramMyClass = new(() => new HashSet(StringComparer.OrdinalIgnoreCase) { "MyString", "MyInt", "MyList", "MyDictionary", "MyComplexDictionary" }); - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -99,7 +99,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -110,7 +110,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -122,7 +122,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -135,7 +135,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -144,7 +144,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp4 = instance.MyList; temp4 ??= new List(); - BindCore(section2, ref temp4, binderOptions); + BindCore(section2, ref temp4, true, binderOptions); instance.MyList = temp4; } @@ -152,7 +152,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp7 = instance.MyDictionary; temp7 ??= new Dictionary(); - BindCore(section5, ref temp7, binderOptions); + BindCore(section5, ref temp7, true, binderOptions); instance.MyDictionary = temp7; } @@ -160,7 +160,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp10 = instance.MyComplexDictionary; temp10 ??= new Dictionary(); - BindCore(section8, ref temp10, binderOptions); + BindCore(section8, ref temp10, true, binderOptions); instance.MyComplexDictionary = temp10; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt index efd58d05fa4e80..d015c47fea35f7 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt @@ -45,14 +45,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration, ref typedObj, binderOptions: null); + BindCore(configuration, ref typedObj, false, binderOptions: null); } #endregion IConfiguration extensions. #region Core binding extensions. private readonly static Lazy> s_configKeys_ProgramMyClass = new(() => new HashSet(StringComparer.OrdinalIgnoreCase) { "MyString", "MyInt", "MyList", "MyDictionary", "MyComplexDictionary" }); - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -63,7 +63,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -74,7 +74,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -86,7 +86,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -99,7 +99,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -108,7 +108,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp4 = instance.MyList; temp4 ??= new List(); - BindCore(section2, ref temp4, binderOptions); + BindCore(section2, ref temp4, true, binderOptions); instance.MyList = temp4; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp7 = instance.MyDictionary; temp7 ??= new Dictionary(); - BindCore(section5, ref temp7, binderOptions); + BindCore(section5, ref temp7, true, binderOptions); instance.MyDictionary = temp7; } @@ -124,7 +124,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp10 = instance.MyComplexDictionary; temp10 ??= new Dictionary(); - BindCore(section8, ref temp10, binderOptions); + BindCore(section8, ref temp10, true, binderOptions); instance.MyComplexDictionary = temp10; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt index a32a635d861679..7ca454eb1884a8 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt @@ -45,14 +45,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration, ref typedObj, GetBinderOptions(configureOptions)); + BindCore(configuration, ref typedObj, false, GetBinderOptions(configureOptions)); } #endregion IConfiguration extensions. #region Core binding extensions. private readonly static Lazy> s_configKeys_ProgramMyClass = new(() => new HashSet(StringComparer.OrdinalIgnoreCase) { "MyString", "MyInt", "MyList", "MyDictionary", "MyComplexDictionary" }); - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -63,7 +63,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -74,7 +74,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -86,7 +86,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -99,7 +99,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -108,7 +108,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp4 = instance.MyList; temp4 ??= new List(); - BindCore(section2, ref temp4, binderOptions); + BindCore(section2, ref temp4, true, binderOptions); instance.MyList = temp4; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp7 = instance.MyDictionary; temp7 ??= new Dictionary(); - BindCore(section5, ref temp7, binderOptions); + BindCore(section5, ref temp7, true, binderOptions); instance.MyDictionary = temp7; } @@ -124,7 +124,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp10 = instance.MyComplexDictionary; temp10 ??= new Dictionary(); - BindCore(section8, ref temp10, binderOptions); + BindCore(section8, ref temp10, true, binderOptions); instance.MyComplexDictionary = temp10; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt index c0f0b9f997c378..2174a01e5b952e 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt @@ -45,14 +45,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration.GetSection(key), ref typedObj, binderOptions: null); + BindCore(configuration.GetSection(key), ref typedObj, false, binderOptions: null); } #endregion IConfiguration extensions. #region Core binding extensions. private readonly static Lazy> s_configKeys_ProgramMyClass = new(() => new HashSet(StringComparer.OrdinalIgnoreCase) { "MyString", "MyInt", "MyList", "MyDictionary", "MyComplexDictionary" }); - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -63,7 +63,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -74,7 +74,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -86,7 +86,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -99,7 +99,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -108,7 +108,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp4 = instance.MyList; temp4 ??= new List(); - BindCore(section2, ref temp4, binderOptions); + BindCore(section2, ref temp4, true, binderOptions); instance.MyList = temp4; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp7 = instance.MyDictionary; temp7 ??= new Dictionary(); - BindCore(section5, ref temp7, binderOptions); + BindCore(section5, ref temp7, true, binderOptions); instance.MyDictionary = temp7; } @@ -124,7 +124,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp10 = instance.MyComplexDictionary; temp10 ??= new Dictionary(); - BindCore(section8, ref temp10, binderOptions); + BindCore(section8, ref temp10, true, binderOptions); instance.MyComplexDictionary = temp10; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt index e7af5356422c4f..2adcc1f8a3a92f 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt @@ -68,20 +68,20 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var instance = new Program.MyClass(); - BindCore(configuration, ref instance, binderOptions); + BindCore(configuration, ref instance, true, binderOptions); return instance; } else if (type == typeof(Program.MyClass2)) { var instance = new Program.MyClass2(); - BindCore(configuration, ref instance, binderOptions); + BindCore(configuration, ref instance, true, binderOptions); return instance; } throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input."); } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -92,16 +92,16 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref int[] instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref int[] instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { var temp2 = new List(); - BindCore(configuration, ref temp2, binderOptions); + BindCore(configuration, ref temp2, true, binderOptions); int originalCount = instance.Length; Array.Resize(ref instance, originalCount + temp2.Count); temp2.CopyTo(instance, originalCount); } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -112,7 +112,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -125,7 +125,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value5, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -134,7 +134,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp8 = instance.MyList; temp8 ??= new List(); - BindCore(section6, ref temp8, binderOptions); + BindCore(section6, ref temp8, true, binderOptions); instance.MyList = temp8; } @@ -142,7 +142,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { int[]? temp11 = instance.MyArray; temp11 ??= new int[0]; - BindCore(section9, ref temp11, binderOptions); + BindCore(section9, ref temp11, true, binderOptions); instance.MyArray = temp11; } @@ -150,12 +150,12 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp14 = instance.MyDictionary; temp14 ??= new Dictionary(); - BindCore(section12, ref temp14, binderOptions); + BindCore(section12, ref temp14, true, binderOptions); instance.MyDictionary = temp14; } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass2), s_configKeys_ProgramMyClass2, configuration, binderOptions); @@ -163,7 +163,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value15, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt index 9af1984a85e490..b6ea538cf23834 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt @@ -55,14 +55,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var instance = new Program.MyClass(); - BindCore(configuration, ref instance, binderOptions); + BindCore(configuration, ref instance, true, binderOptions); return instance; } throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input."); } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -73,16 +73,16 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref int[] instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref int[] instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { var temp1 = new List(); - BindCore(configuration, ref temp1, binderOptions); + BindCore(configuration, ref temp1, true, binderOptions); int originalCount = instance.Length; Array.Resize(ref instance, originalCount + temp1.Count); temp1.CopyTo(instance, originalCount); } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -93,7 +93,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -106,7 +106,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -115,7 +115,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, binderOptions); + BindCore(section5, ref temp7, true, binderOptions); instance.MyList = temp7; } @@ -123,7 +123,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { int[]? temp10 = instance.MyArray; temp10 ??= new int[0]; - BindCore(section8, ref temp10, binderOptions); + BindCore(section8, ref temp10, true, binderOptions); instance.MyArray = temp10; } @@ -131,7 +131,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, binderOptions); + BindCore(section11, ref temp13, true, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt index c7557069602316..37b08853999769 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt @@ -55,14 +55,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var instance = new Program.MyClass(); - BindCore(configuration, ref instance, binderOptions); + BindCore(configuration, ref instance, true, binderOptions); return instance; } throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input."); } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -73,16 +73,16 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref int[] instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref int[] instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { var temp1 = new List(); - BindCore(configuration, ref temp1, binderOptions); + BindCore(configuration, ref temp1, true, binderOptions); int originalCount = instance.Length; Array.Resize(ref instance, originalCount + temp1.Count); temp1.CopyTo(instance, originalCount); } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -93,7 +93,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -106,7 +106,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -115,7 +115,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, binderOptions); + BindCore(section5, ref temp7, true, binderOptions); instance.MyList = temp7; } @@ -123,7 +123,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { int[]? temp10 = instance.MyArray; temp10 ??= new int[0]; - BindCore(section8, ref temp10, binderOptions); + BindCore(section8, ref temp10, true, binderOptions); instance.MyArray = temp10; } @@ -131,7 +131,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, binderOptions); + BindCore(section11, ref temp13, true, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt index 290f0d5aad6c11..77155c7c60c7ae 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt @@ -55,14 +55,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass2)) { var instance = new Program.MyClass2(); - BindCore(configuration, ref instance, binderOptions); + BindCore(configuration, ref instance, true, binderOptions); return instance; } throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input."); } - public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass2), s_configKeys_ProgramMyClass2, configuration, binderOptions); @@ -70,7 +70,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt index 768b66cc4be753..114eb62ee3a122 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt @@ -55,14 +55,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass2)) { var instance = new Program.MyClass2(); - BindCore(configuration, ref instance, binderOptions); + BindCore(configuration, ref instance, true, binderOptions); return instance; } throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input."); } - public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass2), s_configKeys_ProgramMyClass2, configuration, binderOptions); @@ -70,7 +70,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/EmptyConfigType.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/EmptyConfigType.generated.txt index c3db7a0ff408a5..6228da0b9cbb9f 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/EmptyConfigType.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/EmptyConfigType.generated.txt @@ -51,14 +51,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (TypeWithNoMembers_Wrapper)instance; - BindCore(configuration, ref typedObj, binderOptions: null); + BindCore(configuration, ref typedObj, false, binderOptions: null); } #endregion IConfiguration extensions. #region Core binding extensions. private readonly static Lazy> s_configKeys_TypeWithNoMembers_Wrapper = new(() => new HashSet(StringComparer.OrdinalIgnoreCase) { "Member" }); - public static void BindCore(IConfiguration configuration, ref TypeWithNoMembers_Wrapper instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref TypeWithNoMembers_Wrapper instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(TypeWithNoMembers_Wrapper), s_configKeys_TypeWithNoMembers_Wrapper, configuration, binderOptions); diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt index 66bf324796a946..2f7f7151a12ceb 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt @@ -82,14 +82,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, binderOptions); + BindCore(configuration, ref temp, true, binderOptions); return; } throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input."); } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -100,7 +100,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -113,7 +113,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value2, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -122,7 +122,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp5 = instance.MyList; temp5 ??= new List(); - BindCore(section3, ref temp5, binderOptions); + BindCore(section3, ref temp5, true, binderOptions); instance.MyList = temp5; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt index 00e71ae3a18b20..f1c771c92b2eed 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt @@ -92,14 +92,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, binderOptions); + BindCore(configuration, ref temp, true, binderOptions); return; } throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input."); } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -110,7 +110,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -123,7 +123,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value2, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -132,7 +132,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp5 = instance.MyList; temp5 ??= new List(); - BindCore(section3, ref temp5, binderOptions); + BindCore(section3, ref temp5, true, binderOptions); instance.MyList = temp5; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt index 98df8b5d56bfec..db78b0a816f28f 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt @@ -86,14 +86,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, binderOptions); + BindCore(configuration, ref temp, true, binderOptions); return; } throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input."); } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -104,7 +104,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -117,7 +117,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value2, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -126,7 +126,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp5 = instance.MyList; temp5 ??= new List(); - BindCore(section3, ref temp5, binderOptions); + BindCore(section3, ref temp5, true, binderOptions); instance.MyList = temp5; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt index 7fe5faa50da1e0..66981ed011e72b 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt @@ -45,14 +45,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration, ref typedObj, binderOptions: null); + BindCore(configuration, ref typedObj, false, binderOptions: null); } #endregion IConfiguration extensions. #region Core binding extensions. private readonly static Lazy> s_configKeys_ProgramMyClass = new(() => new HashSet(StringComparer.OrdinalIgnoreCase) { "Prop0", "Prop1", "Prop2", "Prop3", "Prop4", "Prop5", "Prop6", "Prop8", "Prop9", "Prop10", "Prop13", "Prop14", "Prop15", "Prop16", "Prop17", "Prop19", "Prop20", "Prop21", "Prop23", "Prop24", "Prop25", "Prop26", "Prop27", "Prop7", "Prop11", "Prop12", "Prop18", "Prop22", "Prop28", "Prop29", "Prop30" }); - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -60,7 +60,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop0 = ParseBool(value0, () => configuration.GetSection("Prop0").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop0 = default; } @@ -69,7 +69,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop1 = ParseByte(value1, () => configuration.GetSection("Prop1").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop1 = default; } @@ -78,7 +78,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop2 = ParseSbyte(value2, () => configuration.GetSection("Prop2").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop2 = default; } @@ -87,7 +87,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop3 = ParseChar(value3, () => configuration.GetSection("Prop3").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop3 = default; } @@ -96,7 +96,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop4 = ParseDouble(value4, () => configuration.GetSection("Prop4").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop4 = default; } @@ -110,7 +110,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop6 = ParseInt(value6, () => configuration.GetSection("Prop6").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop6 = default; } @@ -119,7 +119,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop8 = ParseShort(value7, () => configuration.GetSection("Prop8").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop8 = default; } @@ -128,7 +128,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop9 = ParseLong(value8, () => configuration.GetSection("Prop9").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop9 = default; } @@ -137,7 +137,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop10 = ParseFloat(value9, () => configuration.GetSection("Prop10").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop10 = default; } @@ -146,7 +146,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop13 = ParseUshort(value10, () => configuration.GetSection("Prop13").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop13 = default; } @@ -155,7 +155,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop14 = ParseUint(value11, () => configuration.GetSection("Prop14").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop14 = default; } @@ -164,7 +164,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop15 = ParseUlong(value12, () => configuration.GetSection("Prop15").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop15 = default; } @@ -183,7 +183,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop19 = ParseDateTime(value15, () => configuration.GetSection("Prop19").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop19 = default; } @@ -192,7 +192,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop20 = ParseDateTimeOffset(value16, () => configuration.GetSection("Prop20").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop20 = default; } @@ -201,7 +201,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop21 = ParseDecimal(value17, () => configuration.GetSection("Prop21").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop21 = default; } @@ -210,7 +210,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop23 = ParseTimeSpan(value18, () => configuration.GetSection("Prop23").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop23 = default; } @@ -219,7 +219,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop24 = ParseGuid(value19, () => configuration.GetSection("Prop24").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop24 = default; } @@ -238,7 +238,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop27 = ParseEnum(value22, () => configuration.GetSection("Prop27").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop27 = default; } @@ -247,7 +247,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop7 = ParseInt128(value23, () => configuration.GetSection("Prop7").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop7 = default; } @@ -256,7 +256,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop11 = ParseHalf(value24, () => configuration.GetSection("Prop11").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop11 = default; } @@ -265,7 +265,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop12 = ParseUInt128(value25, () => configuration.GetSection("Prop12").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop12 = default; } @@ -274,7 +274,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop18 = ParseDateOnly(value26, () => configuration.GetSection("Prop18").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop18 = default; } @@ -283,7 +283,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop22 = ParseTimeOnly(value27, () => configuration.GetSection("Prop22").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop22 = default; } @@ -297,7 +297,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop29 = ParseInt(value29, () => configuration.GetSection("Prop29").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop29 = default; } @@ -306,7 +306,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.Prop30 = ParseDateTime(value30, () => configuration.GetSection("Prop30").Path); } - else + else if (defaultValueIfNotFound) { instance.Prop30 = default; } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt index 7c707ad1bf5bde..f0c3cce18518e7 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt @@ -79,14 +79,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, binderOptions); + BindCore(configuration, ref temp, true, binderOptions); return; } throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input."); } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -97,7 +97,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass2), s_configKeys_ProgramMyClass2, configuration, binderOptions); @@ -105,23 +105,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { var value = new Program.MyClass2(); - BindCore(section, ref value, binderOptions); + BindCore(section, ref value, true, binderOptions); instance.Add(value); } } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -132,7 +132,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -145,7 +145,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -154,7 +154,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, binderOptions); + BindCore(section5, ref temp7, true, binderOptions); instance.MyList = temp7; } @@ -162,7 +162,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp10 = instance.MyList2; temp10 ??= new List(); - BindCore(section8, ref temp10, binderOptions); + BindCore(section8, ref temp10, true, binderOptions); instance.MyList2 = temp10; } @@ -170,7 +170,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, binderOptions); + BindCore(section11, ref temp13, true, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt index 7ca01519f6cc22..433fac2ad5364b 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt @@ -79,14 +79,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, binderOptions); + BindCore(configuration, ref temp, true, binderOptions); return; } throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input."); } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -97,7 +97,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass2), s_configKeys_ProgramMyClass2, configuration, binderOptions); @@ -105,23 +105,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { var value = new Program.MyClass2(); - BindCore(section, ref value, binderOptions); + BindCore(section, ref value, true, binderOptions); instance.Add(value); } } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -132,7 +132,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -145,7 +145,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -154,7 +154,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, binderOptions); + BindCore(section5, ref temp7, true, binderOptions); instance.MyList = temp7; } @@ -162,7 +162,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp10 = instance.MyList2; temp10 ??= new List(); - BindCore(section8, ref temp10, binderOptions); + BindCore(section8, ref temp10, true, binderOptions); instance.MyList2 = temp10; } @@ -170,7 +170,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, binderOptions); + BindCore(section11, ref temp13, true, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt index 0b1e1231725f5b..269be4ab9b87d9 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt @@ -79,14 +79,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, binderOptions); + BindCore(configuration, ref temp, true, binderOptions); return; } throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input."); } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -97,7 +97,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass2), s_configKeys_ProgramMyClass2, configuration, binderOptions); @@ -105,23 +105,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { var value = new Program.MyClass2(); - BindCore(section, ref value, binderOptions); + BindCore(section, ref value, true, binderOptions); instance.Add(value); } } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -132,7 +132,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -145,7 +145,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -154,7 +154,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, binderOptions); + BindCore(section5, ref temp7, true, binderOptions); instance.MyList = temp7; } @@ -162,7 +162,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp10 = instance.MyList2; temp10 ??= new List(); - BindCore(section8, ref temp10, binderOptions); + BindCore(section8, ref temp10, true, binderOptions); instance.MyList2 = temp10; } @@ -170,7 +170,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, binderOptions); + BindCore(section11, ref temp13, true, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt index 9239806d3e089e..7573c8b4b13e32 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt @@ -73,14 +73,14 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, binderOptions); + BindCore(configuration, ref temp, true, binderOptions); return; } throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input."); } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -91,7 +91,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass2), s_configKeys_ProgramMyClass2, configuration, binderOptions); @@ -99,23 +99,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } } - public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref List instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { var value = new Program.MyClass2(); - BindCore(section, ref value, binderOptions); + BindCore(section, ref value, true, binderOptions); instance.Add(value); } } - public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Dictionary instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { foreach (IConfigurationSection section in configuration.GetChildren()) { @@ -126,7 +126,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } } - public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions) + public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions); @@ -139,7 +139,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path); } - else + else if (defaultValueIfNotFound) { instance.MyInt = default; } @@ -148,7 +148,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, binderOptions); + BindCore(section5, ref temp7, true, binderOptions); instance.MyList = temp7; } @@ -156,7 +156,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp10 = instance.MyList2; temp10 ??= new List(); - BindCore(section8, ref temp10, binderOptions); + BindCore(section8, ref temp10, true, binderOptions); instance.MyList2 = temp10; } @@ -164,7 +164,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, binderOptions); + BindCore(section11, ref temp13, true, binderOptions); instance.MyDictionary = temp13; } } From f5491cec19c84e21063a9f87383d484b5c056014 Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Tue, 12 Sep 2023 20:17:57 -0500 Subject: [PATCH 3/6] Remove unnecessary bang operator --- .../gen/Emitter/CoreBindingHelpers.cs | 3 +-- .../tests/Common/ConfigurationBinderTests.cs | 4 ++-- .../Baselines/ConfigurationBinder/Bind.generated.txt | 2 +- .../ConfigurationBinder/Bind_Instance.generated.txt | 2 +- .../Bind_Instance_BinderOptions.generated.txt | 2 +- .../Bind_Key_Instance.generated.txt | 2 +- .../Baselines/ConfigurationBinder/Get.generated.txt | 2 +- .../ConfigurationBinder/Get_T.generated.txt | 2 +- .../Get_T_BinderOptions.generated.txt | 2 +- .../OptionsBuilder/BindConfiguration.generated.txt | 2 +- .../Baselines/OptionsBuilder/Bind_T.generated.txt | 2 +- .../Bind_T_BinderOptions.generated.txt | 2 +- .../Baselines/Primitives.generated.txt | 12 ++++++------ .../ServiceCollection/Configure_T.generated.txt | 2 +- .../Configure_T_BinderOptions.generated.txt | 2 +- .../ServiceCollection/Configure_T_name.generated.txt | 2 +- .../Configure_T_name_BinderOptions.generated.txt | 2 +- 17 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs index caee2b17a11289..ef1e25eebf179f 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs @@ -807,7 +807,6 @@ private bool EmitBindImplForMember( { if (canSet) { - string nullBangExpr = member.Type.IsValueType ? string.Empty : "!"; bool useDefaultValueIfSectionValueIsNull = !firstTimeInitialization && member is PropertySpec && member.Type.IsValueType && @@ -818,7 +817,7 @@ member is PropertySpec && stringParsableType, $@"{Identifier.configuration}[""{member.ConfigurationKeyName}""]", sectionPathExpr, - writeOnSuccess: parsedValueExpr => _writer.WriteLine($"{memberAccessExpr} = {parsedValueExpr}{nullBangExpr};"), + writeOnSuccess: parsedValueExpr => _writer.WriteLine($"{memberAccessExpr} = {parsedValueExpr};"), checkForNullSectionValue: true, useDefaultValueIfSectionValueIsNull, useIncrementalStringValueIdentifier: true); diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs index 376e9fd120a5d0..5ecdeeda54a19a 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs @@ -1723,7 +1723,7 @@ public void EnsureCallingThePropertySetter() } [Fact] - public void EnsureNotCallingThePropertySetterOnBindWhenNoOwningConfig() + public void EnsureNotCallingSettersWhenGivenExistingInstanceNotInConfig() { var builder = new ConfigurationBuilder(); builder.AddInMemoryCollection(new KeyValuePair[] { }); @@ -1731,7 +1731,7 @@ public void EnsureNotCallingThePropertySetterOnBindWhenNoOwningConfig() ClassThatThrowsOnSetters instance = new(); - // The setter for MyIntProperty throws. + // The setter for MyIntProperty throws, so this verifies that the setter is not called. config.GetSection("Dmy").Bind(instance); Assert.Equal(42, instance.MyIntProperty); } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt index 22665ee57e70b7..d199e4fbf55fe5 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt @@ -128,7 +128,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value0) { - instance.MyString = value0!; + instance.MyString = value0; } if (configuration["MyInt"] is string value1) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt index d015c47fea35f7..449539995b9b0d 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt @@ -92,7 +92,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value0) { - instance.MyString = value0!; + instance.MyString = value0; } if (configuration["MyInt"] is string value1) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt index 7ca454eb1884a8..0070d13c7c6f1f 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt @@ -92,7 +92,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value0) { - instance.MyString = value0!; + instance.MyString = value0; } if (configuration["MyInt"] is string value1) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt index 2174a01e5b952e..22cf7d8baf5411 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt @@ -92,7 +92,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value0) { - instance.MyString = value0!; + instance.MyString = value0; } if (configuration["MyInt"] is string value1) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt index 2adcc1f8a3a92f..5ea757a2345898 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt @@ -118,7 +118,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value4) { - instance.MyString = value4!; + instance.MyString = value4; } if (configuration["MyInt"] is string value5) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt index b6ea538cf23834..fbcd830a02e3d9 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt @@ -99,7 +99,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value3) { - instance.MyString = value3!; + instance.MyString = value3; } if (configuration["MyInt"] is string value4) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt index 37b08853999769..1ea93505a8dad7 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt @@ -99,7 +99,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value3) { - instance.MyString = value3!; + instance.MyString = value3; } if (configuration["MyInt"] is string value4) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt index 2f7f7151a12ceb..4b0b5a279dbdec 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt @@ -106,7 +106,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value1) { - instance.MyString = value1!; + instance.MyString = value1; } if (configuration["MyInt"] is string value2) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt index f1c771c92b2eed..ae5a8bab848b3f 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value1) { - instance.MyString = value1!; + instance.MyString = value1; } if (configuration["MyInt"] is string value2) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt index db78b0a816f28f..5bbee71c6d6dfa 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt @@ -110,7 +110,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value1) { - instance.MyString = value1!; + instance.MyString = value1; } if (configuration["MyInt"] is string value2) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt index 66981ed011e72b..62f38af9b7538b 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt @@ -103,7 +103,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["Prop5"] is string value5) { - instance.Prop5 = value5!; + instance.Prop5 = value5; } if (configuration["Prop6"] is string value6) @@ -171,12 +171,12 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["Prop16"] is string value13) { - instance.Prop16 = value13!; + instance.Prop16 = value13; } if (configuration["Prop17"] is string value14) { - instance.Prop17 = ParseCultureInfo(value14, () => configuration.GetSection("Prop17").Path)!; + instance.Prop17 = ParseCultureInfo(value14, () => configuration.GetSection("Prop17").Path); } if (configuration["Prop19"] is string value15) @@ -226,12 +226,12 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["Prop25"] is string value20) { - instance.Prop25 = ParseUri(value20, () => configuration.GetSection("Prop25").Path)!; + instance.Prop25 = ParseUri(value20, () => configuration.GetSection("Prop25").Path); } if (configuration["Prop26"] is string value21) { - instance.Prop26 = ParseVersion(value21, () => configuration.GetSection("Prop26").Path)!; + instance.Prop26 = ParseVersion(value21, () => configuration.GetSection("Prop26").Path); } if (configuration["Prop27"] is string value22) @@ -290,7 +290,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["Prop28"] is string value28) { - instance.Prop28 = ParseByteArray(value28, () => configuration.GetSection("Prop28").Path)!; + instance.Prop28 = ParseByteArray(value28, () => configuration.GetSection("Prop28").Path); } if (configuration["Prop29"] is string value29) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt index f0c3cce18518e7..53975b8c71efc9 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt @@ -138,7 +138,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value3) { - instance.MyString = value3!; + instance.MyString = value3; } if (configuration["MyInt"] is string value4) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt index 433fac2ad5364b..75d9be527e5df0 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt @@ -138,7 +138,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value3) { - instance.MyString = value3!; + instance.MyString = value3; } if (configuration["MyInt"] is string value4) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt index 269be4ab9b87d9..c5bdb982f8a9ec 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt @@ -138,7 +138,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value3) { - instance.MyString = value3!; + instance.MyString = value3; } if (configuration["MyInt"] is string value4) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt index 7573c8b4b13e32..2eccf1f5accfbb 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt @@ -132,7 +132,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (configuration["MyString"] is string value3) { - instance.MyString = value3!; + instance.MyString = value3; } if (configuration["MyInt"] is string value4) From 2436027d28e2c878d2a6044d6b5962c684986643 Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Wed, 13 Sep 2023 17:19:52 -0500 Subject: [PATCH 4/6] Don't default a property when it is a collection or child type --- .../gen/Emitter/CoreBindingHelpers.cs | 39 ++++++++++------ .../gen/Emitter/Helpers.cs | 15 ++++++ .../ConfigurationBinderTests.TestClasses.cs | 41 +++++++++++++++++ .../tests/Common/ConfigurationBinderTests.cs | 46 +++++++++++++++++++ .../Baselines/Collections.generated.txt | 8 ++-- .../ConfigurationBinder/Bind.generated.txt | 6 +-- .../Bind_Instance.generated.txt | 6 +-- .../Bind_Instance_BinderOptions.generated.txt | 6 +-- .../Bind_Key_Instance.generated.txt | 6 +-- .../ConfigurationBinder/Get.generated.txt | 8 ++-- .../ConfigurationBinder/Get_T.generated.txt | 8 ++-- .../Get_T_BinderOptions.generated.txt | 8 ++-- .../BindConfiguration.generated.txt | 4 +- .../OptionsBuilder/Bind_T.generated.txt | 4 +- .../Bind_T_BinderOptions.generated.txt | 4 +- .../Configure_T.generated.txt | 10 ++-- .../Configure_T_BinderOptions.generated.txt | 10 ++-- .../Configure_T_name.generated.txt | 10 ++-- ...nfigure_T_name_BinderOptions.generated.txt | 10 ++-- 19 files changed, 180 insertions(+), 69 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs index ef1e25eebf179f..9c8fcf7f169552 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs @@ -97,7 +97,7 @@ private void EmitGetCoreMethod() Expression.sectionPath, writeOnSuccess: parsedValueExpr => _writer.WriteLine($"return {parsedValueExpr};"), checkForNullSectionValue: stringParsableType.StringParsableTypeKind is not StringParsableTypeKind.AssignFromSectionValue, - useDefaultValueIfSectionValueIsNull : false, + useDefaultValueIfSectionValueIsNull: false, useIncrementalStringValueIdentifier: false); } break; @@ -111,7 +111,7 @@ private void EmitGetCoreMethod() { if (complexType.CanInstantiate) { - EmitBindingLogic(complexType, Identifier.instance, Identifier.configuration, InitializationKind.Declaration); + EmitBindingLogic(complexType, Identifier.instance, Identifier.configuration, InitializationKind.Declaration, ValueDefaulting.CallSetter); _writer.WriteLine($"return {Identifier.instance};"); } else if (type is ObjectSpec { InitExceptionMessage: string exMsg }) @@ -209,7 +209,7 @@ private void EmitBindCoreMainMethod() EmitStartBlock($"{conditionKindExpr} ({Identifier.type} == typeof({type.DisplayString}))"); _writer.WriteLine($"var {Identifier.temp} = ({effectiveType.DisplayString}){Identifier.instance};"); - EmitBindingLogic(type, Identifier.temp, Identifier.configuration, InitializationKind.None); + EmitBindingLogic(type, Identifier.temp, Identifier.configuration, InitializationKind.None, ValueDefaulting.None); _writer.WriteLine($"return;"); EmitEndBlock(); } @@ -258,7 +258,7 @@ private void EmitBindCoreMethod(ComplexTypeSpec type) } else { - EmitBindCoreImplForObject((ObjectSpec)effectiveType); + EmitBindCoreImplForObject((ObjectSpec)effectiveType, ValueDefaulting.None); } EmitEndBlock(); @@ -384,7 +384,8 @@ void EmitBindImplForMember(MemberSpec member) parsedMemberAssignmentLhsExpr, sectionPathExpr: GetSectionPathFromConfigurationExpression(configKeyName), canSet: true, - firstTimeInitialization: true); + firstTimeInitialization: true, + ValueDefaulting.None); if (canBindToMember) { @@ -640,7 +641,7 @@ private void EmitPopulationImplForArray(EnumerableSpec type) // Create list and bind elements. string tempIdentifier = GetIncrementalIdentifier(Identifier.temp); - EmitBindingLogic(typeToInstantiate, tempIdentifier, Identifier.configuration, InitializationKind.Declaration); + EmitBindingLogic(typeToInstantiate, tempIdentifier, Identifier.configuration, InitializationKind.Declaration, ValueDefaulting.None); // Resize array and add binded elements. _writer.WriteLine($$""" @@ -679,7 +680,7 @@ private void EmitPopulationImplForEnumerableWithAdd(EnumerableSpec type) break; case ComplexTypeSpec { CanInstantiate: true } complexType: { - EmitBindingLogic(complexType, Identifier.value, Identifier.section, InitializationKind.Declaration); + EmitBindingLogic(complexType, Identifier.value, Identifier.section, InitializationKind.Declaration, ValueDefaulting.None); _writer.WriteLine($"{addExpr}({Identifier.value});"); } break; @@ -756,7 +757,7 @@ void Emit_BindAndAddLogic_ForElement(string parsedKeyExpr) EmitObjectInit(complexElementType, Identifier.element, InitializationKind.SimpleAssignment, Identifier.section); EmitEndBlock(); - EmitBindingLogic(complexElementType, Identifier.element, Identifier.section, InitializationKind.None); + EmitBindingLogic(complexElementType, Identifier.element, Identifier.section, InitializationKind.None, ValueDefaulting.None); _writer.WriteLine($"{instanceIdentifier}[{parsedKeyExpr}] = {Identifier.element};"); } break; @@ -766,7 +767,7 @@ void Emit_BindAndAddLogic_ForElement(string parsedKeyExpr) EmitEndBlock(); } - private void EmitBindCoreImplForObject(ObjectSpec type) + private void EmitBindCoreImplForObject(ObjectSpec type, ValueDefaulting valueDefaulting) { Debug.Assert(type.HasBindableMembers); @@ -785,7 +786,8 @@ private void EmitBindCoreImplForObject(ObjectSpec type) memberAccessExpr: $"{containingTypeRef}.{property.Name}", GetSectionPathFromConfigurationExpression(property.ConfigurationKeyName), canSet: property.CanSet, - firstTimeInitialization: false); + firstTimeInitialization: false, + valueDefaulting); } } } @@ -795,7 +797,8 @@ private bool EmitBindImplForMember( string memberAccessExpr, string sectionPathExpr, bool canSet, - bool firstTimeInitialization) + bool firstTimeInitialization, + ValueDefaulting valueDefaulting) { TypeSpec effectiveMemberType = member.Type.EffectiveType; @@ -842,7 +845,7 @@ member is PropertySpec && EmitBlankLineIfRequired(); EmitStartBlock($"if ({sectionValidationCall} is {Identifier.IConfigurationSection} {sectionIdentifier})"); - EmitBindingLogicForComplexMember(member, memberAccessExpr, sectionIdentifier, canSet); + EmitBindingLogicForComplexMember(member, memberAccessExpr, sectionIdentifier, canSet, valueDefaulting); EmitEndBlock(); return complexType.CanInstantiate; @@ -856,7 +859,8 @@ private void EmitBindingLogicForComplexMember( MemberSpec member, string memberAccessExpr, string configArgExpr, - bool canSet) + bool canSet, + ValueDefaulting valueDefaulting) { TypeSpec memberType = member.Type; @@ -919,7 +923,9 @@ private void EmitBindingLogicForComplexMember( targetObjAccessExpr, configArgExpr, initKind, - writeOnSuccess); + valueDefaulting, + writeOnSuccess + ); } private void EmitBindingLogic( @@ -927,6 +933,7 @@ private void EmitBindingLogic( string memberAccessExpr, string configArgExpr, InitializationKind initKind, + ValueDefaulting valueDefaulting, Action? writeOnSuccess = null) { if (!type.HasBindableMembers) @@ -965,7 +972,7 @@ private void EmitBindingLogic( void EmitBindingLogic(string instanceToBindExpr, InitializationKind initKind) { - string bindCoreCall = $@"{nameof(MethodsToGen_CoreBindingHelper.BindCore)}({configArgExpr}, ref {instanceToBindExpr}, true, {Identifier.binderOptions});"; + string bindCoreCall = $@"{nameof(MethodsToGen_CoreBindingHelper.BindCore)}({configArgExpr}, ref {instanceToBindExpr}, {FormatDefaultValueIfNotFound()}, {Identifier.binderOptions});"; if (type.CanInstantiate) { @@ -997,6 +1004,8 @@ void EmitBindCoreCall() _writer.WriteLine(bindCoreCall); writeOnSuccess?.Invoke(instanceToBindExpr); } + + string FormatDefaultValueIfNotFound() => valueDefaulting == ValueDefaulting.CallSetter ? "true" : "false"; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/Helpers.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/Helpers.cs index 8bac0f4f5af03d..e1e05093e37b87 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/Helpers.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/Helpers.cs @@ -24,6 +24,21 @@ private enum InitializationKind Declaration = 3, } + /// + /// The type of defaulting for a property if it does not have a config entry. + /// This should only be applied for "Get" cases, not "Bind" and is also conditioned + /// on the source generated for a particular property as to whether it uses this value. + /// + private enum ValueDefaulting + { + None = 0, + + /// + /// Call the setter with the default value for the property's Type. + /// + CallSetter = 1, + } + private static class Expression { public const string configurationGetSection = "configuration.GetSection"; diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs index 4e06a9c5b03b4e..67b776c7eb46bd 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs @@ -567,6 +567,47 @@ public struct DeeplyNested } } + public struct StructWithNestedStructAndSetterLogic + { + private string _string; + private int _int32; + + public string String + { + get => _string; + // Setter should not be called for missing values. + set { _string = string.IsNullOrEmpty(value) ? "Hello" : value; } + } + + public int Int32 + { + get => _int32; + set { _int32 = value == 0 ? 42 : value; } + } + + public Nested NestedStruct; + public Nested[] NestedStructs; + + public struct Nested + { + private string _string; + private int _int32; + + public string String + { + get => _string; + // Setter should not be called for missing values. + set { _string = string.IsNullOrEmpty(value) ? "Hello2" : value; } + } + + public int Int32 + { + get => _int32; + set { _int32 = value == 0 ? 43 : value; } + } + } + } + public class BaseClassWithVirtualProperty { private string? PrivateProperty { get; set; } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs index 5ecdeeda54a19a..3178fe4a63a80d 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs @@ -1580,6 +1580,52 @@ public void CanBindNestedStructProperties() Assert.True(bound.ReadWriteNestedStruct.DeeplyNested.Boolean); } + [Fact] + public void CanBindNestedStructProperties_SetterCalledWithMissingConfigEntry() + { + ConfigurationBuilder configurationBuilder = new(); + configurationBuilder.AddInMemoryCollection(new Dictionary + { + { "dmy", "dmy" }, + }); + + IConfiguration config = configurationBuilder.Build(); + + var bound = config.Get(); + Assert.Null(bound.String); + Assert.Null(bound.NestedStruct.String); + Assert.Equal(42, bound.Int32); + Assert.Equal(0, bound.NestedStruct.Int32); + } + + [Fact] + public void CanBindNestedStructProperties_SetterNotCalledWithMissingConfigSection() + { + ConfigurationBuilder configurationBuilder = new(); + configurationBuilder.AddInMemoryCollection(new Dictionary + { + }); + + IConfiguration config = configurationBuilder.Build(); + + var bound = config.Get(); + Assert.Null(bound.String); + Assert.Null(bound.NestedStruct.String); + Assert.Equal(0, bound.Int32); + Assert.Equal(0, bound.NestedStruct.Int32); + } + + [Fact] + public void CanBindNestedStructProperties_SetterCalledWithMissingConfig_Array() + { + var config = TestHelpers.GetConfigurationFromJsonString( + """{"value": [{ }]}"""); + + var bound = config.GetSection("value").Get(); + Assert.Null(bound[0].String); + Assert.Equal(0, bound[0].Int32); + } + [Fact] public void IgnoresReadOnlyNestedStructProperties() { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt index 0cf1955ebc198a..ad852622710456 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt @@ -169,7 +169,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Program.CustomDictionary? temp3 = instance.CustomDictionary; temp3 ??= new Program.CustomDictionary(); - BindCore(section1, ref temp3, true, binderOptions); + BindCore(section1, ref temp3, false, binderOptions); instance.CustomDictionary = temp3; } @@ -177,7 +177,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Program.CustomList? temp6 = instance.CustomList; temp6 ??= new Program.CustomList(); - BindCore(section4, ref temp6, true, binderOptions); + BindCore(section4, ref temp6, false, binderOptions); instance.CustomList = temp6; } @@ -185,7 +185,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { IReadOnlyList? temp9 = instance.IReadOnlyList; temp9 = temp9 is null ? new List() : new List(temp9); - BindCore(section7, ref temp9, true, binderOptions); + BindCore(section7, ref temp9, false, binderOptions); instance.IReadOnlyList = temp9; } @@ -193,7 +193,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { IReadOnlyDictionary? temp12 = instance.IReadOnlyDictionary; temp12 = temp12 is null ? new Dictionary() : temp12.ToDictionary(pair => pair.Key, pair => pair.Value); - BindCore(section10, ref temp12, true, binderOptions); + BindCore(section10, ref temp12, false, binderOptions); instance.IReadOnlyDictionary = temp12; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt index d199e4fbf55fe5..7db80b5bb93277 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt @@ -144,7 +144,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp4 = instance.MyList; temp4 ??= new List(); - BindCore(section2, ref temp4, true, binderOptions); + BindCore(section2, ref temp4, false, binderOptions); instance.MyList = temp4; } @@ -152,7 +152,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp7 = instance.MyDictionary; temp7 ??= new Dictionary(); - BindCore(section5, ref temp7, true, binderOptions); + BindCore(section5, ref temp7, false, binderOptions); instance.MyDictionary = temp7; } @@ -160,7 +160,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp10 = instance.MyComplexDictionary; temp10 ??= new Dictionary(); - BindCore(section8, ref temp10, true, binderOptions); + BindCore(section8, ref temp10, false, binderOptions); instance.MyComplexDictionary = temp10; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt index 449539995b9b0d..c26ace1b8ed1aa 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt @@ -108,7 +108,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp4 = instance.MyList; temp4 ??= new List(); - BindCore(section2, ref temp4, true, binderOptions); + BindCore(section2, ref temp4, false, binderOptions); instance.MyList = temp4; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp7 = instance.MyDictionary; temp7 ??= new Dictionary(); - BindCore(section5, ref temp7, true, binderOptions); + BindCore(section5, ref temp7, false, binderOptions); instance.MyDictionary = temp7; } @@ -124,7 +124,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp10 = instance.MyComplexDictionary; temp10 ??= new Dictionary(); - BindCore(section8, ref temp10, true, binderOptions); + BindCore(section8, ref temp10, false, binderOptions); instance.MyComplexDictionary = temp10; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt index 0070d13c7c6f1f..e4278d30d29fde 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt @@ -108,7 +108,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp4 = instance.MyList; temp4 ??= new List(); - BindCore(section2, ref temp4, true, binderOptions); + BindCore(section2, ref temp4, false, binderOptions); instance.MyList = temp4; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp7 = instance.MyDictionary; temp7 ??= new Dictionary(); - BindCore(section5, ref temp7, true, binderOptions); + BindCore(section5, ref temp7, false, binderOptions); instance.MyDictionary = temp7; } @@ -124,7 +124,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp10 = instance.MyComplexDictionary; temp10 ??= new Dictionary(); - BindCore(section8, ref temp10, true, binderOptions); + BindCore(section8, ref temp10, false, binderOptions); instance.MyComplexDictionary = temp10; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt index 22cf7d8baf5411..145beeb95b4c33 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt @@ -108,7 +108,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp4 = instance.MyList; temp4 ??= new List(); - BindCore(section2, ref temp4, true, binderOptions); + BindCore(section2, ref temp4, false, binderOptions); instance.MyList = temp4; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp7 = instance.MyDictionary; temp7 ??= new Dictionary(); - BindCore(section5, ref temp7, true, binderOptions); + BindCore(section5, ref temp7, false, binderOptions); instance.MyDictionary = temp7; } @@ -124,7 +124,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp10 = instance.MyComplexDictionary; temp10 ??= new Dictionary(); - BindCore(section8, ref temp10, true, binderOptions); + BindCore(section8, ref temp10, false, binderOptions); instance.MyComplexDictionary = temp10; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt index 5ea757a2345898..5b3253899d5c6a 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt @@ -95,7 +95,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration public static void BindCore(IConfiguration configuration, ref int[] instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { var temp2 = new List(); - BindCore(configuration, ref temp2, true, binderOptions); + BindCore(configuration, ref temp2, false, binderOptions); int originalCount = instance.Length; Array.Resize(ref instance, originalCount + temp2.Count); temp2.CopyTo(instance, originalCount); @@ -134,7 +134,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp8 = instance.MyList; temp8 ??= new List(); - BindCore(section6, ref temp8, true, binderOptions); + BindCore(section6, ref temp8, false, binderOptions); instance.MyList = temp8; } @@ -142,7 +142,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { int[]? temp11 = instance.MyArray; temp11 ??= new int[0]; - BindCore(section9, ref temp11, true, binderOptions); + BindCore(section9, ref temp11, false, binderOptions); instance.MyArray = temp11; } @@ -150,7 +150,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp14 = instance.MyDictionary; temp14 ??= new Dictionary(); - BindCore(section12, ref temp14, true, binderOptions); + BindCore(section12, ref temp14, false, binderOptions); instance.MyDictionary = temp14; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt index fbcd830a02e3d9..391a2683106677 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt @@ -76,7 +76,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration public static void BindCore(IConfiguration configuration, ref int[] instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { var temp1 = new List(); - BindCore(configuration, ref temp1, true, binderOptions); + BindCore(configuration, ref temp1, false, binderOptions); int originalCount = instance.Length; Array.Resize(ref instance, originalCount + temp1.Count); temp1.CopyTo(instance, originalCount); @@ -115,7 +115,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, true, binderOptions); + BindCore(section5, ref temp7, false, binderOptions); instance.MyList = temp7; } @@ -123,7 +123,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { int[]? temp10 = instance.MyArray; temp10 ??= new int[0]; - BindCore(section8, ref temp10, true, binderOptions); + BindCore(section8, ref temp10, false, binderOptions); instance.MyArray = temp10; } @@ -131,7 +131,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, true, binderOptions); + BindCore(section11, ref temp13, false, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt index 1ea93505a8dad7..aae3961c96a3e0 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt @@ -76,7 +76,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration public static void BindCore(IConfiguration configuration, ref int[] instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { var temp1 = new List(); - BindCore(configuration, ref temp1, true, binderOptions); + BindCore(configuration, ref temp1, false, binderOptions); int originalCount = instance.Length; Array.Resize(ref instance, originalCount + temp1.Count); temp1.CopyTo(instance, originalCount); @@ -115,7 +115,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, true, binderOptions); + BindCore(section5, ref temp7, false, binderOptions); instance.MyList = temp7; } @@ -123,7 +123,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { int[]? temp10 = instance.MyArray; temp10 ??= new int[0]; - BindCore(section8, ref temp10, true, binderOptions); + BindCore(section8, ref temp10, false, binderOptions); instance.MyArray = temp10; } @@ -131,7 +131,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, true, binderOptions); + BindCore(section11, ref temp13, false, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt index 4b0b5a279dbdec..b8b11603c69dc7 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt @@ -82,7 +82,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, true, binderOptions); + BindCore(configuration, ref temp, false, binderOptions); return; } @@ -122,7 +122,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp5 = instance.MyList; temp5 ??= new List(); - BindCore(section3, ref temp5, true, binderOptions); + BindCore(section3, ref temp5, false, binderOptions); instance.MyList = temp5; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt index ae5a8bab848b3f..204ac9069acfcb 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt @@ -92,7 +92,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, true, binderOptions); + BindCore(configuration, ref temp, false, binderOptions); return; } @@ -132,7 +132,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp5 = instance.MyList; temp5 ??= new List(); - BindCore(section3, ref temp5, true, binderOptions); + BindCore(section3, ref temp5, false, binderOptions); instance.MyList = temp5; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt index 5bbee71c6d6dfa..85fc461aeaf2f7 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt @@ -86,7 +86,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, true, binderOptions); + BindCore(configuration, ref temp, false, binderOptions); return; } @@ -126,7 +126,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp5 = instance.MyList; temp5 ??= new List(); - BindCore(section3, ref temp5, true, binderOptions); + BindCore(section3, ref temp5, false, binderOptions); instance.MyList = temp5; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt index 53975b8c71efc9..b72471bf1c6c24 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt @@ -79,7 +79,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, true, binderOptions); + BindCore(configuration, ref temp, false, binderOptions); return; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration foreach (IConfigurationSection section in configuration.GetChildren()) { var value = new Program.MyClass2(); - BindCore(section, ref value, true, binderOptions); + BindCore(section, ref value, false, binderOptions); instance.Add(value); } } @@ -154,7 +154,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, true, binderOptions); + BindCore(section5, ref temp7, false, binderOptions); instance.MyList = temp7; } @@ -162,7 +162,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp10 = instance.MyList2; temp10 ??= new List(); - BindCore(section8, ref temp10, true, binderOptions); + BindCore(section8, ref temp10, false, binderOptions); instance.MyList2 = temp10; } @@ -170,7 +170,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, true, binderOptions); + BindCore(section11, ref temp13, false, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt index 75d9be527e5df0..7223c2a825a72a 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt @@ -79,7 +79,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, true, binderOptions); + BindCore(configuration, ref temp, false, binderOptions); return; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration foreach (IConfigurationSection section in configuration.GetChildren()) { var value = new Program.MyClass2(); - BindCore(section, ref value, true, binderOptions); + BindCore(section, ref value, false, binderOptions); instance.Add(value); } } @@ -154,7 +154,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, true, binderOptions); + BindCore(section5, ref temp7, false, binderOptions); instance.MyList = temp7; } @@ -162,7 +162,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp10 = instance.MyList2; temp10 ??= new List(); - BindCore(section8, ref temp10, true, binderOptions); + BindCore(section8, ref temp10, false, binderOptions); instance.MyList2 = temp10; } @@ -170,7 +170,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, true, binderOptions); + BindCore(section11, ref temp13, false, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt index c5bdb982f8a9ec..4f4b69890260dd 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt @@ -79,7 +79,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, true, binderOptions); + BindCore(configuration, ref temp, false, binderOptions); return; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration foreach (IConfigurationSection section in configuration.GetChildren()) { var value = new Program.MyClass2(); - BindCore(section, ref value, true, binderOptions); + BindCore(section, ref value, false, binderOptions); instance.Add(value); } } @@ -154,7 +154,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, true, binderOptions); + BindCore(section5, ref temp7, false, binderOptions); instance.MyList = temp7; } @@ -162,7 +162,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp10 = instance.MyList2; temp10 ??= new List(); - BindCore(section8, ref temp10, true, binderOptions); + BindCore(section8, ref temp10, false, binderOptions); instance.MyList2 = temp10; } @@ -170,7 +170,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, true, binderOptions); + BindCore(section11, ref temp13, false, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt index 2eccf1f5accfbb..9b2dc54dab3593 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt @@ -73,7 +73,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, true, binderOptions); + BindCore(configuration, ref temp, false, binderOptions); return; } @@ -110,7 +110,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration foreach (IConfigurationSection section in configuration.GetChildren()) { var value = new Program.MyClass2(); - BindCore(section, ref value, true, binderOptions); + BindCore(section, ref value, false, binderOptions); instance.Add(value); } } @@ -148,7 +148,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, true, binderOptions); + BindCore(section5, ref temp7, false, binderOptions); instance.MyList = temp7; } @@ -156,7 +156,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp10 = instance.MyList2; temp10 ??= new List(); - BindCore(section8, ref temp10, true, binderOptions); + BindCore(section8, ref temp10, false, binderOptions); instance.MyList2 = temp10; } @@ -164,7 +164,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, true, binderOptions); + BindCore(section11, ref temp13, false, binderOptions); instance.MyDictionary = temp13; } } From 17a93032c9a9c9e74535744aeaee821a623cf26a Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Thu, 14 Sep 2023 09:23:13 -0500 Subject: [PATCH 5/6] Simply use of new variable; add to test --- .../gen/Emitter/CoreBindingHelpers.cs | 20 ++++++++----------- .../ConfigurationBinderTests.TestClasses.cs | 7 +++++++ .../tests/Common/ConfigurationBinderTests.cs | 2 ++ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs index 9c8fcf7f169552..1af482e48e66b9 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs @@ -258,7 +258,7 @@ private void EmitBindCoreMethod(ComplexTypeSpec type) } else { - EmitBindCoreImplForObject((ObjectSpec)effectiveType, ValueDefaulting.None); + EmitBindCoreImplForObject((ObjectSpec)effectiveType); } EmitEndBlock(); @@ -384,8 +384,7 @@ void EmitBindImplForMember(MemberSpec member) parsedMemberAssignmentLhsExpr, sectionPathExpr: GetSectionPathFromConfigurationExpression(configKeyName), canSet: true, - firstTimeInitialization: true, - ValueDefaulting.None); + firstTimeInitialization: true); if (canBindToMember) { @@ -767,7 +766,7 @@ void Emit_BindAndAddLogic_ForElement(string parsedKeyExpr) EmitEndBlock(); } - private void EmitBindCoreImplForObject(ObjectSpec type, ValueDefaulting valueDefaulting) + private void EmitBindCoreImplForObject(ObjectSpec type) { Debug.Assert(type.HasBindableMembers); @@ -786,8 +785,7 @@ private void EmitBindCoreImplForObject(ObjectSpec type, ValueDefaulting valueDef memberAccessExpr: $"{containingTypeRef}.{property.Name}", GetSectionPathFromConfigurationExpression(property.ConfigurationKeyName), canSet: property.CanSet, - firstTimeInitialization: false, - valueDefaulting); + firstTimeInitialization: false); } } } @@ -797,8 +795,7 @@ private bool EmitBindImplForMember( string memberAccessExpr, string sectionPathExpr, bool canSet, - bool firstTimeInitialization, - ValueDefaulting valueDefaulting) + bool firstTimeInitialization) { TypeSpec effectiveMemberType = member.Type.EffectiveType; @@ -845,7 +842,7 @@ member is PropertySpec && EmitBlankLineIfRequired(); EmitStartBlock($"if ({sectionValidationCall} is {Identifier.IConfigurationSection} {sectionIdentifier})"); - EmitBindingLogicForComplexMember(member, memberAccessExpr, sectionIdentifier, canSet, valueDefaulting); + EmitBindingLogicForComplexMember(member, memberAccessExpr, sectionIdentifier, canSet); EmitEndBlock(); return complexType.CanInstantiate; @@ -859,8 +856,7 @@ private void EmitBindingLogicForComplexMember( MemberSpec member, string memberAccessExpr, string configArgExpr, - bool canSet, - ValueDefaulting valueDefaulting) + bool canSet) { TypeSpec memberType = member.Type; @@ -923,7 +919,7 @@ private void EmitBindingLogicForComplexMember( targetObjAccessExpr, configArgExpr, initKind, - valueDefaulting, + ValueDefaulting.None, writeOnSuccess ); } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs index 67b776c7eb46bd..f47cdbe6dbbb54 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs @@ -444,6 +444,7 @@ public class OptionWithCollectionProperties { private int _otherCode; private int _otherCodeNullable; + private string _otherCodeString = "default"; private object _otherCodeNull; private Uri _otherCodeUri; private ICollection blacklist = new HashSet(); @@ -477,6 +478,12 @@ public int? OtherCodeNullable set => _otherCodeNullable = !value.HasValue ? 3 : value.Value; } + public string OtherCodeString + { + get => _otherCodeString; + set => _otherCodeString = value; + } + public object? OtherCodeNull { get => _otherCodeNull; diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs index 3178fe4a63a80d..7c955e789184c8 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs @@ -1604,6 +1604,7 @@ public void CanBindNestedStructProperties_SetterNotCalledWithMissingConfigSectio ConfigurationBuilder configurationBuilder = new(); configurationBuilder.AddInMemoryCollection(new Dictionary { + // An empty value will not trigger defaulting. }); IConfiguration config = configurationBuilder.Build(); @@ -1764,6 +1765,7 @@ public void EnsureCallingThePropertySetter() // These don't exist in configuration and setters are not called since they are nullable. Assert.Equal(0, options.OtherCodeNullable); + Assert.Equal("default", options.OtherCodeString); Assert.Null(options.OtherCodeNull); Assert.Null(options.OtherCodeUri); } From d3e71c5d468bfbc28447d0f481b3447a9f27692a Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Thu, 14 Sep 2023 14:23:39 -0500 Subject: [PATCH 6/6] Review feedback --- .../gen/Emitter/ConfigurationBinder.cs | 2 +- .../gen/Emitter/CoreBindingHelpers.cs | 21 +++++++------------ .../gen/Emitter/Helpers.cs | 2 ++ .../gen/Specs/Members/MemberSpec.cs | 1 - .../gen/Specs/Members/ParameterSpec.cs | 2 ++ .../Baselines/Collections.generated.txt | 10 ++++----- .../ConfigurationBinder/Bind.generated.txt | 12 +++++------ .../Bind_Instance.generated.txt | 8 +++---- .../Bind_Instance_BinderOptions.generated.txt | 8 +++---- .../Bind_Key_Instance.generated.txt | 8 +++---- .../ConfigurationBinder/Get.generated.txt | 12 +++++------ .../ConfigurationBinder/Get_T.generated.txt | 10 ++++----- .../Get_T_BinderOptions.generated.txt | 10 ++++----- .../Get_TypeOf.generated.txt | 2 +- .../Get_TypeOf_BinderOptions.generated.txt | 2 +- .../Baselines/EmptyConfigType.generated.txt | 2 +- .../BindConfiguration.generated.txt | 4 ++-- .../OptionsBuilder/Bind_T.generated.txt | 4 ++-- .../Bind_T_BinderOptions.generated.txt | 4 ++-- .../Baselines/Primitives.generated.txt | 2 +- .../Configure_T.generated.txt | 10 ++++----- .../Configure_T_BinderOptions.generated.txt | 10 ++++----- .../Configure_T_name.generated.txt | 10 ++++----- ...nfigure_T_name_BinderOptions.generated.txt | 10 ++++----- 24 files changed, 82 insertions(+), 84 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/ConfigurationBinder.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/ConfigurationBinder.cs index 781bbc22a08b64..f1c7d5f7ff2150 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/ConfigurationBinder.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/ConfigurationBinder.cs @@ -148,7 +148,7 @@ void EmitMethods(MethodsToGen_ConfigurationBinder method, string additionalParam EmitCheckForNullArgument_WithBlankLine(Identifier.instance, voidReturn: true); _writer.WriteLine($$""" var {{Identifier.typedObj}} = ({{type.EffectiveType.DisplayString}}){{Identifier.instance}}; - {{nameof(MethodsToGen_CoreBindingHelper.BindCore)}}({{configExpression}}, ref {{Identifier.typedObj}}, false, {{binderOptionsArg}}); + {{nameof(MethodsToGen_CoreBindingHelper.BindCore)}}({{configExpression}}, ref {{Identifier.typedObj}}, defaultValueIfNotFound: false, {{binderOptionsArg}}); """); } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs index 1af482e48e66b9..90531efe1b0c10 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs @@ -309,10 +309,6 @@ private void EmitInitializeMethod(ObjectSpec type) { if (property.ShouldBindTo && property.MatchingCtorParam is null) { - // Currently properties don't have an 'else' failure case. Doing that may collide with the - // feature to set properties to their default values if they don't exist in the config section. - Debug.Assert(property.ErrorOnFailedBinding == false); - EmitBindImplForMember(property); } } @@ -340,8 +336,6 @@ private void EmitInitializeMethod(ObjectSpec type) void EmitBindImplForMember(MemberSpec member) { TypeSpec memberType = member.Type; - bool errorOnFailedBinding = member.ErrorOnFailedBinding; - string parsedMemberDeclarationLhs = $"{memberType.DisplayString} {member.Name}"; string configKeyName = member.ConfigurationKeyName; string parsedMemberAssignmentLhsExpr; @@ -350,7 +344,7 @@ void EmitBindImplForMember(MemberSpec member) { case ParsableFromStringSpec { StringParsableTypeKind: StringParsableTypeKind.AssignFromSectionValue }: { - if (errorOnFailedBinding) + if (member is ParameterSpec parameter && parameter.ErrorOnFailedBinding) { string condition = $@"if ({Identifier.configuration}[""{configKeyName}""] is not {parsedMemberDeclarationLhs})"; EmitThrowBlock(condition); @@ -384,11 +378,11 @@ void EmitBindImplForMember(MemberSpec member) parsedMemberAssignmentLhsExpr, sectionPathExpr: GetSectionPathFromConfigurationExpression(configKeyName), canSet: true, - firstTimeInitialization: true); + InitializationKind.None); if (canBindToMember) { - if (errorOnFailedBinding) + if (member is ParameterSpec parameter && parameter.ErrorOnFailedBinding) { // Add exception logic for parameter ctors; must be present in configuration object. EmitThrowBlock(condition: "else"); @@ -785,7 +779,7 @@ private void EmitBindCoreImplForObject(ObjectSpec type) memberAccessExpr: $"{containingTypeRef}.{property.Name}", GetSectionPathFromConfigurationExpression(property.ConfigurationKeyName), canSet: property.CanSet, - firstTimeInitialization: false); + InitializationKind.Declaration); } } } @@ -795,7 +789,7 @@ private bool EmitBindImplForMember( string memberAccessExpr, string sectionPathExpr, bool canSet, - bool firstTimeInitialization) + InitializationKind initializationKind) { TypeSpec effectiveMemberType = member.Type.EffectiveType; @@ -807,7 +801,8 @@ private bool EmitBindImplForMember( { if (canSet) { - bool useDefaultValueIfSectionValueIsNull = !firstTimeInitialization && + bool useDefaultValueIfSectionValueIsNull = + initializationKind == InitializationKind.Declaration && member is PropertySpec && member.Type.IsValueType && member.Type.SpecKind is not TypeSpecKind.Nullable; @@ -968,7 +963,7 @@ private void EmitBindingLogic( void EmitBindingLogic(string instanceToBindExpr, InitializationKind initKind) { - string bindCoreCall = $@"{nameof(MethodsToGen_CoreBindingHelper.BindCore)}({configArgExpr}, ref {instanceToBindExpr}, {FormatDefaultValueIfNotFound()}, {Identifier.binderOptions});"; + string bindCoreCall = $@"{nameof(MethodsToGen_CoreBindingHelper.BindCore)}({configArgExpr}, ref {instanceToBindExpr}, defaultValueIfNotFound: {FormatDefaultValueIfNotFound()}, {Identifier.binderOptions});"; if (type.CanInstantiate) { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/Helpers.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/Helpers.cs index e1e05093e37b87..a7db2fb5163979 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/Helpers.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/Helpers.cs @@ -28,6 +28,8 @@ private enum InitializationKind /// The type of defaulting for a property if it does not have a config entry. /// This should only be applied for "Get" cases, not "Bind" and is also conditioned /// on the source generated for a particular property as to whether it uses this value. + /// Note this is different than "InitializationKind.Declaration" since it only applied to + /// complex types and not arrays\enumerables. /// private enum ValueDefaulting { diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Members/MemberSpec.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Members/MemberSpec.cs index 4bf674f597502a..effd550482595d 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Members/MemberSpec.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Members/MemberSpec.cs @@ -16,7 +16,6 @@ public MemberSpec(ISymbol member) } public string Name { get; } - public bool ErrorOnFailedBinding { get; protected set; } public string DefaultValueExpr { get; protected set; } public required TypeSpec Type { get; init; } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Members/ParameterSpec.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Members/ParameterSpec.cs index 9b5e4360c11169..0f17a6247f74d2 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Members/ParameterSpec.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Specs/Members/ParameterSpec.cs @@ -26,6 +26,8 @@ public ParameterSpec(IParameterSymbol parameter) : base(parameter) } } + public bool ErrorOnFailedBinding { get; private set; } + public RefKind RefKind { get; } public override bool CanGet => false; diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt index ad852622710456..ddd52c68b99892 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt @@ -56,7 +56,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClassWithCustomCollections)) { var instance = new Program.MyClassWithCustomCollections(); - BindCore(configuration, ref instance, true, binderOptions); + BindCore(configuration, ref instance, defaultValueIfNotFound: true, binderOptions); return instance; } @@ -169,7 +169,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Program.CustomDictionary? temp3 = instance.CustomDictionary; temp3 ??= new Program.CustomDictionary(); - BindCore(section1, ref temp3, false, binderOptions); + BindCore(section1, ref temp3, defaultValueIfNotFound: false, binderOptions); instance.CustomDictionary = temp3; } @@ -177,7 +177,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Program.CustomList? temp6 = instance.CustomList; temp6 ??= new Program.CustomList(); - BindCore(section4, ref temp6, false, binderOptions); + BindCore(section4, ref temp6, defaultValueIfNotFound: false, binderOptions); instance.CustomList = temp6; } @@ -185,7 +185,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { IReadOnlyList? temp9 = instance.IReadOnlyList; temp9 = temp9 is null ? new List() : new List(temp9); - BindCore(section7, ref temp9, false, binderOptions); + BindCore(section7, ref temp9, defaultValueIfNotFound: false, binderOptions); instance.IReadOnlyList = temp9; } @@ -193,7 +193,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { IReadOnlyDictionary? temp12 = instance.IReadOnlyDictionary; temp12 = temp12 is null ? new Dictionary() : temp12.ToDictionary(pair => pair.Key, pair => pair.Value); - BindCore(section10, ref temp12, false, binderOptions); + BindCore(section10, ref temp12, defaultValueIfNotFound: false, binderOptions); instance.IReadOnlyDictionary = temp12; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt index 7db80b5bb93277..fc0dda4b5b3ae9 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt @@ -45,7 +45,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration, ref typedObj, false, binderOptions: null); + BindCore(configuration, ref typedObj, defaultValueIfNotFound: false, binderOptions: null); } /// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively. @@ -63,7 +63,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration, ref typedObj, false, GetBinderOptions(configureOptions)); + BindCore(configuration, ref typedObj, defaultValueIfNotFound: false, GetBinderOptions(configureOptions)); } /// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively. @@ -81,7 +81,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration.GetSection(key), ref typedObj, false, binderOptions: null); + BindCore(configuration.GetSection(key), ref typedObj, defaultValueIfNotFound: false, binderOptions: null); } #endregion IConfiguration extensions. @@ -144,7 +144,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp4 = instance.MyList; temp4 ??= new List(); - BindCore(section2, ref temp4, false, binderOptions); + BindCore(section2, ref temp4, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp4; } @@ -152,7 +152,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp7 = instance.MyDictionary; temp7 ??= new Dictionary(); - BindCore(section5, ref temp7, false, binderOptions); + BindCore(section5, ref temp7, defaultValueIfNotFound: false, binderOptions); instance.MyDictionary = temp7; } @@ -160,7 +160,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp10 = instance.MyComplexDictionary; temp10 ??= new Dictionary(); - BindCore(section8, ref temp10, false, binderOptions); + BindCore(section8, ref temp10, defaultValueIfNotFound: false, binderOptions); instance.MyComplexDictionary = temp10; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt index c26ace1b8ed1aa..9cbc0a22c5c84d 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt @@ -45,7 +45,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration, ref typedObj, false, binderOptions: null); + BindCore(configuration, ref typedObj, defaultValueIfNotFound: false, binderOptions: null); } #endregion IConfiguration extensions. @@ -108,7 +108,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp4 = instance.MyList; temp4 ??= new List(); - BindCore(section2, ref temp4, false, binderOptions); + BindCore(section2, ref temp4, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp4; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp7 = instance.MyDictionary; temp7 ??= new Dictionary(); - BindCore(section5, ref temp7, false, binderOptions); + BindCore(section5, ref temp7, defaultValueIfNotFound: false, binderOptions); instance.MyDictionary = temp7; } @@ -124,7 +124,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp10 = instance.MyComplexDictionary; temp10 ??= new Dictionary(); - BindCore(section8, ref temp10, false, binderOptions); + BindCore(section8, ref temp10, defaultValueIfNotFound: false, binderOptions); instance.MyComplexDictionary = temp10; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt index e4278d30d29fde..f3efa07fc0c5c2 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt @@ -45,7 +45,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration, ref typedObj, false, GetBinderOptions(configureOptions)); + BindCore(configuration, ref typedObj, defaultValueIfNotFound: false, GetBinderOptions(configureOptions)); } #endregion IConfiguration extensions. @@ -108,7 +108,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp4 = instance.MyList; temp4 ??= new List(); - BindCore(section2, ref temp4, false, binderOptions); + BindCore(section2, ref temp4, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp4; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp7 = instance.MyDictionary; temp7 ??= new Dictionary(); - BindCore(section5, ref temp7, false, binderOptions); + BindCore(section5, ref temp7, defaultValueIfNotFound: false, binderOptions); instance.MyDictionary = temp7; } @@ -124,7 +124,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp10 = instance.MyComplexDictionary; temp10 ??= new Dictionary(); - BindCore(section8, ref temp10, false, binderOptions); + BindCore(section8, ref temp10, defaultValueIfNotFound: false, binderOptions); instance.MyComplexDictionary = temp10; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt index 145beeb95b4c33..89b82d31bc19ae 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt @@ -45,7 +45,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration.GetSection(key), ref typedObj, false, binderOptions: null); + BindCore(configuration.GetSection(key), ref typedObj, defaultValueIfNotFound: false, binderOptions: null); } #endregion IConfiguration extensions. @@ -108,7 +108,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp4 = instance.MyList; temp4 ??= new List(); - BindCore(section2, ref temp4, false, binderOptions); + BindCore(section2, ref temp4, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp4; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp7 = instance.MyDictionary; temp7 ??= new Dictionary(); - BindCore(section5, ref temp7, false, binderOptions); + BindCore(section5, ref temp7, defaultValueIfNotFound: false, binderOptions); instance.MyDictionary = temp7; } @@ -124,7 +124,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp10 = instance.MyComplexDictionary; temp10 ??= new Dictionary(); - BindCore(section8, ref temp10, false, binderOptions); + BindCore(section8, ref temp10, defaultValueIfNotFound: false, binderOptions); instance.MyComplexDictionary = temp10; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt index 5b3253899d5c6a..5e7eeae29254a4 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt @@ -68,13 +68,13 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var instance = new Program.MyClass(); - BindCore(configuration, ref instance, true, binderOptions); + BindCore(configuration, ref instance, defaultValueIfNotFound: true, binderOptions); return instance; } else if (type == typeof(Program.MyClass2)) { var instance = new Program.MyClass2(); - BindCore(configuration, ref instance, true, binderOptions); + BindCore(configuration, ref instance, defaultValueIfNotFound: true, binderOptions); return instance; } @@ -95,7 +95,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration public static void BindCore(IConfiguration configuration, ref int[] instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { var temp2 = new List(); - BindCore(configuration, ref temp2, false, binderOptions); + BindCore(configuration, ref temp2, defaultValueIfNotFound: false, binderOptions); int originalCount = instance.Length; Array.Resize(ref instance, originalCount + temp2.Count); temp2.CopyTo(instance, originalCount); @@ -134,7 +134,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp8 = instance.MyList; temp8 ??= new List(); - BindCore(section6, ref temp8, false, binderOptions); + BindCore(section6, ref temp8, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp8; } @@ -142,7 +142,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { int[]? temp11 = instance.MyArray; temp11 ??= new int[0]; - BindCore(section9, ref temp11, false, binderOptions); + BindCore(section9, ref temp11, defaultValueIfNotFound: false, binderOptions); instance.MyArray = temp11; } @@ -150,7 +150,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp14 = instance.MyDictionary; temp14 ??= new Dictionary(); - BindCore(section12, ref temp14, false, binderOptions); + BindCore(section12, ref temp14, defaultValueIfNotFound: false, binderOptions); instance.MyDictionary = temp14; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt index 391a2683106677..3fc5176bf50f09 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt @@ -55,7 +55,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var instance = new Program.MyClass(); - BindCore(configuration, ref instance, true, binderOptions); + BindCore(configuration, ref instance, defaultValueIfNotFound: true, binderOptions); return instance; } @@ -76,7 +76,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration public static void BindCore(IConfiguration configuration, ref int[] instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { var temp1 = new List(); - BindCore(configuration, ref temp1, false, binderOptions); + BindCore(configuration, ref temp1, defaultValueIfNotFound: false, binderOptions); int originalCount = instance.Length; Array.Resize(ref instance, originalCount + temp1.Count); temp1.CopyTo(instance, originalCount); @@ -115,7 +115,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, false, binderOptions); + BindCore(section5, ref temp7, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp7; } @@ -123,7 +123,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { int[]? temp10 = instance.MyArray; temp10 ??= new int[0]; - BindCore(section8, ref temp10, false, binderOptions); + BindCore(section8, ref temp10, defaultValueIfNotFound: false, binderOptions); instance.MyArray = temp10; } @@ -131,7 +131,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, false, binderOptions); + BindCore(section11, ref temp13, defaultValueIfNotFound: false, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt index aae3961c96a3e0..81c23d7ceea65a 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt @@ -55,7 +55,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var instance = new Program.MyClass(); - BindCore(configuration, ref instance, true, binderOptions); + BindCore(configuration, ref instance, defaultValueIfNotFound: true, binderOptions); return instance; } @@ -76,7 +76,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration public static void BindCore(IConfiguration configuration, ref int[] instance, bool defaultValueIfNotFound, BinderOptions? binderOptions) { var temp1 = new List(); - BindCore(configuration, ref temp1, false, binderOptions); + BindCore(configuration, ref temp1, defaultValueIfNotFound: false, binderOptions); int originalCount = instance.Length; Array.Resize(ref instance, originalCount + temp1.Count); temp1.CopyTo(instance, originalCount); @@ -115,7 +115,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, false, binderOptions); + BindCore(section5, ref temp7, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp7; } @@ -123,7 +123,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { int[]? temp10 = instance.MyArray; temp10 ??= new int[0]; - BindCore(section8, ref temp10, false, binderOptions); + BindCore(section8, ref temp10, defaultValueIfNotFound: false, binderOptions); instance.MyArray = temp10; } @@ -131,7 +131,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, false, binderOptions); + BindCore(section11, ref temp13, defaultValueIfNotFound: false, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt index 77155c7c60c7ae..eca2001123ff01 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt @@ -55,7 +55,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass2)) { var instance = new Program.MyClass2(); - BindCore(configuration, ref instance, true, binderOptions); + BindCore(configuration, ref instance, defaultValueIfNotFound: true, binderOptions); return instance; } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt index 114eb62ee3a122..7883f4a50da0f6 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt @@ -55,7 +55,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass2)) { var instance = new Program.MyClass2(); - BindCore(configuration, ref instance, true, binderOptions); + BindCore(configuration, ref instance, defaultValueIfNotFound: true, binderOptions); return instance; } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/EmptyConfigType.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/EmptyConfigType.generated.txt index 6228da0b9cbb9f..404ce7561cc47c 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/EmptyConfigType.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/EmptyConfigType.generated.txt @@ -51,7 +51,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (TypeWithNoMembers_Wrapper)instance; - BindCore(configuration, ref typedObj, false, binderOptions: null); + BindCore(configuration, ref typedObj, defaultValueIfNotFound: false, binderOptions: null); } #endregion IConfiguration extensions. diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt index b8b11603c69dc7..44f1df2e78232d 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt @@ -82,7 +82,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, false, binderOptions); + BindCore(configuration, ref temp, defaultValueIfNotFound: false, binderOptions); return; } @@ -122,7 +122,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp5 = instance.MyList; temp5 ??= new List(); - BindCore(section3, ref temp5, false, binderOptions); + BindCore(section3, ref temp5, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp5; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt index 204ac9069acfcb..8d7c70a27b3f2e 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt @@ -92,7 +92,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, false, binderOptions); + BindCore(configuration, ref temp, defaultValueIfNotFound: false, binderOptions); return; } @@ -132,7 +132,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp5 = instance.MyList; temp5 ??= new List(); - BindCore(section3, ref temp5, false, binderOptions); + BindCore(section3, ref temp5, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp5; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt index 85fc461aeaf2f7..385079af1709a4 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T_BinderOptions.generated.txt @@ -86,7 +86,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, false, binderOptions); + BindCore(configuration, ref temp, defaultValueIfNotFound: false, binderOptions); return; } @@ -126,7 +126,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp5 = instance.MyList; temp5 ??= new List(); - BindCore(section3, ref temp5, false, binderOptions); + BindCore(section3, ref temp5, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp5; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt index 62f38af9b7538b..a8373ca527095a 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Primitives.generated.txt @@ -45,7 +45,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } var typedObj = (Program.MyClass)instance; - BindCore(configuration, ref typedObj, false, binderOptions: null); + BindCore(configuration, ref typedObj, defaultValueIfNotFound: false, binderOptions: null); } #endregion IConfiguration extensions. diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt index b72471bf1c6c24..3e38a484c82552 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T.generated.txt @@ -79,7 +79,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, false, binderOptions); + BindCore(configuration, ref temp, defaultValueIfNotFound: false, binderOptions); return; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration foreach (IConfigurationSection section in configuration.GetChildren()) { var value = new Program.MyClass2(); - BindCore(section, ref value, false, binderOptions); + BindCore(section, ref value, defaultValueIfNotFound: false, binderOptions); instance.Add(value); } } @@ -154,7 +154,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, false, binderOptions); + BindCore(section5, ref temp7, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp7; } @@ -162,7 +162,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp10 = instance.MyList2; temp10 ??= new List(); - BindCore(section8, ref temp10, false, binderOptions); + BindCore(section8, ref temp10, defaultValueIfNotFound: false, binderOptions); instance.MyList2 = temp10; } @@ -170,7 +170,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, false, binderOptions); + BindCore(section11, ref temp13, defaultValueIfNotFound: false, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt index 7223c2a825a72a..186e93a49207b2 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_BinderOptions.generated.txt @@ -79,7 +79,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, false, binderOptions); + BindCore(configuration, ref temp, defaultValueIfNotFound: false, binderOptions); return; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration foreach (IConfigurationSection section in configuration.GetChildren()) { var value = new Program.MyClass2(); - BindCore(section, ref value, false, binderOptions); + BindCore(section, ref value, defaultValueIfNotFound: false, binderOptions); instance.Add(value); } } @@ -154,7 +154,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, false, binderOptions); + BindCore(section5, ref temp7, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp7; } @@ -162,7 +162,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp10 = instance.MyList2; temp10 ??= new List(); - BindCore(section8, ref temp10, false, binderOptions); + BindCore(section8, ref temp10, defaultValueIfNotFound: false, binderOptions); instance.MyList2 = temp10; } @@ -170,7 +170,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, false, binderOptions); + BindCore(section11, ref temp13, defaultValueIfNotFound: false, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt index 4f4b69890260dd..7958adb1125338 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name.generated.txt @@ -79,7 +79,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, false, binderOptions); + BindCore(configuration, ref temp, defaultValueIfNotFound: false, binderOptions); return; } @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration foreach (IConfigurationSection section in configuration.GetChildren()) { var value = new Program.MyClass2(); - BindCore(section, ref value, false, binderOptions); + BindCore(section, ref value, defaultValueIfNotFound: false, binderOptions); instance.Add(value); } } @@ -154,7 +154,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, false, binderOptions); + BindCore(section5, ref temp7, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp7; } @@ -162,7 +162,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp10 = instance.MyList2; temp10 ??= new List(); - BindCore(section8, ref temp10, false, binderOptions); + BindCore(section8, ref temp10, defaultValueIfNotFound: false, binderOptions); instance.MyList2 = temp10; } @@ -170,7 +170,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, false, binderOptions); + BindCore(section11, ref temp13, defaultValueIfNotFound: false, binderOptions); instance.MyDictionary = temp13; } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt index 9b2dc54dab3593..b87d0c0a259cc8 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ServiceCollection/Configure_T_name_BinderOptions.generated.txt @@ -73,7 +73,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration if (type == typeof(Program.MyClass)) { var temp = (Program.MyClass)instance; - BindCore(configuration, ref temp, false, binderOptions); + BindCore(configuration, ref temp, defaultValueIfNotFound: false, binderOptions); return; } @@ -110,7 +110,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration foreach (IConfigurationSection section in configuration.GetChildren()) { var value = new Program.MyClass2(); - BindCore(section, ref value, false, binderOptions); + BindCore(section, ref value, defaultValueIfNotFound: false, binderOptions); instance.Add(value); } } @@ -148,7 +148,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp7 = instance.MyList; temp7 ??= new List(); - BindCore(section5, ref temp7, false, binderOptions); + BindCore(section5, ref temp7, defaultValueIfNotFound: false, binderOptions); instance.MyList = temp7; } @@ -156,7 +156,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { List? temp10 = instance.MyList2; temp10 ??= new List(); - BindCore(section8, ref temp10, false, binderOptions); + BindCore(section8, ref temp10, defaultValueIfNotFound: false, binderOptions); instance.MyList2 = temp10; } @@ -164,7 +164,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration { Dictionary? temp13 = instance.MyDictionary; temp13 ??= new Dictionary(); - BindCore(section11, ref temp13, false, binderOptions); + BindCore(section11, ref temp13, defaultValueIfNotFound: false, binderOptions); instance.MyDictionary = temp13; } }