Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
Improve enum parsing in config binder generator#89823
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -429,10 +429,24 @@ private void EmitHelperMethods() | ||
| _emitBlankLineBeforeNextStatement = true; | ||
| } | ||
| bool enumTypeExists = false; | ||
| foreach (ParsableFromStringSpec type in _sourceGenSpec.PrimitivesForHelperGen) | ||
| { | ||
| EmitBlankLineIfRequired(); | ||
| EmitPrimitiveParseMethod(type); | ||
| if (type.StringParsableTypeKind == StringParsableTypeKind.Enum) | ||
| { | ||
| if (!enumTypeExists) | ||
| { | ||
| EmitEnumParseMethod(); | ||
| enumTypeExists = true; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| EmitPrimitiveParseMethod(type); | ||
| } | ||
| } | ||
| } | ||
| @@ -518,6 +532,30 @@ private void EmitGetBinderOptionsHelper() | ||
| """); | ||
| } | ||
| private void EmitEnumParseMethod() | ||
| { | ||
| string innerExceptionTypeDisplayString = _useFullyQualifiedNames ? "global::System.Exception" : "Exception"; | ||
| string exceptionArg1 = string.Format(ExceptionMessages.FailedBinding, $"{{{Identifier.getPath}()}}", $"{{typeof(T)}}"); | ||
| _writer.WriteLine($$""" | ||
| public static T ParseEnum<T>(string value, Func<string?> getPath) where T : struct | ||
| { | ||
| try | ||
| { | ||
| #if NETFRAMEWORK || NETSTANDARD2_0 | ||
| return (T)Enum.Parse(typeof(T), value, ignoreCase: true); | ||
| #else | ||
| return Enum.Parse<T>(value, ignoreCase: true); | ||
layomia marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| #endif | ||
| } | ||
| catch ({{innerExceptionTypeDisplayString}} {{Identifier.exception}}) | ||
| { | ||
| throw new {{GetInvalidOperationDisplayName()}}($"{{exceptionArg1}}", {{Identifier.exception}}); | ||
| } | ||
| } | ||
| """); | ||
| } | ||
| private void EmitPrimitiveParseMethod(ParsableFromStringSpec type) | ||
| { | ||
| string innerExceptionTypeDisplayString; | ||
| @@ -546,10 +584,7 @@ private void EmitPrimitiveParseMethod(ParsableFromStringSpec type) | ||
| switch (typeKind) | ||
| { | ||
| case StringParsableTypeKind.Enum: | ||
| { | ||
| parsedValueExpr = $"({typeDisplayString}){Identifier.Enum}.{Identifier.Parse}(typeof({typeDisplayString}), {Identifier.value}, ignoreCase: true)"; | ||
| } | ||
| break; | ||
| return; | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should never fall into this case based on other diffs above. | ||
| case StringParsableTypeKind.ByteArray: | ||
| { | ||
| parsedValueExpr = $"Convert.FromBase64String({Identifier.value})"; | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the parser should not include enum types in the
PrimitivesForHelperGenlist. The emitter should have minimal/no post-processing of the input data. TheSourceGenerationSpeccould have a boolean property indicating whether an enum was part of the input graph.