-
Notifications
You must be signed in to change notification settings - Fork 6
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
Fix casing of generated services and methods #8
Conversation
Motivation: Given a method name, say `ImportCSV`, from a `.proto` file, the generated method name in upper and lower camel case is `ImportCsv` and `importCsv` respectively. The generated method name (which is already expected to be in upper camel case) in upper and lower camel case should be `ImportCSV` and `importCSV` respectively. Modifications: - Replace the method used for generating service and method names in lower camel case with a newly implemented method. - Do not convert the base names of services and methods to upper camel case as they are expected to already be in upper camel case. Result: The casing of service and method names will be preserved when generating stubs.
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.
Broadly looks good. I left a few comments inline about simplifying a couple of bits.
package struct CamelCaser { | ||
/// Converts a string from upper camel case to lower camel case. | ||
package static func toLowerCamelCase(_ s: String) -> String { | ||
if s.isEmpty { return "" } |
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.
I don't think we need this check. If the string is empty then indexOfFirstLowerCase
will be nil
and we'll return s.lowercased()
(which is fine)
|
||
package struct CamelCaser { | ||
/// Converts a string from upper camel case to lower camel case. | ||
package static func toLowerCamelCase(_ s: String) -> String { |
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.
Single character names are rarely a good idea. input
is fine here.
package static func toLowerCamelCase(_ s: String) -> String { | ||
if s.isEmpty { return "" } | ||
|
||
let indexOfFirstLowerCase = s.firstIndex(where: { $0 != "_" && $0.lowercased() == String($0) }) |
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.
I don't think the "_" is relevant here. We also don't need to convert the character into a string, we can just do:
let indexOfFirstLowerCase = s.firstIndex(where: { $0 != "_" && $0.lowercased() == String($0) }) | |
let indexOfFirstLowerCase = s.firstIndex(where: { $0.isLowercase }) |
package static func toLowerCamelCase(_ s: String) -> String { | ||
if s.isEmpty { return "" } | ||
|
||
let indexOfFirstLowerCase = s.firstIndex(where: { $0 != "_" && $0.lowercased() == String($0) }) |
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.
Also the correct casing of indexOfFirstLowerCase
is indexOfFirstLowercase
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.
Ironic 😂
} else { | ||
// `s` did not contain any lower case letter. | ||
return s.lowercased() | ||
} |
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 can do an early exit if indexOfFirstLowercase
is nil:
guard let indexOfFirstLowerCase = s.firstIndex(where: { ... }) else {
return s.lowercased()
}
@@ -15,7 +15,6 @@ | |||
*/ | |||
|
|||
internal import Foundation | |||
internal import SwiftProtobuf |
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.
Was this import not used for anything?
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.
It was imported to access NamingUtils
which is no longer 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.
NamingUtils
comes from the SwiftProtobufPluginLibrary
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.
Oh yes. That means SwiftProtobuf was already not in use.
@@ -15,7 +15,6 @@ | |||
*/ | |||
|
|||
internal import Foundation | |||
internal import SwiftProtobuf |
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.
NamingUtils
comes from the SwiftProtobufPluginLibrary
* limitations under the License. | ||
*/ | ||
|
||
package struct CamelCaser { |
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.
This is just being used as a namespace so should be an enum
:
package struct CamelCaser { | |
package enum CamelCaser { |
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.
Great, thank you @clintonpi
Motivation:
Given a method name, say
ImportCSV
, from a.proto
file, the generated method name in upper and lower camel case isImportCsv
andimportCsv
respectively. The generated method name (which is already expected to be in upper camel case) in upper and lower camel case should beImportCSV
andimportCSV
respectively.Modifications:
Result:
The casing of service and method names will be preserved when generating stubs.