Skip to content

Commit d42ae69

Browse files
authored
Refactor the Swift Settings in Package.swift (#558)
- Use the new Swift 6 `@available` macro to remove requirement on `.platform` in Package.swift. - DRY: define the swift settings once for all in `Package.swift` ### Motivation: - Remove the requirement to build on macOS 15 in `Package.swift`. This allows library builders and end users to be more flexible on their dependency requirements. - The code is optionally compiled on macOS 15 and Linux, but SPM don't enforce it anymore. - Avoid repeating ourself. Be sure the same settings are applied on all targets. ### Modifications: - Create a `var swiftSetting: [SwiftSettings]` and reuse it for all targets. - Use `AvailabilityMacro=LambdaSwift 2.0:macOS 15.0` - Add this on top of the majority struct / classes ```swift #if swift(>=6.1) @available(LambdaSwift 2.0, *) #endif ``` ### Result: When using Swift 6.1, there is no more SPM dependency on macOS 15
1 parent ec28c96 commit d42ae69

35 files changed

+148
-29
lines changed

Package.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
import PackageDescription
44

5+
let defaultSwiftSettings: [SwiftSetting] =
6+
[
7+
.enableExperimentalFeature(
8+
"AvailabilityMacro=LambdaSwift 2.0:macOS 15.0"
9+
)
10+
]
11+
512
let package = Package(
613
name: "swift-aws-lambda-runtime",
7-
platforms: [.macOS(.v15)],
814
products: [
915
.library(name: "AWSLambdaRuntime", targets: ["AWSLambdaRuntime"]),
1016
// plugin to package the lambda, creating an archive that can be uploaded to AWS
@@ -43,7 +49,8 @@ let package = Package(
4349
package: "swift-service-lifecycle",
4450
condition: .when(traits: ["ServiceLifecycleSupport"])
4551
),
46-
]
52+
],
53+
swiftSettings: defaultSwiftSettings
4754
),
4855
.plugin(
4956
name: "AWSLambdaPackager",
@@ -67,8 +74,10 @@ let package = Package(
6774
.byName(name: "AWSLambdaRuntime"),
6875
.product(name: "NIOTestUtils", package: "swift-nio"),
6976
.product(name: "NIOFoundationCompat", package: "swift-nio"),
70-
]
77+
],
78+
swiftSettings: defaultSwiftSettings
7179
),
80+
7281
// for perf testing
7382
.executableTarget(
7483
name: "MockServer",
@@ -77,7 +86,8 @@ let package = Package(
7786
.product(name: "NIOHTTP1", package: "swift-nio"),
7887
.product(name: "NIOCore", package: "swift-nio"),
7988
.product(name: "NIOPosix", package: "swift-nio"),
80-
]
89+
],
90+
swiftSettings: defaultSwiftSettings
8191
),
8292
]
8393
)

Package@swift-6.0.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
import PackageDescription
44

5+
let defaultSwiftSettings: [SwiftSetting] = [
6+
.define("FoundationJSONSupport"),
7+
.define("ServiceLifecycleSupport"),
8+
.define("LocalServerSupport"),
9+
.enableExperimentalFeature(
10+
"AvailabilityMacro=LambdaSwift 2.0:macOS 15.0"
11+
),
12+
]
13+
514
let package = Package(
615
name: "swift-aws-lambda-runtime",
7-
platforms: [.macOS(.v15)],
816
products: [
917
.library(name: "AWSLambdaRuntime", targets: ["AWSLambdaRuntime"]),
1018
// plugin to package the lambda, creating an archive that can be uploaded to AWS
@@ -28,11 +36,7 @@ let package = Package(
2836
.product(name: "NIOPosix", package: "swift-nio"),
2937
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
3038
],
31-
swiftSettings: [
32-
.define("FoundationJSONSupport"),
33-
.define("ServiceLifecycleSupport"),
34-
.define("LocalServerSupport"),
35-
]
39+
swiftSettings: defaultSwiftSettings
3640
),
3741
.plugin(
3842
name: "AWSLambdaPackager",
@@ -57,11 +61,7 @@ let package = Package(
5761
.product(name: "NIOTestUtils", package: "swift-nio"),
5862
.product(name: "NIOFoundationCompat", package: "swift-nio"),
5963
],
60-
swiftSettings: [
61-
.define("FoundationJSONSupport"),
62-
.define("ServiceLifecycleSupport"),
63-
.define("LocalServerSupport"),
64-
]
64+
swiftSettings: defaultSwiftSettings
6565
),
6666
// for perf testing
6767
.executableTarget(
@@ -71,7 +71,8 @@ let package = Package(
7171
.product(name: "NIOHTTP1", package: "swift-nio"),
7272
.product(name: "NIOCore", package: "swift-nio"),
7373
.product(name: "NIOPosix", package: "swift-nio"),
74-
]
74+
],
75+
swiftSettings: defaultSwiftSettings
7576
),
7677
]
7778
)

Sources/AWSLambdaRuntime/ControlPlaneRequest.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@
1515
import NIOCore
1616
import NIOHTTP1
1717

18+
@available(LambdaSwift 2.0, *)
1819
enum ControlPlaneRequest: Hashable {
1920
case next
2021
case invocationResponse(String, ByteBuffer?)
2122
case invocationError(String, ErrorResponse)
2223
case initializationError(ErrorResponse)
2324
}
2425

26+
@available(LambdaSwift 2.0, *)
2527
enum ControlPlaneResponse: Hashable {
2628
case next(InvocationMetadata, ByteBuffer)
2729
case accepted
2830
case error(ErrorResponse)
2931
}
3032

3133
@usableFromInline
34+
@available(LambdaSwift 2.0, *)
3235
package struct InvocationMetadata: Hashable, Sendable {
3336
@usableFromInline
3437
package let requestID: String

Sources/AWSLambdaRuntime/ControlPlaneRequestEncoder.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import NIOCore
1616

17+
@available(LambdaSwift 2.0, *)
1718
struct ControlPlaneRequestEncoder: _EmittingChannelHandler {
1819
typealias OutboundOut = ByteBuffer
1920

Sources/AWSLambdaRuntime/FoundationSupport/Context+Foundation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import FoundationEssentials
1919
import struct Foundation.Date
2020
#endif
2121

22+
@available(LambdaSwift 2.0, *)
2223
extension LambdaContext {
2324
/// Returns the deadline as a Date for the Lambda function execution.
2425
/// I'm not sure how usefull it is to have this as a Date, with only seconds precision,

Sources/AWSLambdaRuntime/FoundationSupport/Lambda+JSON.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public struct LambdaJSONOutputEncoder<Output: Encodable>: LambdaOutputEncoder {
5959
}
6060
}
6161

62+
@available(LambdaSwift 2.0, *)
6263
extension LambdaCodableAdapter {
6364
/// Initializes an instance given an encoder, decoder, and a handler with a non-`Void` output.
6465
/// - Parameters:
@@ -84,6 +85,7 @@ extension LambdaCodableAdapter {
8485
}
8586
}
8687

88+
@available(LambdaSwift 2.0, *)
8789
extension LambdaRuntime {
8890
/// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a non-`Void` return type**.
8991
/// - Parameters:

Sources/AWSLambdaRuntime/Lambda+Codable.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public struct VoidEncoder: LambdaOutputEncoder {
4848
}
4949

5050
/// Adapts a ``LambdaHandler`` conforming handler to conform to ``LambdaWithBackgroundProcessingHandler``.
51+
@available(LambdaSwift 2.0, *)
5152
public struct LambdaHandlerAdapter<
5253
Event: Decodable,
5354
Output,
@@ -80,6 +81,7 @@ public struct LambdaHandlerAdapter<
8081
}
8182

8283
/// Adapts a ``LambdaWithBackgroundProcessingHandler`` conforming handler to conform to ``StreamingLambdaHandler``.
84+
@available(LambdaSwift 2.0, *)
8385
public struct LambdaCodableAdapter<
8486
Handler: LambdaWithBackgroundProcessingHandler,
8587
Event: Decodable,

Sources/AWSLambdaRuntime/Lambda+LocalServer.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import Synchronization
3737
// )
3838
// }
3939
// }
40+
@available(LambdaSwift 2.0, *)
4041
extension Lambda {
4142
/// Execute code in the context of a mock Lambda server.
4243
///
@@ -84,6 +85,7 @@ extension Lambda {
8485
/// 1. POST /invoke - the client posts the event to the lambda function
8586
///
8687
/// This server passes the data received from /invoke POST request to the lambda function (GET /next) and then forwards the response back to the client.
88+
@available(LambdaSwift 2.0, *)
8789
internal struct LambdaHTTPServer {
8890
private let invocationEndpoint: String
8991

Sources/AWSLambdaRuntime/Lambda.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import ucrt
2929
#error("Unsupported platform")
3030
#endif
3131

32+
@available(LambdaSwift 2.0, *)
3233
public enum Lambda {
3334
@inlinable
3435
package static func runLoop<RuntimeClient: LambdaRuntimeClientProtocol, Handler>(
@@ -98,6 +99,7 @@ public enum Lambda {
9899

99100
// MARK: - Public API
100101

102+
@available(LambdaSwift 2.0, *)
101103
extension Lambda {
102104
/// Utility to access/read environment variables
103105
public static func env(_ name: String) -> String? {

Sources/AWSLambdaRuntime/LambdaClock.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import ucrt
5151
/// The Lambda execution environment uses UTC as a timezone,
5252
/// `LambdaClock` operates in UTC and does not account for time zones.
5353
/// see: TZ in https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
54+
@available(LambdaSwift 2.0, *)
5455
public struct LambdaClock: Clock {
5556
public typealias Duration = Swift.Duration
5657

0 commit comments

Comments
 (0)