Przeglądaj źródła

Add a codegen option to allow stub names to not have their first character lowercased (#1123)

Motivation:

Protobuf suggests that RPC methods should always start with an uppercase
character. Function names in Swift typically start with a lowercase
character. As such, by default, protoc-gen-grpc-swift will lowercase the
first character.

Having an uppercase RPC method is by no means a requirement and since
routing is not case sensitive, defining two methods 'Foo' and 'foo'
within the same service will lead to compilation errors since both
generated Swift function names will be 'foo'.

Modifications:

- Add an escape hatch to the plugin to allow for method casing to be
  kept, this defaults to false
- Add a test service using these options
- Update docs

Result:

Users can generate code which dos not alter the casing of the RPC name
George Barnett 5 lat temu
rodzic
commit
f89a4d86fb

+ 5 - 1
Sources/protoc-gen-grpc-swift/Generator-Names.swift

@@ -71,7 +71,11 @@ extension Generator {
 
   internal var methodFunctionName: String {
     let name = method.name
-    return name.prefix(1).lowercased() + name.dropFirst()
+    if self.options.keepMethodCasing {
+      return name
+    } else {
+      return name.prefix(1).lowercased() + name.dropFirst()
+    }
   }
 
   internal var methodInputName: String {

+ 8 - 0
Sources/protoc-gen-grpc-swift/options.swift

@@ -55,6 +55,7 @@ final class GeneratorOptions {
   private(set) var generateServer = true
   private(set) var generateClient = true
   private(set) var generateTestClient = false
+  private(set) var keepMethodCasing = false
   private(set) var protoToModuleMappings = ProtoFileToModuleMappings()
   private(set) var fileNaming = FileNaming.FullPath
   private(set) var extraModuleImports: [String] = []
@@ -91,6 +92,13 @@ final class GeneratorOptions {
           throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
         }
 
+      case "KeepMethodCasing":
+        if let value = Bool(pair.value) {
+          self.keepMethodCasing = value
+        } else {
+          throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
+        }
+
       case "ProtoPathModuleMappings":
         if !pair.value.isEmpty {
           do {