Skip to content

Commit 32e290b

Browse files
authored
Update the HelloWorld examples (#1424)
1 parent d3f5f64 commit 32e290b

File tree

4 files changed

+43
-43
lines changed

4 files changed

+43
-43
lines changed

Package.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ let cgrpcZlibTargetName = cgrpcZlibProductName
2727

2828
let includeNIOSSL = ProcessInfo.processInfo.environment["GRPC_NO_NIO_SSL"] == nil
2929

30+
#if swift(>=5.6)
31+
// swift-argument-parser raised its minimum Swift version in 1.1.0 but
32+
// also accidentally broke API. This was fixed in "1.1.1".
33+
let argumentParserMinimumVersion: Version = "1.1.1"
34+
#else
35+
let argumentParserMinimumVersion: Version = "1.0.0"
36+
#endif
37+
3038
// MARK: - Package Dependencies
3139

3240
let packageDependencies: [Package.Dependency] = [
@@ -57,7 +65,7 @@ let packageDependencies: [Package.Dependency] = [
5765
),
5866
.package(
5967
url: "https://github.com/apple/swift-argument-parser.git",
60-
from: "1.0.0"
68+
from: argumentParserMinimumVersion
6169
),
6270
].appending(
6371
.package(

Sources/Examples/HelloWorld/Client/main.swift

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,22 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#if compiler(>=5.6)
1617
import ArgumentParser
1718
import GRPC
1819
import HelloWorldModel
1920
import NIOCore
2021
import NIOPosix
2122

22-
func greet(name: String?, client greeter: Helloworld_GreeterNIOClient) {
23-
// Form the request with the name, if one was provided.
24-
let request = Helloworld_HelloRequest.with {
25-
$0.name = name ?? ""
26-
}
27-
28-
// Make the RPC call to the server.
29-
let sayHello = greeter.sayHello(request)
30-
31-
// wait() on the response to stop the program from exiting before the response is received.
32-
do {
33-
let response = try sayHello.response.wait()
34-
print("Greeter received: \(response.message)")
35-
} catch {
36-
print("Greeter failed: \(error)")
37-
}
38-
}
39-
40-
struct HelloWorld: ParsableCommand {
23+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
24+
struct HelloWorld: AsyncParsableCommand {
4125
@Option(help: "The port to connect to")
4226
var port: Int = 1234
4327

4428
@Argument(help: "The name to greet")
4529
var name: String?
4630

47-
func run() throws {
31+
func run() async throws {
4832
// Setup an `EventLoopGroup` for the connection to run on.
4933
//
5034
// See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
@@ -68,11 +52,20 @@ struct HelloWorld: ParsableCommand {
6852
}
6953

7054
// Provide the connection to the generated client.
71-
let greeter = Helloworld_GreeterNIOClient(channel: channel)
55+
let greeter = Helloworld_GreeterAsyncClient(channel: channel)
56+
57+
// Form the request with the name, if one was provided.
58+
let request = Helloworld_HelloRequest.with {
59+
$0.name = self.name ?? ""
60+
}
7261

73-
// Do the greeting.
74-
greet(name: self.name, client: greeter)
62+
do {
63+
let greeting = try await greeter.sayHello(request)
64+
print("Greeter received: \(greeting.message)")
65+
} catch {
66+
print("Greeter failed: \(error)")
67+
}
7568
}
7669
}
7770

78-
HelloWorld.main()
71+
#endif // compiler(>=5.6)

Sources/Examples/HelloWorld/Server/GreeterProvider.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,23 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#if compiler(>=5.6)
1617
import GRPC
1718
import HelloWorldModel
1819
import NIOCore
1920

20-
class GreeterProvider: Helloworld_GreeterProvider {
21-
var interceptors: Helloworld_GreeterServerInterceptorFactoryProtocol?
21+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
22+
final class GreeterProvider: Helloworld_GreeterAsyncProvider {
23+
let interceptors: Helloworld_GreeterServerInterceptorFactoryProtocol? = nil
2224

2325
func sayHello(
2426
request: Helloworld_HelloRequest,
25-
context: StatusOnlyCallContext
26-
) -> EventLoopFuture<Helloworld_HelloReply> {
27+
context: GRPCAsyncServerCallContext
28+
) async throws -> Helloworld_HelloReply {
2729
let recipient = request.name.isEmpty ? "stranger" : request.name
28-
let response = Helloworld_HelloReply.with {
30+
return Helloworld_HelloReply.with {
2931
$0.message = "Hello \(recipient)!"
3032
}
31-
return context.eventLoop.makeSucceededFuture(response)
3233
}
3334
}
35+
#endif // compiler(>=5.6)

Sources/Examples/HelloWorld/Server/main.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,35 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#if compiler(>=5.6)
1617
import ArgumentParser
1718
import GRPC
1819
import HelloWorldModel
1920
import NIOCore
2021
import NIOPosix
2122

22-
struct HelloWorld: ParsableCommand {
23+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
24+
struct HelloWorld: AsyncParsableCommand {
2325
@Option(help: "The port to listen on for new connections")
2426
var port = 1234
2527

26-
func run() throws {
28+
func run() async throws {
2729
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
2830
defer {
2931
try! group.syncShutdownGracefully()
3032
}
3133

3234
// Start the server and print its address once it has started.
33-
let server = Server.insecure(group: group)
35+
let server = try await Server.insecure(group: group)
3436
.withServiceProviders([GreeterProvider()])
3537
.bind(host: "localhost", port: self.port)
38+
.get()
3639

37-
server.map {
38-
$0.channel.localAddress
39-
}.whenSuccess { address in
40-
print("server started on port \(address!.port!)")
41-
}
40+
print("server started on port \(server.channel.localAddress!.port!)")
4241

4342
// Wait on the server's `onClose` future to stop the program from exiting.
44-
_ = try server.flatMap {
45-
$0.onClose
46-
}.wait()
43+
try await server.onClose.get()
4744
}
4845
}
4946

50-
HelloWorld.main()
47+
#endif // compiler(>=5.6)

0 commit comments

Comments
 (0)