-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[RFC] Accessing all annotations on an ivar/type/def #9322
Comments
A concern with One problem is, the following: @[Bar]
def foo
end is parsed as: Expressions.new([
Annotation.new(Path.new(["Bar"])),
Def.new("foo", [] of Arg),
] of ASTNode) and not as a single For The extension is simple; if we add |
Can we change the AST so that annotations belong to the node they annotate? It seems weird to have them just as previous expressions. I suppose this could be a breaking change, but perhaps we can keep the backwards-compatible API until 2.0. |
I been thinking about #6984 and thought of another use case that it would be better suited for.
For the serializer shard I been working on, I have a concept of
PropertyMetadata
. An array of these types is built out via a macro as a way to generically describe a property; such as its type, name, owning class, etc.I also have a concept of
ExclusionStrategies
, which allow applying custom runtime logic to determine if a given property should be skipped. A common implementation I could see for this is defining some custom annotation, applying it to a property/class, and then using the presence of the annotation, or values within the annotation, as part of the exclusion strategy logic.However, given Crystal doesn't have a way to access annotations at runtime, nor is there a clean way to generically do it at compile time, I thought of a pretty decent solution. If there is a method to return all annotations applied to a given ivar/type/method, then I could include this information as part of the
PropertyMetadata
. For example,manager_id
could be represented like:Then the logic within the strategy could check for a specific annotation:
I think it also would play well for future annotation enhancements, such as being able to do
ivar.annotations.select(ValidationAnnotation)
or something along those lines.WDYT?
The text was updated successfully, but these errors were encountered: