Skip to content

Commit e00345b

Browse files
authored
Merge branch 'main' into v2/json-codegen
2 parents e6b2137 + e4da2bb commit e00345b

File tree

7 files changed

+51
-54
lines changed

7 files changed

+51
-54
lines changed

Sources/GRPCCore/Call/Server/ServerContext.swift

-17
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,6 @@ public struct ServerContext: Sendable {
1919
/// A description of the method being called.
2020
public var descriptor: MethodDescriptor
2121

22-
/// A description of the remote peer.
23-
///
24-
/// The format of the description should follow the pattern "<transport>:<address>" where
25-
/// "<transport>" indicates the underlying network transport (such as "ipv4", "unix", or
26-
/// "in-process"). This is a guideline for how descriptions should be formatted; different
27-
/// implementations may not follow this format so you shouldn't make assumptions based on it.
28-
///
29-
/// Some examples include:
30-
/// - "ipv4:127.0.0.1:31415",
31-
/// - "ipv6:[::1]:443",
32-
/// - "in-process:27182".
33-
@available(*, deprecated, renamed: "remotePeer")
34-
public var peer: String {
35-
get { remotePeer }
36-
set { remotePeer = newValue }
37-
}
38-
3922
/// A description of the remote peer.
4023
///
4124
/// The format of the description should follow the pattern "<transport>:<address>" where

Sources/GRPCCore/Documentation.docc/Development/Design.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ concurrency.
173173
Most users won't use ``GRPCClient`` to execute RPCs directly, instead they will
174174
use the generated client stubs which wrap the ``GRPCClient``. Users are
175175
responsible for creating the client and running it (which starts and runs the
176-
underlying transport). This is done by calling ``GRPCClient/run()``. The client
176+
underlying transport). This is done by calling ``GRPCClient/runConnections()``. The client
177177
can be shutdown gracefully by calling ``GRPCClient/beginGracefulShutdown()``
178178
which will stop new RPCs from starting (by failing them with
179179
``RPCError/Code-swift.struct/unavailable``) but allow existing ones to continue.
180180
Existing work can be stopped more abruptly by cancelling the task where
181-
``GRPCClient/run()`` is executing.
181+
``GRPCClient/runConnections()`` is executing.
182182

183183
#### Server
184184

Sources/GRPCCore/GRPCClient.swift

+1-6
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,11 @@ public final class GRPCClient<Transport: ClientTransport>: Sendable {
227227
}
228228
}
229229

230-
@available(*, deprecated, renamed: "runConnections", message: "It'll be removed before v2.")
231-
public func run() async throws {
232-
try await self.runConnections()
233-
}
234-
235230
/// Close the client.
236231
///
237232
/// The transport will be closed: this means that it will be given enough time to wait for
238233
/// in-flight RPCs to finish executing, but no new RPCs will be accepted. You can cancel the task
239-
/// executing ``run()`` if you want to abruptly stop in-flight RPCs.
234+
/// executing ``runConnections()`` if you want to abruptly stop in-flight RPCs.
240235
public func beginGracefulShutdown() {
241236
let wasRunning = self.stateMachine.withLock { $0.state.beginGracefulShutdown() }
242237
if wasRunning {

Sources/GRPCCore/MethodDescriptor.swift

-11
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,6 @@ public struct MethodDescriptor: Sendable, Hashable {
5151
self.service = ServiceDescriptor(fullyQualifiedService: fullyQualifiedService)
5252
self.method = method
5353
}
54-
55-
@available(*, deprecated, renamed: "init(fullyQualifiedService:method:)")
56-
/// Creates a new method descriptor.
57-
///
58-
/// - Parameters:
59-
/// - service: The fully qualified name of the service, including the package
60-
/// name. For example, "helloworld.Greeter".
61-
/// - method: The name of the method. For example, "SayHello".
62-
public init(service: String, method: String) {
63-
self.init(fullyQualifiedService: service, method: method)
64-
}
6554
}
6655

6756
extension MethodDescriptor: CustomStringConvertible {

Tests/GRPCCoreTests/Test Utilities/Coding+JSON.swift

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
import GRPCCore
1718

1819
import struct Foundation.Data

Tests/GRPCInProcessTransportTests/InProcessTransportTests.swift

+2-18
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct InProcessTransportTests {
8080
request: ClientRequest(message: ()),
8181
descriptor: .peerInfo,
8282
serializer: VoidSerializer(),
83-
deserializer: PeerInfoDeserializer(),
83+
deserializer: JSONDeserializer<PeerInfo>(),
8484
options: .defaults
8585
) {
8686
try $0.message
@@ -142,7 +142,7 @@ private struct TestService: RegistrableRPCService {
142142
router.registerHandler(
143143
forMethod: .peerInfo,
144144
deserializer: VoidDeserializer(),
145-
serializer: PeerInfoSerializer(),
145+
serializer: JSONSerializer<PeerInfo>(),
146146
handler: {
147147
let response = try await self.peerInfo(
148148
request: ServerRequest<Void>(stream: $0),
@@ -171,22 +171,6 @@ private struct PeerInfo: Codable {
171171
var remote: String
172172
}
173173

174-
private struct PeerInfoSerializer: MessageSerializer {
175-
func serialize<Bytes: GRPCContiguousBytes>(_ message: PeerInfo) throws -> Bytes {
176-
Bytes("\(message.local) \(message.remote)".utf8)
177-
}
178-
}
179-
180-
private struct PeerInfoDeserializer: MessageDeserializer {
181-
func deserialize<Bytes: GRPCContiguousBytes>(_ serializedMessageBytes: Bytes) throws -> PeerInfo {
182-
let stringPeerInfo = serializedMessageBytes.withUnsafeBytes {
183-
String(decoding: $0, as: UTF8.self)
184-
}
185-
let peerInfoComponents = stringPeerInfo.split(separator: " ")
186-
return PeerInfo(local: String(peerInfoComponents[0]), remote: String(peerInfoComponents[1]))
187-
}
188-
}
189-
190174
private struct UTF8Serializer: MessageSerializer {
191175
func serialize<Bytes: GRPCContiguousBytes>(_ message: String) throws -> Bytes {
192176
Bytes(message.utf8)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2025, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import GRPCCore
18+
19+
import struct Foundation.Data
20+
import class Foundation.JSONDecoder
21+
import class Foundation.JSONEncoder
22+
23+
struct JSONSerializer<Message: Codable>: MessageSerializer {
24+
func serialize<Bytes: GRPCContiguousBytes>(_ message: Message) throws -> Bytes {
25+
do {
26+
let jsonEncoder = JSONEncoder()
27+
let data = try jsonEncoder.encode(message)
28+
return Bytes(data)
29+
} catch {
30+
throw RPCError(code: .internalError, message: "Can't serialize message to JSON. \(error)")
31+
}
32+
}
33+
}
34+
35+
struct JSONDeserializer<Message: Codable>: MessageDeserializer {
36+
func deserialize<Bytes: GRPCContiguousBytes>(_ serializedMessageBytes: Bytes) throws -> Message {
37+
do {
38+
let jsonDecoder = JSONDecoder()
39+
let data = serializedMessageBytes.withUnsafeBytes { Data($0) }
40+
return try jsonDecoder.decode(Message.self, from: data)
41+
} catch {
42+
throw RPCError(code: .internalError, message: "Can't deserialze message from JSON. \(error)")
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)