Skip to content

Commit d0df0de

Browse files
tomerdGitHub Enterprise
authored and
GitHub Enterprise
committed
code style (#9)
motivation: nicer code changes * rename Result to ResultType since swift is getting formal Result soon * use Handler instead of T for generics in CodableLambda which is more readable * simpler bytes->String decoding
1 parent b034c82 commit d0df0de

File tree

7 files changed

+14
-17
lines changed

7 files changed

+14
-17
lines changed

Sources/SwiftAwsLambda/Lambda+Codable.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extension Lambda {
2727
/// Run a Lambda defined by implementing the `LambdaCodableHandler` protocol, having `In` and `Out` are `Decodable` and `Encodable` respectively.
2828
///
2929
/// - note: This is a blocking operation that will run forever, as it's lifecycle is managed by the AWS Lambda Runtime Engine.
30-
public static func run<T>(_ handler: T) where T: LambdaCodableHandler {
30+
public static func run<Handler>(_ handler: Handler) where Handler: LambdaCodableHandler {
3131
self.run(handler as LambdaHandler)
3232
}
3333

@@ -37,13 +37,13 @@ extension Lambda {
3737
}
3838

3939
// for testing
40-
internal static func run<T>(handler: T, maxTimes: Int = 0) -> LambdaLifecycleResult where T: LambdaCodableHandler {
40+
internal static func run<Handler>(handler: Handler, maxTimes: Int = 0) -> LambdaLifecycleResult where Handler: LambdaCodableHandler {
4141
return self.run(handler: handler as LambdaHandler, maxTimes: maxTimes)
4242
}
4343
}
4444

4545
/// A result type for a Lambda that returns a generic `Out`, having `Out` extend `Encodable`.
46-
public typealias LambdaCodableResult<Out> = Result<Out, String>
46+
public typealias LambdaCodableResult<Out> = ResultType<Out, String>
4747

4848
/// A callback for a Lambda that returns a `LambdaCodableResult<Out>` result type, having `Out` extend `Encodable`.
4949
public typealias LambdaCodableCallback<Out> = (LambdaCodableResult<Out>) -> Void

Sources/SwiftAwsLambda/Lambda+String.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension Lambda {
4040
}
4141

4242
/// A result type for a Lambda that returns a `String`.
43-
public typealias LambdaStringResult = Result<String, String>
43+
public typealias LambdaStringResult = ResultType<String, String>
4444

4545
/// A callback for a Lambda that returns a `LambdaStringResult` result type.
4646
public typealias LambdaStringCallback = (LambdaStringResult) -> Void
@@ -53,13 +53,10 @@ public protocol LambdaStringHandler: LambdaHandler {
5353
func handle(context: LambdaContext, payload: String, callback: @escaping LambdaStringCallback)
5454
}
5555

56-
/// Default implementation of `String` -> `[UInt8]` encoding and `[UInt8]` -> `String' decoding
56+
/// Default implementation of `String` -> `[UInt8]` encoding and `[UInt8]` -> `String' decoding
5757
public extension LambdaStringHandler {
5858
func handle(context: LambdaContext, payload: [UInt8], callback: @escaping LambdaCallback) {
59-
guard let payloadAsString = String(bytes: payload, encoding: .utf8) else {
60-
return callback(.failure("failed casting payload to String"))
61-
}
62-
self.handle(context: context, payload: payloadAsString, callback: { result in
59+
self.handle(context: context, payload: String(decoding: payload, as: UTF8.self), callback: { result in
6360
switch result {
6461
case let .success(string):
6562
return callback(.success([UInt8](string.utf8)))

Sources/SwiftAwsLambda/Lambda.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public enum Lambda {
139139
}
140140

141141
/// A result type for a Lambda that returns a `[UInt8]`.
142-
public typealias LambdaResult = Result<[UInt8], String>
142+
public typealias LambdaResult = ResultType<[UInt8], String>
143143

144144
public typealias LambdaCallback = (LambdaResult) -> Void
145145

@@ -169,7 +169,7 @@ public struct LambdaContext {
169169
}
170170
}
171171

172-
internal typealias LambdaLifecycleResult = Result<Int, Error>
172+
internal typealias LambdaLifecycleResult = ResultType<Int, Error>
173173

174174
private struct LambdaClosureWrapper: LambdaHandler {
175175
private let closure: LambdaClosure

Sources/SwiftAwsLambda/LambdaRunner.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal final class LambdaRunner {
6363
}
6464
}
6565

66-
internal typealias LambdaRunResult = Result<(), Error>
66+
internal typealias LambdaRunResult = ResultType<(), Error>
6767

6868
private extension LambdaHandler {
6969
func handle(context: LambdaContext, payload: [UInt8], promise: EventLoopPromise<LambdaResult>) {

Sources/SwiftAwsLambda/LambdaRuntimeClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ internal class LambdaRuntimeClient {
7676
}
7777
}
7878

79-
internal typealias RequestWorkResult = Result<(LambdaContext, [UInt8]), LambdaRuntimeClientError>
80-
internal typealias PostResultsResult = Result<(), LambdaRuntimeClientError>
79+
internal typealias RequestWorkResult = ResultType<(LambdaContext, [UInt8]), LambdaRuntimeClientError>
80+
internal typealias PostResultsResult = ResultType<(), LambdaRuntimeClientError>
8181

8282
internal enum LambdaRuntimeClientError: Error {
8383
case badStatusCode(HTTPResponseStatus)

Sources/SwiftAwsLambda/Utils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Dispatch
1616
import NIO
1717

1818
/// Genric result type, that is used throught the library.
19-
public enum Result<Value, Error> {
19+
public enum ResultType<Value, Error> {
2020
case success(Value)
2121
case failure(Error)
2222
}

Tests/SwiftAwsLambdaTests/MockLambdaServer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ internal protocol LambdaServerBehavior {
189189
func processError(requestId: String, error: ErrorResponse) -> ProcessErrorResult
190190
}
191191

192-
internal typealias GetWorkResult = Result<(String, String), GetWorkError>
192+
internal typealias GetWorkResult = ResultType<(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 = Result<(), ProcessError>
212+
internal typealias ProcessErrorResult = ResultType<(), ProcessError>
213213

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

0 commit comments

Comments
 (0)