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

feat(rosetta): add skeleton of Java snippet translator #985

Merged
merged 98 commits into from
Dec 24, 2019

Conversation

skinny85
Copy link
Contributor

Includes a testing harness for the translators that executes
by automatically detecting files in a directory,
and using those files as the expected translation values.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Includes a testing harness for the translators that executes
by automatically detecting files in a directory,
and using those files as the expected translation values.
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Use describe() for files and test() for languages. Use beforeAll() to
set up the translator in a way that Jest can account for the time spent
there, and be sure to clean up afterwards because the thing is huge.

Translate all existing Python tests into the new format, and make sure
TSC won't compile the example TypeScript files.
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

packages/jsii-rosetta/lib/languages/java.ts Outdated Show resolved Hide resolved
}

/*
* Because structs are represented in Java as interfaces with a JsiiProxy implementation,
Copy link
Contributor

Choose a reason for hiding this comment

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

But what if a user wants to write a struct themselves in Java, to be used in Java? Then there will be no JsiiProxy. They would probably write a class to hold their "props". See the C# generator.

This translator is about code the user would write, not mirroring what jsii does for them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you elaborate on what the translation should be? Let's say we have

interface MyStruct {
  readonly a: string;
  b: number;
}

in TypeScript. Should I generate

a) an interface with getters (and setters for non-readonly fields)?
b) a class with a constructor taking all fields as arguments, and just assigning them in that constructor?
c) a class that has a nested static Builder class that is used to create it, and the constructor of the class itself is private, similar to what JSII is doing?
d) a simple class with just the fields, and Lombok annotations (@Builder, @Data, etc.)?
e) something else...?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, good question. I'd like the syntactically most lightweight thing that roughly expresses the intent. I have a feeling the builder would add a lot of noise to make it work nicely at the consumer site, so I think I'd prefer either b or d (or b as a POJO).

I guess between those it depends on how reasonable it is for people to have Lombok installed. If not, I think POJOs are a reasonable compromise? Or honestly, publicly accessible fields without getters or setters (is what I did in C#). In fact, that is what structs are, just a bag of data. readonly doesn't really come into it, because this is only for structs and all fields are readonly by definition.

I can't really tell you the solution here, only what I value: readability and simplicity. I don't want all the syntax of a "super up-to-snuff" class definition get in the way of the rest of the example. People know how to translate from plain public attributes or POJOs to their preferred immutable/builder/deserialize/Lombok flavors that apply.

I think non-final public variables are the simplest solution. Only thing that gives me slight pause there is that people will get stuck up parroting how "that's an anti-pattern" to us and missing the forest for the trees.

Wrap those suckers up in getters and setters that don't do anything other than manipulating those same members (and crucially, never will, either!) and suddenly nobody bats an eye. Go figure.

I need your opinion here. Do what seems right to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. I've settled on private fields + getters and fluent setters, so creating instances of the class looks something like this:

new MyStruct()
        .a("a")
        .b(42);

, which seems the closest we can get in Java to JavaScript's object literal syntax without generating a separate Builder class, which would be very verbose, like you said.

Let me know if this works!

packages/jsii-rosetta/lib/languages/java.ts Outdated Show resolved Hide resolved
packages/jsii-rosetta/lib/languages/java.ts Outdated Show resolved Hide resolved
packages/jsii-rosetta/lib/languages/java.ts Show resolved Hide resolved
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

…argument as ClassName.Builder.create() expressions.
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@rix0rrr rix0rrr merged commit d591b85 into master Dec 24, 2019
@rix0rrr rix0rrr deleted the feat/java-snippets-translation branch December 24, 2019 12:55
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.

4 participants