Skip to content

Commit 951a20a

Browse files
authored
Merge pull request #184 from MrMage/more-improvements
improved error handling and structure throughout the project
2 parents 13351ff + 5d32204 commit 951a20a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2025
-914
lines changed

.gitignore

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22
project.xcworkspace
33
xcuserdata
44
.build
5-
protoc-gen-swift
6-
protoc-gen-swiftgrpc
5+
/protoc-gen-swift
6+
/protoc-gen-swiftgrpc
77
third_party/**
8-
Plugin/Packages/**
9-
Plugin/Sources/protoc-gen-swiftgrpc/templates.swift
10-
Plugin/protoc-*
11-
Plugin/swiftgrpc.log
12-
Plugin/echo.*.swift
13-
Echo
14-
test.out
15-
echo.pid
16-
SwiftGRPC.xcodeproj
8+
/Echo
9+
/test.out
10+
/echo.pid
11+
/SwiftGRPC.xcodeproj
1712
Package.resolved

.travis-install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
cd
2929
mkdir -p local
3030

31-
if [ $TRAVIS_OS_NAME == "osx" ]; then
31+
if [ "$TRAVIS_OS_NAME" == "osx" ]; then
3232
PROTOC_URL=https://github.com/google/protobuf/releases/download/v3.5.1/protoc-3.5.1-osx-x86_64.zip
3333
else
3434
# Install swift

.travis.yml

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ os:
1919
- linux
2020
- osx
2121

22+
cache:
23+
apt: true
24+
directories:
25+
- .build/checkouts
26+
- .build/repositories
27+
2228
# Use Ubuntu 14.04
2329
dist: trusty
2430

@@ -29,23 +35,25 @@ sudo: false
2935
addons:
3036
apt:
3137
packages:
32-
- clang-3.8
33-
- lldb-3.8
34-
- libicu-dev
35-
- libtool
36-
- libcurl4-openssl-dev
37-
- libbsd-dev
38-
- build-essential
39-
- libssl-dev
40-
- uuid-dev
41-
- curl
42-
- unzip
38+
- clang-3.8
39+
- lldb-3.8
40+
- libicu-dev
41+
- libtool
42+
- libcurl4-openssl-dev
43+
- libbsd-dev
44+
- build-essential
45+
- libssl-dev
46+
- uuid-dev
47+
- curl
48+
- unzip
4349

4450
install: ./.travis-install.sh
4551

4652
script:
4753
- export PATH=$HOME/local/bin:$PATH
4854
- export LD_LIBRARY_PATH=$HOME/local/lib
55+
- swift package -v resolve
56+
- make all
4957
- make test
5058
- make test-plugin
5159
- make test-echo

Examples/EchoXcode/Echo/EchoViewController.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class EchoViewController: NSViewController, NSTextFieldDelegate {
4444
do {
4545
try callServer(address: addressField.stringValue,
4646
host: "example.com")
47-
} catch (let error) {
47+
} catch {
4848
print(error)
4949
}
5050
}
@@ -54,7 +54,7 @@ class EchoViewController: NSViewController, NSTextFieldDelegate {
5454
if nowStreaming {
5555
do {
5656
try sendClose()
57-
} catch (let error) {
57+
} catch {
5858
print(error)
5959
}
6060
}
@@ -66,7 +66,7 @@ class EchoViewController: NSViewController, NSTextFieldDelegate {
6666
if nowStreaming {
6767
do {
6868
try sendClose()
69-
} catch (let error) {
69+
} catch {
7070
print(error)
7171
}
7272
}
@@ -76,7 +76,7 @@ class EchoViewController: NSViewController, NSTextFieldDelegate {
7676
if nowStreaming {
7777
do {
7878
try sendClose()
79-
} catch (let error) {
79+
} catch {
8080
print(error)
8181
}
8282
}
@@ -151,7 +151,7 @@ class EchoViewController: NSViewController, NSTextFieldDelegate {
151151
}
152152
try receiveExpandMessages()
153153
displayMessageSent(requestMessage.text)
154-
} catch (let error) {
154+
} catch {
155155
self.displayMessageReceived("No message received. \(error)")
156156
}
157157
}
@@ -169,7 +169,7 @@ class EchoViewController: NSViewController, NSTextFieldDelegate {
169169
}
170170
}
171171
try sendCollectMessage()
172-
} catch (let error) {
172+
} catch {
173173
self.displayMessageReceived("No message received. \(error)")
174174
}
175175
} else if callSelectButton.selectedSegment == 3 {
@@ -187,7 +187,7 @@ class EchoViewController: NSViewController, NSTextFieldDelegate {
187187
}
188188
}
189189
try sendUpdateMessage()
190-
} catch (let error) {
190+
} catch {
191191
self.displayMessageReceived("No message received. \(error)")
192192
}
193193
}

Examples/Google/NaturalLanguage/Sources/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ if let provider = DefaultTokenProvider(scopes: scopes) {
5555
do {
5656
let result = try service.annotatetext(request)
5757
print("\(result)")
58-
} catch (let error) {
58+
} catch {
5959
print("ERROR: \(error)")
6060
}
6161
}

Examples/SimpleXcode/Simple/Document.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ class Document: NSDocument {
181181
}
182182
}
183183
}
184-
} catch (let callError) {
185-
Swift.print("call error \(callError)")
184+
} catch {
185+
Swift.print("call error \(error)")
186186
}
187187
self.log("------------------------------")
188188
sleep(1)
@@ -241,8 +241,8 @@ class Document: NSDocument {
241241
trailingMetadata: trailingMetadataToSend)
242242

243243
self.log("------------------------------")
244-
} catch (let callError) {
245-
Swift.print("call error \(callError)")
244+
} catch {
245+
Swift.print("call error \(error)")
246246
}
247247
}
248248

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ all:
88

99
project:
1010
swift package generate-xcodeproj
11+
# Optional: set the generated project's indentation settings.
12+
-ruby fix-indentation-settings.rb
1113

1214
test: all
1315
swift test -v $(CFLAGS)

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ let package = Package(
2323
.library(name: "SwiftGRPC", targets: ["SwiftGRPC"]),
2424
],
2525
dependencies: [
26-
.package(url: "https://github.com/Zewo/zlib.git", from: "0.4.0"),
26+
.package(url: "https://github.com/apple/swift-nio-zlib-support.git", from: "1.0.0"),
2727
.package(url: "https://github.com/apple/swift-protobuf.git", from: "1.0.2"),
2828
.package(url: "https://github.com/kylef/Commander.git", from: "0.8.0")
2929
],
3030
targets: [
3131
.target(name: "SwiftGRPC",
3232
dependencies: ["CgRPC", "SwiftProtobuf"]),
3333
.target(name: "CgRPC",
34-
dependencies: ["BoringSSL", "zlib"]),
34+
dependencies: ["BoringSSL"]),
3535
.target(name: "RootsEncoder"),
3636
.target(name: "protoc-gen-swiftgrpc",
3737
dependencies: [

Sources/Examples/Echo/EchoProvider.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,34 @@ class EchoProvider: Echo_EchoProvider {
3838
}
3939
}
4040
session.waitForSendOperationsToFinish()
41+
try session.close(withStatus: .ok, completion: nil)
4142
}
4243

4344
// collect collects a sequence of messages and returns them concatenated when the caller closes.
4445
func collect(session: Echo_EchoCollectSession) throws {
4546
var parts: [String] = []
4647
while true {
4748
do {
48-
let request = try session.receive()
49+
guard let request = try session.receive()
50+
else { break } // End of stream
4951
parts.append(request.text)
50-
} catch ServerError.endOfStream {
52+
} catch {
53+
print("collect error: \(error)")
5154
break
52-
} catch (let error) {
53-
print("\(error)")
5455
}
5556
}
5657
var response = Echo_EchoResponse()
5758
response.text = "Swift echo collect: " + parts.joined(separator: " ")
58-
try session.sendAndClose(response)
59+
try session.sendAndClose(response: response, status: .ok, completion: nil)
5960
}
6061

6162
// update streams back messages as they are received in an input stream.
6263
func update(session: Echo_EchoUpdateSession) throws {
6364
var count = 0
6465
while true {
6566
do {
66-
let request = try session.receive()
67+
guard let request = try session.receive()
68+
else { break } // End of stream
6769
var response = Echo_EchoResponse()
6870
response.text = "Swift echo update (\(count)): \(request.text)"
6971
count += 1
@@ -72,14 +74,12 @@ class EchoProvider: Echo_EchoProvider {
7274
print("update error: \(error)")
7375
}
7476
}
75-
} catch ServerError.endOfStream {
76-
break
77-
} catch (let error) {
78-
print("\(error)")
77+
} catch {
78+
print("update error: \(error)")
7979
break
8080
}
8181
}
8282
session.waitForSendOperationsToFinish()
83-
try session.close()
83+
try session.close(withStatus: .ok, completion: nil)
8484
}
8585
}

Sources/Examples/Echo/Generated/echo.grpc.swift

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ fileprivate final class Echo_EchoGetCallBase: ClientCallUnaryBase<Echo_EchoReque
3333

3434
internal protocol Echo_EchoExpandCall: ClientCallServerStreaming {
3535
/// Call this to wait for a result. Blocking.
36-
func receive() throws -> Echo_EchoResponse
36+
func receive() throws -> Echo_EchoResponse?
3737
/// Call this to wait for a result. Nonblocking.
38-
func receive(completion: @escaping (Echo_EchoResponse?, ClientError?) -> Void) throws
38+
func receive(completion: @escaping (ResultOrRPCError<Echo_EchoResponse?>) -> Void) throws
3939
}
4040

4141
fileprivate final class Echo_EchoExpandCallBase: ClientCallServerStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoExpandCall {
@@ -47,13 +47,15 @@ class Echo_EchoExpandCallTestStub: ClientCallServerStreamingTestStub<Echo_EchoRe
4747
}
4848

4949
internal protocol Echo_EchoCollectCall: ClientCallClientStreaming {
50-
/// Call this to send each message in the request stream. Nonblocking.
50+
/// Send a message to the stream. Nonblocking.
5151
func send(_ message: Echo_EchoRequest, completion: @escaping (Error?) -> Void) throws
52+
/// Send a message to the stream and wait for the send operation to finish. Blocking.
53+
func send(_ message: Echo_EchoRequest) throws
5254

5355
/// Call this to close the connection and wait for a response. Blocking.
5456
func closeAndReceive() throws -> Echo_EchoResponse
5557
/// Call this to close the connection and wait for a response. Nonblocking.
56-
func closeAndReceive(completion: @escaping (Echo_EchoResponse?, ClientError?) -> Void) throws
58+
func closeAndReceive(completion: @escaping (ResultOrRPCError<Echo_EchoResponse>) -> Void) throws
5759
}
5860

5961
fileprivate final class Echo_EchoCollectCallBase: ClientCallClientStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectCall {
@@ -68,12 +70,14 @@ class Echo_EchoCollectCallTestStub: ClientCallClientStreamingTestStub<Echo_EchoR
6870

6971
internal protocol Echo_EchoUpdateCall: ClientCallBidirectionalStreaming {
7072
/// Call this to wait for a result. Blocking.
71-
func receive() throws -> Echo_EchoResponse
73+
func receive() throws -> Echo_EchoResponse?
7274
/// Call this to wait for a result. Nonblocking.
73-
func receive(completion: @escaping (Echo_EchoResponse?, ClientError?) -> Void) throws
75+
func receive(completion: @escaping (ResultOrRPCError<Echo_EchoResponse?>) -> Void) throws
7476

75-
/// Call this to send each message in the request stream.
77+
/// Send a message to the stream. Nonblocking.
7678
func send(_ message: Echo_EchoRequest, completion: @escaping (Error?) -> Void) throws
79+
/// Send a message to the stream and wait for the send operation to finish. Blocking.
80+
func send(_ message: Echo_EchoRequest) throws
7781

7882
/// Call this to close the sending connection. Blocking.
7983
func closeSend() throws
@@ -201,35 +205,53 @@ fileprivate final class Echo_EchoGetSessionBase: ServerSessionUnaryBase<Echo_Ech
201205
class Echo_EchoGetSessionTestStub: ServerSessionUnaryTestStub, Echo_EchoGetSession {}
202206

203207
internal protocol Echo_EchoExpandSession: ServerSessionServerStreaming {
204-
/// Send a message. Nonblocking.
205-
func send(_ response: Echo_EchoResponse, completion: ((Error?) -> Void)?) throws
208+
/// Send a message to the stream. Nonblocking.
209+
func send(_ message: Echo_EchoResponse, completion: @escaping (Error?) -> Void) throws
210+
/// Send a message to the stream and wait for the send operation to finish. Blocking.
211+
func send(_ message: Echo_EchoResponse) throws
212+
213+
/// Close the connection and send the status. Non-blocking.
214+
/// You MUST call this method once you are done processing the request.
215+
func close(withStatus status: ServerStatus, completion: (() -> Void)?) throws
206216
}
207217

208218
fileprivate final class Echo_EchoExpandSessionBase: ServerSessionServerStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoExpandSession {}
209219

210220
class Echo_EchoExpandSessionTestStub: ServerSessionServerStreamingTestStub<Echo_EchoResponse>, Echo_EchoExpandSession {}
211221

212222
internal protocol Echo_EchoCollectSession: ServerSessionClientStreaming {
213-
/// Receive a message. Blocks until a message is received or the client closes the connection.
214-
func receive() throws -> Echo_EchoRequest
215-
216-
/// Send a response and close the connection.
217-
func sendAndClose(_ response: Echo_EchoResponse) throws
223+
/// Call this to wait for a result. Blocking.
224+
func receive() throws -> Echo_EchoRequest?
225+
/// Call this to wait for a result. Nonblocking.
226+
func receive(completion: @escaping (ResultOrRPCError<Echo_EchoRequest?>) -> Void) throws
227+
228+
/// You MUST call one of these two methods once you are done processing the request.
229+
/// Close the connection and send a single result. Non-blocking.
230+
func sendAndClose(response: Echo_EchoResponse, status: ServerStatus, completion: (() -> Void)?) throws
231+
/// Close the connection and send an error. Non-blocking.
232+
/// Use this method if you encountered an error that makes it impossible to send a response.
233+
/// Accordingly, it does not make sense to call this method with a status of `.ok`.
234+
func sendErrorAndClose(status: ServerStatus, completion: (() -> Void)?) throws
218235
}
219236

220237
fileprivate final class Echo_EchoCollectSessionBase: ServerSessionClientStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectSession {}
221238

222239
class Echo_EchoCollectSessionTestStub: ServerSessionClientStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectSession {}
223240

224241
internal protocol Echo_EchoUpdateSession: ServerSessionBidirectionalStreaming {
225-
/// Receive a message. Blocks until a message is received or the client closes the connection.
226-
func receive() throws -> Echo_EchoRequest
242+
/// Call this to wait for a result. Blocking.
243+
func receive() throws -> Echo_EchoRequest?
244+
/// Call this to wait for a result. Nonblocking.
245+
func receive(completion: @escaping (ResultOrRPCError<Echo_EchoRequest?>) -> Void) throws
227246

228-
/// Send a message. Nonblocking.
229-
func send(_ response: Echo_EchoResponse, completion: ((Error?) -> Void)?) throws
247+
/// Send a message to the stream. Nonblocking.
248+
func send(_ message: Echo_EchoResponse, completion: @escaping (Error?) -> Void) throws
249+
/// Send a message to the stream and wait for the send operation to finish. Blocking.
250+
func send(_ message: Echo_EchoResponse) throws
230251

231-
/// Close a connection. Blocks until the connection is closed.
232-
func close() throws
252+
/// Close the connection and send the status. Non-blocking.
253+
/// You MUST call this method once you are done processing the request.
254+
func close(withStatus status: ServerStatus, completion: (() -> Void)?) throws
233255
}
234256

235257
fileprivate final class Echo_EchoUpdateSessionBase: ServerSessionBidirectionalStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateSession {}

0 commit comments

Comments
 (0)