Browse Source

Copy metadata when making requests.

The underlying gRPC core adds additional properties to metadata before making requests.
Tim Burks 9 years ago
parent
commit
4baeabf956

+ 4 - 4
Examples/Echo/Swift/Generated/echo.client.pb.swift

@@ -319,24 +319,24 @@ public class Echo_EchoService {
 
   // Synchronous. Unary.
   public func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
-    return try Echo_EchoGetCall(channel).run(request:request, metadata:metadata)
+    return try Echo_EchoGetCall(channel).run(request:request, metadata:metadata.copy() as! Metadata)
   }
   // Asynchronous. Server-streaming.
   // Send the initial message.
   // Use methods on the returned object to get streamed responses.
   public func expand(_ request: Echo_EchoRequest) throws -> Echo_EchoExpandCall {
-    return try Echo_EchoExpandCall(channel).run(request:request, metadata:metadata)
+    return try Echo_EchoExpandCall(channel).run(request:request, metadata:metadata.copy() as! Metadata)
   }
   // Asynchronous. Client-streaming.
   // Use methods on the returned object to stream messages and
   // to close the connection and wait for a final response.
   public func collect() throws -> Echo_EchoCollectCall {
-    return try Echo_EchoCollectCall(channel).run(metadata:metadata)
+    return try Echo_EchoCollectCall(channel).run(metadata:metadata.copy() as! Metadata)
   }
   // Asynchronous. Bidirectional-streaming.
   // Use methods on the returned object to stream messages,
   // to wait for replies, and to close the connection.
   public func update() throws -> Echo_EchoUpdateCall {
-    return try Echo_EchoUpdateCall(channel).run(metadata:metadata)
+    return try Echo_EchoUpdateCall(channel).run(metadata:metadata.copy() as! Metadata)
   }
 }

+ 4 - 4
Plugin/swiftgrpc.templates/client.pb.swift

@@ -101,7 +101,7 @@ public class {{ protoFile.package|capitalize }}_{{ service.name }}Service {
   //-{% if not method.clientStreaming and not method.serverStreaming %}
   // Synchronous. Unary.
   public func {{ method.name|lowercase }}(_ request: {{ method|input }}) throws -> {{ method|output }} {
-    return try {{ .|call:protoFile,service,method }}(channel).run(request:request, metadata:metadata)
+    return try {{ .|call:protoFile,service,method }}(channel).run(request:request, metadata:metadata.copy() as! Metadata)
   }
   //-{% endif %}
   //-{% if not method.clientStreaming and method.serverStreaming %}
@@ -109,7 +109,7 @@ public class {{ protoFile.package|capitalize }}_{{ service.name }}Service {
   // Send the initial message.
   // Use methods on the returned object to get streamed responses.
   public func {{ method.name|lowercase }}(_ request: {{ method|input }}) throws -> {{ .|call:protoFile,service,method }} {
-    return try {{ .|call:protoFile,service,method }}(channel).run(request:request, metadata:metadata)
+    return try {{ .|call:protoFile,service,method }}(channel).run(request:request, metadata:metadata.copy() as! Metadata)
   }
   //-{% endif %}
   //-{% if method.clientStreaming and not method.serverStreaming %}
@@ -117,7 +117,7 @@ public class {{ protoFile.package|capitalize }}_{{ service.name }}Service {
   // Use methods on the returned object to stream messages and
   // to close the connection and wait for a final response.
   public func {{ method.name|lowercase }}() throws -> {{ .|call:protoFile,service,method }} {
-    return try {{ .|call:protoFile,service,method }}(channel).run(metadata:metadata)
+    return try {{ .|call:protoFile,service,method }}(channel).run(metadata:metadata.copy() as! Metadata)
   }
   //-{% endif %}
   //-{% if method.clientStreaming and method.serverStreaming %}
@@ -125,7 +125,7 @@ public class {{ protoFile.package|capitalize }}_{{ service.name }}Service {
   // Use methods on the returned object to stream messages,
   // to wait for replies, and to close the connection.
   public func {{ method.name|lowercase }}() throws -> {{ .|call:protoFile,service,method }} {
-    return try {{ .|call:protoFile,service,method }}(channel).run(metadata:metadata)
+    return try {{ .|call:protoFile,service,method }}(channel).run(metadata:metadata.copy() as! Metadata)
   }
   //-{% endif %}
   //-{% endfor %}

+ 9 - 1
Sources/gRPC/Metadata.swift

@@ -46,7 +46,7 @@ private struct MetadataPair {
 }
 
 /// Metadata sent with gRPC messages
-public class Metadata : CustomStringConvertible {
+public class Metadata : CustomStringConvertible, NSCopying {
 
   /// Pointer to underlying C representation
   var underlyingArray: UnsafeMutableRawPointer
@@ -110,4 +110,12 @@ public class Metadata : CustomStringConvertible {
     }
     return result
   }
+
+  public func copy(with zone: NSZone? = nil) -> Any {
+    let copy = Metadata()
+    for i in 0..<count() {
+      copy.add(key:self.key(i), value:self.value(i))
+    }
+    return copy
+  }
 }