-
Notifications
You must be signed in to change notification settings - Fork 53
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
feat(ast): extend support for annotation named parameters #1012
Conversation
…rameters with name and value in annotation.
import com.google.auto.value.AutoValue; | ||
|
||
@AutoValue | ||
public abstract class ClazzValue implements ObjectValue { |
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.
We already have a way to achieve the same thing, see the code here and the golden test file here. I think it's better not to have different ways to achieve the same thing, it would create more maintenance burden in the future. If you want, you could extract the common logic to a util method/class, something like createClazzValueExpr
or createClazzVariableExpr
, which only takes type as an argument.
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.
Ah, thanks for the tip. I couldn't wrap my head around needing a Value
instead of Variable
.
Arrays.asList(ValueExpr.withValue(StringObjectValue.withValue(description)))); | ||
} | ||
|
||
public Builder setDescriptions(List<Expr> exprList) { |
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 like this improvement in general, but we should be careful exposing a setter that can set descriptions to any Expr
, you can technically pass a NewObjectExpr
or a ReturnExpr
and everything will work until we try to compile the generated code, which could be too late to find an issue.
Correct me if I'm wrong, I think we only allow ValueExpr
, AssignmentExpr
, or VariableExpr
(for clazz if you remove CLazzValue
). So we could either add some type checks in the build()
method, or only expose methods to add descriptions for the above types, like addDescription(ValueExpr expr)
, addDescription(AssignmentExpr expr)
and addDescription(VariableExpr expr)
.
Basically anything that could restrict people from passing any types of Expr
would be great.
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.
Good point, let me refactor this part and expose methods for each expr type.
…nnecessary ClazzValue class.
Kudos, SonarCloud Quality Gate passed! |
Adding a small enhancement to this PR since it's related.
Field annotations are pretty common in Spring syntax, this enhancement is needed to generate Spring autoconfig code as I planned. |
Kudos, SonarCloud Quality Gate passed! |
* This PR is a follow-up patch to #1012 which added support for annotations on VariableExpr. It updates ImportWriterVisitor so that import generation also covers these annotations.
* This PR is a follow-up patch to #1012 which added support for annotations on VariableExpr. It updates ImportWriterVisitor so that import generation also covers these annotations.
This change is to extend annotation arguments support. **Before**: Only one argument that consist of a single `String` can be added to annotation description. **After**: For single unnamed parameter/description, add support for `ValueExpr` and `VariableExpr` in addition to `String`. Also allow adding multiple named parameters of type `VariableExpr`. So that annotations like below can be generated: ``` @ConditionalOnClass(VisionServiceClient.class) @ConditionalOnProperty(value = "spring.cloud.gcp.vision.enabled", matchIfMissing = true) ``` this improvement is an addition and does not affect existing usage of `setDescription(String description)`. Added tests in [JavaWriterVisitorTest.java](https://github.com/googleapis/gapic-generator-java/pull/1012/files#diff-60163f71e845b4cc97f9ccd7101646aa243fa5317a77cd326a7f61143618f62f) also shows the new feature usages. Adding a small enhancement to this PR since it's related. In addition to previous changes, I added an optional field to VariableExpr allowing Annotations. Prior to this, only annotations on ClassDefinition and MethodDefinition are supported. With this addition, I will be able to generate code like: ``` @NestedConfigurationProperty private final Credentials credentials = new Credentials(); // or @Autowired private LanguageServiceClient autoClient; ``` Field annotations are pretty common in Spring syntax, this enhancement is needed to generate Spring autoconfig code as I planned. Note: Added a todo note in `VariableExpr` only as a nice to have feature for future, it's not blocking any use cases for now. But it seems reasonable to have target info in `AnnotationNode` and apply checks on it when adding annotation nodes.
* This PR is a follow-up patch to #1012 which added support for annotations on VariableExpr. It updates ImportWriterVisitor so that import generation also covers these annotations.
) This change is cherry-picked from an AST fix made in the spring branch (https://togithub.com/googleapis/gapic-generator-java/pull/1208). It fixes `ImportWriter` to account for types introduced by annotation parameters, which as enabled in #1012.
🤖 I have created a release *beep* *boop* --- ## [2.8.26](https://togithub.com/googleapis/java-core/compare/v2.8.25...v2.8.26) (2022-11-07) ### Dependencies * Update dependency com.google.api-client:google-api-client-bom to v2.0.1 ([#1011](https://togithub.com/googleapis/java-core/issues/1011)) ([bbcaed9](https://togithub.com/googleapis/java-core/commit/bbcaed9a9688a054adbb8d4615af14566b98a297)) * Update dependency com.google.api.grpc:proto-google-common-protos to v2.10.0 ([#1010](https://togithub.com/googleapis/java-core/issues/1010)) ([1f41e6b](https://togithub.com/googleapis/java-core/commit/1f41e6b9bf86ea94dbd0efda348d46f04e62baa4)) * Update dependency com.google.api.grpc:proto-google-iam-v1 to v1.6.7 ([#1005](https://togithub.com/googleapis/java-core/issues/1005)) ([aac06c2](https://togithub.com/googleapis/java-core/commit/aac06c231ae59e4e913a228328202bdceda39ec4)) --- This PR was generated with [Release Please](https://togithub.com/googleapis/release-please). See [documentation](https://togithub.com/googleapis/release-please#release-please).
[updated]
This change is to extend annotation arguments support.
Before: Only one argument that consist of a single
String
can be added to annotation description.After: For single unnamed parameter/description, add support for
ValueExpr
andVariableExpr
in addition toString
. Also allow adding multiple named parameters of typeVariableExpr
. So that annotations like below can be generated:this improvement is an addition and does not affect existing usage of
setDescription(String description)
.Added tests in JavaWriterVisitorTest.java also shows the new feature usages.