Skip to content

Commit fb2e551

Browse files
committed
cleanup and refactoring
motivation: cleaner code base changes: * define generic Result type, and make LambdaResult a type alias * rename RunLambdaResult to LambdaRunResult * remove redundant test and dead code
1 parent 2927b39 commit fb2e551

File tree

12 files changed

+36
-74
lines changed

12 files changed

+36
-74
lines changed

Sources/SwiftAwsLambda/Lambda+Codable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension Lambda {
3434
}
3535
}
3636

37-
public typealias LambdaCodableResult<Out> = LambdaResult<Out, String>
37+
public typealias LambdaCodableResult<Out> = Result<Out, String>
3838

3939
public typealias LambdaCodableCallback<Out> = (LambdaCodableResult<Out>) -> Void
4040

@@ -62,7 +62,7 @@ public class LambdaCodableCodec<In: Decodable, Out: Encodable> {
6262
}
6363

6464
public extension LambdaCodableHandler {
65-
func handle(context: LambdaContext, payload: [UInt8], callback: @escaping (LambdaResult<[UInt8], String>) -> Void) {
65+
func handle(context: LambdaContext, payload: [UInt8], callback: @escaping (LambdaResult) -> Void) {
6666
guard let payloadAsCodable = codec.decode(payload) else {
6767
return callback(.failure("failed decoding payload (in)"))
6868
}

Sources/SwiftAwsLambda/Lambda+String.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ extension Lambda {
3232
}
3333
}
3434

35-
public typealias LambdaStringResult = LambdaResult<String, String>
35+
public typealias LambdaStringResult = Result<String, String>
36+
3637
public typealias LambdaStringCallback = (LambdaStringResult) -> Void
3738

3839
public typealias LambdaStringClosure = (LambdaContext, String, LambdaStringCallback) -> Void

Sources/SwiftAwsLambda/Lambda.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,27 @@ public final class Lambda {
6666
}
6767
}
6868

69-
public enum LambdaResult<Value, Error> {
70-
case success(Value)
71-
case failure(Error)
72-
}
69+
public typealias LambdaResult = Result<[UInt8], String>
7370

74-
public typealias LambdaLifecycleResult = LambdaResult<Int, Error>
71+
public typealias LambdaLifecycleResult = Result<Int, Error>
7572

76-
public typealias LambdaCallback = (LambdaResult<[UInt8], String>) -> Void
73+
public typealias LambdaCallback = (LambdaResult) -> Void
7774

7875
public typealias LambdaClosure = (LambdaContext, [UInt8], LambdaCallback) -> Void
7976

8077
public protocol LambdaHandler {
8178
func handle(context: LambdaContext, payload: [UInt8], callback: @escaping LambdaCallback)
8279
}
8380

81+
public struct LambdaContext {
82+
public let requestId: String
83+
public let traceId: String?
84+
public let invokedFunctionArn: String?
85+
public let cognitoIdentity: String?
86+
public let clientContext: String?
87+
public let deadlineNs: String?
88+
}
89+
8490
private class LambdaClosureWrapper: LambdaHandler {
8591
private let closure: LambdaClosure
8692
init(_ closure: @escaping LambdaClosure) {

Sources/SwiftAwsLambda/LambdaContext.swift

Lines changed: 0 additions & 22 deletions
This file was deleted.

Sources/SwiftAwsLambda/LambdaRunner.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal final class LambdaRunner {
2828
}
2929

3030
// TODO: should we move the event loop as an argument instead of instance variable?
31-
func run() -> EventLoopFuture<RunLambdaResult> {
31+
func run() -> EventLoopFuture<LambdaRunResult> {
3232
print("lambda invocation sequence starting")
3333
// 1. request work from lambda runtime engine
3434
return runtimeClient.requestWork().then { workRequestResult in
@@ -39,7 +39,7 @@ internal final class LambdaRunner {
3939
case let .success(context, payload):
4040
// 2. send work to handler
4141
print("sending work to lambda handler \(self.lambdaHandler)")
42-
let promise: EventLoopPromise<LambdaResult<[UInt8], String>> = self.eventLoop.newPromise()
42+
let promise: EventLoopPromise<LambdaResult> = self.eventLoop.newPromise()
4343
self.lambdaHandler.handle(context: context, payload: payload, promise: promise)
4444
return promise.futureResult.then { lambdaResult in
4545
// 3. report results to runtime engine
@@ -64,13 +64,13 @@ internal final class LambdaRunner {
6464
}
6565
}
6666

67-
internal typealias RunLambdaResult = LambdaResult<(), Error>
67+
internal typealias LambdaRunResult = Result<(), Error>
6868

6969
private extension LambdaHandler {
70-
func handle(context: LambdaContext, payload: [UInt8], promise: EventLoopPromise<LambdaResult<[UInt8], String>>) {
70+
func handle(context: LambdaContext, payload: [UInt8], promise: EventLoopPromise<LambdaResult>) {
7171
// offloading so user code never blocks the eventloop
7272
DispatchQueue(label: "lambda-\(context.requestId)").async {
73-
self.handle(context: context, payload: payload, callback: { (result: LambdaResult<[UInt8], String>) in
73+
self.handle(context: context, payload: payload, callback: { (result: LambdaResult) in
7474
promise.succeed(result: result)
7575
})
7676
}

Sources/SwiftAwsLambda/LambdaRuntimeClient.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal class LambdaRuntimeClient {
4545
}
4646
}
4747

48-
func reportResults(context: LambdaContext, result: LambdaResult<[UInt8], String>) -> EventLoopFuture<PostResultsResult> {
48+
func reportResults(context: LambdaContext, result: LambdaResult) -> EventLoopFuture<PostResultsResult> {
4949
var url = baseUrl + Consts.invokationURLPrefix + "/" + context.requestId
5050
var body: ByteBuffer
5151
switch result {
@@ -72,8 +72,8 @@ internal class LambdaRuntimeClient {
7272
}
7373
}
7474

75-
internal typealias RequestWorkResult = LambdaResult<(LambdaContext, [UInt8]), LambdaRuntimeClientError>
76-
internal typealias PostResultsResult = LambdaResult<(), LambdaRuntimeClientError>
75+
internal typealias RequestWorkResult = Result<(LambdaContext, [UInt8]), LambdaRuntimeClientError>
76+
internal typealias PostResultsResult = Result<(), LambdaRuntimeClientError>
7777

7878
internal enum LambdaRuntimeClientError: Error {
7979
case badStatusCode

Sources/SwiftAwsLambda/Utils.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
import NIO
1616

17+
public enum Result<Value, Error> {
18+
case success(Value)
19+
case failure(Error)
20+
}
21+
1722
internal enum Consts {
1823
static let hostPortEnvVariableName = "AWS_LAMBDA_RUNTIME_API"
1924

Sources/SwiftAwsLambdaStringSample/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import SwiftAwsLambda
1616

1717
// in this example we are receiving and responding with strings
18-
let result = Lambda.run { (context: LambdaContext, payload: String, callback: LambdaStringCallback) in
18+
let result = 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
}

Tests/SwiftAwsLambdaTests/LambdaTest+XCTest.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ extension LambdaTest {
3030
("testFailure", testFailure),
3131
("testClosureSuccess", testClosureSuccess),
3232
("testClosureFailure", testClosureFailure),
33-
("testStringSuceess", testStringSuceess),
3433
]
3534
}
3635
}

Tests/SwiftAwsLambdaTests/LambdaTest.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,6 @@ class LambdaTest: XCTestCase {
5050
try server.stop().wait()
5151
assertLambdaLifecycleResult(result: result, shouldFailWithError: LambdaRuntimeClientError.badStatusCode)
5252
}
53-
54-
// FIXME: seepraet test class?
55-
56-
func testStringSuceess() throws {
57-
class EchoStringHandler: LambdaStringHandler {
58-
func handle(context _: LambdaContext, payload: String, callback: @escaping LambdaStringCallback) {
59-
callback(.success(payload))
60-
}
61-
}
62-
let maxTimes = Int.random(in: 1 ... 10)
63-
let server = try MockLambdaServer(behavior: GoodBehavior()).start().wait()
64-
let result = Lambda.run(handler: EchoStringHandler(), maxTimes: maxTimes) // blocking
65-
try server.stop().wait()
66-
assertLambdaLifecycleResult(result: result, shoudHaveRun: maxTimes)
67-
}
6853
}
6954

7055
private func assertLambdaLifecycleResult(result: LambdaLifecycleResult, shoudHaveRun: Int = 0, shouldFailWithError: Error? = nil) {

Tests/SwiftAwsLambdaTests/MockLambdaServer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ internal final class HTTPHandler: ChannelInboundHandler {
135135
} else if requestHead.uri.hasSuffix(Consts.postResponseURLSuffix) {
136136
guard let requestId = requestHead.uri.split(separator: "/").dropFirst(2).first,
137137
let response = requestBody
138-
else {
138+
else {
139139
return writeResponse(ctx: ctx, version: requestHead.version, status: .badRequest)
140140
}
141141
switch behavior.processResponse(requestId: String(requestId), response: response) {
@@ -148,7 +148,7 @@ internal final class HTTPHandler: ChannelInboundHandler {
148148
guard let requestId = requestHead.uri.split(separator: "/").dropFirst(2).first,
149149
let json = requestBody,
150150
let error = ErrorResponse.fromJson(json)
151-
else {
151+
else {
152152
return writeResponse(ctx: ctx, version: requestHead.version, status: .badRequest)
153153
}
154154
switch behavior.processError(requestId: String(requestId), error: error) {
@@ -189,7 +189,7 @@ internal protocol LambdaServerBehavior {
189189
func processError(requestId: String, error: ErrorResponse) -> ProcessErrorResult
190190
}
191191

192-
internal typealias GetWorkResult = LambdaResult<(String, String), GetWorkError>
192+
internal typealias GetWorkResult = Result<(String, String), GetWorkError>
193193

194194
internal enum GetWorkError: Int {
195195
case badRequest = 400
@@ -209,7 +209,7 @@ internal enum ProcessResponseError: Int {
209209
case internalServerError = 500
210210
}
211211

212-
internal typealias ProcessErrorResult = LambdaResult<(), ProcessError>
212+
internal typealias ProcessErrorResult = Result<(), ProcessError>
213213

214214
internal enum ProcessError: Int, Error {
215215
case invalidErrorShape = 299

Tests/SwiftAwsLambdaTests/Utils.swift

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,15 @@ import NIO
1616
@testable import SwiftAwsLambda
1717
import XCTest
1818

19-
func runLambda(behavior: LambdaServerBehavior, handler: LambdaHandler) throws -> RunLambdaResult {
20-
/* let eventLoop = MultiThreadedEventLoopGroup(numberOfThreads: 1).next()
21-
let server = MockLambdaServer(behavior: behavior)
22-
return server.start().hopTo(eventLoop: eventLoop).then { _ in
23-
let runner = LambdaRunner(eventLoop: eventLoop, lambdaHandler: handler)
24-
return runner.run().then { result in
25-
server.stop().hopTo(eventLoop: eventLoop).then { _ in
26-
print("\(server) stopped")
27-
return eventLoop.newSucceededFuture(result: result)
28-
}
29-
}
30-
} */
31-
19+
func runLambda(behavior: LambdaServerBehavior, handler: LambdaHandler) throws -> LambdaRunResult {
3220
let runner = LambdaRunner(handler)
3321
let server = try MockLambdaServer(behavior: behavior).start().wait()
3422
let result = try runner.run().wait()
3523
try server.stop().wait()
3624
return result
3725
}
3826

39-
func assertRunLambdaResult(result: RunLambdaResult, shouldFailWithError: Error? = nil) {
27+
func assertRunLambdaResult(result: LambdaRunResult, shouldFailWithError: Error? = nil) {
4028
switch result {
4129
case .success:
4230
if nil != shouldFailWithError {

0 commit comments

Comments
 (0)