Skip to content

Commit 62882cc

Browse files
Generated reflection service code (#1659)
Motivation: In order to develop the Reflection Service, the specific proto file should be included in the module and the service code generated from it. Modifications: Created the module for the Reflection Service, added the proto file, updated the Package.swift to include the module, updated the Makefile to inlude targets for generating the server code, created the ReflectionProvider. Result: The Reflection Service can now be implemented.
1 parent 09c46b0 commit 62882cc

File tree

6 files changed

+1166
-0
lines changed

6 files changed

+1166
-0
lines changed

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ ${SERIALIZATION_GRPC_REFLECTION}: ${ECHO_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
131131
.PHONY:
132132
generate-reflection-data: ${SERIALIZATION_GRPC_REFLECTION}
133133

134+
REFLECTION_PROTO=Sources/GRPCReflectionService/Model/reflection.proto
135+
REFLECTION_PB=$(REFLECTION_PROTO:.proto=.pb.swift)
136+
REFLECTION_GRPC=$(REFLECTION_PROTO:.proto=.grpc.swift)
137+
138+
# For Reflection we'll generate only the Server code.
139+
${REFLECTION_GRPC}: ${REFLECTION_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
140+
protoc $< \
141+
--proto_path=$(dir $<) \
142+
--plugin=${PROTOC_GEN_GRPC_SWIFT} \
143+
--grpc-swift_opt=Client=false \
144+
--grpc-swift_out=$(dir $<)
145+
146+
# Generates protobufs and gRPC server for the Reflection Service
147+
.PHONY:
148+
generate-reflection: ${REFLECTION_PB} ${REFLECTION_GRPC}
149+
134150
### Testing ####################################################################
135151

136152
# Normal test suite.

Package.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ extension Target.Dependency {
8989
static let interopTestModels: Self = .target(name: "GRPCInteroperabilityTestModels")
9090
static let interopTestImplementation: Self =
9191
.target(name: "GRPCInteroperabilityTestsImplementation")
92+
static let reflectionService: Self = .target(name: "GRPCReflectionService")
9293

9394
// Product dependencies
9495
static let argumentParser: Self = .product(
@@ -428,6 +429,19 @@ extension Target {
428429
"README.md",
429430
]
430431
)
432+
433+
static let reflectionService: Target = .target(
434+
name: "GRPCReflectionService",
435+
dependencies: [
436+
.grpc,
437+
.nio,
438+
.protobuf,
439+
],
440+
path: "Sources/GRPCReflectionService",
441+
exclude: [
442+
"Model/reflection.proto",
443+
]
444+
)
431445
}
432446

433447
// MARK: - Products
@@ -471,6 +485,7 @@ let package = Package(
471485
.cgrpcZlib,
472486
.protocGenGRPCSwift,
473487
.grpcSwiftPlugin,
488+
.reflectionService,
474489

475490
// Tests etc.
476491
.grpcTests,
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//
2+
// DO NOT EDIT.
3+
// swift-format-ignore-file
4+
//
5+
// Generated by the protocol buffer compiler.
6+
// Source: reflection.proto
7+
//
8+
import GRPC
9+
import NIO
10+
import NIOConcurrencyHelpers
11+
import SwiftProtobuf
12+
13+
14+
/// To build a server, implement a class that conforms to this protocol.
15+
internal protocol Reflection_ServerReflectionProvider: CallHandlerProvider {
16+
var interceptors: Reflection_ServerReflectionServerInterceptorFactoryProtocol? { get }
17+
18+
/// The reflection service is structured as a bidirectional stream, ensuring
19+
/// all related requests go to a single server.
20+
func serverReflectionInfo(context: StreamingResponseCallContext<Reflection_ServerReflectionResponse>) -> EventLoopFuture<(StreamEvent<Reflection_ServerReflectionRequest>) -> Void>
21+
}
22+
23+
extension Reflection_ServerReflectionProvider {
24+
internal var serviceName: Substring {
25+
return Reflection_ServerReflectionServerMetadata.serviceDescriptor.fullName[...]
26+
}
27+
28+
/// Determines, calls and returns the appropriate request handler, depending on the request's method.
29+
/// Returns nil for methods not handled by this service.
30+
internal func handle(
31+
method name: Substring,
32+
context: CallHandlerContext
33+
) -> GRPCServerHandlerProtocol? {
34+
switch name {
35+
case "ServerReflectionInfo":
36+
return BidirectionalStreamingServerHandler(
37+
context: context,
38+
requestDeserializer: ProtobufDeserializer<Reflection_ServerReflectionRequest>(),
39+
responseSerializer: ProtobufSerializer<Reflection_ServerReflectionResponse>(),
40+
interceptors: self.interceptors?.makeServerReflectionInfoInterceptors() ?? [],
41+
observerFactory: self.serverReflectionInfo(context:)
42+
)
43+
44+
default:
45+
return nil
46+
}
47+
}
48+
}
49+
50+
/// To implement a server, implement an object which conforms to this protocol.
51+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
52+
internal protocol Reflection_ServerReflectionAsyncProvider: CallHandlerProvider, Sendable {
53+
static var serviceDescriptor: GRPCServiceDescriptor { get }
54+
var interceptors: Reflection_ServerReflectionServerInterceptorFactoryProtocol? { get }
55+
56+
/// The reflection service is structured as a bidirectional stream, ensuring
57+
/// all related requests go to a single server.
58+
func serverReflectionInfo(
59+
requestStream: GRPCAsyncRequestStream<Reflection_ServerReflectionRequest>,
60+
responseStream: GRPCAsyncResponseStreamWriter<Reflection_ServerReflectionResponse>,
61+
context: GRPCAsyncServerCallContext
62+
) async throws
63+
}
64+
65+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
66+
extension Reflection_ServerReflectionAsyncProvider {
67+
internal static var serviceDescriptor: GRPCServiceDescriptor {
68+
return Reflection_ServerReflectionServerMetadata.serviceDescriptor
69+
}
70+
71+
internal var serviceName: Substring {
72+
return Reflection_ServerReflectionServerMetadata.serviceDescriptor.fullName[...]
73+
}
74+
75+
internal var interceptors: Reflection_ServerReflectionServerInterceptorFactoryProtocol? {
76+
return nil
77+
}
78+
79+
internal func handle(
80+
method name: Substring,
81+
context: CallHandlerContext
82+
) -> GRPCServerHandlerProtocol? {
83+
switch name {
84+
case "ServerReflectionInfo":
85+
return GRPCAsyncServerHandler(
86+
context: context,
87+
requestDeserializer: ProtobufDeserializer<Reflection_ServerReflectionRequest>(),
88+
responseSerializer: ProtobufSerializer<Reflection_ServerReflectionResponse>(),
89+
interceptors: self.interceptors?.makeServerReflectionInfoInterceptors() ?? [],
90+
wrapping: { try await self.serverReflectionInfo(requestStream: $0, responseStream: $1, context: $2) }
91+
)
92+
93+
default:
94+
return nil
95+
}
96+
}
97+
}
98+
99+
internal protocol Reflection_ServerReflectionServerInterceptorFactoryProtocol: Sendable {
100+
101+
/// - Returns: Interceptors to use when handling 'serverReflectionInfo'.
102+
/// Defaults to calling `self.makeInterceptors()`.
103+
func makeServerReflectionInfoInterceptors() -> [ServerInterceptor<Reflection_ServerReflectionRequest, Reflection_ServerReflectionResponse>]
104+
}
105+
106+
internal enum Reflection_ServerReflectionServerMetadata {
107+
internal static let serviceDescriptor = GRPCServiceDescriptor(
108+
name: "ServerReflection",
109+
fullName: "reflection.ServerReflection",
110+
methods: [
111+
Reflection_ServerReflectionServerMetadata.Methods.serverReflectionInfo,
112+
]
113+
)
114+
115+
internal enum Methods {
116+
internal static let serverReflectionInfo = GRPCMethodDescriptor(
117+
name: "ServerReflectionInfo",
118+
path: "/reflection.ServerReflection/ServerReflectionInfo",
119+
type: GRPCCallType.bidirectionalStreaming
120+
)
121+
}
122+
}

0 commit comments

Comments
 (0)