-
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
[ggj] feat: support file header (apache license) for outer class definition #157
Conversation
src/main/java/com/google/api/generator/gapic/composer/FileHeader.java
Outdated
Show resolved
Hide resolved
src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java
Outdated
Show resolved
Hide resolved
src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java
Outdated
Show resolved
Hide resolved
src/main/java/com/google/api/generator/gapic/composer/FileHeader.java
Outdated
Show resolved
Hide resolved
Arrays.stream(fileHeadeStrings) | ||
.forEach( | ||
s -> { | ||
apacheFileHeader.add(CommentStatement.withComment(LineComment.withComment(s))); |
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.
Clients (example)currently have this in a BlockComment, so let's use that instead.
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.
src/main/java/com/google/api/generator/gapic/composer/FileHeader.java
Outdated
Show resolved
Hide resolved
src/main/java/com/google/api/generator/gapic/composer/FileHeader.java
Outdated
Show resolved
Hide resolved
...in/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java
Outdated
Show resolved
Hide resolved
src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java
Outdated
Show resolved
Hide resolved
src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java
Outdated
Show resolved
Hide resolved
src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java
Outdated
Show resolved
Hide resolved
…ator-java into file-header
Left some questions in the unresolved comments @miraleung Summarized them here for clarity:
|
src/main/java/com/google/api/generator/gapic/composer/ApacheLicenseComposer.java
Outdated
Show resolved
Hide resolved
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class ApacheLicenseComposer { |
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.
Looking at this again, maybe call this class "ApacheLicense" instead, since it doesn't do any composing?
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.
since it's not a composer, how about moving it to utils
?
src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java
Outdated
Show resolved
Hide resolved
@@ -100,6 +102,7 @@ private static GapicClass generateGenericClass(Kind kind, String name, Service s | |||
|
|||
ClassDefinition classDef = | |||
ClassDefinition.builder() | |||
.setFileHeader(ApacheLicenseComposer.APACHE_LICENSE) |
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 currently generate many classes, and this method is a placeholder that will be going away. Can you think of ways to restructure this such that we can add a license to a class and test that in isolation? How would we incorporate that into the existing classes?
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.
sure, good point. Since we are creating different GapicClasses in the composers, I think a central place to add a file header is GapicClass
class. Call GapicClass.create().addApacheLicnese()
for adding license to existing class. 3 reasons are in consideration:
- ClassDefinition is another alternative place to have this helper, but in the end, it's gapic class that needs/wraps it, so putting the helper in gapic class might make more sense.
- The way to incorporate header to the existing class is
GapicClass.create(kind, classDef).addApacheLicense();
- it can be independently tested in
GapicClassTest
without altering composer files.
Alternatives:
- tried to have a private helper
addApacheLicense(GapicClass)
inComposer
class, but in that way we will call it byclazzes.add(**Composer.instance().generate().addApacheLicense())
I think adding file header is more suitable in a class level, instead of composer level. - add the helper to
ApacheLicense
as a util, butApacheLicense.addLicenseToGapicClass()
method should makes more sense to directly be called from the class sideGapicClass.addApacheLicense()
Let me know your thoughts @miraleung Thanks
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.
GapicClass has no extra logic beyond holding data, so let's keep it that way. Let's add a helper method in Composer.java that adds license headers to each GapicClass instead.
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.
updated the Composer
class and add ComposerTest
to test file header addition in isolation.
@@ -62,14 +62,15 @@ | |||
|
|||
private static final String COLON = ":"; | |||
private static final String COMMA = ","; | |||
private static final String BLOCK_COMMENT_START = "/**"; |
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.
Please separate the changes in #168 from this PR.
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.
Hold this PR until #168 is merged.
src/main/java/com/google/api/generator/gapic/composer/Composer.java
Outdated
Show resolved
Hide resolved
@AutoValue.Builder | ||
public abstract static class Builder { | ||
public abstract Builder setFileHeader(List<CommentStatement> fileHeader); |
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.
Does it make sense to error-out in build()
if this is a nested class?
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.
yes, added one condition check in build
to confirm nested classes do not have file headers.
src/main/java/com/google/api/generator/gapic/composer/Composer.java
Outdated
Show resolved
Hide resolved
@@ -347,4 +348,8 @@ private static ForStatement createForStatement() { | |||
.setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr()))) | |||
.build(); | |||
} | |||
// Create a simple block comment to stand for the Apache License header. | |||
private static List<CommentStatement> createFileHeader() { |
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.
If this is just used once, do we need a helper method for it? Would prefer to just add the logic to the method where it's used.
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.
Updated with adding a unit test (nestClassWithFileHeader) which also uses this. So I guess we can keep it. But this similar helper is only called once in JavaWriterVisitorTest, so removed it there. Thanks
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.
LGTM with nits addressed.
src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java
Outdated
Show resolved
Hide resolved
src/main/java/com/google/api/generator/gapic/utils/ApacheLicense.java
Outdated
Show resolved
Hide resolved
This PR was generated using Autosynth. 🌈 Synth log will be available here: https://source.cloud.google.com/results/invocations/4d7892ee-6265-4e89-b8a8-1c509f6b7076/targets - [ ] To automatically regenerate this PR, check this box. Source-Link: googleapis/synthtool@da29da3
No description provided.