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

Add custom TypeAdapter for Java models that will allow for setting _bits field #177

Merged
merged 7 commits into from
Mar 13, 2019

Conversation

RicoYao
Copy link
Contributor

@RicoYao RicoYao commented Mar 8, 2019

Without this, models created from GSON won't have their isVARSet() methods working properly.

Unfortunately it's not possible to configure GSON to call property setters during deserialization (it uses reflection instead) so we need a custom TypeAdapter.

Also cleaned up some tabbing and spacing issues.

…its field

Without this, models created from GSON won't have their isVARSet() methods working properly.
Examples/Java/Sources/Board.java Outdated Show resolved Hide resolved
Copy link
Collaborator

@rahul-malik rahul-malik left a comment

Choose a reason for hiding this comment

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

@RicoYao - The changes for the TypeAdapter and additional DSL work in JavaIR look good! I'm mostly just concerned around the rendering changes when using --> since it seems a bit more verbose and potentially harder to follow. Thoughts?

})
}

if !innerClasses.isEmpty {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems like the issue with a lot of the formatting here is just handling empty scopes? My only worry is that I feel like the code has becomes a bit less readable / maintainable after these changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was to handle a few things:

  1. Avoid having an empty line if the enums/properties/methods/innerClasses is empty.
  2. Avoid having tabbed empty lines. I think I can solve this in a different way by tweaking the indent() string extension function.
  3. Other little tweaks like adding an empty line in between each method.

I tried to simplify it a bit. Let me know if you still think it's too un-readable.

Copy link
Contributor Author

@RicoYao RicoYao Mar 13, 2019

Choose a reason for hiding this comment

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

by tweaking the indent() string extension function.

Ended up tweaking --> instead of indent()

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you, this looks a lot better! Did you get a chance to run make integration_test after? I think this might alter the output of the other languages

Sources/Core/JavaModelRenderer.swift Show resolved Hide resolved
] }

let read = JavaIR.method(
annotations: ["Override"],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Question: Should we create an enum represent the various valid annotations?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I plan to add the ability for custom annotations to be passed in through a command-line option / auxillary schema file so I don't think I can restrict it to an enum.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So a swift enum with a associated type could work here

Something like this:

enum JavaAnnotation {
  case override
  case nonnull
  .....
  case custom(name: String, fromPackage: String) // Needed for imports? 
}

Then the ones passed in via the CLI can be represented using the custom case

Before working on a PR for this would you mind putting up an RFC around the use case and general approach you'd like to take? I have some thoughts on adding more customization points to Plank and we might be able to collaborate on a solution

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll sync up with you on custom annotations before I add any code for that.

In the meantime, I've switched to an enum to lock it down to allowed annotations as you suggested.

@RicoYao
Copy link
Contributor Author

RicoYao commented Mar 13, 2019

@RicoYao - The changes for the TypeAdapter and additional DSL work in JavaIR look good! I'm mostly just concerned around the rendering changes when using --> since it seems a bit more verbose and potentially harder to follow. Thoughts?

Good call. I hopefully made it a bit more readable. Let me know.

Sources/Core/JavaFileRenderer.swift Outdated Show resolved Hide resolved
Sources/Core/JavaFileRenderer.swift Outdated Show resolved Hide resolved
})
}

if !innerClasses.isEmpty {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you, this looks a lot better! Did you get a chance to run make integration_test after? I think this might alter the output of the other languages

Sources/Core/JavaIR.swift Outdated Show resolved Hide resolved
Sources/Core/JavaIR.swift Outdated Show resolved Hide resolved
@RicoYao
Copy link
Contributor Author

RicoYao commented Mar 13, 2019

Thank you, this looks a lot better! Did you get a chance to run make integration_test after? I think this might alter the output of the other languages

Yes, I have been running make integration_test for each of these commits. I was surprised to see no change to the other languages, especially after the change to -->. I don't think changed any configs to restrict to building java examples only, but maybe I did something local that I forgot about.

Copy link
Collaborator

@rahul-malik rahul-malik left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for going back and forth on the comments and revising the PR. Once CI passes I'll merge unless you think I should hold off.

let modifiers: JavaModifier
let body: [String]
let signature: String

func render() -> [String] {
// HACK: We should actually have an enum / optionset that we can check for abstract, static, ...
let annotationLines = annotations.map { "@\($0)" }
let annotationLines = annotations.map { "@\($0.rawValue)" }
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you'll need to sort the annotations to prevent the output from becoming non-deterministic

Copy link
Collaborator

Choose a reason for hiding this comment

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

The output stability test should fail if I'm right on this one

Copy link
Collaborator

Choose a reason for hiding this comment

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

Wait you didn't actually change that so if this was going to fail it would have already...

@@ -35,7 +35,7 @@ public struct JavaModelRenderer: JavaFileRenderer {
param.snakeCaseToPropertyName()
}.joined(separator: ",\n")

return JavaIR.method(annotations: ["Override"], [.public], "int hashCode()") { [
return JavaIR.method(annotations: [JavaAnnotation.override], [.public], "int hashCode()") { [
Copy link
Collaborator

@rahul-malik rahul-malik Mar 13, 2019

Choose a reason for hiding this comment

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

you should be able to remove JavaAnnotation and just put .override here and elsewhere. only reason to do this is if the compiler is unable to infer the value types

Copy link
Collaborator

Choose a reason for hiding this comment

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

This can be updated in a followup PR so we can merge this

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 will fix in a separate PR. I also realized I missed converting the string-based annotations in JavaIR.Property so I'll fix that too.

@rahul-malik rahul-malik merged commit 47ad603 into pinterest:master Mar 13, 2019
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