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

Support TimeSpan type in the RangeAttribute emitted code by options validation source generator #94119

Closed
tarekgh opened this issue Oct 27, 2023 · 13 comments
Labels
area-Extensions-Options source-generator Indicates an issue with a source generator feature
Milestone

Comments

@tarekgh
Copy link
Member

tarekgh commented Oct 27, 2023

The RangeAttribute employs a TypeConverter, which renders it incompatible with AOT compilation. The Options source generator identifies the usage of the RangeAttribute and generates strongly typed attribute code. The generator currently supports this only for a predefined list of basic types, as indicated here: link.

TimeSpan is not included in the supported list, despite its use in real application scenarios. This issue is being tracked to add support for TimeSpan and possibly other types like DateTimeOffset.

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Oct 27, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Oct 27, 2023
@tarekgh tarekgh added area-Extensions-Options source-generator Indicates an issue with a source generator feature and removed untriaged New issue has not been triaged by the area owner needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Oct 27, 2023
@tarekgh tarekgh added this to the 9.0.0 milestone Oct 27, 2023
@ghost
Copy link

ghost commented Oct 27, 2023

Tagging subscribers to this area: @dotnet/area-extensions-options
See info in area-owners.md if you want to be subscribed.

Issue Details

The RangeAttribute employs a TypeConverter, which renders it incompatible with AOT compilation. The Options source generator identifies the usage of the RangeAttribute and generates strongly typed attribute code. The generator currently supports this only for a predefined list of basic types, as indicated here: link.

TimeSpan is not included in the supported list, despite its use in real application scenarios. This issue is being tracked to add support for TimeSpan and possibly other types like DateTimeOffset.

Author: tarekgh
Assignees: -
Labels:

area-Extensions-Options, source-generator

Milestone: -

@tarekgh
Copy link
Member Author

tarekgh commented Oct 27, 2023

CC @eerhardt @ericstj

@Clockwork-Muse
Copy link
Contributor

TimeSpan is going to be a problem due to the whole "the value 24+ is read as days instead of hours" thing.

@tarekgh
Copy link
Member Author

tarekgh commented Oct 28, 2023

is going to be a problem due to the whole "the value 24+ is read as days instead of hours" thing.

Why do you think this is a problem? The behavior is well defined, and users have control on the values they can use in the range attribute.

@Clockwork-Muse
Copy link
Contributor

Principle of least surprise problems. The behavior is well defined, sure, but that assumes that the people doing the configuration know that whatever application has been written in C#, and that the element type follows the given behavior.
Config makes it more likely to obscure this requirement...

@tarekgh
Copy link
Member Author

tarekgh commented Oct 28, 2023

The change here is not going to add or introduce any behavior change. The RangeAttribute today can accept TimeSpan and have the defined behavior. The support we are talking about here is the source generator using RangeAttribute. Cannot have different behavior for source generator than the runtime.

@eiriktsarpalis
Copy link
Member

The RangeAttribute employs a TypeConverter, which renders it incompatible with AOT compilation.

I believe this could be addressed in part using the API as originally proposed here: #82526. API review at the time indicated that we should focus on shipping a new generic range type instead, but wondering if this scenario is sufficient to reconsider. cc @jeffhandley

@tarekgh
Copy link
Member Author

tarekgh commented Oct 29, 2023

@eiriktsarpalis this will help except of users already using the constructor public [RangeAttribute(Type type, string minimum, string maximum) we need to handle this case in the source gen till we do other things. I can imagine we can run into other similar types too (like DateTimeOffset as another example).

@eiriktsarpalis
Copy link
Member

Given that this constructor only works with a small number of known types, it is conceivable that the source generator could intercept it, parse the type symbol and strings at compile time, replacing it with a call to the proposed ICompable constructor.

@tarekgh
Copy link
Member Author

tarekgh commented Oct 29, 2023

This is not going to help in anything for this case because we must convert the string values to the object type (like TimeSpan in this case). The whole issue we are trying to handle here is avoid using type converter. This is why the source generator handles a limited set of types.

Another side question, can the C# attribute accept interface types in the constructors as the proposal has? This is interesting to me. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/attributes#2224-attribute-parameter-types

@tarekgh
Copy link
Member Author

tarekgh commented Oct 29, 2023

oh, I see now the proposal is changed to have a generic type attribute. that make sense now :-)

@eiriktsarpalis
Copy link
Member

I think it should be possible to have it work for the case of the non-generic variant as well.

@eerhardt
Copy link
Member

I added my thoughts to that proposal: #82526 (comment). If we went with something like that, we wouldn't need to change the source gen at all.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Nov 16, 2023
@tarekgh tarekgh modified the milestones: 9.0.0, 8.0.x Nov 16, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Nov 17, 2023
@tarekgh tarekgh closed this as completed Nov 17, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Options source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

No branches or pull requests

4 participants