Revert "Fix binding logic for dictionaries with complex elements (#89045)" (#89071)
authorViktor Hofer <viktor.hofer@microsoft.com>
Tue, 18 Jul 2023 09:54:30 +0000 (11:54 +0200)
committerGitHub <noreply@github.com>
Tue, 18 Jul 2023 09:54:30 +0000 (11:54 +0200)
This reverts commit 9fedcc558e8496424ee6af9fd3062c27ea3fd6e7.

src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/CoreBindingHelper.cs
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs

index 7c01fd9..bdf5606 100644 (file)
@@ -731,7 +731,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
                                 """);
                         }
 
-                        EmitBindCoreCall(elementType, $"{Identifier.element}", Identifier.section, InitializationKind.None);
+                        EmitBindCoreCall(elementType, $"{Identifier.element}!", Identifier.section, InitializationKind.None);
                         _writer.WriteLine($"{objIdentifier}[{parsedKeyExpr}] = {Identifier.element};");
                     }
 
index afa4b12..5e4855a 100644 (file)
@@ -3,8 +3,6 @@
 
 using System;
 using System.Collections.Generic;
-using System.ComponentModel;
-using System.Diagnostics.CodeAnalysis;
 using System.Globalization;
 using System.Linq;
 using Microsoft.Extensions.Configuration;
@@ -666,59 +664,5 @@ namespace Microsoft.Extensions
 
             public int MyInt { get; }
         }
-
-        [TypeConverter(typeof(GeolocationTypeConverter))]
-        public struct Geolocation : IEquatable<Geolocation>, IParsable<Geolocation>
-        {
-            public static readonly Geolocation Zero = new(0, 0);
-
-            public Geolocation(double latitude, double longitude)
-            {
-                Latitude = latitude;
-                Longitude = longitude;
-            }
-
-            public double Latitude { get; set; }
-
-            public double Longitude { get; set; }
-
-            private sealed class GeolocationTypeConverter : TypeConverter
-            {
-                public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
-                {
-                    if (sourceType == typeof(string) || sourceType == typeof(Geolocation))
-                    {
-                        return true;
-                    }
-
-                    return base.CanConvertFrom(context, sourceType);
-                }
-
-                public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
-                {
-                    if (value is string s)
-                    {
-                        return Parse(s, culture);
-                    }
-                    else if (value is Geolocation geolocation)
-                    {
-                        return geolocation;
-                    }
-
-                    return base.ConvertFrom(context, culture, value);
-                }
-            }
-
-            public bool Equals(Geolocation other) => Latitude == other.Latitude && Longitude == other.Longitude;
-
-            public static Geolocation Parse(string s, IFormatProvider? provider) => throw new NotImplementedException();
-
-            public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out Geolocation result) => throw new NotImplementedException();
-        }
-
-        public class GeolocationWrapper
-        {
-            public Geolocation Location { get; set; }
-        }
     }
 }
index d2f5f08..d9dfefe 100644 (file)
@@ -1900,70 +1900,5 @@ if (!System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Launc
             GenericOptionsWithParamCtor<string> obj2 = configuration.Get<GenericOptionsWithParamCtor<string>>();
             Assert.Equal("MyString", obj2.Value);
         }
-
-        [Fact]
-        public void ObjWith_IParsableT_And_TypeConverter()
-        {
-            var configuration = TestHelpers.GetConfigurationFromJsonString("""
-                {
-                    "Location":
-                    {
-                        "Latitude": 3,
-                        "Longitude": 4,
-                    }
-                }
-                """);
-
-            // Neither IParsableT or TypeConverter impl are honored (https://github.com/dotnet/runtime/issues/83599).
-
-            GeolocationWrapper obj = configuration.Get<GeolocationWrapper>();
-            ValidateGeolocation(obj.Location);
-
-            configuration = TestHelpers.GetConfigurationFromJsonString(""" { "Geolocation": "3, 4", } """);
-            obj = configuration.Get<GeolocationWrapper>();
-            Assert.Equal(Geolocation.Zero, obj.Location);
-        }
-
-        [Fact]
-        public void ComplexObj_As_Dictionary_Element()
-        {
-            var configuration = TestHelpers.GetConfigurationFromJsonString("""
-                {
-                    "First":
-                    {
-                        "Latitude": 3,
-                        "Longitude": 4,
-                    }
-                }
-                """);
-
-            Geolocation obj = configuration.Get<IDictionary<string, Geolocation>>()["First"];
-            ValidateGeolocation(obj);
-
-            obj = configuration.Get<IReadOnlyDictionary<string, Geolocation>>()["First"];
-            ValidateGeolocation(obj);
-        }
-
-        [Fact]
-        public void ComplexObj_As_Enumerable_Element()
-        {
-            var  configuration = TestHelpers.GetConfigurationFromJsonString("""{ "Enumerable": [{ "Latitude": 3, "Longitude": 4 }] }""")
-                .GetSection("Enumerable");
-
-            Geolocation obj = configuration.Get<IList<Geolocation>>()[0];
-            ValidateGeolocation(obj);
-
-            obj = configuration.Get<Geolocation[]>()[0];
-            ValidateGeolocation(obj);
-
-            obj = configuration.Get<IReadOnlyList<Geolocation>>()[0];
-            ValidateGeolocation(obj);
-        }
-
-        private void ValidateGeolocation(Geolocation location)
-        {
-            Assert.Equal(3, location.Latitude);
-            Assert.Equal(4, location.Longitude);
-        }
     }
 }