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

Don't use ObjectiveC-based handling of reserved keywords for Java #187

Merged
merged 11 commits into from
Mar 20, 2019

Conversation

RicoYao
Copy link
Contributor

@RicoYao RicoYao commented Mar 18, 2019

  • Java has its own set of different reserved keywords.
  • Also use a "uid" property name for "id" (ObjC uses "identifier). This is pinterest-specific and we may have to add in a custom override for this instead later.

extension String {
func indent() -> String {
// We indent with tabs and in a post process the tabs are changed to a specific number of spaces
return "\t" + self
}

/// All components separated by _ will be capitalized including the first one
func snakeCaseToCamelCase() -> String {
func snakeCaseToCamelCase(_ language: Languages = Languages.objectiveC) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

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

Some idea: What if we would create a "Language" Enum / Struct that knows that language Self is, e.g. either Objective-C, Java or some other language and that would contain the logic around reserved word replacement and capabilities such as creating snakeCaseToCamelCase, snakeCaseToPropertyName, snakeCaseToPropertyName. We could have one instance of language and passing this around and so we don't have to have this if else branching in a String extension as well as you don't have to pass in .java, .objectiveC everywhere as parameter. You would just call snakeCaseToCamelCase on the Language object you have in the Renderer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Moved to Languages enum.

@@ -137,35 +137,119 @@ let objectiveCReservedWords = Set<String>([
"while",
])

// TODO: "id" is technically allowed. It's possible not everyone wants this replacement.
let javaReservedWordReplacements = [
"id": "uid",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any particular reason for this? The mapping for ObjC is largely because “id” is a reserved word

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As mentioned in a separate comment this is pinterest-specific.

var str: String = self

if let replacementString = objectiveCReservedWordReplacements[self.lowercased()] as String? {
str = replacementString
if language == .objectiveC {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use a switch over if/else and if the language type is .flow we should do nothing

The benefit is exhaustive switching will raise errors as new languages are added

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


// https://en.wikipedia.org/wiki/List_of_Java_keywords
let javaReservedWords = Set<String>([
"abstract",
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should move these new functions as extensions on the language enum.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@rahul-malik
Copy link
Collaborator

@RicoYao

Also use a "uid" property name for "id" (ObjC uses "identifier). This is pinterest-specific and we may have to add in a custom override for this instead later.

Why is this necessary? Just lots of code that would need to be updated if this was changed?

@RicoYao
Copy link
Contributor Author

RicoYao commented Mar 19, 2019

@RicoYao

Also use a "uid" property name for "id" (ObjC uses "identifier). This is pinterest-specific and we may have to add in a custom override for this instead later.

Why is this necessary? Just lots of code that would need to be updated if this was changed?

In addition to code changes, there's a lot of institutional behavior around uid that would be painful to change.

- Java has its own set of different reserved keywords.
- Also use a "uid" property name for "id" (ObjC uses "identifier). This is pinterest-specific and we may have to add in a custom override for this instead later.
"while",
])

// TODO: "id" is technically allowed. It's possible not everyone wants this replacement.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you file an issue to revisit this? I don't want to necessarily block internal progress but I think this warrants a further discussion

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -90,7 +90,7 @@ extension JavaFileRenderer {
fatalError("Bad reference found in schema for class: \(className)")
}
case .oneOf(types: _):
return "\(className)\(param.snakeCaseToCamelCase())"
return "\(className)\(Languages.java.snakeCaseToCamelCase(param))"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you file a ticket for abstracting the language type here ? I think the context of java / objc render should be able to influence which snakeCaseToCamelCase logic is used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@RicoYao
Copy link
Contributor Author

RicoYao commented Mar 20, 2019

@rahul-malik Do you happen to know what I else I need to change to get the LinuxTestIndex to pass?

@rahul-malik
Copy link
Collaborator

rahul-malik commented Mar 20, 2019

@RicoYao - Did you run make build_test_linux_index? Linux TestRunner for Swift required generating classes that declare the test classes so we have a script to do this generation.

Please file a ticket to improve this since it's not obvious, I'll take care of making this a part of the lint step so it's a bit more obvious

@rahul-malik
Copy link
Collaborator

rahul-malik commented Mar 20, 2019

@RicoYao - I just looked at the failure log, run that make command and commit the changes and this should be fixed. sorry for the confusion!

@RicoYao
Copy link
Contributor Author

RicoYao commented Mar 20, 2019

Thanks @rahul-malik that did the trick! FYI, I also had to run make format to fix some issues on the generated changes from make build_test_index_linux.

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.

3 participants