Skip to content

Commit 88f3b50

Browse files
authored
swift 5.2 (#37)
motivation: amazon linux support will land in 5.2 changes: * remove inline as its default in 5.2 * remove linux test and lean on --enable-test-discovery * adjust package syntax * format code to match 5.2
1 parent e410413 commit 88f3b50

31 files changed

+72
-597
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Package.pins
88
Package.resolved
99
.podspecs
1010
.swiftpm
11+
.swift-version

Package.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.0
1+
// swift-tools-version:5.2
22

33
import PackageDescription
44

@@ -13,12 +13,19 @@ let package = Package(
1313
.package(url: "https://github.com/swift-server/swift-backtrace.git", from: "1.1.0"),
1414
],
1515
targets: [
16-
.target(name: "SwiftAwsLambda", dependencies: ["Logging", "Backtrace", "NIOHTTP1", "NIOFoundationCompat"]),
16+
.target(name: "SwiftAwsLambda", dependencies: [
17+
.product(name: "Logging", package: "swift-log"),
18+
.product(name: "Backtrace", package: "swift-backtrace"),
19+
.product(name: "NIOHTTP1", package: "swift-nio"),
20+
.product(name: "NIOFoundationCompat", package: "swift-nio"),
21+
]),
1722
.testTarget(name: "SwiftAwsLambdaTests", dependencies: ["SwiftAwsLambda"]),
1823
// samples
1924
.target(name: "SwiftAwsLambdaStringSample", dependencies: ["SwiftAwsLambda"]),
2025
.target(name: "SwiftAwsLambdaCodableSample", dependencies: ["SwiftAwsLambda"]),
2126
// perf tests
22-
.target(name: "MockServer", dependencies: ["Logging", "NIOHTTP1"]),
27+
.target(name: "MockServer", dependencies: [
28+
.product(name: "NIOHTTP1", package: "swift-nio"),
29+
]),
2330
]
2431
)

Sources/SwiftAwsLambda/HttpClient.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ internal final class HTTPClient {
3434
}
3535

3636
func get(url: String, timeout: TimeAmount? = nil) -> EventLoopFuture<Response> {
37-
return self.execute(Request(targetHost: self.targetHost,
38-
url: url,
39-
method: .GET,
40-
timeout: timeout ?? self.configuration.requestTimeout))
37+
self.execute(Request(targetHost: self.targetHost,
38+
url: url,
39+
method: .GET,
40+
timeout: timeout ?? self.configuration.requestTimeout))
4141
}
4242

4343
func post(url: String, body: ByteBuffer?, timeout: TimeAmount? = nil) -> EventLoopFuture<Response> {
44-
return self.execute(Request(targetHost: self.targetHost,
45-
url: url,
46-
method: .POST,
47-
body: body,
48-
timeout: timeout ?? self.configuration.requestTimeout))
44+
self.execute(Request(targetHost: self.targetHost,
45+
url: url,
46+
method: .POST,
47+
body: body,
48+
timeout: timeout ?? self.configuration.requestTimeout))
4949
}
5050

5151
// TODO: cap reconnect attempt

Sources/SwiftAwsLambda/Lambda+Codable.swift

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,27 @@ extension Lambda {
2222
/// Run a Lambda defined by implementing the `CodableLambdaClosure` function.
2323
///
2424
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
25-
@inlinable
2625
public static func run<In: Decodable, Out: Encodable>(_ closure: @escaping CodableLambdaClosure<In, Out>) {
2726
self.run(closure: closure)
2827
}
2928

3029
/// Run a Lambda defined by implementing the `CodableVoidLambdaClosure` function.
3130
///
3231
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
33-
@inlinable
3432
public static func run<In: Decodable>(_ closure: @escaping CodableVoidLambdaClosure<In>) {
3533
self.run(closure: closure)
3634
}
3735

3836
// for testing
39-
@inlinable
4037
@discardableResult
4138
internal static func run<In: Decodable, Out: Encodable>(configuration: Configuration = .init(), closure: @escaping CodableLambdaClosure<In, Out>) -> Result<Int, Error> {
42-
return self.run(configuration: configuration, handler: CodableLambdaClosureWrapper(closure))
39+
self.run(configuration: configuration, handler: CodableLambdaClosureWrapper(closure))
4340
}
4441

4542
// for testing
46-
@inlinable
4743
@discardableResult
4844
internal static func run<In: Decodable>(configuration: Configuration = .init(), closure: @escaping CodableVoidLambdaClosure<In>) -> Result<Int, Error> {
49-
return self.run(configuration: configuration, handler: CodableVoidLambdaClosureWrapper(closure))
45+
self.run(configuration: configuration, handler: CodableVoidLambdaClosureWrapper(closure))
5046
}
5147
}
5248

@@ -56,41 +52,31 @@ public typealias CodableLambdaClosure<In: Decodable, Out: Encodable> = (Lambda.C
5652
/// A processing closure for a Lambda that takes a `In` and returns a `Result<Void, Error>` via a `CompletionHandler` asynchronously.
5753
public typealias CodableVoidLambdaClosure<In: Decodable> = (Lambda.Context, In, @escaping (Result<Void, Error>) -> Void) -> Void
5854

59-
@usableFromInline
6055
internal struct CodableLambdaClosureWrapper<In: Decodable, Out: Encodable>: LambdaHandler {
61-
@usableFromInline
6256
typealias In = In
63-
@usableFromInline
6457
typealias Out = Out
6558

6659
private let closure: CodableLambdaClosure<In, Out>
6760

68-
@usableFromInline
6961
init(_ closure: @escaping CodableLambdaClosure<In, Out>) {
7062
self.closure = closure
7163
}
7264

73-
@usableFromInline
7465
func handle(context: Lambda.Context, payload: In, callback: @escaping (Result<Out, Error>) -> Void) {
7566
self.closure(context, payload, callback)
7667
}
7768
}
7869

79-
@usableFromInline
8070
internal struct CodableVoidLambdaClosureWrapper<In: Decodable>: LambdaHandler {
81-
@usableFromInline
8271
typealias In = In
83-
@usableFromInline
8472
typealias Out = Void
8573

8674
private let closure: CodableVoidLambdaClosure<In>
8775

88-
@usableFromInline
8976
init(_ closure: @escaping CodableVoidLambdaClosure<In>) {
9077
self.closure = closure
9178
}
9279

93-
@usableFromInline
9480
func handle(context: Lambda.Context, payload: In, callback: @escaping (Result<Out, Error>) -> Void) {
9581
self.closure(context, payload, callback)
9682
}
@@ -110,17 +96,17 @@ public extension EventLoopLambdaHandler where In: Decodable, Out: Encodable {
11096

11197
func decode(buffer: ByteBuffer) throws -> In {
11298
// FIXME: reusable JSONDecoder
113-
return try JSONDecoder().decode(In.self, from: buffer)
99+
try JSONDecoder().decode(In.self, from: buffer)
114100
}
115101
}
116102

117103
public extension EventLoopLambdaHandler where In: Decodable, Out == Void {
118104
func encode(allocator: ByteBufferAllocator, value: Void) throws -> ByteBuffer? {
119-
return nil
105+
nil
120106
}
121107

122108
func decode(buffer: ByteBuffer) throws -> In {
123109
// FIXME: reusable JSONDecoder
124-
return try JSONDecoder().decode(In.self, from: buffer)
110+
try JSONDecoder().decode(In.self, from: buffer)
125111
}
126112
}

Sources/SwiftAwsLambda/Lambda+String.swift

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,27 @@ extension Lambda {
1818
/// Run a Lambda defined by implementing the `StringLambdaClosure` function.
1919
///
2020
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
21-
@inlinable
2221
public static func run(_ closure: @escaping StringLambdaClosure) {
2322
self.run(closure: closure)
2423
}
2524

2625
/// Run a Lambda defined by implementing the `StringVoidLambdaClosure` function.
2726
///
2827
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
29-
@inlinable
3028
public static func run(_ closure: @escaping StringVoidLambdaClosure) {
3129
self.run(closure: closure)
3230
}
3331

3432
// for testing
35-
@inlinable
3633
@discardableResult
3734
internal static func run(configuration: Configuration = .init(), closure: @escaping StringLambdaClosure) -> Result<Int, Error> {
38-
return self.run(configuration: configuration, handler: StringLambdaClosureWrapper(closure))
35+
self.run(configuration: configuration, handler: StringLambdaClosureWrapper(closure))
3936
}
4037

4138
// for testing
42-
@inlinable
4339
@discardableResult
4440
internal static func run(configuration: Configuration = .init(), closure: @escaping StringVoidLambdaClosure) -> Result<Int, Error> {
45-
return self.run(configuration: configuration, handler: StringVoidLambdaClosureWrapper(closure))
41+
self.run(configuration: configuration, handler: StringVoidLambdaClosureWrapper(closure))
4642
}
4743
}
4844

@@ -52,41 +48,31 @@ public typealias StringLambdaClosure = (Lambda.Context, String, @escaping (Resul
5248
/// A processing closure for a Lambda that takes a `String` and returns a `Result<Void, Error>` via a `CompletionHandler` asynchronously.
5349
public typealias StringVoidLambdaClosure = (Lambda.Context, String, @escaping (Result<Void, Error>) -> Void) -> Void
5450

55-
@usableFromInline
5651
internal struct StringLambdaClosureWrapper: LambdaHandler {
57-
@usableFromInline
5852
typealias In = String
59-
@usableFromInline
6053
typealias Out = String
6154

6255
private let closure: StringLambdaClosure
6356

64-
@usableFromInline
6557
init(_ closure: @escaping StringLambdaClosure) {
6658
self.closure = closure
6759
}
6860

69-
@usableFromInline
7061
func handle(context: Lambda.Context, payload: In, callback: @escaping (Result<Out, Error>) -> Void) {
7162
self.closure(context, payload, callback)
7263
}
7364
}
7465

75-
@usableFromInline
7666
internal struct StringVoidLambdaClosureWrapper: LambdaHandler {
77-
@usableFromInline
7867
typealias In = String
79-
@usableFromInline
8068
typealias Out = Void
8169

8270
private let closure: StringVoidLambdaClosure
8371

84-
@usableFromInline
8572
init(_ closure: @escaping StringVoidLambdaClosure) {
8673
self.closure = closure
8774
}
8875

89-
@usableFromInline
9076
func handle(context: Lambda.Context, payload: In, callback: @escaping (Result<Out, Error>) -> Void) {
9177
self.closure(context, payload, callback)
9278
}
@@ -112,7 +98,7 @@ public extension EventLoopLambdaHandler where In == String, Out == String {
11298

11399
public extension EventLoopLambdaHandler where In == String, Out == Void {
114100
func encode(allocator: ByteBufferAllocator, value: Void) throws -> ByteBuffer? {
115-
return nil
101+
nil
116102
}
117103

118104
func decode(buffer: ByteBuffer) throws -> String {

Sources/SwiftAwsLambda/Lambda.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,34 @@ public enum Lambda {
2626
/// Run a Lambda defined by implementing the `LambdaHandler` protocol.
2727
///
2828
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
29-
@inlinable
3029
public static func run(_ handler: ByteBufferLambdaHandler) {
3130
self.run(handler: handler)
3231
}
3332

3433
/// Run a Lambda defined by implementing the `LambdaHandler` protocol provided via a `LambdaHandlerFactory`.
3534
///
3635
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
37-
@inlinable
3836
public static func run(_ factory: @escaping LambdaHandlerFactory) {
3937
self.run(factory: factory)
4038
}
4139

4240
/// Run a Lambda defined by implementing the `LambdaHandler` protocol provided via a factory, typically a constructor.
4341
///
4442
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
45-
@inlinable
4643
public static func run(_ factory: @escaping (EventLoop) throws -> ByteBufferLambdaHandler) {
4744
self.run(factory: factory)
4845
}
4946

5047
// for testing and internal use
51-
@inlinable
5248
@discardableResult
5349
internal static func run(configuration: Configuration = .init(), handler: ByteBufferLambdaHandler) -> Result<Int, Error> {
54-
return self.run(configuration: configuration, factory: { $0.makeSucceededFuture(handler) })
50+
self.run(configuration: configuration, factory: { $0.makeSucceededFuture(handler) })
5551
}
5652

5753
// for testing and internal use
58-
@inlinable
5954
@discardableResult
6055
internal static func run(configuration: Configuration = .init(), factory: @escaping (EventLoop) throws -> ByteBufferLambdaHandler) -> Result<Int, Error> {
61-
return self.run(configuration: configuration, factory: { eventloop -> EventLoopFuture<ByteBufferLambdaHandler> in
56+
self.run(configuration: configuration, factory: { eventloop -> EventLoopFuture<ByteBufferLambdaHandler> in
6257
do {
6358
let handler = try factory(eventloop)
6459
return eventloop.makeSucceededFuture(handler)
@@ -69,7 +64,6 @@ public enum Lambda {
6964
}
7065

7166
// for testing and internal use
72-
@inlinable
7367
@discardableResult
7468
internal static func run(configuration: Configuration = .init(), factory: @escaping LambdaHandlerFactory) -> Result<Int, Error> {
7569
do {
@@ -82,7 +76,6 @@ public enum Lambda {
8276
}
8377
}
8478

85-
@usableFromInline
8679
internal static func runAsync(eventLoopGroup: EventLoopGroup, configuration: Configuration, factory: @escaping LambdaHandlerFactory) -> EventLoopFuture<Int> {
8780
Backtrace.install()
8881
var logger = Logger(label: "Lambda")

Sources/SwiftAwsLambda/LambdaConfiguration.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ import Logging
1717
import NIO
1818

1919
extension Lambda {
20-
@usableFromInline
2120
internal struct Configuration: CustomStringConvertible {
2221
let general: General
2322
let lifecycle: Lifecycle
2423
let runtimeEngine: RuntimeEngine
2524

26-
@usableFromInline
2725
init() {
2826
self.init(general: .init(), lifecycle: .init(), runtimeEngine: .init())
2927
}
@@ -42,7 +40,7 @@ extension Lambda {
4240
}
4341

4442
var description: String {
45-
return "\(General.self)(logLevel: \(self.logLevel))"
43+
"\(General.self)(logLevel: \(self.logLevel))"
4644
}
4745
}
4846

@@ -59,7 +57,7 @@ extension Lambda {
5957
}
6058

6159
var description: String {
62-
return "\(Lifecycle.self)(id: \(self.id), maxTimes: \(self.maxTimes), stopSignal: \(self.stopSignal))"
60+
"\(Lifecycle.self)(id: \(self.id), maxTimes: \(self.maxTimes), stopSignal: \(self.stopSignal))"
6361
}
6462
}
6563

@@ -81,13 +79,12 @@ extension Lambda {
8179
}
8280

8381
var description: String {
84-
return "\(RuntimeEngine.self)(ip: \(self.ip), port: \(self.port), keepAlive: \(self.keepAlive), requestTimeout: \(String(describing: self.requestTimeout))"
82+
"\(RuntimeEngine.self)(ip: \(self.ip), port: \(self.port), keepAlive: \(self.keepAlive), requestTimeout: \(String(describing: self.requestTimeout))"
8583
}
8684
}
8785

88-
@usableFromInline
8986
var description: String {
90-
return "\(Configuration.self)\n \(self.general))\n \(self.lifecycle)\n \(self.runtimeEngine)"
87+
"\(Configuration.self)\n \(self.general))\n \(self.lifecycle)\n \(self.runtimeEngine)"
9188
}
9289
}
9390
}

Sources/SwiftAwsLambda/LambdaLifecycle.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import NIO
1717
import NIOConcurrencyHelpers
1818

1919
extension Lambda {
20-
@usableFromInline
2120
internal final class Lifecycle {
2221
private let eventLoop: EventLoop
2322
private let logger: Logger
@@ -42,7 +41,7 @@ extension Lambda {
4241

4342
private var state: State {
4443
get {
45-
return self.stateLock.withLock {
44+
self.stateLock.withLock {
4645
self._state
4746
}
4847
}

Sources/SwiftAwsLambda/LambdaRunner.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ private extension Lambda.Context {
8989
private extension EventLoopFuture {
9090
// callback does not have side effects, failing with original result
9191
func peekError(_ callback: @escaping (Error) -> Void) -> EventLoopFuture<Value> {
92-
return self.flatMapError { error in
92+
self.flatMapError { error in
9393
callback(error)
9494
return self
9595
}
9696
}
9797

9898
// callback does not have side effects, failing with original result
9999
func peekError(_ callback: @escaping (Error) -> EventLoopFuture<Void>) -> EventLoopFuture<Value> {
100-
return self.flatMapError { error in
100+
self.flatMapError { error in
101101
let promise = self.eventLoop.makePromise(of: Value.self)
102102
callback(error).whenComplete { _ in
103103
promise.completeWith(self)
@@ -107,7 +107,7 @@ private extension EventLoopFuture {
107107
}
108108

109109
func mapResult<NewValue>(_ callback: @escaping (Result<Value, Error>) -> NewValue) -> EventLoopFuture<NewValue> {
110-
return self.map { value in
110+
self.map { value in
111111
callback(.success(value))
112112
}.flatMapErrorThrowing { error in
113113
callback(.failure(error))

0 commit comments

Comments
 (0)