Skip to content

Update examples and tutorials #2139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Examples/echo/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ let package = Package(
name: "echo",
platforms: [.macOS("15.0")],
dependencies: [
.package(url: "https://github.com/grpc/grpc-swift", exact: "2.0.0-alpha.1"),
.package(url: "https://github.com/grpc/grpc-swift-protobuf", exact: "1.0.0-alpha.1"),
.package(url: "https://github.com/grpc/grpc-swift-nio-transport", branch: "1.0.0-alpha.1"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
.package(url: "https://github.com/grpc/grpc-swift.git", exact: "2.0.0-beta.1"),
.package(url: "https://github.com/grpc/grpc-swift-protobuf.git", exact: "1.0.0-beta.1"),
.package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", branch: "1.0.0-beta.1"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.0"),
],
targets: [
.executableTarget(
Expand Down
982 changes: 742 additions & 240 deletions Examples/echo/Sources/Generated/echo.grpc.swift

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Examples/echo/Sources/Subcommands/Collect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Collect: AsyncParsableCommand {
let client = GRPCClient(
transport: try .http2NIOPosix(
target: self.arguments.target,
config: .defaults(transportSecurity: .plaintext)
transportSecurity: .plaintext
)
)

Expand All @@ -39,7 +39,7 @@ struct Collect: AsyncParsableCommand {
try await client.run()
}

let echo = Echo_Echo_Client(wrapping: client)
let echo = Echo_Echo.Client(wrapping: client)

for _ in 0 ..< self.arguments.repetitions {
let message = try await echo.collect { writer in
Expand Down
4 changes: 2 additions & 2 deletions Examples/echo/Sources/Subcommands/Expand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Expand: AsyncParsableCommand {
let client = GRPCClient(
transport: try .http2NIOPosix(
target: self.arguments.target,
config: .defaults(transportSecurity: .plaintext)
transportSecurity: .plaintext
)
)

Expand All @@ -39,7 +39,7 @@ struct Expand: AsyncParsableCommand {
try await client.run()
}

let echo = Echo_Echo_Client(wrapping: client)
let echo = Echo_Echo.Client(wrapping: client)

for _ in 0 ..< self.arguments.repetitions {
let message = Echo_EchoRequest.with { $0.text = self.arguments.message }
Expand Down
4 changes: 2 additions & 2 deletions Examples/echo/Sources/Subcommands/Get.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Get: AsyncParsableCommand {
let client = GRPCClient(
transport: try .http2NIOPosix(
target: self.arguments.target,
config: .defaults(transportSecurity: .plaintext)
transportSecurity: .plaintext
)
)

Expand All @@ -37,7 +37,7 @@ struct Get: AsyncParsableCommand {
try await client.run()
}

let echo = Echo_Echo_Client(wrapping: client)
let echo = Echo_Echo.Client(wrapping: client)

for _ in 0 ..< self.arguments.repetitions {
let message = Echo_EchoRequest.with { $0.text = self.arguments.message }
Expand Down
44 changes: 20 additions & 24 deletions Examples/echo/Sources/Subcommands/Serve.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Serve: AsyncParsableCommand {
let server = GRPCServer(
transport: .http2NIOPosix(
address: .ipv4(host: "127.0.0.1", port: self.port),
config: .defaults(transportSecurity: .plaintext)
transportSecurity: .plaintext
),
services: [EchoService()]
)
Expand All @@ -42,44 +42,40 @@ struct Serve: AsyncParsableCommand {
}
}

struct EchoService: Echo_Echo_ServiceProtocol {
struct EchoService: Echo_Echo.SimpleServiceProtocol {
func get(
request: ServerRequest<Echo_EchoRequest>,
request: Echo_EchoRequest,
context: ServerContext
) async throws -> ServerResponse<Echo_EchoResponse> {
return ServerResponse(message: .with { $0.text = request.message.text })
) async throws -> Echo_EchoResponse {
return .with { $0.text = request.text }
}

func collect(
request: StreamingServerRequest<Echo_EchoRequest>,
request: RPCAsyncSequence<Echo_EchoRequest, any Error>,
context: ServerContext
) async throws -> ServerResponse<Echo_EchoResponse> {
let messages = try await request.messages.reduce(into: []) { $0.append($1.text) }
) async throws -> Echo_EchoResponse {
let messages = try await request.reduce(into: []) { $0.append($1.text) }
let joined = messages.joined(separator: " ")
return ServerResponse(message: .with { $0.text = joined })
return .with { $0.text = joined }
}

func expand(
request: ServerRequest<Echo_EchoRequest>,
request: Echo_EchoRequest,
response: RPCWriter<Echo_EchoResponse>,
context: ServerContext
) async throws -> StreamingServerResponse<Echo_EchoResponse> {
return StreamingServerResponse { writer in
let parts = request.message.text.split(separator: " ")
let messages = parts.map { part in Echo_EchoResponse.with { $0.text = String(part) } }
try await writer.write(contentsOf: messages)
return [:]
}
) async throws {
let parts = request.text.split(separator: " ")
let messages = parts.map { part in Echo_EchoResponse.with { $0.text = String(part) } }
try await response.write(contentsOf: messages)
}

func update(
request: StreamingServerRequest<Echo_EchoRequest>,
request: RPCAsyncSequence<Echo_EchoRequest, any Error>,
response: RPCWriter<Echo_EchoResponse>,
context: ServerContext
) async throws -> StreamingServerResponse<Echo_EchoResponse> {
return StreamingServerResponse { writer in
for try await message in request.messages {
try await writer.write(.with { $0.text = message.text })
}
return [:]
) async throws {
for try await message in request {
try await response.write(.with { $0.text = message.text })
}
}
}
4 changes: 2 additions & 2 deletions Examples/echo/Sources/Subcommands/Update.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Update: AsyncParsableCommand {
let client = GRPCClient(
transport: try .http2NIOPosix(
target: self.arguments.target,
config: .defaults(transportSecurity: .plaintext)
transportSecurity: .plaintext
)
)

Expand All @@ -39,7 +39,7 @@ struct Update: AsyncParsableCommand {
try await client.run()
}

let echo = Echo_Echo_Client(wrapping: client)
let echo = Echo_Echo.Client(wrapping: client)

for _ in 0 ..< self.arguments.repetitions {
try await echo.update { writer in
Expand Down
8 changes: 4 additions & 4 deletions Examples/hello-world/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ let package = Package(
name: "hello-world",
platforms: [.macOS("15.0")],
dependencies: [
.package(url: "https://github.com/grpc/grpc-swift", exact: "2.0.0-alpha.1"),
.package(url: "https://github.com/grpc/grpc-swift-protobuf", exact: "1.0.0-alpha.1"),
.package(url: "https://github.com/grpc/grpc-swift-nio-transport", exact: "1.0.0-alpha.1"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
.package(url: "https://github.com/grpc/grpc-swift.git", exact: "2.0.0-beta.1"),
.package(url: "https://github.com/grpc/grpc-swift-protobuf.git", exact: "1.0.0-beta.1"),
.package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", exact: "1.0.0-beta.1"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.0"),
],
targets: [
.executableTarget(
Expand Down
Loading
Loading