forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add individual compilation tests for each config binding overload (do…
…tnet#86910) * Add individual compilation tests for each config binding overload * Update ConfigurationBindingGeneratorTests.cs Restore exclusion of browser/wasm for test runs.
- Loading branch information
Showing
22 changed files
with
2,245 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
180 changes: 180 additions & 0 deletions
180
...ns.Configuration.Binder/tests/SourceGenerationTests/Baselines/Bind_Instance.generated.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
// <auto-generated/> | ||
#nullable enable | ||
|
||
internal static class GeneratedConfigurationBinder | ||
{ | ||
public static void Bind(this global::Microsoft.Extensions.Configuration.IConfiguration configuration, global::Program.MyClass obj) => global::Microsoft.Extensions.Configuration.Binder.SourceGeneration.Helpers.BindCore(configuration, ref obj, binderOptions: null); | ||
} | ||
|
||
namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration | ||
{ | ||
using Microsoft.Extensions.Configuration; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
|
||
internal static class Helpers | ||
{ | ||
public static void BindCore(IConfiguration configuration, ref List<int> obj, BinderOptions? binderOptions) | ||
{ | ||
if (obj is null) | ||
{ | ||
throw new ArgumentNullException(nameof(obj)); | ||
} | ||
|
||
foreach (IConfigurationSection section in configuration.GetChildren()) | ||
{ | ||
int element; | ||
if (section.Value is string stringValue0) | ||
{ | ||
element = ParseInt(stringValue0, () => section.Path); | ||
obj.Add(element); | ||
} | ||
} | ||
} | ||
|
||
public static void BindCore(IConfiguration configuration, ref Dictionary<string, string> obj, BinderOptions? binderOptions) | ||
{ | ||
if (obj is null) | ||
{ | ||
throw new ArgumentNullException(nameof(obj)); | ||
} | ||
|
||
foreach (IConfigurationSection section in configuration.GetChildren()) | ||
{ | ||
string key = section.Key; | ||
if (section.Value is string stringValue1) | ||
{ | ||
obj[key] = stringValue1; | ||
} | ||
} | ||
} | ||
|
||
public static void BindCore(IConfiguration configuration, ref Program.MyClass2 obj, BinderOptions? binderOptions) | ||
{ | ||
if (obj is null) | ||
{ | ||
throw new ArgumentNullException(nameof(obj)); | ||
} | ||
|
||
} | ||
|
||
public static void BindCore(IConfiguration configuration, ref Dictionary<string, Program.MyClass2> obj, BinderOptions? binderOptions) | ||
{ | ||
if (obj is null) | ||
{ | ||
throw new ArgumentNullException(nameof(obj)); | ||
} | ||
|
||
foreach (IConfigurationSection section in configuration.GetChildren()) | ||
{ | ||
string key = section.Key; | ||
if (!(obj.TryGetValue(key, out Program.MyClass2? element) && element is not null)) | ||
{ | ||
element = new Program.MyClass2(); | ||
} | ||
BindCore(section, ref element!, binderOptions); | ||
obj[key] = element; | ||
} | ||
} | ||
|
||
public static void BindCore(IConfiguration configuration, ref Program.MyClass obj, BinderOptions? binderOptions) | ||
{ | ||
if (obj is null) | ||
{ | ||
throw new ArgumentNullException(nameof(obj)); | ||
} | ||
|
||
List<string>? temp = null; | ||
foreach (IConfigurationSection section in configuration.GetChildren()) | ||
{ | ||
switch (section.Key) | ||
{ | ||
case "MyString": | ||
{ | ||
if (configuration["MyString"] is string stringValue3) | ||
{ | ||
obj.MyString = stringValue3; | ||
} | ||
} | ||
break; | ||
case "MyInt": | ||
{ | ||
if (configuration["MyInt"] is string stringValue4) | ||
{ | ||
obj.MyInt = ParseInt(stringValue4, () => section.Path); | ||
} | ||
} | ||
break; | ||
case "MyList": | ||
{ | ||
if (HasChildren(section)) | ||
{ | ||
List<int> temp5 = obj.MyList; | ||
temp5 ??= new List<int>(); | ||
BindCore(section, ref temp5, binderOptions); | ||
obj.MyList = temp5; | ||
} | ||
} | ||
break; | ||
case "MyDictionary": | ||
{ | ||
if (HasChildren(section)) | ||
{ | ||
Dictionary<string, string> temp6 = obj.MyDictionary; | ||
temp6 ??= new Dictionary<string, string>(); | ||
BindCore(section, ref temp6, binderOptions); | ||
obj.MyDictionary = temp6; | ||
} | ||
} | ||
break; | ||
case "MyComplexDictionary": | ||
{ | ||
if (HasChildren(section)) | ||
{ | ||
Dictionary<string, Program.MyClass2> temp7 = obj.MyComplexDictionary; | ||
temp7 ??= new Dictionary<string, Program.MyClass2>(); | ||
BindCore(section, ref temp7, binderOptions); | ||
obj.MyComplexDictionary = temp7; | ||
} | ||
} | ||
break; | ||
default: | ||
{ | ||
if (binderOptions?.ErrorOnUnknownConfiguration == true) | ||
{ | ||
(temp ??= new List<string>()).Add($"'{section.Key}'"); | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
|
||
if (temp is not null) | ||
{ | ||
throw new InvalidOperationException($"'ErrorOnUnknownConfiguration' was set on the provided BinderOptions, but the following properties were not found on the instance of {typeof(Program.MyClass)}: {string.Join(", ", temp)}"); | ||
} | ||
} | ||
|
||
public static bool HasChildren(IConfiguration configuration) | ||
{ | ||
foreach (IConfigurationSection section in configuration.GetChildren()) | ||
{ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static int ParseInt(string stringValue, Func<string?> getPath) | ||
{ | ||
try | ||
{ | ||
return int.Parse(stringValue, NumberStyles.Integer, CultureInfo.InvariantCulture); | ||
} | ||
catch (Exception exception) | ||
{ | ||
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(int)}'.", exception); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.