Skip to content

Commit c1aebc8

Browse files
committed
use type system
1 parent 23bf3e1 commit c1aebc8

16 files changed

+222
-234
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import PackageDescription
44

55
let package = Package(
66
name: "swift-aws-lambda-runtime",
7+
platforms: [ .macOS(.v10_15) ],
78
products: [
89
// this library exports `AWSLambdaRuntimeCore` and adds Foundation convenience methods
910
.library(name: "AWSLambdaRuntime", targets: ["AWSLambdaRuntime"]),

Package@swift-5.5.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.4
1+
// swift-tools-version:5.5
22

33
import PackageDescription
44

Package@swift-5.4.swift renamed to Package@swift-5.6.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.4
1+
// swift-tools-version:5.6
22

33
import PackageDescription
44

Sources/AWSLambdaRuntime/Lambda+Codable.swift

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import NIOFoundationCompat
2121

2222
// MARK: - LambdaHandler Codable support
2323

24-
#if compiler(>=5.5) && canImport(_Concurrency)
2524
/// Implementation of a`ByteBuffer` to `Event` decoding.
2625
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
2726
extension LambdaHandler where Event: Decodable {
@@ -35,8 +34,8 @@ extension LambdaHandler where Event: Decodable {
3534
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
3635
extension LambdaHandler where Output: Encodable {
3736
@inlinable
38-
public func encode(allocator: ByteBufferAllocator, value: Output) throws -> ByteBuffer? {
39-
try self.encoder.encode(value, using: allocator)
37+
public func encode(value: Output, into buffer: inout ByteBuffer) throws {
38+
try self.encoder.encode(value, into: &buffer)
4039
}
4140
}
4241

@@ -57,7 +56,6 @@ extension LambdaHandler where Output: Encodable {
5756
Lambda.defaultJSONEncoder
5857
}
5958
}
60-
#endif
6159

6260
// MARK: - EventLoopLambdaHandler Codable support
6361

@@ -72,8 +70,8 @@ extension EventLoopLambdaHandler where Event: Decodable {
7270
/// Implementation of `Output` to `ByteBuffer` encoding.
7371
extension EventLoopLambdaHandler where Output: Encodable {
7472
@inlinable
75-
public func encode(allocator: ByteBufferAllocator, value: Output) throws -> ByteBuffer? {
76-
try self.encoder.encode(value, using: allocator)
73+
public func encode(value: Output, into buffer: inout ByteBuffer) throws {
74+
try self.encoder.encode(value, into: &buffer)
7775
}
7876
}
7977

@@ -98,7 +96,7 @@ public protocol LambdaCodableDecoder {
9896
}
9997

10098
public protocol LambdaCodableEncoder {
101-
func encode<T: Encodable>(_ value: T, using allocator: ByteBufferAllocator) throws -> ByteBuffer
99+
func encode<T: Encodable>(_ value: T, into buffer: inout ByteBuffer) throws
102100
}
103101

104102
extension Lambda {
@@ -108,11 +106,4 @@ extension Lambda {
108106

109107
extension JSONDecoder: LambdaCodableDecoder {}
110108

111-
extension JSONEncoder: LambdaCodableEncoder {
112-
public func encode<T>(_ value: T, using allocator: ByteBufferAllocator) throws -> ByteBuffer where T: Encodable {
113-
// nio will resize the buffer if necessary
114-
var buffer = allocator.buffer(capacity: 1024)
115-
try self.encode(value, into: &buffer)
116-
return buffer
117-
}
118-
}
109+
extension JSONEncoder: LambdaCodableEncoder {}

Sources/AWSLambdaRuntimeCore/Lambda+String.swift

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,46 @@
1313
//===----------------------------------------------------------------------===//
1414
import NIOCore
1515

16-
extension EventLoopLambdaHandler where Event == String {
16+
// MARK: - LambdaHandler String support
17+
18+
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
19+
extension LambdaHandler where Event == String {
1720
/// Implementation of a `ByteBuffer` to `String` decoding.
1821
@inlinable
1922
public func decode(buffer: ByteBuffer) throws -> String {
20-
var buffer = buffer
21-
guard let string = buffer.readString(length: buffer.readableBytes) else {
22-
fatalError("buffer.readString(length: buffer.readableBytes) failed")
23+
guard let value = buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) else {
24+
throw CodecError.invalidString
2325
}
24-
return string
26+
return value
2527
}
2628
}
2729

28-
extension EventLoopLambdaHandler where Output == String {
30+
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
31+
extension LambdaHandler where Output == String {
2932
/// Implementation of `String` to `ByteBuffer` encoding.
3033
@inlinable
31-
public func encode(allocator: ByteBufferAllocator, value: String) throws -> ByteBuffer? {
32-
// FIXME: reusable buffer
33-
var buffer = allocator.buffer(capacity: value.utf8.count)
34+
public func encode(value: String, into buffer: inout ByteBuffer) throws {
35+
buffer.writeString(value)
36+
}
37+
}
38+
39+
// MARK: - EventLoopLambdaHandler String support
40+
41+
extension EventLoopLambdaHandler where Event == String {
42+
/// Implementation of `String` to `ByteBuffer` encoding.
43+
@inlinable
44+
public func decode(buffer: ByteBuffer) throws -> String {
45+
guard let value = buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) else {
46+
throw CodecError.invalidString
47+
}
48+
return value
49+
}
50+
}
51+
52+
extension EventLoopLambdaHandler where Output == String {
53+
/// Implementation of a `ByteBuffer` to `String` decoding.
54+
@inlinable
55+
public func encode(value: String, into buffer: inout ByteBuffer) throws {
3456
buffer.writeString(value)
35-
return buffer
3657
}
3758
}

Sources/AWSLambdaRuntimeCore/Lambda.swift

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,24 @@ public enum Lambda {
3434
///
3535
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
3636

37-
internal static func run<Handler: ByteBufferLambdaHandler>(
37+
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
38+
internal static func run<Handler: LambdaHandler>(
3839
configuration: LambdaConfiguration = .init(),
3940
handlerType: Handler.Type
4041
) -> Result<Int, Error> {
41-
Self.run(configuration: configuration, initializationHandler: Handler.makeHandler(context:))
42+
Self.run(configuration: configuration, handlerType: CodableLambdaHandler<Handler, Handler.Event, Handler.Output>.self)
4243
}
4344

4445
internal static func run<Handler: EventLoopLambdaHandler>(
4546
configuration: LambdaConfiguration = .init(),
4647
handlerType: Handler.Type
4748
) -> Result<Int, Error> {
48-
Self.run(configuration: configuration, initializationHandler: Handler.makeHandler(context:))
49+
Self.run(configuration: configuration, handlerType: CodableEventLoopLambdaHandler<Handler, Handler.Event, Handler.Output>.self)
4950
}
5051

51-
@available(macOS 12, *)
52-
internal static func run<Handler: LambdaHandler>(
52+
internal static func run<Handler: ByteBufferLambdaHandler>(
5353
configuration: LambdaConfiguration = .init(),
5454
handlerType: Handler.Type
55-
) -> Result<Int, Error> {
56-
Self.run(configuration: configuration, initializationHandler: Handler.makeHandler(context:))
57-
58-
}
59-
private static func run(
60-
configuration: LambdaConfiguration = .init(),
61-
initializationHandler: @escaping _InitializationHandler
6255
) -> Result<Int, Error> {
6356
let _run = { (configuration: LambdaConfiguration) -> Result<Int, Error> in
6457
Backtrace.install()
@@ -67,7 +60,7 @@ public enum Lambda {
6760

6861
var result: Result<Int, Error>!
6962
MultiThreadedEventLoopGroup.withCurrentThreadAsEventLoop { eventLoop in
70-
let runtime = LambdaRuntime(eventLoop: eventLoop, logger: logger, configuration: configuration, initializationHandler: initializationHandler)
63+
let runtime = LambdaRuntime(handlerType, eventLoop: eventLoop, logger: logger, configuration: configuration)
7164
#if DEBUG
7265
let signalSource = trap(signal: configuration.lifecycle.stopSignal) { signal in
7366
logger.info("intercepted signal: \(signal)")
@@ -89,7 +82,6 @@ public enum Lambda {
8982
result = lifecycleResult
9083
}
9184
}
92-
9385
logger.info("shutdown completed")
9486
return result
9587
}

0 commit comments

Comments
 (0)