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

[ggj] feat: support file header (apache license) for outer class definition #157

Merged
merged 30 commits into from
Aug 19, 2020

Conversation

xiaozhenliu-gg5
Copy link
Contributor

No description provided.

@xiaozhenliu-gg5 xiaozhenliu-gg5 added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Aug 6, 2020
@xiaozhenliu-gg5 xiaozhenliu-gg5 removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Aug 6, 2020
@xiaozhenliu-gg5 xiaozhenliu-gg5 changed the title [ggj][wip]feat: support file header (apache license) for outer class definition [ggj] feat: support file header (apache license) for outer class definition Aug 6, 2020
Arrays.stream(fileHeadeStrings)
.forEach(
s -> {
apacheFileHeader.add(CommentStatement.withComment(LineComment.withComment(s)));
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix for BlockComment is here: #168
It cannot be wrapped in the current blockComment because the formatting will be messed up, more details are in #168 PR description. Thanks

@xiaozhenliu-gg5
Copy link
Contributor Author

xiaozhenliu-gg5 commented Aug 9, 2020

Left some questions in the unresolved comments @miraleung Summarized them here for clarity:

  1. altered BlockComment class for supporting the file header composer.
  2. all file header addition to *Composer classes are removed since it's optional, but add the file header to Composer.java itself and test it in new ComposerTest.java.

import java.util.Arrays;
import java.util.List;

public class ApacheLicenseComposer {
Copy link
Contributor

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?

Copy link
Contributor Author

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?

@@ -100,6 +102,7 @@ private static GapicClass generateGenericClass(Kind kind, String name, Service s

ClassDefinition classDef =
ClassDefinition.builder()
.setFileHeader(ApacheLicenseComposer.APACHE_LICENSE)
Copy link
Contributor

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?

Copy link
Contributor Author

@xiaozhenliu-gg5 xiaozhenliu-gg5 Aug 11, 2020

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:

  1. 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.
  2. The way to incorporate header to the existing class is GapicClass.create(kind, classDef).addApacheLicense();
  3. it can be independently tested in GapicClassTest without altering composer files.

Alternatives:

  1. tried to have a private helper addApacheLicense(GapicClass) in Composer class, but in that way we will call it by clazzes.add(**Composer.instance().generate().addApacheLicense()) I think adding file header is more suitable in a class level, instead of composer level.
  2. add the helper to ApacheLicense as a util, but ApacheLicense.addLicenseToGapicClass() method should makes more sense to directly be called from the class side GapicClass.addApacheLicense()
    Let me know your thoughts @miraleung Thanks

Copy link
Contributor

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.

Copy link
Contributor Author

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 = "/**";
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setFileHeader(List<CommentStatement> fileHeader);
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@@ -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() {
Copy link
Contributor

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.

Copy link
Contributor Author

@xiaozhenliu-gg5 xiaozhenliu-gg5 Aug 18, 2020

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

Copy link
Contributor

@miraleung miraleung left a 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.

@xiaozhenliu-gg5 xiaozhenliu-gg5 merged commit 286d0dd into master Aug 19, 2020
@xiaozhenliu-gg5 xiaozhenliu-gg5 deleted the file-header branch September 14, 2020 22:15
suztomo pushed a commit that referenced this pull request Mar 21, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants