Skip to content

Commit 63de883

Browse files
committed
Apply package traits
1 parent 61cd5d5 commit 63de883

File tree

111 files changed

+304
-283
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+304
-283
lines changed

Package.swift

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:6.0
1+
// swift-tools-version:6.1
22

33
import PackageDescription
44

@@ -8,34 +8,38 @@ let package = Package(
88
products: [
99
// this library exports `AWSLambdaRuntimeCore` and adds Foundation convenience methods
1010
.library(name: "AWSLambdaRuntime", targets: ["AWSLambdaRuntime"]),
11-
// this has all the main functionality for lambda and it does not link Foundation
12-
.library(name: "AWSLambdaRuntimeCore", targets: ["AWSLambdaRuntimeCore"]),
1311
// plugin to package the lambda, creating an archive that can be uploaded to AWS
1412
// requires Linux or at least macOS v15
1513
.plugin(name: "AWSLambdaPackager", targets: ["AWSLambdaPackager"]),
1614
// for testing only
1715
.library(name: "AWSLambdaTesting", targets: ["AWSLambdaTesting"]),
1816
],
17+
traits: [
18+
"FoundationJSONSupport",
19+
"ServiceLifecycleSupport",
20+
"LocalServerSupport",
21+
.default(
22+
enabledTraits: [
23+
"FoundationJSONSupport",
24+
"ServiceLifecycleSupport",
25+
"LocalServerSupport",
26+
]
27+
)
28+
],
1929
dependencies: [
2030
.package(url: "https://github.com/apple/swift-nio.git", from: "2.81.0"),
2131
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.4"),
2232
.package(url: "https://github.com/apple/swift-collections.git", from: "1.1.4"),
33+
.package(url: "https://github.com/apple/swift-service-lifecycle.git", from: "2.6.3", traits: ["ServiceLifecycleSupport"]),
2334
],
2435
targets: [
2536
.target(
2637
name: "AWSLambdaRuntime",
2738
dependencies: [
28-
.byName(name: "AWSLambdaRuntimeCore"),
2939
.product(name: "NIOCore", package: "swift-nio"),
30-
]
31-
),
32-
.target(
33-
name: "AWSLambdaRuntimeCore",
34-
dependencies: [
3540
.product(name: "DequeModule", package: "swift-collections"),
3641
.product(name: "Logging", package: "swift-log"),
3742
.product(name: "NIOHTTP1", package: "swift-nio"),
38-
.product(name: "NIOCore", package: "swift-nio"),
3943
.product(name: "NIOPosix", package: "swift-nio"),
4044
]
4145
),
@@ -58,15 +62,15 @@ let package = Package(
5862
.testTarget(
5963
name: "AWSLambdaRuntimeCoreTests",
6064
dependencies: [
61-
.byName(name: "AWSLambdaRuntimeCore"),
65+
.byName(name: "AWSLambdaRuntime"),
6266
.product(name: "NIOTestUtils", package: "swift-nio"),
6367
.product(name: "NIOFoundationCompat", package: "swift-nio"),
6468
]
6569
),
6670
.testTarget(
6771
name: "AWSLambdaRuntimeTests",
6872
dependencies: [
69-
.byName(name: "AWSLambdaRuntimeCore"),
73+
// .byName(name: "AWSLambdaRuntimeCore"),
7074
.byName(name: "AWSLambdaRuntime"),
7175
]
7276
),

Sources/AWSLambdaRuntime/Context+Foundation.swift renamed to Sources/AWSLambdaRuntime/FoundationSupport/Context+Foundation.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import AWSLambdaRuntimeCore
16-
15+
#if FoundationJSONSupport
1716
#if canImport(FoundationEssentials)
1817
import FoundationEssentials
1918
#else
@@ -26,3 +25,4 @@ extension LambdaContext {
2625
return Date(timeIntervalSince1970: secondsSinceEpoch)
2726
}
2827
}
28+
#endif // trait: FoundationJSONSupport
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#if FoundationJSONSupport
16+
import NIOCore
17+
18+
#if canImport(FoundationEssentials)
19+
import FoundationEssentials
20+
#else
21+
import struct Foundation.Data
22+
import class Foundation.JSONDecoder
23+
import class Foundation.JSONEncoder
24+
#endif
25+
26+
public struct LambdaJSONEventDecoder: LambdaEventDecoder {
27+
@usableFromInline let jsonDecoder: JSONDecoder
28+
29+
@inlinable
30+
public init(_ jsonDecoder: JSONDecoder) {
31+
self.jsonDecoder = jsonDecoder
32+
}
33+
34+
@inlinable
35+
public func decode<Event>(_ type: Event.Type, from buffer: NIOCore.ByteBuffer) throws -> Event
36+
where Event: Decodable {
37+
try buffer.getJSONDecodable(
38+
Event.self,
39+
decoder: self.jsonDecoder,
40+
at: buffer.readerIndex,
41+
length: buffer.readableBytes
42+
)! // must work, enough readable bytes
43+
}
44+
}
45+
46+
public struct LambdaJSONOutputEncoder<Output: Encodable>: LambdaOutputEncoder {
47+
@usableFromInline let jsonEncoder: JSONEncoder
48+
49+
@inlinable
50+
public init(_ jsonEncoder: JSONEncoder) {
51+
self.jsonEncoder = jsonEncoder
52+
}
53+
54+
@inlinable
55+
public func encode(_ value: Output, into buffer: inout ByteBuffer) throws {
56+
try buffer.writeJSONEncodable(value, encoder: self.jsonEncoder)
57+
}
58+
}
59+
60+
extension LambdaCodableAdapter {
61+
/// Initializes an instance given an encoder, decoder, and a handler with a non-`Void` output.
62+
/// - Parameters:
63+
/// - encoder: The encoder object that will be used to encode the generic `Output` obtained from the `handler`'s `outputWriter` into a `ByteBuffer`. By default, a JSONEncoder is used.
64+
/// - decoder: The decoder object that will be used to decode the received `ByteBuffer` event into the generic `Event` type served to the `handler`. By default, a JSONDecoder is used.
65+
/// - handler: The handler object.
66+
public init(
67+
encoder: JSONEncoder = JSONEncoder(),
68+
decoder: JSONDecoder = JSONDecoder(),
69+
handler: sending Handler
70+
)
71+
where
72+
Output: Encodable,
73+
Output == Handler.Output,
74+
Encoder == LambdaJSONOutputEncoder<Output>,
75+
Decoder == LambdaJSONEventDecoder
76+
{
77+
self.init(
78+
encoder: LambdaJSONOutputEncoder(encoder),
79+
decoder: LambdaJSONEventDecoder(decoder),
80+
handler: handler
81+
)
82+
}
83+
}
84+
85+
extension LambdaRuntime {
86+
/// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a non-`Void` return type**.
87+
/// - Parameters:
88+
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
89+
/// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`. `JSONEncoder()` used as default.
90+
/// - body: The handler in the form of a closure.
91+
public convenience init<Event: Decodable, Output>(
92+
decoder: JSONDecoder = JSONDecoder(),
93+
encoder: JSONEncoder = JSONEncoder(),
94+
body: sending @escaping (Event, LambdaContext) async throws -> Output
95+
)
96+
where
97+
Handler == LambdaCodableAdapter<
98+
LambdaHandlerAdapter<Event, Output, ClosureHandler<Event, Output>>,
99+
Event,
100+
Output,
101+
LambdaJSONEventDecoder,
102+
LambdaJSONOutputEncoder<Output>
103+
>
104+
{
105+
let handler = LambdaCodableAdapter(
106+
encoder: encoder,
107+
decoder: decoder,
108+
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
109+
)
110+
111+
self.init(handler: handler)
112+
}
113+
114+
/// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a `Void` return type**.
115+
/// - Parameter body: The handler in the form of a closure.
116+
/// - Parameter decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
117+
public convenience init<Event: Decodable>(
118+
decoder: JSONDecoder = JSONDecoder(),
119+
body: sending @escaping (Event, LambdaContext) async throws -> Void
120+
)
121+
where
122+
Handler == LambdaCodableAdapter<
123+
LambdaHandlerAdapter<Event, Void, ClosureHandler<Event, Void>>,
124+
Event,
125+
Void,
126+
LambdaJSONEventDecoder,
127+
VoidEncoder
128+
>
129+
{
130+
let handler = LambdaCodableAdapter(
131+
decoder: LambdaJSONEventDecoder(decoder),
132+
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
133+
)
134+
135+
self.init(handler: handler)
136+
}
137+
}
138+
#endif // trait: FoundationJSONSupport

Sources/AWSLambdaRuntime/Vendored/ByteBuffer-foundation.swift renamed to Sources/AWSLambdaRuntime/FoundationSupport/Vendored/ByteBuffer-foundation.swift

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
//
2727
//===----------------------------------------------------------------------===//
2828

29+
#if FoundationJSONSupport
2930
import NIOCore
3031

3132
#if canImport(FoundationEssentials)
@@ -104,3 +105,4 @@ extension ByteBuffer {
104105
}
105106
}
106107
}
108+
#endif // trait: FoundationJSONSupport

Sources/AWSLambdaRuntime/Vendored/JSON+ByteBuffer.swift renamed to Sources/AWSLambdaRuntime/FoundationSupport/Vendored/JSON+ByteBuffer.swift

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
//
2727
//===----------------------------------------------------------------------===//
2828

29+
#if FoundationJSONSupport
2930
import NIOCore
3031

3132
#if canImport(FoundationEssentials)
@@ -147,3 +148,4 @@ extension JSONEncoder {
147148
return buffer
148149
}
149150
}
151+
#endif // trait: FoundationJSONSupport

0 commit comments

Comments
 (0)