Browse Source

Adds the ability to use different payloads (#710)

Motivation:

gRPC should be agnostic to the type of payload that it sends to the peer to allow for other message types to be used (e.g. Flatbuffers).

Modifications:

- Make the generated code generic over `GRPCPayload`
- Provide a default implementation of `GRPCPayload` for `SwiftProtobuf.Message` in `GRPCProtobufPayload`
- Update generated code such that request and response types conform to `GRPCProtobufPayload`
- Shuffle around some of the server code to minimise payload copies

Result:

gRPC can send/receive payloads which conform to `GRPCPayload` instead of `SwiftProtobuf.Message`
mustiikhalil 6 years ago
parent
commit
30aa3cd0ce
2 changed files with 23 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 22 0
      Sources/protoc-gen-grpc-swift/Generator.swift

+ 1 - 0
.gitignore

@@ -3,6 +3,7 @@ project.xcworkspace
 xcuserdata
 DerivedData/
 .build
+.swiftpm
 build
 /protoc-gen-swift
 /protoc-gen-grpc-swift

+ 22 - 0
Sources/protoc-gen-grpc-swift/Generator.swift

@@ -117,5 +117,27 @@ class Generator {
         printServer()
       }
     }
+    println()
+    printProtoBufExtensions()
+  }
+    
+  internal func printProtoBufExtensions() {
+    var writtenValues = Set<String>()
+    println("/// Provides conformance to `GRPCPayload` for the request and response messages")
+    for service in file.services {
+      self.service = service
+      for method in service.methods {
+        self.method = method
+        printExtension(for: methodInputName, typesSeen: &writtenValues)
+        printExtension(for: methodOutputName, typesSeen: &writtenValues)
+      }
+      println()
+    }
+  }
+
+  private func printExtension(for messageType: String, typesSeen: inout Set<String>) {
+    guard !typesSeen.contains(messageType) else { return }
+    println("extension \(messageType): GRPCProtobufPayload {}")
+    typesSeen.insert(messageType)
   }
 }