Skip to content

Commit dd92021

Browse files
tomerdGitHub Enterprise
authored and
GitHub Enterprise
committed
hide LambdaLifecycleResult from users (#6)
hide LambdaLifecycleResult from users motivation: cleaner end-user API changes: * make LambdaLifecycleResult internal * change run, _run method signatures to hide LambdaLifecycleResult from end-user API * adjust tests
1 parent b362bcb commit dd92021

File tree

9 files changed

+39
-46
lines changed

9 files changed

+39
-46
lines changed

Sources/SwiftAwsLambda/Lambda+Codable.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@
1515
import Foundation
1616

1717
extension Lambda {
18-
public static func run<In: Decodable, Out: Encodable>(_ closure: @escaping LambdaCodableClosure<In, Out>) -> LambdaLifecycleResult {
19-
return self.run(LambdaClosureWrapper(closure))
18+
public static func run<In: Decodable, Out: Encodable>(_ closure: @escaping LambdaCodableClosure<In, Out>) {
19+
self.run(LambdaClosureWrapper(closure))
2020
}
2121

22-
public static func run<T>(_ handler: T) -> LambdaLifecycleResult where T: LambdaCodableHandler {
23-
return self.run(handler as LambdaHandler)
22+
public static func run<T>(_ handler: T) where T: LambdaCodableHandler {
23+
self.run(handler as LambdaHandler)
2424
}
2525

2626
// for testing
27-
internal static func run<In: Decodable, Out: Encodable>(closure: @escaping LambdaCodableClosure<In, Out>, maxTimes: Int) -> LambdaLifecycleResult {
27+
internal static func run<In: Decodable, Out: Encodable>(maxTimes: Int = 0, _ closure: @escaping LambdaCodableClosure<In, Out>) -> LambdaLifecycleResult {
2828
return self.run(handler: LambdaClosureWrapper(closure), maxTimes: maxTimes)
2929
}
3030

3131
// for testing
32-
internal static func run<T>(handler: T, maxTimes: Int) -> LambdaLifecycleResult where T: LambdaCodableHandler {
32+
internal static func run<T>(handler: T, maxTimes: Int = 0) -> LambdaLifecycleResult where T: LambdaCodableHandler {
3333
return self.run(handler: handler as LambdaHandler, maxTimes: maxTimes)
3434
}
3535
}

Sources/SwiftAwsLambda/Lambda+String.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
extension Lambda {
16-
public static func run(_ closure: @escaping LambdaStringClosure) -> LambdaLifecycleResult {
17-
return self.run(LambdaClosureWrapper(closure))
16+
public static func run(_ closure: @escaping LambdaStringClosure) {
17+
self.run(LambdaClosureWrapper(closure))
1818
}
1919

20-
public static func run(_ handler: LambdaStringHandler) -> LambdaLifecycleResult {
21-
return self.run(handler as LambdaHandler)
20+
public static func run(_ handler: LambdaStringHandler) {
21+
self.run(handler as LambdaHandler)
2222
}
2323

2424
// for testing
25-
internal static func run(closure: @escaping LambdaStringClosure, maxTimes: Int) -> LambdaLifecycleResult {
25+
internal static func run(maxTimes: Int = 0, _ closure: @escaping LambdaStringClosure) -> LambdaLifecycleResult {
2626
return self.run(handler: LambdaClosureWrapper(closure), maxTimes: maxTimes)
2727
}
2828

2929
// for testing
30-
internal static func run(handler: LambdaStringHandler, maxTimes: Int) -> LambdaLifecycleResult {
30+
internal static func run(handler: LambdaStringHandler, maxTimes: Int = 0) -> LambdaLifecycleResult {
3131
return self.run(handler: handler as LambdaHandler, maxTimes: maxTimes)
3232
}
3333
}

Sources/SwiftAwsLambda/Lambda.swift

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,29 @@ import Foundation
1616
import NIO
1717

1818
public enum Lambda {
19-
public static func run(_ closure: @escaping LambdaClosure) -> LambdaLifecycleResult {
20-
return self._run(handler: LambdaClosureWrapper(closure))
19+
public static func run(_ closure: @escaping LambdaClosure) {
20+
let _: LambdaLifecycleResult = run(closure)
2121
}
2222

23-
public static func run(_ handler: LambdaHandler) -> LambdaLifecycleResult {
24-
return self._run(handler: handler)
23+
public static func run(_ handler: LambdaHandler) {
24+
let _: LambdaLifecycleResult = run(handler: handler)
2525
}
2626

2727
// for testing
28-
internal static func run(closure: @escaping LambdaClosure, maxTimes: Int) -> LambdaLifecycleResult {
29-
return self._run(handler: LambdaClosureWrapper(closure), maxTimes: maxTimes)
28+
internal static func run(maxTimes: Int = 0, stopSignal: Signal = .INT, _ closure: @escaping LambdaClosure) -> LambdaLifecycleResult {
29+
return self.run(handler: LambdaClosureWrapper(closure), maxTimes: maxTimes, stopSignal: stopSignal)
3030
}
3131

3232
// for testing
33-
internal static func run(handler: LambdaHandler, maxTimes: Int) -> LambdaLifecycleResult {
34-
return self._run(handler: handler, maxTimes: maxTimes)
35-
}
36-
37-
internal static func _run(handler: LambdaHandler, maxTimes: Int = 0, stopSignal: Signal = .INT) -> LambdaLifecycleResult {
33+
internal static func run(handler: LambdaHandler, maxTimes: Int = 0, stopSignal: Signal = .INT) -> LambdaLifecycleResult {
3834
do {
39-
return try self._run(handler: handler, maxTimes: maxTimes, stopSignal: stopSignal).wait()
35+
return try self.runAsync(handler: handler, maxTimes: maxTimes, stopSignal: stopSignal).wait()
4036
} catch {
4137
return .failure(error)
4238
}
4339
}
4440

45-
internal static func _run(handler: LambdaHandler, maxTimes: Int = 0, stopSignal: Signal = .INT) -> EventLoopFuture<LambdaLifecycleResult> {
41+
internal static func runAsync(handler: LambdaHandler, maxTimes: Int = 0, stopSignal: Signal = .INT) -> EventLoopFuture<LambdaLifecycleResult> {
4642
let lifecycle = Lifecycle(handler: handler, maxTimes: maxTimes)
4743
let signalSource = trap(signal: stopSignal) { signal in
4844
print("intercepted signal: \(signal)")
@@ -138,8 +134,6 @@ public enum Lambda {
138134

139135
public typealias LambdaResult = Result<[UInt8], String>
140136

141-
public typealias LambdaLifecycleResult = Result<Int, Error>
142-
143137
public typealias LambdaCallback = (LambdaResult) -> Void
144138

145139
public typealias LambdaClosure = (LambdaContext, [UInt8], LambdaCallback) -> Void
@@ -166,6 +160,8 @@ public struct LambdaContext {
166160
}
167161
}
168162

163+
internal typealias LambdaLifecycleResult = Result<Int, Error>
164+
169165
private struct LambdaClosureWrapper: LambdaHandler {
170166
private let closure: LambdaClosure
171167
init(_ closure: @escaping LambdaClosure) {

Sources/SwiftAwsLambdaCodableSample/main.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ private class Res: Codable {}
1919

2020
// in this example we are receiving and responding with codables. Req and Res above are examples of how to use
2121
// codables to model your reqeuest and response objects
22-
let result = Lambda.run { (_: LambdaContext, _: Req, callback: LambdaCodableCallback<Res>) in
22+
Lambda.run { (_: LambdaContext, _: Req, callback: LambdaCodableCallback<Res>) in
2323
callback(.success(Res()))
2424
}
2525

26-
print(result)
2726
print("Bye!")

Sources/SwiftAwsLambdaSample/main.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
import SwiftAwsLambda
1616

1717
// in this example we are receiving and responding with bytes (default)
18-
let result = Lambda.run { (_: LambdaContext, payload: [UInt8], callback: LambdaCallback) in
18+
Lambda.run { (_: LambdaContext, payload: [UInt8], callback: LambdaCallback) in
1919
// as an example, respond with the reverse the input payload
2020
callback(.success(payload.reversed()))
2121
}
2222

23-
print(result)
2423
print("Bye!")

Sources/SwiftAwsLambdaStringSample/main.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
import SwiftAwsLambda
1616

1717
// in this example we are receiving and responding with strings
18-
let result = Lambda.run { (_: LambdaContext, payload: String, callback: LambdaStringCallback) in
18+
Lambda.run { (_: LambdaContext, payload: String, callback: LambdaStringCallback) in
1919
// as an example, respond with the reverse the input payload
2020
callback(.success(String(payload.reversed())))
2121
}
2222

23-
print(result)
2423
print("Bye!")

Tests/SwiftAwsLambdaTests/Lambda+CodeableTest.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@ class CodableLambdaTest: XCTestCase {
2626

2727
func testFailure() throws {
2828
let server = try MockLambdaServer(behavior: BadBehavior()).start().wait()
29-
let result = Lambda.run(CodableEchoHandler())
29+
let result = Lambda.run(handler: CodableEchoHandler())
3030
try server.stop().wait()
3131
assertLambdaLifecycleResult(result: result, shouldFailWithError: LambdaRuntimeClientError.badStatusCode(.internalServerError))
3232
}
3333

3434
func testClosureSuccess() throws {
3535
let maxTimes = Int.random(in: 1 ... 10)
3636
let server = try MockLambdaServer(behavior: GoodBehavior()).start().wait()
37-
let result = Lambda.run(closure: { (_: LambdaContext, payload: Req, callback: LambdaCodableCallback<Res>) in
37+
let result = Lambda.run(maxTimes: maxTimes) { (_: LambdaContext, payload: Req, callback: LambdaCodableCallback<Res>) in
3838
callback(.success(Res(requestId: payload.requestId)))
39-
}, maxTimes: maxTimes)
39+
}
4040
try server.stop().wait()
4141
assertLambdaLifecycleResult(result: result, shoudHaveRun: maxTimes)
4242
}
4343

4444
func testClosureFailure() throws {
4545
let server = try MockLambdaServer(behavior: BadBehavior()).start().wait()
46-
let result = Lambda.run { (_: LambdaContext, payload: Req, callback: LambdaCodableCallback<Res>) in
46+
let result: LambdaLifecycleResult = Lambda.run { (_: LambdaContext, payload: Req, callback: LambdaCodableCallback<Res>) in
4747
callback(.success(Res(requestId: payload.requestId)))
4848
}
4949
try server.stop().wait()

Tests/SwiftAwsLambdaTests/Lambda+StringTest.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@ class StringLambdaTest: XCTestCase {
2626

2727
func testFailure() throws {
2828
let server = try MockLambdaServer(behavior: BadBehavior()).start().wait()
29-
let result = Lambda.run(StringEchoHandler())
29+
let result = Lambda.run(handler: StringEchoHandler())
3030
try server.stop().wait()
3131
assertLambdaLifecycleResult(result: result, shouldFailWithError: LambdaRuntimeClientError.badStatusCode(.internalServerError))
3232
}
3333

3434
func testClosureSuccess() throws {
3535
let maxTimes = Int.random(in: 1 ... 10)
3636
let server = try MockLambdaServer(behavior: GoodBehavior()).start().wait()
37-
let result = Lambda.run(closure: { (_: LambdaContext, payload: String, callback: LambdaStringCallback) in
37+
let result = Lambda.run(maxTimes: maxTimes) { (_: LambdaContext, payload: String, callback: LambdaStringCallback) in
3838
callback(.success(payload))
39-
}, maxTimes: maxTimes)
39+
}
4040
try server.stop().wait()
4141
assertLambdaLifecycleResult(result: result, shoudHaveRun: maxTimes)
4242
}
4343

4444
func testClosureFailure() throws {
4545
let server = try MockLambdaServer(behavior: BadBehavior()).start().wait()
46-
let result = Lambda.run { (_: LambdaContext, payload: String, callback: LambdaStringCallback) in
46+
let result: LambdaLifecycleResult = Lambda.run { (_: LambdaContext, payload: String, callback: LambdaStringCallback) in
4747
callback(.success(payload))
4848
}
4949
try server.stop().wait()

Tests/SwiftAwsLambdaTests/LambdaTest.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ class LambdaTest: XCTestCase {
2727

2828
func testFailure() throws {
2929
let server = try MockLambdaServer(behavior: BadBehavior()).start().wait()
30-
let result = Lambda.run(EchoHandler())
30+
let result = Lambda.run(handler: EchoHandler())
3131
try server.stop().wait()
3232
assertLambdaLifecycleResult(result: result, shouldFailWithError: LambdaRuntimeClientError.badStatusCode(.internalServerError))
3333
}
3434

3535
func testClosureSuccess() throws {
3636
let maxTimes = Int.random(in: 1 ... 10)
3737
let server = try MockLambdaServer(behavior: GoodBehavior()).start().wait()
38-
let result = Lambda.run(closure: { (_: LambdaContext, payload: [UInt8], callback: LambdaCallback) in
38+
let result = Lambda.run(maxTimes: maxTimes) { (_: LambdaContext, payload: [UInt8], callback: LambdaCallback) in
3939
callback(.success(payload))
40-
}, maxTimes: maxTimes)
40+
}
4141
try server.stop().wait()
4242
assertLambdaLifecycleResult(result: result, shoudHaveRun: maxTimes)
4343
}
4444

4545
func testClosureFailure() throws {
4646
let server = try MockLambdaServer(behavior: BadBehavior()).start().wait()
47-
let result = Lambda.run { (_: LambdaContext, payload: [UInt8], callback: LambdaCallback) in
47+
let result: LambdaLifecycleResult = Lambda.run { (_: LambdaContext, payload: [UInt8], callback: LambdaCallback) in
4848
callback(.success(payload))
4949
}
5050
try server.stop().wait()
@@ -60,7 +60,7 @@ class LambdaTest: XCTestCase {
6060
}
6161
let signal = Signal.ALRM
6262
let max = 50
63-
let future: EventLoopFuture<LambdaLifecycleResult> = Lambda._run(handler: MyHandler(), maxTimes: max, stopSignal: signal)
63+
let future = Lambda.runAsync(handler: MyHandler(), maxTimes: max, stopSignal: signal)
6464
DispatchQueue(label: "test").async {
6565
kill(getpid(), signal.rawValue)
6666
}

0 commit comments

Comments
 (0)