Skip to content

Commit b76d42f

Browse files
committed
Generate code for error example
1 parent c6916c4 commit b76d42f

File tree

2 files changed

+491
-0
lines changed

2 files changed

+491
-0
lines changed
Lines changed: 362 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,362 @@
1+
/// Copyright 2015 gRPC authors.
2+
///
3+
/// Licensed under the Apache License, Version 2.0 (the "License");
4+
/// you may not use this file except in compliance with the License.
5+
/// You may obtain a copy of the License at
6+
///
7+
/// http://www.apache.org/licenses/LICENSE-2.0
8+
///
9+
/// Unless required by applicable law or agreed to in writing, software
10+
/// distributed under the License is distributed on an "AS IS" BASIS,
11+
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
/// See the License for the specific language governing permissions and
13+
/// limitations under the License.
14+
15+
// DO NOT EDIT.
16+
// swift-format-ignore-file
17+
//
18+
// Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
19+
// Source: helloworld.proto
20+
//
21+
// For information on using the generated types, please see the documentation:
22+
// https://github.com/grpc/grpc-swift
23+
24+
import GRPCCore
25+
import GRPCProtobuf
26+
27+
// MARK: - helloworld.Greeter
28+
29+
/// Namespace containing generated types for the "helloworld.Greeter" service.
30+
internal enum Helloworld_Greeter {
31+
/// Service descriptor for the "helloworld.Greeter" service.
32+
internal static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService: "helloworld.Greeter")
33+
/// Namespace for method metadata.
34+
internal enum Method {
35+
/// Namespace for "SayHello" metadata.
36+
internal enum SayHello {
37+
/// Request type for "SayHello".
38+
internal typealias Input = Helloworld_HelloRequest
39+
/// Response type for "SayHello".
40+
internal typealias Output = Helloworld_HelloReply
41+
/// Descriptor for "SayHello".
42+
internal static let descriptor = GRPCCore.MethodDescriptor(
43+
service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "helloworld.Greeter"),
44+
method: "SayHello"
45+
)
46+
}
47+
/// Descriptors for all methods in the "helloworld.Greeter" service.
48+
internal static let descriptors: [GRPCCore.MethodDescriptor] = [
49+
SayHello.descriptor
50+
]
51+
}
52+
}
53+
54+
extension GRPCCore.ServiceDescriptor {
55+
/// Service descriptor for the "helloworld.Greeter" service.
56+
internal static let helloworld_Greeter = GRPCCore.ServiceDescriptor(fullyQualifiedService: "helloworld.Greeter")
57+
}
58+
59+
// MARK: helloworld.Greeter (server)
60+
61+
extension Helloworld_Greeter {
62+
/// Streaming variant of the service protocol for the "helloworld.Greeter" service.
63+
///
64+
/// This protocol is the lowest-level of the service protocols generated for this service
65+
/// giving you the most flexibility over the implementation of your service. This comes at
66+
/// the cost of more verbose and less strict APIs. Each RPC requires you to implement it in
67+
/// terms of a request stream and response stream. Where only a single request or response
68+
/// message is expected, you are responsible for enforcing this invariant is maintained.
69+
///
70+
/// Where possible, prefer using the stricter, less-verbose ``ServiceProtocol``
71+
/// or ``SimpleServiceProtocol`` instead.
72+
///
73+
/// > Source IDL Documentation:
74+
/// >
75+
/// > The greeting service definition.
76+
internal protocol StreamingServiceProtocol: GRPCCore.RegistrableRPCService {
77+
/// Handle the "SayHello" method.
78+
///
79+
/// > Source IDL Documentation:
80+
/// >
81+
/// > Sends a greeting
82+
///
83+
/// - Parameters:
84+
/// - request: A streaming request of `Helloworld_HelloRequest` messages.
85+
/// - context: Context providing information about the RPC.
86+
/// - Throws: Any error which occurred during the processing of the request. Thrown errors
87+
/// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
88+
/// to an internal error.
89+
/// - Returns: A streaming response of `Helloworld_HelloReply` messages.
90+
func sayHello(
91+
request: GRPCCore.StreamingServerRequest<Helloworld_HelloRequest>,
92+
context: GRPCCore.ServerContext
93+
) async throws -> GRPCCore.StreamingServerResponse<Helloworld_HelloReply>
94+
}
95+
96+
/// Service protocol for the "helloworld.Greeter" service.
97+
///
98+
/// This protocol is higher level than ``StreamingServiceProtocol`` but lower level than
99+
/// the ``SimpleServiceProtocol``, it provides access to request and response metadata and
100+
/// trailing response metadata. If you don't need these then consider using
101+
/// the ``SimpleServiceProtocol``. If you need fine grained control over your RPCs then
102+
/// use ``StreamingServiceProtocol``.
103+
///
104+
/// > Source IDL Documentation:
105+
/// >
106+
/// > The greeting service definition.
107+
internal protocol ServiceProtocol: Helloworld_Greeter.StreamingServiceProtocol {
108+
/// Handle the "SayHello" method.
109+
///
110+
/// > Source IDL Documentation:
111+
/// >
112+
/// > Sends a greeting
113+
///
114+
/// - Parameters:
115+
/// - request: A request containing a single `Helloworld_HelloRequest` message.
116+
/// - context: Context providing information about the RPC.
117+
/// - Throws: Any error which occurred during the processing of the request. Thrown errors
118+
/// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
119+
/// to an internal error.
120+
/// - Returns: A response containing a single `Helloworld_HelloReply` message.
121+
func sayHello(
122+
request: GRPCCore.ServerRequest<Helloworld_HelloRequest>,
123+
context: GRPCCore.ServerContext
124+
) async throws -> GRPCCore.ServerResponse<Helloworld_HelloReply>
125+
}
126+
127+
/// Simple service protocol for the "helloworld.Greeter" service.
128+
///
129+
/// This is the highest level protocol for the service. The API is the easiest to use but
130+
/// doesn't provide access to request or response metadata. If you need access to these
131+
/// then use ``ServiceProtocol`` instead.
132+
///
133+
/// > Source IDL Documentation:
134+
/// >
135+
/// > The greeting service definition.
136+
internal protocol SimpleServiceProtocol: Helloworld_Greeter.ServiceProtocol {
137+
/// Handle the "SayHello" method.
138+
///
139+
/// > Source IDL Documentation:
140+
/// >
141+
/// > Sends a greeting
142+
///
143+
/// - Parameters:
144+
/// - request: A `Helloworld_HelloRequest` message.
145+
/// - context: Context providing information about the RPC.
146+
/// - Throws: Any error which occurred during the processing of the request. Thrown errors
147+
/// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
148+
/// to an internal error.
149+
/// - Returns: A `Helloworld_HelloReply` to respond with.
150+
func sayHello(
151+
request: Helloworld_HelloRequest,
152+
context: GRPCCore.ServerContext
153+
) async throws -> Helloworld_HelloReply
154+
}
155+
}
156+
157+
// Default implementation of 'registerMethods(with:)'.
158+
extension Helloworld_Greeter.StreamingServiceProtocol {
159+
internal func registerMethods(with router: inout GRPCCore.RPCRouter) {
160+
router.registerHandler(
161+
forMethod: Helloworld_Greeter.Method.SayHello.descriptor,
162+
deserializer: GRPCProtobuf.ProtobufDeserializer<Helloworld_HelloRequest>(),
163+
serializer: GRPCProtobuf.ProtobufSerializer<Helloworld_HelloReply>(),
164+
handler: { request, context in
165+
try await self.sayHello(
166+
request: request,
167+
context: context
168+
)
169+
}
170+
)
171+
}
172+
}
173+
174+
// Default implementation of streaming methods from 'StreamingServiceProtocol'.
175+
extension Helloworld_Greeter.ServiceProtocol {
176+
internal func sayHello(
177+
request: GRPCCore.StreamingServerRequest<Helloworld_HelloRequest>,
178+
context: GRPCCore.ServerContext
179+
) async throws -> GRPCCore.StreamingServerResponse<Helloworld_HelloReply> {
180+
let response = try await self.sayHello(
181+
request: GRPCCore.ServerRequest(stream: request),
182+
context: context
183+
)
184+
return GRPCCore.StreamingServerResponse(single: response)
185+
}
186+
}
187+
188+
// Default implementation of methods from 'ServiceProtocol'.
189+
extension Helloworld_Greeter.SimpleServiceProtocol {
190+
internal func sayHello(
191+
request: GRPCCore.ServerRequest<Helloworld_HelloRequest>,
192+
context: GRPCCore.ServerContext
193+
) async throws -> GRPCCore.ServerResponse<Helloworld_HelloReply> {
194+
return GRPCCore.ServerResponse<Helloworld_HelloReply>(
195+
message: try await self.sayHello(
196+
request: request.message,
197+
context: context
198+
),
199+
metadata: [:]
200+
)
201+
}
202+
}
203+
204+
// MARK: helloworld.Greeter (client)
205+
206+
extension Helloworld_Greeter {
207+
/// Generated client protocol for the "helloworld.Greeter" service.
208+
///
209+
/// You don't need to implement this protocol directly, use the generated
210+
/// implementation, ``Client``.
211+
///
212+
/// > Source IDL Documentation:
213+
/// >
214+
/// > The greeting service definition.
215+
internal protocol ClientProtocol: Sendable {
216+
/// Call the "SayHello" method.
217+
///
218+
/// > Source IDL Documentation:
219+
/// >
220+
/// > Sends a greeting
221+
///
222+
/// - Parameters:
223+
/// - request: A request containing a single `Helloworld_HelloRequest` message.
224+
/// - serializer: A serializer for `Helloworld_HelloRequest` messages.
225+
/// - deserializer: A deserializer for `Helloworld_HelloReply` messages.
226+
/// - options: Options to apply to this RPC.
227+
/// - handleResponse: A closure which handles the response, the result of which is
228+
/// returned to the caller. Returning from the closure will cancel the RPC if it
229+
/// hasn't already finished.
230+
/// - Returns: The result of `handleResponse`.
231+
func sayHello<Result>(
232+
request: GRPCCore.ClientRequest<Helloworld_HelloRequest>,
233+
serializer: some GRPCCore.MessageSerializer<Helloworld_HelloRequest>,
234+
deserializer: some GRPCCore.MessageDeserializer<Helloworld_HelloReply>,
235+
options: GRPCCore.CallOptions,
236+
onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Helloworld_HelloReply>) async throws -> Result
237+
) async throws -> Result where Result: Sendable
238+
}
239+
240+
/// Generated client for the "helloworld.Greeter" service.
241+
///
242+
/// The ``Client`` provides an implementation of ``ClientProtocol`` which wraps
243+
/// a `GRPCCore.GRPCCClient`. The underlying `GRPCClient` provides the long-lived
244+
/// means of communication with the remote peer.
245+
///
246+
/// > Source IDL Documentation:
247+
/// >
248+
/// > The greeting service definition.
249+
internal struct Client: ClientProtocol {
250+
private let client: GRPCCore.GRPCClient
251+
252+
/// Creates a new client wrapping the provided `GRPCCore.GRPCClient`.
253+
///
254+
/// - Parameters:
255+
/// - client: A `GRPCCore.GRPCClient` providing a communication channel to the service.
256+
internal init(wrapping client: GRPCCore.GRPCClient) {
257+
self.client = client
258+
}
259+
260+
/// Call the "SayHello" method.
261+
///
262+
/// > Source IDL Documentation:
263+
/// >
264+
/// > Sends a greeting
265+
///
266+
/// - Parameters:
267+
/// - request: A request containing a single `Helloworld_HelloRequest` message.
268+
/// - serializer: A serializer for `Helloworld_HelloRequest` messages.
269+
/// - deserializer: A deserializer for `Helloworld_HelloReply` messages.
270+
/// - options: Options to apply to this RPC.
271+
/// - handleResponse: A closure which handles the response, the result of which is
272+
/// returned to the caller. Returning from the closure will cancel the RPC if it
273+
/// hasn't already finished.
274+
/// - Returns: The result of `handleResponse`.
275+
internal func sayHello<Result>(
276+
request: GRPCCore.ClientRequest<Helloworld_HelloRequest>,
277+
serializer: some GRPCCore.MessageSerializer<Helloworld_HelloRequest>,
278+
deserializer: some GRPCCore.MessageDeserializer<Helloworld_HelloReply>,
279+
options: GRPCCore.CallOptions = .defaults,
280+
onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Helloworld_HelloReply>) async throws -> Result = { response in
281+
try response.message
282+
}
283+
) async throws -> Result where Result: Sendable {
284+
try await self.client.unary(
285+
request: request,
286+
descriptor: Helloworld_Greeter.Method.SayHello.descriptor,
287+
serializer: serializer,
288+
deserializer: deserializer,
289+
options: options,
290+
onResponse: handleResponse
291+
)
292+
}
293+
}
294+
}
295+
296+
// Helpers providing default arguments to 'ClientProtocol' methods.
297+
extension Helloworld_Greeter.ClientProtocol {
298+
/// Call the "SayHello" method.
299+
///
300+
/// > Source IDL Documentation:
301+
/// >
302+
/// > Sends a greeting
303+
///
304+
/// - Parameters:
305+
/// - request: A request containing a single `Helloworld_HelloRequest` message.
306+
/// - options: Options to apply to this RPC.
307+
/// - handleResponse: A closure which handles the response, the result of which is
308+
/// returned to the caller. Returning from the closure will cancel the RPC if it
309+
/// hasn't already finished.
310+
/// - Returns: The result of `handleResponse`.
311+
internal func sayHello<Result>(
312+
request: GRPCCore.ClientRequest<Helloworld_HelloRequest>,
313+
options: GRPCCore.CallOptions = .defaults,
314+
onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Helloworld_HelloReply>) async throws -> Result = { response in
315+
try response.message
316+
}
317+
) async throws -> Result where Result: Sendable {
318+
try await self.sayHello(
319+
request: request,
320+
serializer: GRPCProtobuf.ProtobufSerializer<Helloworld_HelloRequest>(),
321+
deserializer: GRPCProtobuf.ProtobufDeserializer<Helloworld_HelloReply>(),
322+
options: options,
323+
onResponse: handleResponse
324+
)
325+
}
326+
}
327+
328+
// Helpers providing sugared APIs for 'ClientProtocol' methods.
329+
extension Helloworld_Greeter.ClientProtocol {
330+
/// Call the "SayHello" method.
331+
///
332+
/// > Source IDL Documentation:
333+
/// >
334+
/// > Sends a greeting
335+
///
336+
/// - Parameters:
337+
/// - message: request message to send.
338+
/// - metadata: Additional metadata to send, defaults to empty.
339+
/// - options: Options to apply to this RPC, defaults to `.defaults`.
340+
/// - handleResponse: A closure which handles the response, the result of which is
341+
/// returned to the caller. Returning from the closure will cancel the RPC if it
342+
/// hasn't already finished.
343+
/// - Returns: The result of `handleResponse`.
344+
internal func sayHello<Result>(
345+
_ message: Helloworld_HelloRequest,
346+
metadata: GRPCCore.Metadata = [:],
347+
options: GRPCCore.CallOptions = .defaults,
348+
onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Helloworld_HelloReply>) async throws -> Result = { response in
349+
try response.message
350+
}
351+
) async throws -> Result where Result: Sendable {
352+
let request = GRPCCore.ClientRequest<Helloworld_HelloRequest>(
353+
message: message,
354+
metadata: metadata
355+
)
356+
return try await self.sayHello(
357+
request: request,
358+
options: options,
359+
onResponse: handleResponse
360+
)
361+
}
362+
}

0 commit comments

Comments
 (0)