Skip to content

Commit

Permalink
Config generator emits lambdas for error path that force allocation o…
Browse files Browse the repository at this point in the history
…n success path (#100257)

* first draft

* fix failing tests

* delete no more used Identifier.getPath

---------

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
Co-authored-by: Tarek Mahmoud Sayed <tarekms@microsoft.com>
  • Loading branch information
3 people authored May 13, 2024
1 parent 4b8fcd5 commit a37af85
Show file tree
Hide file tree
Showing 54 changed files with 430 additions and 430 deletions.
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);
}
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

0 comments on commit a37af85

Please sign in to comment.