Skip to content

Commit e7e9515

Browse files
authored
Fix Route Guide tutorial compilation errors and implementation issues (#32)
1 parent 94275b0 commit e7e9515

12 files changed

+90
-40
lines changed

Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step04-unary.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
2222
context: ServerContext
2323
) async throws -> Routeguide_Feature {
2424
let feature = self.findFeature(
25-
latitude: request.message.latitude,
26-
longitude: request.message.longitude
25+
latitude: request.latitude,
26+
longitude: request.longitude
2727
)
2828
}
2929

Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step05-unary.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
2222
context: ServerContext
2323
) async throws -> Routeguide_Feature {
2424
let feature = self.findFeature(
25-
latitude: request.message.latitude,
26-
longitude: request.message.longitude
25+
latitude: request.latitude,
26+
longitude: request.longitude
2727
)
2828

2929
if let feature {
@@ -33,8 +33,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
3333
let unknownFeature = Routeguide_Feature.with {
3434
$0.name = ""
3535
$0.location = .with {
36-
$0.latitude = request.message.latitude
37-
$0.longitude = request.message.longitude
36+
$0.latitude = request.latitude
37+
$0.longitude = request.longitude
3838
}
3939
}
4040
return unknownFeature

Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step06-server-streaming.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
2222
context: ServerContext
2323
) async throws -> Routeguide_Feature {
2424
let feature = self.findFeature(
25-
latitude: request.message.latitude,
26-
longitude: request.message.longitude
25+
latitude: request.latitude,
26+
longitude: request.longitude
2727
)
2828

2929
if let feature {
@@ -33,8 +33,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
3333
let unknownFeature = Routeguide_Feature.with {
3434
$0.name = ""
3535
$0.location = .with {
36-
$0.latitude = request.message.latitude
37-
$0.longitude = request.message.longitude
36+
$0.latitude = request.latitude
37+
$0.longitude = request.longitude
3838
}
3939
}
4040
return unknownFeature
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
2323
context: ServerContext
2424
) async throws -> Routeguide_Feature {
2525
let feature = self.findFeature(
26-
latitude: request.message.latitude,
27-
longitude: request.message.longitude
26+
latitude: request.latitude,
27+
longitude: request.longitude
2828
)
2929

3030
if let feature {
@@ -34,8 +34,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
3434
let unknownFeature = Routeguide_Feature.with {
3535
$0.name = ""
3636
$0.location = .with {
37-
$0.latitude = request.message.latitude
38-
$0.longitude = request.message.longitude
37+
$0.latitude = request.latitude
38+
$0.longitude = request.longitude
3939
}
4040
}
4141
return unknownFeature
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
2424
context: ServerContext
2525
) async throws -> Routeguide_Feature {
2626
let feature = self.findFeature(
27-
latitude: request.message.latitude,
28-
longitude: request.message.longitude
27+
latitude: request.latitude,
28+
longitude: request.longitude
2929
)
3030

3131
if let feature {
@@ -35,8 +35,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
3535
let unknownFeature = Routeguide_Feature.with {
3636
$0.name = ""
3737
$0.location = .with {
38-
$0.latitude = request.message.latitude
39-
$0.longitude = request.message.longitude
38+
$0.latitude = request.latitude
39+
$0.longitude = request.longitude
4040
}
4141
}
4242
return unknownFeature
@@ -95,10 +95,6 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
9595
response: RPCWriter<Routeguide_RouteNote>,
9696
context: ServerContext
9797
) async throws {
98-
for try await note in request {
99-
let notes = self.receivedNotes.recordNote(note)
100-
try await response.write(contentsOf: notes)
101-
}
10298
}
10399
}
104100

Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step09-bidi-streaming.swift

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,40 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
66
/// Known features.
77
private let features: [Routeguide_Feature]
88

9+
/// Notes recorded by clients.
10+
private let receivedNotes: Notes
11+
12+
/// A thread-safe store for notes sent by clients.
13+
private final class Notes: Sendable {
14+
private let notes: Mutex<[Routeguide_RouteNote]>
15+
16+
init() {
17+
self.notes = Mutex([])
18+
}
19+
20+
/// Records a note and returns all other notes recorded at the same location.
21+
///
22+
/// - Parameter receivedNote: A note to record.
23+
/// - Returns: Other notes recorded at the same location.
24+
func recordNote(_ receivedNote: Routeguide_RouteNote) -> [Routeguide_RouteNote] {
25+
return self.notes.withLock { notes in
26+
var notesFromSameLocation: [Routeguide_RouteNote] = []
27+
for note in notes {
28+
if note.location == receivedNote.location {
29+
notesFromSameLocation.append(note)
30+
}
31+
}
32+
notes.append(receivedNote)
33+
return notesFromSameLocation
34+
}
35+
}
36+
}
37+
938
/// Creates a new route guide service.
1039
/// - Parameter features: Known features.
1140
init(features: [Routeguide_Feature]) {
1241
self.features = features
42+
self.receivedNotes = Notes()
1343
}
1444

1545
/// Returns the first feature found at the given location, if one exists.
@@ -24,8 +54,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
2454
context: ServerContext
2555
) async throws -> Routeguide_Feature {
2656
let feature = self.findFeature(
27-
latitude: request.message.latitude,
28-
longitude: request.message.longitude
57+
latitude: request.latitude,
58+
longitude: request.longitude
2959
)
3060

3161
if let feature {
@@ -35,8 +65,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
3565
let unknownFeature = Routeguide_Feature.with {
3666
$0.name = ""
3767
$0.location = .with {
38-
$0.latitude = request.message.latitude
39-
$0.longitude = request.message.longitude
68+
$0.latitude = request.latitude
69+
$0.longitude = request.longitude
4070
}
4171
}
4272
return unknownFeature
@@ -95,6 +125,10 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
95125
response: RPCWriter<Routeguide_RouteNote>,
96126
context: ServerContext
97127
) async throws {
128+
for try await note in request {
129+
let notes = self.receivedNotes.recordNote(note)
130+
try await response.write(contentsOf: notes)
131+
}
98132
}
99133
}
100134

Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec05-step04-load-features.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct RouteGuide: AsyncParsableCommand {
1212
}
1313
}
1414

15-
private static func loadFeatures() throws -> [Routeguide_Feature] {
15+
func loadFeatures() throws -> [Routeguide_Feature] {
1616
guard let url = Bundle.module.url(forResource: "route_guide_db", withExtension: "json") else {
1717
throw ExitCode.failure
1818
}

Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step06-get-feature.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ extension RouteGuide {
1414
}
1515
}
1616

17-
private func getFeature(using routeGuide: Routeguide_RouteGuide.Client) async throws {
17+
private func getFeature(
18+
using routeGuide: Routeguide_RouteGuide.Client<some ClientTransport>
19+
) async throws {
1820
print("→ Calling 'GetFeature'")
1921

2022
let point = Routeguide_Point.with {

Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step07-list-features.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ extension RouteGuide {
1515
}
1616
}
1717

18-
private func getFeature(using routeGuide: Routeguide_RouteGuide.Client) async throws {
18+
private func getFeature(
19+
using routeGuide: Routeguide_RouteGuide.Client<some ClientTransport>
20+
) async throws {
1921
print("→ Calling 'GetFeature'")
2022

2123
let point = Routeguide_Point.with {
@@ -27,7 +29,9 @@ extension RouteGuide {
2729
print("Got feature '\(feature.name)'")
2830
}
2931

30-
private func listFeatures(using routeGuide: Routeguide_RouteGuide.Client) async throws {
32+
private func listFeatures(
33+
using routeGuide: Routeguide_RouteGuide.Client<some ClientTransport>
34+
) async throws {
3135
print("→ Calling 'ListFeatures'")
3236

3337
let boundingRectangle = Routeguide_Rectangle.with {

Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step08-record-route.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ extension RouteGuide {
1616
}
1717
}
1818

19-
private func getFeature(using routeGuide: Routeguide_RouteGuide.Client) async throws {
19+
private func getFeature(
20+
using routeGuide: Routeguide_RouteGuide.Client<some ClientTransport>
21+
) async throws {
2022
print("→ Calling 'GetFeature'")
2123

2224
let point = Routeguide_Point.with {
@@ -28,7 +30,9 @@ extension RouteGuide {
2830
print("Got feature '\(feature.name)'")
2931
}
3032

31-
private func listFeatures(using routeGuide: Routeguide_RouteGuide.Client) async throws {
33+
private func listFeatures(
34+
using routeGuide: Routeguide_RouteGuide.Client<some ClientTransport>
35+
) async throws {
3236
print("→ Calling 'ListFeatures'")
3337

3438
let boundingRectangle = Routeguide_Rectangle.with {
@@ -51,7 +55,9 @@ extension RouteGuide {
5155
}
5256
}
5357

54-
private func recordRoute(using routeGuide: Routeguide_RouteGuide.Client) async throws {
58+
private func recordRoute(
59+
using routeGuide: Routeguide_RouteGuide.Client<some ClientTransport>
60+
) async throws {
5561
print("→ Calling 'RecordRoute'")
5662

5763
let features = try self.loadFeatures()

0 commit comments

Comments
 (0)