Skip to content

Commit 1277018

Browse files
committed
apply swift format
1 parent 1f78f51 commit 1277018

File tree

12 files changed

+926
-861
lines changed

12 files changed

+926
-861
lines changed

Examples/SAM/HttpApiLambda/Lambda.swift

+32-29
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,38 @@ import Foundation
1818

1919
@main
2020
struct HttpApiLambda: LambdaHandler {
21-
init() {}
22-
init(context: LambdaInitializationContext) async throws {
23-
context.logger.info(
24-
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info" )")
25-
}
26-
27-
// the return value must be either APIGatewayV2Response or any Encodable struct
28-
func handle(_ event: APIGatewayV2Request, context: AWSLambdaRuntimeCore.LambdaContext) async throws -> APIGatewayV2Response {
29-
30-
var header = HTTPHeaders()
31-
do {
32-
context.logger.debug("HTTP API Message received")
33-
34-
header["content-type"] = "application/json"
35-
36-
// echo the request in the response
37-
let data = try JSONEncoder().encode(event)
38-
let response = String(decoding: data, as: UTF8.self)
39-
40-
// if you want control on the status code and headers, return an APIGatewayV2Response
41-
// otherwise, just return any Encodable struct, the runtime will wrap it for you
42-
return APIGatewayV2Response(statusCode: .ok, headers: header, body: response)
43-
44-
} catch {
45-
// should never happen as the decoding was made by the runtime
46-
// when the input event is malformed, this function is not even called
47-
header["content-type"] = "text/plain"
48-
return APIGatewayV2Response(statusCode: .badRequest, headers: header, body: "\(error.localizedDescription)")
21+
init() {}
22+
init(context: LambdaInitializationContext) async throws {
23+
context.logger.info(
24+
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info" )")
25+
}
26+
27+
// the return value must be either APIGatewayV2Response or any Encodable struct
28+
func handle(_ event: APIGatewayV2Request, context: AWSLambdaRuntimeCore.LambdaContext)
29+
async throws -> APIGatewayV2Response
30+
{
31+
32+
var header = HTTPHeaders()
33+
do {
34+
context.logger.debug("HTTP API Message received")
35+
36+
header["content-type"] = "application/json"
37+
38+
// echo the request in the response
39+
let data = try JSONEncoder().encode(event)
40+
let response = String(decoding: data, as: UTF8.self)
41+
42+
// if you want control on the status code and headers, return an APIGatewayV2Response
43+
// otherwise, just return any Encodable struct, the runtime will wrap it for you
44+
return APIGatewayV2Response(statusCode: .ok, headers: header, body: response)
45+
46+
} catch {
47+
// should never happen as the decoding was made by the runtime
48+
// when the input event is malformed, this function is not even called
49+
header["content-type"] = "text/plain"
50+
return APIGatewayV2Response(
51+
statusCode: .badRequest, headers: header, body: "\(error.localizedDescription)")
4952

50-
}
5153
}
54+
}
5255
}

Examples/SAM/Package.swift

+11-10
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,36 @@ let package = Package(
2525
products: [
2626
.executable(name: "HttpApiLambda", targets: ["HttpApiLambda"]),
2727
.executable(name: "SQSLambda", targets: ["SQSLambda"]),
28-
.executable(name: "UrlLambda", targets: ["UrlLambda"])
28+
.executable(name: "UrlLambda", targets: ["UrlLambda"]),
2929
],
3030
dependencies: [
31-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha.3"),
31+
.package(
32+
url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha.3"),
3233
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main"),
33-
.package(path: "../.."),
34+
.package(path: "../.."),
3435
],
3536
targets: [
3637
.executableTarget(
3738
name: "HttpApiLambda",
3839
dependencies: [
3940
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
40-
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events")
41+
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
4142
],
4243
path: "./HttpApiLambda"
4344
),
4445
.executableTarget(
4546
name: "UrlLambda",
4647
dependencies: [
4748
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
48-
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events")
49+
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
4950
],
5051
path: "./UrlLambda"
5152
),
5253
.executableTarget(
5354
name: "SQSLambda",
5455
dependencies: [
5556
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
56-
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events")
57+
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
5758
],
5859
path: "./SQSLambda"
5960
),
@@ -63,12 +64,12 @@ let package = Package(
6364
"HttpApiLambda", "SQSLambda",
6465
.product(name: "AWSLambdaTesting", package: "swift-aws-lambda-runtime"),
6566
],
66-
// testing data
67+
// testing data
6768
resources: [
6869
.process("data/apiv2.json"),
69-
.process("data/sqs.json")
70+
.process("data/sqs.json"),
7071
]
71-
)
72+
),
7273
]
7374
)
7475

@@ -79,4 +80,4 @@ let package = Package(
7980
// .package(name: "swift-aws-lambda-runtime", path: "../../../swift-aws-lambda-runtime"),
8081
// .package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main")
8182
// ]
82-
// }
83+
// }

Examples/SAM/SQSLambda/Lambda.swift

+15-14
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,24 @@ import Foundation
1818

1919
@main
2020
struct SQSLambda: LambdaHandler {
21-
typealias Event = SQSEvent
22-
typealias Output = Void
21+
typealias Event = SQSEvent
22+
typealias Output = Void
2323

24-
init() {}
25-
init(context: LambdaInitializationContext) async throws {
26-
context.logger.info(
27-
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info" )")
28-
}
24+
init() {}
25+
init(context: LambdaInitializationContext) async throws {
26+
context.logger.info(
27+
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info" )")
28+
}
2929

30-
func handle(_ event: Event, context: AWSLambdaRuntimeCore.LambdaContext) async throws -> Output {
30+
func handle(_ event: Event, context: AWSLambdaRuntimeCore.LambdaContext) async throws -> Output {
3131

32-
context.logger.info("Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "not defined" )" )
33-
context.logger.debug("SQS Message received, with \(event.records.count) record")
32+
context.logger.info(
33+
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "not defined" )")
34+
context.logger.debug("SQS Message received, with \(event.records.count) record")
3435

35-
for msg in event.records {
36-
context.logger.debug("Message ID : \(msg.messageId)")
37-
context.logger.debug("Message body : \(msg.body)")
38-
}
36+
for msg in event.records {
37+
context.logger.debug("Message ID : \(msg.messageId)")
38+
context.logger.debug("Message body : \(msg.body)")
3939
}
40+
}
4041
}

Examples/SAM/Tests/LambdaTests/HttpApiLambdaTest.swift

+20-19
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,32 @@ import AWSLambdaEvents
1616
import AWSLambdaRuntime
1717
import AWSLambdaTesting
1818
import XCTest
19+
1920
@testable import HttpApiLambda
2021

2122
class HttpApiLambdaTests: LambdaTest {
2223

23-
func testHttpAPiLambda() async throws {
24+
func testHttpAPiLambda() async throws {
2425

25-
// given
26-
let eventData = try self.loadTestData(file: .apiGatewayV2)
27-
let event = try JSONDecoder().decode(APIGatewayV2Request.self, from: eventData)
26+
// given
27+
let eventData = try self.loadTestData(file: .apiGatewayV2)
28+
let event = try JSONDecoder().decode(APIGatewayV2Request.self, from: eventData)
2829

29-
do {
30-
// when
31-
let result = try await Lambda.test(HttpApiLambda.self, with: event)
30+
do {
31+
// when
32+
let result = try await Lambda.test(HttpApiLambda.self, with: event)
3233

33-
// then
34-
XCTAssertEqual(result.statusCode.code, 200)
35-
XCTAssertNotNil(result.headers)
36-
if let headers = result.headers {
37-
XCTAssertNotNil(headers["content-type"])
38-
if let contentType = headers["content-type"] {
39-
XCTAssertTrue(contentType == "application/json")
40-
}
41-
}
42-
} catch {
43-
XCTFail("Lambda invocation should not throw error : \(error)")
44-
}
34+
// then
35+
XCTAssertEqual(result.statusCode.code, 200)
36+
XCTAssertNotNil(result.headers)
37+
if let headers = result.headers {
38+
XCTAssertNotNil(headers["content-type"])
39+
if let contentType = headers["content-type"] {
40+
XCTAssertTrue(contentType == "application/json")
4541
}
42+
}
43+
} catch {
44+
XCTFail("Lambda invocation should not throw error : \(error)")
45+
}
46+
}
4647
}

Examples/SAM/Tests/LambdaTests/LambdaTest.swift

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ import Foundation
22
import XCTest
33

44
enum TestData: String {
5-
case apiGatewayV2 = "apiv2"
6-
case sqs = "sqs"
5+
case apiGatewayV2 = "apiv2"
6+
case sqs = "sqs"
77
}
88

99
class LambdaTest: XCTestCase {
10-
// return the URL of a test file
11-
// files are copied to the bundle during build by the `resources` directive in `Package.swift`
12-
private func urlForTestData(file: TestData) throws -> URL {
13-
let filePath = Bundle.module.path(forResource: file.rawValue, ofType: "json")!
14-
return URL(fileURLWithPath: filePath)
15-
}
10+
// return the URL of a test file
11+
// files are copied to the bundle during build by the `resources` directive in `Package.swift`
12+
private func urlForTestData(file: TestData) throws -> URL {
13+
let filePath = Bundle.module.path(forResource: file.rawValue, ofType: "json")!
14+
return URL(fileURLWithPath: filePath)
15+
}
1616

17-
// load a test file added as a resource to the executable bundle
18-
func loadTestData(file: TestData) throws -> Data {
19-
// load list from file
20-
return try Data(contentsOf: urlForTestData(file: file))
21-
}
17+
// load a test file added as a resource to the executable bundle
18+
func loadTestData(file: TestData) throws -> Data {
19+
// load list from file
20+
return try Data(contentsOf: urlForTestData(file: file))
21+
}
2222
}

Examples/SAM/Tests/LambdaTests/SQSLambdaTest.swift

+14-13
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,26 @@ import AWSLambdaEvents
1616
import AWSLambdaRuntime
1717
import AWSLambdaTesting
1818
import XCTest
19+
1920
@testable import SQSLambda
2021

2122
class SQSLambdaTests: LambdaTest {
2223

23-
func testSQSLambda() async throws {
24+
func testSQSLambda() async throws {
2425

25-
// given
26-
let eventData = try self.loadTestData(file: .sqs)
27-
let event = try JSONDecoder().decode(SQSEvent.self, from: eventData)
26+
// given
27+
let eventData = try self.loadTestData(file: .sqs)
28+
let event = try JSONDecoder().decode(SQSEvent.self, from: eventData)
2829

29-
// when
30-
do {
31-
try await Lambda.test(SQSLambda.self, with: event)
32-
} catch {
33-
XCTFail("Lambda invocation should not throw error : \(error)")
34-
}
30+
// when
31+
do {
32+
try await Lambda.test(SQSLambda.self, with: event)
33+
} catch {
34+
XCTFail("Lambda invocation should not throw error : \(error)")
35+
}
3536

36-
// then
37-
// SQS Lambda returns Void
37+
// then
38+
// SQS Lambda returns Void
3839

39-
}
40+
}
4041
}

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ test-coverage:
22
swift test --enable-code-coverage
33
./Scripts/ProcessCoverage.swift \
44
`swift test --show-codecov-path` .build/coverage.json \
5-
.build/coverage.html .build/coverage.svg
5+
.build/coverage.html .build/coverage.svg
6+
format:
7+
swift format --in-place --recursive Sources Plugins Examples/SAM
8+
swift format --in-place --recursive Package.swift

0 commit comments

Comments
 (0)