Selaa lähdekoodia

Update examples (#2163)

Motivation:

We changed a few API across the packages so the examples need to be
brought up-to-date.

Modifications:

- Update generated code
- Update dependencies
- Fix warnings

Result:

Examples are up-to-date
George Barnett 10 kuukautta sitten
vanhempi
commit
b143369c7b

+ 3 - 3
Examples/echo/Package.swift

@@ -21,9 +21,9 @@ let package = Package(
   name: "echo",
   platforms: [.macOS("15.0")],
   dependencies: [
-    .package(url: "https://github.com/grpc/grpc-swift.git", exact: "2.0.0-beta.2"),
-    .package(url: "https://github.com/grpc/grpc-swift-protobuf.git", exact: "1.0.0-beta.2"),
-    .package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", exact: "1.0.0-beta.2"),
+    .package(url: "https://github.com/grpc/grpc-swift.git", branch: "main"),
+    .package(url: "https://github.com/grpc/grpc-swift-protobuf.git", branch: "main"),
+    .package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", branch: "main"),
     .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.0"),
   ],
   targets: [

+ 4 - 4
Examples/echo/Sources/Generated/echo.grpc.swift

@@ -347,7 +347,7 @@ extension Echo_Echo {
 
 // Default implementation of 'registerMethods(with:)'.
 extension Echo_Echo.StreamingServiceProtocol {
-    internal func registerMethods(with router: inout GRPCCore.RPCRouter) {
+    internal func registerMethods<Transport>(with router: inout GRPCCore.RPCRouter<Transport>) where Transport: GRPCCore.ServerTransport {
         router.registerHandler(
             forMethod: Echo_Echo.Method.Get.descriptor,
             deserializer: GRPCProtobuf.ProtobufDeserializer<Echo_EchoRequest>(),
@@ -600,14 +600,14 @@ extension Echo_Echo {
     /// The ``Client`` provides an implementation of ``ClientProtocol`` which wraps
     /// a `GRPCCore.GRPCCClient`. The underlying `GRPCClient` provides the long-lived
     /// means of communication with the remote peer.
-    internal struct Client: ClientProtocol {
-        private let client: GRPCCore.GRPCClient
+    internal struct Client<Transport>: ClientProtocol where Transport: GRPCCore.ClientTransport {
+        private let client: GRPCCore.GRPCClient<Transport>
 
         /// Creates a new client wrapping the provided `GRPCCore.GRPCClient`.
         ///
         /// - Parameters:
         ///   - client: A `GRPCCore.GRPCClient` providing a communication channel to the service.
-        internal init(wrapping client: GRPCCore.GRPCClient) {
+        internal init(wrapping client: GRPCCore.GRPCClient<Transport>) {
             self.client = client
         }
 

+ 3 - 11
Examples/echo/Sources/Subcommands/Collect.swift

@@ -27,18 +27,12 @@ struct Collect: AsyncParsableCommand {
   var arguments: ClientArguments
 
   func run() async throws {
-    let client = GRPCClient(
-      transport: try .http2NIOPosix(
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
         target: self.arguments.target,
         transportSecurity: .plaintext
       )
-    )
-
-    try await withThrowingDiscardingTaskGroup { group in
-      group.addTask {
-        try await client.run()
-      }
-
+    ) { client in
       let echo = Echo_Echo.Client(wrapping: client)
 
       for _ in 0 ..< self.arguments.repetitions {
@@ -50,8 +44,6 @@ struct Collect: AsyncParsableCommand {
         }
         print("collect ← \(message.text)")
       }
-
-      client.beginGracefulShutdown()
     }
   }
 }

+ 3 - 11
Examples/echo/Sources/Subcommands/Expand.swift

@@ -27,18 +27,12 @@ struct Expand: AsyncParsableCommand {
   var arguments: ClientArguments
 
   func run() async throws {
-    let client = GRPCClient(
-      transport: try .http2NIOPosix(
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
         target: self.arguments.target,
         transportSecurity: .plaintext
       )
-    )
-
-    try await withThrowingDiscardingTaskGroup { group in
-      group.addTask {
-        try await client.run()
-      }
-
+    ) { client in
       let echo = Echo_Echo.Client(wrapping: client)
 
       for _ in 0 ..< self.arguments.repetitions {
@@ -50,8 +44,6 @@ struct Expand: AsyncParsableCommand {
           }
         }
       }
-
-      client.beginGracefulShutdown()
     }
   }
 }

+ 3 - 11
Examples/echo/Sources/Subcommands/Get.swift

@@ -25,18 +25,12 @@ struct Get: AsyncParsableCommand {
   var arguments: ClientArguments
 
   func run() async throws {
-    let client = GRPCClient(
-      transport: try .http2NIOPosix(
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
         target: self.arguments.target,
         transportSecurity: .plaintext
       )
-    )
-
-    try await withThrowingDiscardingTaskGroup { group in
-      group.addTask {
-        try await client.run()
-      }
-
+    ) { client in
       let echo = Echo_Echo.Client(wrapping: client)
 
       for _ in 0 ..< self.arguments.repetitions {
@@ -45,8 +39,6 @@ struct Get: AsyncParsableCommand {
         let response = try await echo.get(message)
         print("get ← \(response.text)")
       }
-
-      client.beginGracefulShutdown()
     }
   }
 }

+ 3 - 11
Examples/echo/Sources/Subcommands/Update.swift

@@ -27,18 +27,12 @@ struct Update: AsyncParsableCommand {
   var arguments: ClientArguments
 
   func run() async throws {
-    let client = GRPCClient(
-      transport: try .http2NIOPosix(
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
         target: self.arguments.target,
         transportSecurity: .plaintext
       )
-    )
-
-    try await withThrowingDiscardingTaskGroup { group in
-      group.addTask {
-        try await client.run()
-      }
-
+    ) { client in
       let echo = Echo_Echo.Client(wrapping: client)
 
       for _ in 0 ..< self.arguments.repetitions {
@@ -53,8 +47,6 @@ struct Update: AsyncParsableCommand {
           }
         }
       }
-
-      client.beginGracefulShutdown()
     }
   }
 }

+ 2 - 2
Examples/error-details/Package.swift

@@ -21,8 +21,8 @@ let package = Package(
   name: "error-details",
   platforms: [.macOS(.v15)],
   dependencies: [
-    .package(url: "https://github.com/grpc/grpc-swift.git", exact: "2.0.0-beta.2"),
-    .package(url: "https://github.com/grpc/grpc-swift-protobuf.git", exact: "1.0.0-beta.2"),
+    .package(url: "https://github.com/grpc/grpc-swift.git", branch: "main"),
+    .package(url: "https://github.com/grpc/grpc-swift-protobuf.git", branch: "main"),
   ],
   targets: [
     .executableTarget(

+ 1 - 1
Examples/error-details/Sources/DetailedErrorExample.swift

@@ -29,7 +29,7 @@ struct DetailedErrorExample {
     }
   }
 
-  static func doRPC(_ greeter: Helloworld_Greeter.Client) async throws {
+  static func doRPC(_ greeter: Helloworld_Greeter.Client<some ClientTransport>) async throws {
     do {
       let reply = try await greeter.sayHello(.with { $0.name = "(ignored)" })
       print("Unexpected reply: \(reply.message)")

+ 4 - 4
Examples/error-details/Sources/Generated/helloworld.grpc.swift

@@ -156,7 +156,7 @@ extension Helloworld_Greeter {
 
 // Default implementation of 'registerMethods(with:)'.
 extension Helloworld_Greeter.StreamingServiceProtocol {
-    internal func registerMethods(with router: inout GRPCCore.RPCRouter) {
+    internal func registerMethods<Transport>(with router: inout GRPCCore.RPCRouter<Transport>) where Transport: GRPCCore.ServerTransport {
         router.registerHandler(
             forMethod: Helloworld_Greeter.Method.SayHello.descriptor,
             deserializer: GRPCProtobuf.ProtobufDeserializer<Helloworld_HelloRequest>(),
@@ -246,14 +246,14 @@ extension Helloworld_Greeter {
     /// > Source IDL Documentation:
     /// >
     /// > The greeting service definition.
-    internal struct Client: ClientProtocol {
-        private let client: GRPCCore.GRPCClient
+    internal struct Client<Transport>: ClientProtocol where Transport: GRPCCore.ClientTransport {
+        private let client: GRPCCore.GRPCClient<Transport>
 
         /// Creates a new client wrapping the provided `GRPCCore.GRPCClient`.
         ///
         /// - Parameters:
         ///   - client: A `GRPCCore.GRPCClient` providing a communication channel to the service.
-        internal init(wrapping client: GRPCCore.GRPCClient) {
+        internal init(wrapping client: GRPCCore.GRPCClient<Transport>) {
             self.client = client
         }
 

+ 3 - 3
Examples/hello-world/Package.swift

@@ -21,9 +21,9 @@ let package = Package(
   name: "hello-world",
   platforms: [.macOS("15.0")],
   dependencies: [
-    .package(url: "https://github.com/grpc/grpc-swift.git", exact: "2.0.0-beta.2"),
-    .package(url: "https://github.com/grpc/grpc-swift-protobuf.git", exact: "1.0.0-beta.2"),
-    .package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", exact: "1.0.0-beta.2"),
+    .package(url: "https://github.com/grpc/grpc-swift.git", branch: "main"),
+    .package(url: "https://github.com/grpc/grpc-swift-protobuf.git", branch: "main"),
+    .package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", branch: "main"),
     .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.0"),
   ],
   targets: [

+ 4 - 4
Examples/hello-world/Sources/Generated/helloworld.grpc.swift

@@ -156,7 +156,7 @@ extension Helloworld_Greeter {
 
 // Default implementation of 'registerMethods(with:)'.
 extension Helloworld_Greeter.StreamingServiceProtocol {
-    internal func registerMethods(with router: inout GRPCCore.RPCRouter) {
+    internal func registerMethods<Transport>(with router: inout GRPCCore.RPCRouter<Transport>) where Transport: GRPCCore.ServerTransport {
         router.registerHandler(
             forMethod: Helloworld_Greeter.Method.SayHello.descriptor,
             deserializer: GRPCProtobuf.ProtobufDeserializer<Helloworld_HelloRequest>(),
@@ -246,14 +246,14 @@ extension Helloworld_Greeter {
     /// > Source IDL Documentation:
     /// >
     /// > The greeting service definition.
-    internal struct Client: ClientProtocol {
-        private let client: GRPCCore.GRPCClient
+    internal struct Client<Transport>: ClientProtocol where Transport: GRPCCore.ClientTransport {
+        private let client: GRPCCore.GRPCClient<Transport>
 
         /// Creates a new client wrapping the provided `GRPCCore.GRPCClient`.
         ///
         /// - Parameters:
         ///   - client: A `GRPCCore.GRPCClient` providing a communication channel to the service.
-        internal init(wrapping client: GRPCCore.GRPCClient) {
+        internal init(wrapping client: GRPCCore.GRPCClient<Transport>) {
             self.client = client
         }
 

+ 5 - 15
Examples/hello-world/Sources/Subcommands/Greet.swift

@@ -29,22 +29,12 @@ struct Greet: AsyncParsableCommand {
   var name: String = ""
 
   func run() async throws {
-    try await withThrowingDiscardingTaskGroup { group in
-      let client = GRPCClient(
-        transport: try .http2NIOPosix(
-          target: .ipv4(host: "127.0.0.1", port: self.port),
-          transportSecurity: .plaintext
-        )
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .ipv4(host: "127.0.0.1", port: self.port),
+        transportSecurity: .plaintext
       )
-
-      group.addTask {
-        try await client.run()
-      }
-
-      defer {
-        client.beginGracefulShutdown()
-      }
-
+    ) { client in
       let greeter = Helloworld_Greeter.Client(wrapping: client)
       let reply = try await greeter.sayHello(.with { $0.name = self.name })
       print(reply.message)

+ 4 - 4
Examples/reflection-server/Package.swift

@@ -21,10 +21,10 @@ let package = Package(
   name: "reflection-server",
   platforms: [.macOS(.v15)],
   dependencies: [
-    .package(url: "https://github.com/grpc/grpc-swift.git", exact: "2.0.0-beta.2"),
-    .package(url: "https://github.com/grpc/grpc-swift-protobuf.git", exact: "1.0.0-beta.2"),
-    .package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", exact: "1.0.0-beta.2"),
-    .package(url: "https://github.com/grpc/grpc-swift-extras.git", exact: "1.0.0-beta.2"),
+    .package(url: "https://github.com/grpc/grpc-swift.git", branch: "main"),
+    .package(url: "https://github.com/grpc/grpc-swift-protobuf.git", branch: "main"),
+    .package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", branch: "main"),
+    .package(url: "https://github.com/grpc/grpc-swift-extras.git", branch: "main"),
     .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.0"),
   ],
   targets: [

+ 4 - 4
Examples/reflection-server/Sources/Generated/echo.grpc.swift

@@ -347,7 +347,7 @@ extension Echo_Echo {
 
 // Default implementation of 'registerMethods(with:)'.
 extension Echo_Echo.StreamingServiceProtocol {
-    internal func registerMethods(with router: inout GRPCCore.RPCRouter) {
+    internal func registerMethods<Transport>(with router: inout GRPCCore.RPCRouter<Transport>) where Transport: GRPCCore.ServerTransport {
         router.registerHandler(
             forMethod: Echo_Echo.Method.Get.descriptor,
             deserializer: GRPCProtobuf.ProtobufDeserializer<Echo_EchoRequest>(),
@@ -600,14 +600,14 @@ extension Echo_Echo {
     /// The ``Client`` provides an implementation of ``ClientProtocol`` which wraps
     /// a `GRPCCore.GRPCCClient`. The underlying `GRPCClient` provides the long-lived
     /// means of communication with the remote peer.
-    internal struct Client: ClientProtocol {
-        private let client: GRPCCore.GRPCClient
+    internal struct Client<Transport>: ClientProtocol where Transport: GRPCCore.ClientTransport {
+        private let client: GRPCCore.GRPCClient<Transport>
 
         /// Creates a new client wrapping the provided `GRPCCore.GRPCClient`.
         ///
         /// - Parameters:
         ///   - client: A `GRPCCore.GRPCClient` providing a communication channel to the service.
-        internal init(wrapping client: GRPCCore.GRPCClient) {
+        internal init(wrapping client: GRPCCore.GRPCClient<Transport>) {
             self.client = client
         }
 

+ 3 - 3
Examples/route-guide/Package.swift

@@ -21,9 +21,9 @@ let package = Package(
   name: "route-guide",
   platforms: [.macOS("15.0")],
   dependencies: [
-    .package(url: "https://github.com/grpc/grpc-swift.git", exact: "2.0.0-beta.2"),
-    .package(url: "https://github.com/grpc/grpc-swift-protobuf.git", exact: "1.0.0-beta.2"),
-    .package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", exact: "1.0.0-beta.2"),
+    .package(url: "https://github.com/grpc/grpc-swift.git", branch: "main"),
+    .package(url: "https://github.com/grpc/grpc-swift-protobuf.git", branch: "main"),
+    .package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", branch: "main"),
     .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.0"),
   ],
   targets: [

+ 4 - 4
Examples/route-guide/Sources/Generated/route_guide.grpc.swift

@@ -407,7 +407,7 @@ extension Routeguide_RouteGuide {
 
 // Default implementation of 'registerMethods(with:)'.
 extension Routeguide_RouteGuide.StreamingServiceProtocol {
-    internal func registerMethods(with router: inout GRPCCore.RPCRouter) {
+    internal func registerMethods<Transport>(with router: inout GRPCCore.RPCRouter<Transport>) where Transport: GRPCCore.ServerTransport {
         router.registerHandler(
             forMethod: Routeguide_RouteGuide.Method.GetFeature.descriptor,
             deserializer: GRPCProtobuf.ProtobufDeserializer<Routeguide_Point>(),
@@ -684,14 +684,14 @@ extension Routeguide_RouteGuide {
     /// > Source IDL Documentation:
     /// >
     /// > Interface exported by the server.
-    internal struct Client: ClientProtocol {
-        private let client: GRPCCore.GRPCClient
+    internal struct Client<Transport>: ClientProtocol where Transport: GRPCCore.ClientTransport {
+        private let client: GRPCCore.GRPCClient<Transport>
 
         /// Creates a new client wrapping the provided `GRPCCore.GRPCClient`.
         ///
         /// - Parameters:
         ///   - client: A `GRPCCore.GRPCClient` providing a communication channel to the service.
-        internal init(wrapping client: GRPCCore.GRPCClient) {
+        internal init(wrapping client: GRPCCore.GRPCClient<Transport>) {
             self.client = client
         }
 

+ 6 - 13
Examples/route-guide/Sources/Subcommands/GetFeature.swift

@@ -37,17 +37,12 @@ struct GetFeature: AsyncParsableCommand {
   var longitude: Int32 = -746_143_763
 
   func run() async throws {
-    let transport = try HTTP2ClientTransport.Posix(
-      target: .ipv4(host: "127.0.0.1", port: self.port),
-      transportSecurity: .plaintext
-    )
-    let client = GRPCClient(transport: transport)
-
-    try await withThrowingDiscardingTaskGroup { group in
-      group.addTask {
-        try await client.run()
-      }
-
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .ipv4(host: "127.0.0.1", port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
       let routeGuide = Routeguide_RouteGuide.Client(wrapping: client)
 
       let point = Routeguide_Point.with {
@@ -62,8 +57,6 @@ struct GetFeature: AsyncParsableCommand {
       } else {
         print("Found '\(feature.name)' at (\(self.latitude), \(self.longitude))")
       }
-
-      client.beginGracefulShutdown()
     }
   }
 }

+ 6 - 14
Examples/route-guide/Sources/Subcommands/ListFeatures.swift

@@ -51,17 +51,12 @@ struct ListFeatures: AsyncParsableCommand {
   var maxLongitude: Int32 = -730_000_000
 
   func run() async throws {
-    let transport = try HTTP2ClientTransport.Posix(
-      target: .ipv4(host: "127.0.0.1", port: self.port),
-      transportSecurity: .plaintext
-    )
-    let client = GRPCClient(transport: transport)
-
-    try await withThrowingDiscardingTaskGroup { group in
-      group.addTask {
-        try await client.run()
-      }
-
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .ipv4(host: "127.0.0.1", port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
       let routeGuide = Routeguide_RouteGuide.Client(wrapping: client)
       let boundingRectangle = Routeguide_Rectangle.with {
         $0.lo.latitude = self.minLatitude
@@ -78,9 +73,6 @@ struct ListFeatures: AsyncParsableCommand {
           print("(\(count)) \(feature.name) at (\(lat), \(lon))")
         }
       }
-
-      client.beginGracefulShutdown()
     }
-
   }
 }

+ 6 - 13
Examples/route-guide/Sources/Subcommands/RecordRoute.swift

@@ -30,17 +30,12 @@ struct RecordRoute: AsyncParsableCommand {
   var points: Int = 10
 
   func run() async throws {
-    let transport = try HTTP2ClientTransport.Posix(
-      target: .ipv4(host: "127.0.0.1", port: self.port),
-      transportSecurity: .plaintext
-    )
-    let client = GRPCClient(transport: transport)
-
-    try await withThrowingDiscardingTaskGroup { group in
-      group.addTask {
-        try await client.run()
-      }
-
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .ipv4(host: "127.0.0.1", port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
       let routeGuide = Routeguide_RouteGuide.Client(wrapping: client)
 
       // Get all features.
@@ -67,8 +62,6 @@ struct RecordRoute: AsyncParsableCommand {
         a distance \(summary.distance) metres.
         """
       print(text)
-
-      client.beginGracefulShutdown()
     }
   }
 }

+ 6 - 13
Examples/route-guide/Sources/Subcommands/RouteChat.swift

@@ -30,17 +30,12 @@ struct RouteChat: AsyncParsableCommand {
   var port: Int = 31415
 
   func run() async throws {
-    let transport = try HTTP2ClientTransport.Posix(
-      target: .ipv4(host: "127.0.0.1", port: self.port),
-      transportSecurity: .plaintext
-    )
-    let client = GRPCClient(transport: transport)
-
-    try await withThrowingDiscardingTaskGroup { group in
-      group.addTask {
-        try await client.run()
-      }
-
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .ipv4(host: "127.0.0.1", port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
       let routeGuide = Routeguide_RouteGuide.Client(wrapping: client)
 
       try await routeGuide.routeChat { writer in
@@ -67,8 +62,6 @@ struct RouteChat: AsyncParsableCommand {
           print("Received note: '\(note.message) at (\(lat), \(lon))'")
         }
       }
-
-      client.beginGracefulShutdown()
     }
   }
 }