Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config generator emits lambdas for error path that force allocation on success path #100257

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,11 @@ private void EmitGetBinderOptionsHelper()

private void EmitEnumParseMethod()
{
string exceptionArg1 = string.Format(ExceptionMessages.FailedBinding, $"{{{Identifier.getPath}()}}", $"{{typeof(T)}}");
string exceptionArg1 = string.Format(ExceptionMessages.FailedBinding, $"{{{Identifier.path}}}", $"{{typeof(T)}}");

string parseEnumCall = _emitGenericParseEnum ? "Enum.Parse<T>(value, ignoreCase: true)" : "(T)Enum.Parse(typeof(T), value, ignoreCase: true)";
_writer.WriteLine($$"""
public static T ParseEnum<T>(string value, Func<string?> getPath) where T : struct
public static T ParseEnum<T>(string value, string? path) where T : struct
{
try
{
Expand Down Expand Up @@ -639,9 +639,9 @@ private void EmitPrimitiveParseMethod(ParsableFromStringSpec type)
}
}

string exceptionArg1 = string.Format(ExceptionMessages.FailedBinding, $"{{{Identifier.getPath}()}}", $"{{typeof({typeFQN})}}");
string exceptionArg1 = string.Format(ExceptionMessages.FailedBinding, $"{{{Identifier.path}}}", $"{{typeof({typeFQN})}}");

EmitStartBlock($"public static {typeFQN} {TypeIndex.GetParseMethodName(type)}(string {Identifier.value}, Func<string?> {Identifier.getPath})");
EmitStartBlock($"public static {typeFQN} {TypeIndex.GetParseMethodName(type)}(string {Identifier.value}, string? {Identifier.path})");
EmitEndBlock($$"""
try
{
Expand Down Expand Up @@ -1084,8 +1084,8 @@ private void EmitBindingLogic(
string parsedValueExpr = typeKind switch
{
StringParsableTypeKind.AssignFromSectionValue => stringValueToParse_Expr,
StringParsableTypeKind.Enum => $"ParseEnum<{type.TypeRef.FullyQualifiedName}>({stringValueToParse_Expr}, () => {sectionPathExpr})",
_ => $"{TypeIndex.GetParseMethodName(type)}({stringValueToParse_Expr}, () => {sectionPathExpr})",
StringParsableTypeKind.Enum => $"ParseEnum<{type.TypeRef.FullyQualifiedName}>({stringValueToParse_Expr}, {sectionPathExpr})",
_ => $"{TypeIndex.GetParseMethodName(type)}({stringValueToParse_Expr}, {sectionPathExpr})",
};

if (!checkForNullSectionValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ private static class Identifier
public const string element = nameof(element);
public const string enumValue = nameof(enumValue);
public const string exception = nameof(exception);
public const string getPath = nameof(getPath);
public const string key = nameof(key);
public const string name = nameof(name);
public const string instance = nameof(instance);
public const string optionsBuilder = nameof(optionsBuilder);
public const string originalCount = nameof(originalCount);
public const string path = nameof(path);
public const string section = nameof(section);
public const string sectionKey = nameof(sectionKey);
public const string services = nameof(services);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
if (section.Value is string value)
{
instance[section.Key] = ParseInt(value, () => section.Path);
instance[section.Key] = ParseInt(value, section.Path);
}
}
}
Expand All @@ -100,7 +100,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
if (section.Value is string value)
{
temp.Add(ParseInt(value, () => section.Path));
temp.Add(ParseInt(value, section.Path));
}
}
}
Expand All @@ -116,7 +116,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
if (section.Value is string value)
{
temp[section.Key] = ParseInt(value, () => section.Path);
temp[section.Key] = ParseInt(value, section.Path);
}
}
}
Expand Down Expand Up @@ -217,15 +217,15 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
return binderOptions;
}

public static int ParseInt(string value, Func<string?> getPath)
public static int ParseInt(string value, string? path)
{
try
{
return int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(int)}'.", exception);
throw new InvalidOperationException($"Failed to convert configuration value at '{path}' to type '{typeof(int)}'.", exception);
}
}
#endregion Core binding extensions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
if (section.Value is string value)
{
instance.Add(ParseInt(value, () => section.Path));
instance.Add(ParseInt(value, section.Path));
}
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (configuration["MyInt"] is string value1)
{
instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
instance.MyInt = ParseInt(value1, configuration.GetSection("MyInt").Path);
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
}
else if (defaultValueIfNotFound)
{
Expand Down Expand Up @@ -219,15 +219,15 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
return binderOptions;
}

public static int ParseInt(string value, Func<string?> getPath)
public static int ParseInt(string value, string? path)
{
try
{
return int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(int)}'.", exception);
throw new InvalidOperationException($"Failed to convert configuration value at '{path}' to type '{typeof(int)}'.", exception);
}
}
#endregion Core binding extensions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
if (section.Value is string value)
{
instance.Add(ParseInt(value, () => section.Path));
instance.Add(ParseInt(value, section.Path));
}
}
}
Expand Down Expand Up @@ -101,7 +101,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (configuration["MyInt"] is string value1)
{
instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
instance.MyInt = ParseInt(value1, configuration.GetSection("MyInt").Path);
}
else if (defaultValueIfNotFound)
{
Expand Down Expand Up @@ -165,15 +165,15 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
return null;
}

public static int ParseInt(string value, Func<string?> getPath)
public static int ParseInt(string value, string? path)
{
try
{
return int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(int)}'.", exception);
throw new InvalidOperationException($"Failed to convert configuration value at '{path}' to type '{typeof(int)}'.", exception);
}
}
#endregion Core binding extensions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
if (section.Value is string value)
{
instance.Add(ParseInt(value, () => section.Path));
instance.Add(ParseInt(value, section.Path));
}
}
}
Expand Down Expand Up @@ -101,7 +101,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (configuration["MyInt"] is string value1)
{
instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
instance.MyInt = ParseInt(value1, configuration.GetSection("MyInt").Path);
}
else if (defaultValueIfNotFound)
{
Expand Down Expand Up @@ -183,15 +183,15 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
return binderOptions;
}

public static int ParseInt(string value, Func<string?> getPath)
public static int ParseInt(string value, string? path)
{
try
{
return int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(int)}'.", exception);
throw new InvalidOperationException($"Failed to convert configuration value at '{path}' to type '{typeof(int)}'.", exception);
}
}
#endregion Core binding extensions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
if (section.Value is string value)
{
instance.Add(ParseInt(value, () => section.Path));
instance.Add(ParseInt(value, section.Path));
}
}
}
Expand Down Expand Up @@ -101,7 +101,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (configuration["MyInt"] is string value1)
{
instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
instance.MyInt = ParseInt(value1, configuration.GetSection("MyInt").Path);
}
else if (defaultValueIfNotFound)
{
Expand Down Expand Up @@ -165,15 +165,15 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
return null;
}

public static int ParseInt(string value, Func<string?> getPath)
public static int ParseInt(string value, string? path)
{
try
{
return int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(int)}'.", exception);
throw new InvalidOperationException($"Failed to convert configuration value at '{path}' to type '{typeof(int)}'.", exception);
}
}
#endregion Core binding extensions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
if (section.Value is string value)
{
instance.Add(ParseInt(value, () => section.Path));
instance.Add(ParseInt(value, section.Path));
}
}
}
Expand All @@ -104,7 +104,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
if (section.Value is string value)
{
temp2.Add(ParseInt(value, () => section.Path));
temp2.Add(ParseInt(value, section.Path));
}
}

Expand Down Expand Up @@ -135,7 +135,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (configuration["MyInt"] is string value4)
{
instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path);
instance.MyInt = ParseInt(value4, configuration.GetSection("MyInt").Path);
}
else if (defaultValueIfNotFound)
{
Expand Down Expand Up @@ -173,7 +173,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (configuration["MyInt"] is string value14)
{
instance.MyInt = ParseInt(value14, () => configuration.GetSection("MyInt").Path);
instance.MyInt = ParseInt(value14, configuration.GetSection("MyInt").Path);
}
else if (defaultValueIfNotFound)
{
Expand Down Expand Up @@ -240,15 +240,15 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
return binderOptions;
}

public static int ParseInt(string value, Func<string?> getPath)
public static int ParseInt(string value, string? path)
{
try
{
return int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(int)}'.", exception);
throw new InvalidOperationException($"Failed to convert configuration value at '{path}' to type '{typeof(int)}'.", exception);
}
}
#endregion Core binding extensions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,69 +67,69 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (type == typeof(int))
{
return ParseInt(value, () => section.Path);
return ParseInt(value, section.Path);
}
else if (type == typeof(bool?))
{
return ParseBool(value, () => section.Path);
return ParseBool(value, section.Path);
}
else if (type == typeof(byte[]))
{
return ParseByteArray(value, () => section.Path);
return ParseByteArray(value, section.Path);
}
else if (type == typeof(global::System.Globalization.CultureInfo))
{
return ParseSystemGlobalizationCultureInfo(value, () => section.Path);
return ParseSystemGlobalizationCultureInfo(value, section.Path);
}

return null;
}

public static int ParseInt(string value, Func<string?> getPath)
public static int ParseInt(string value, string? path)
{
try
{
return int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(int)}'.", exception);
throw new InvalidOperationException($"Failed to convert configuration value at '{path}' to type '{typeof(int)}'.", exception);
}
}

public static bool ParseBool(string value, Func<string?> getPath)
public static bool ParseBool(string value, string? path)
{
try
{
return bool.Parse(value);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(bool)}'.", exception);
throw new InvalidOperationException($"Failed to convert configuration value at '{path}' to type '{typeof(bool)}'.", exception);
}
}

public static byte[] ParseByteArray(string value, Func<string?> getPath)
public static byte[] ParseByteArray(string value, string? path)
{
try
{
return Convert.FromBase64String(value);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(byte[])}'.", exception);
throw new InvalidOperationException($"Failed to convert configuration value at '{path}' to type '{typeof(byte[])}'.", exception);
}
}

public static global::System.Globalization.CultureInfo ParseSystemGlobalizationCultureInfo(string value, Func<string?> getPath)
public static global::System.Globalization.CultureInfo ParseSystemGlobalizationCultureInfo(string value, string? path)
{
try
{
return CultureInfo.GetCultureInfo(value);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(global::System.Globalization.CultureInfo)}'.", exception);
throw new InvalidOperationException($"Failed to convert configuration value at '{path}' to type '{typeof(global::System.Globalization.CultureInfo)}'.", exception);
}
}
#endregion Core binding extensions.
Expand Down
Loading