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
This commit is contained in:
Tom Doron
2018-12-03 18:25:46 -08:00
committed by GitHub Enterprise
parent e3f3a979ee
commit 20309e2ad7
7 changed files with 14 additions and 17 deletions
+3 -3
View File
@@ -27,7 +27,7 @@ extension Lambda {
/// Run a Lambda defined by implementing the `LambdaCodableHandler` protocol, having `In` and `Out` are `Decodable` and `Encodable` respectively.
///
/// - note: This is a blocking operation that will run forever, as it's lifecycle is managed by the AWS Lambda Runtime Engine.
public static func run<T>(_ handler: T) where T: LambdaCodableHandler {
public static func run<Handler>(_ handler: Handler) where Handler: LambdaCodableHandler {
self.run(handler as LambdaHandler)
}
@@ -37,13 +37,13 @@ extension Lambda {
}
// for testing
internal static func run<T>(handler: T, maxTimes: Int = 0) -> LambdaLifecycleResult where T: LambdaCodableHandler {
internal static func run<Handler>(handler: Handler, maxTimes: Int = 0) -> LambdaLifecycleResult where Handler: LambdaCodableHandler {
return self.run(handler: handler as LambdaHandler, maxTimes: maxTimes)
}
}
/// A result type for a Lambda that returns a generic `Out`, having `Out` extend `Encodable`.
public typealias LambdaCodableResult<Out> = Result<Out, String>
public typealias LambdaCodableResult<Out> = ResultType<Out, String>
/// A callback for a Lambda that returns a `LambdaCodableResult<Out>` result type, having `Out` extend `Encodable`.
public typealias LambdaCodableCallback<Out> = (LambdaCodableResult<Out>) -> Void
+3 -6
View File
@@ -40,7 +40,7 @@ extension Lambda {
}
/// A result type for a Lambda that returns a `String`.
public typealias LambdaStringResult = Result<String, String>
public typealias LambdaStringResult = ResultType<String, String>
/// A callback for a Lambda that returns a `LambdaStringResult` result type.
public typealias LambdaStringCallback = (LambdaStringResult) -> Void
@@ -53,13 +53,10 @@ public protocol LambdaStringHandler: LambdaHandler {
func handle(context: LambdaContext, payload: String, callback: @escaping LambdaStringCallback)
}
/// Default implementation of `String` -> `[UInt8]` encoding and `[UInt8]` -> `String' decoding
/// Default implementation of `String` -> `[UInt8]` encoding and `[UInt8]` -> `String' decoding
public extension LambdaStringHandler {
func handle(context: LambdaContext, payload: [UInt8], callback: @escaping LambdaCallback) {
guard let payloadAsString = String(bytes: payload, encoding: .utf8) else {
return callback(.failure("failed casting payload to String"))
}
self.handle(context: context, payload: payloadAsString, callback: { result in
self.handle(context: context, payload: String(decoding: payload, as: UTF8.self), callback: { result in
switch result {
case let .success(string):
return callback(.success([UInt8](string.utf8)))
+2 -2
View File
@@ -139,7 +139,7 @@ public enum Lambda {
}
/// A result type for a Lambda that returns a `[UInt8]`.
public typealias LambdaResult = Result<[UInt8], String>
public typealias LambdaResult = ResultType<[UInt8], String>
public typealias LambdaCallback = (LambdaResult) -> Void
@@ -169,7 +169,7 @@ public struct LambdaContext {
}
}
internal typealias LambdaLifecycleResult = Result<Int, Error>
internal typealias LambdaLifecycleResult = ResultType<Int, Error>
private struct LambdaClosureWrapper: LambdaHandler {
private let closure: LambdaClosure
+1 -1
View File
@@ -63,7 +63,7 @@ internal final class LambdaRunner {
}
}
internal typealias LambdaRunResult = Result<(), Error>
internal typealias LambdaRunResult = ResultType<(), Error>
private extension LambdaHandler {
func handle(context: LambdaContext, payload: [UInt8], promise: EventLoopPromise<LambdaResult>) {
@@ -76,8 +76,8 @@ internal class LambdaRuntimeClient {
}
}
internal typealias RequestWorkResult = Result<(LambdaContext, [UInt8]), LambdaRuntimeClientError>
internal typealias PostResultsResult = Result<(), LambdaRuntimeClientError>
internal typealias RequestWorkResult = ResultType<(LambdaContext, [UInt8]), LambdaRuntimeClientError>
internal typealias PostResultsResult = ResultType<(), LambdaRuntimeClientError>
internal enum LambdaRuntimeClientError: Error {
case badStatusCode(HTTPResponseStatus)
+1 -1
View File
@@ -16,7 +16,7 @@ import Dispatch
import NIO
/// Genric result type, that is used throught the library.
public enum Result<Value, Error> {
public enum ResultType<Value, Error> {
case success(Value)
case failure(Error)
}
@@ -189,7 +189,7 @@ internal protocol LambdaServerBehavior {
func processError(requestId: String, error: ErrorResponse) -> ProcessErrorResult
}
internal typealias GetWorkResult = Result<(String, String), GetWorkError>
internal typealias GetWorkResult = ResultType<(String, String), GetWorkError>
internal enum GetWorkError: Int {
case badRequest = 400
@@ -209,7 +209,7 @@ internal enum ProcessResponseError: Int {
case internalServerError = 500
}
internal typealias ProcessErrorResult = Result<(), ProcessError>
internal typealias ProcessErrorResult = ResultType<(), ProcessError>
internal enum ProcessError: Int, Error {
case invalidErrorShape = 299