Skip to content

Commit

Permalink
Strict Concurrency Support (#15)
Browse files Browse the repository at this point in the history
* Remove SwiftLint, enable strict concurrency

* Upgrade dependencies

* Fix errors from change to HTTPTypes

* Sendable  conformances

* Fix warnings

* Add Danger workflow

* Sort rules

* Disable optional_data_string_conversion

* SwiftLint fixes

* Replace @retroactive with fully qualified names

* generic -> regular

* Update toolchain version
  • Loading branch information
mgacy authored Oct 22, 2024
1 parent 17985a0 commit 3d5faf8
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 126 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/danger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Run Danger
on:
workflow_dispatch:
pull_request:
types:
- opened
- reopened
- synchronize
- ready_for_review

jobs:
build:
if: github.event.pull_request.draft == false
name: Run Danger
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
pull-requests: write
steps:
- name: Git checkout
uses: actions/checkout@v4
- name: Danger
uses: 417-72KI/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31 changes: 16 additions & 15 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@ excluded:
- .build

opt_in_rules:
- array_init
- attributes
- closure_end_indentation
- closure_spacing
- empty_count
- explicit_init
- closure_spacing
- overridden_super_call
- redundant_nil_coalescing
- private_outlet
- extension_access_modifier
- fatal_error_message
- first_where
- let_var_whitespace
- literal_expression_end_indentation
- nimble_operator
- attributes
- operator_usage_whitespace
- closure_end_indentation
- first_where
- overridden_super_call
- pattern_matching_keywords
- private_outlet
- prohibited_super_call
- fatal_error_message
- vertical_parameter_alignment_on_call
- let_var_whitespace
- redundant_nil_coalescing
- unneeded_parentheses_in_closure_argument
- extension_access_modifier
- pattern_matching_keywords
- array_init
- literal_expression_end_indentation
- vertical_parameter_alignment_on_call

disabled_rules:
- void_return
- multiple_closures_with_trailing_closure
- optional_data_string_conversion
- vertical_parameter_alignment_on_call
- void_return

identifier_name:
excluded:
Expand Down
15 changes: 15 additions & 0 deletions Dangerfile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Danger

extension String: Error {}

let danger = Danger()

if danger.github.pullRequest.body == nil {
danger.fail("Please add a description to this Pull Request")
}

SwiftLint
.lint(
.all(directory: nil),
configFile: ".swiftlint.yml"
)
103 changes: 20 additions & 83 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
{
"pins" : [
{
"identity" : "collectionconcurrencykit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/JohnSundell/CollectionConcurrencyKit.git",
"state" : {
"revision" : "b4f23e24b5a1bff301efc5e70871083ca029ff95",
"version" : "0.2.0"
}
},
{
"identity" : "cryptoswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/krzyzanowskim/CryptoSwift.git",
"state" : {
"revision" : "db51c407d3be4a051484a141bf0bff36c43d3b1e",
"version" : "1.8.0"
}
},
{
"identity" : "sourcekitten",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/SourceKitten.git",
"state" : {
"revision" : "b6dc09ee51dfb0c66e042d2328c017483a1a5d56",
"version" : "0.34.1"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "8f4d2753f0e4778c76d5f05ad16c74f707390531",
"version" : "1.2.3"
}
},
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
Expand All @@ -50,17 +14,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/swift-aws-lambda-events",
"state" : {
"revision" : "997094fe7c55762c07179419a1686230dfa13bef",
"version" : "0.2.0"
"revision" : "a0b64e9d0d5de872169ef645fda2e33575ee42d0",
"version" : "0.4.0"
}
},
{
"identity" : "swift-aws-lambda-runtime",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/swift-aws-lambda-runtime.git",
"state" : {
"revision" : "8d9f44b7838750b103ad2ad49055e8333d719e8a",
"version" : "1.0.0-alpha.2"
"revision" : "5ecc24fa54e61756fe7a7a01f2451da1cffa916e",
"version" : "1.0.0-alpha.3"
}
},
{
Expand All @@ -73,66 +37,39 @@
}
},
{
"identity" : "swift-log",
"identity" : "swift-http-types",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"location" : "https://github.com/apple/swift-http-types.git",
"state" : {
"revision" : "532d8b529501fb73a2455b179e0bbb6d49b652ed",
"version" : "1.5.3"
"revision" : "ae67c8178eb46944fd85e4dc6dd970e1f3ed6ccd",
"version" : "1.3.0"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "702cd7c56d5d44eeba73fdf83918339b26dc855c",
"version" : "2.62.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "6ad4ea24b01559dde0773e3d091f1b9e36175036",
"version" : "509.0.2"
}
},
{
"identity" : "swiftlint",
"kind" : "remoteSourceControl",
"location" : "https://github.com/realm/SwiftLint.git",
"state" : {
"revision" : "f17a4f9dfb6a6afb0408426354e4180daaf49cee",
"version" : "0.54.0"
}
},
{
"identity" : "swiftytexttable",
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/scottrhoyt/SwiftyTextTable.git",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "c6df6cf533d120716bff38f8ff9885e1ce2a4ac3",
"version" : "0.9.0"
"revision" : "9cb486020ebf03bfa5b5df985387a14a98744537",
"version" : "1.6.1"
}
},
{
"identity" : "swxmlhash",
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/drmohundro/SWXMLHash.git",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "a853604c9e9a83ad9954c7e3d2a565273982471f",
"version" : "7.0.2"
"revision" : "f7dc3f527576c398709b017584392fb58592e7f5",
"version" : "2.75.0"
}
},
{
"identity" : "yams",
"identity" : "swift-system",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/Yams.git",
"location" : "https://github.com/apple/swift-system.git",
"state" : {
"revision" : "0d9ee7ea8c4ebd4a489ad7a73d5c6cad55d6fed3",
"version" : "5.0.6"
"revision" : "c8a44d836fe7913603e246acab7c528c2e780168",
"version" : "1.4.0"
}
}
],
Expand Down
17 changes: 7 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.7
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -14,10 +14,10 @@ let package = Package(
.library(name: "LambdaMocks", targets: ["LambdaMocks"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.4.2")),
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.43.1")),
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", .upToNextMajor(from: "0.2.0")),
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", "1.0.0-alpha.1"..<"1.0.0-beta.999")
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.2"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.43.1"),
.package(url: "https://github.com/swift-server/swift-aws-lambda-events", from: "0.2.0"),
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime", from: "1.0.0-alpha.3")
]
)

Expand All @@ -33,7 +33,7 @@ let targets: [Target] = [
name: "LambdaExtras",
dependencies: [
"LambdaExtrasCore",
.product(name: "AWSLambdaRuntime",package: "swift-aws-lambda-runtime"),
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events")
]
),
Expand All @@ -54,11 +54,8 @@ let targets: [Target] = [
)
]

#if os(macOS)
package.dependencies.append(.package(url: "https://github.com/realm/SwiftLint.git", from: "0.54.0"))
for target in targets {
target.plugins = [.plugin(name: "SwiftLintPlugin", package: "SwiftLint")]
target.swiftSettings = [.enableExperimentalFeature("StrictConcurrency")]
}
#endif

package.targets = targets
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Swifty helpers for working with AWS Lambda.

## 📱 Requirements

Swift 5.7 toolchain with Swift Package Manager.
Swift 5.9 toolchain with Swift Package Manager.

## 🖥 Installation

Expand Down Expand Up @@ -32,8 +32,8 @@ Where `<product>` is one of the following:

This package is intended to support the creation of lambdas composed of 2 parts:

- a generic target with a handler implementing the core logic without AWS dependencies
- an executable target using that generic one
- a regular target with a handler implementing the core logic without AWS dependencies
- an executable target using that regular one

### Handler

Expand Down
11 changes: 6 additions & 5 deletions Sources/LambdaExtras/APIGatewayCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import AWSLambdaEvents
import AWSLambdaRuntime
import Foundation
import HTTPTypes
import LambdaExtrasCore

/// A coder for APIGateway events.
public struct APIGatewayCoder<E, O>: LambdaCoding where E: Codable, E: Sendable, O: Sendable {
public struct APIGatewayCoder<E, O>: LambdaCoding, Sendable where E: Codable, E: Sendable, O: Sendable {
/// A JSON decoder.
let decoder: JSONDecoder

Expand Down Expand Up @@ -52,16 +53,16 @@ public struct APIGatewayCoder<E, O>: LambdaCoding where E: Codable, E: Sendable,
}

public func encode(error: Error) throws -> APIGatewayV2Response {
let statusCode: HTTPResponseStatus
let status: HTTPResponse.Status
switch error {
case HandlerError.emptyBody:
statusCode = .badRequest
status = .badRequest
default:
statusCode = .internalServerError
status = .internalServerError
}

return APIGatewayV2Response(
statusCode: statusCode,
statusCode: status,
body: try errorBodyProvider(error.localizedDescription))
}
}
2 changes: 1 addition & 1 deletion Sources/LambdaExtrasCore/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public extension Optional {
}
}

extension String: LocalizedError {
extension Swift.String: Foundation.LocalizedError {
public var errorDescription: String? { self }
public var failureReason: String? { self }
}
2 changes: 1 addition & 1 deletion Sources/LambdaExtrasCore/HandlerError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

/// An error that occurs during lambda handler execution.
public enum HandlerError: Error, Equatable, LocalizedError {
public enum HandlerError: Error, Equatable, LocalizedError, Sendable {
/// The request is missing a body.
case emptyBody
/// The lambda context is missing an expected environment variable.
Expand Down
7 changes: 4 additions & 3 deletions Sources/LambdaMocks/Extensions/APIGatewayV2+Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import AWSLambdaEvents
import Foundation
import HTTPTypes

// MARK: - Helpers

Expand Down Expand Up @@ -53,7 +54,7 @@ public extension APIGatewayV2Request {
static func mock<T: Encodable>(
_ bodyValue: T,
encodedWith encoder: JSONEncoder = .init(),
method: HTTPMethod = .POST,
method: HTTPRequest.Method = .post,
rawPath: String = "/endpoint",
cookies: [String]? = nil,
headers: [String: String] = [:],
Expand Down Expand Up @@ -87,7 +88,7 @@ public extension APIGatewayV2Request {
/// - Returns: The mock request.
static func mock(
// swiftlint:disable:previous function_body_length
method: HTTPMethod = .POST,
method: HTTPRequest.Method = .post,
rawPath: String = "/endpoint",
cookies: [String]? = nil,
headers: [String: String] = [:],
Expand Down Expand Up @@ -206,7 +207,7 @@ public extension APIGatewayV2Response {
static func mock<T: Encodable>(
_ bodyValue: T,
encodedWith encoder: JSONEncoder = .init(),
statusCode: HTTPResponseStatus = .ok,
statusCode: HTTPResponse.Status = .ok,
headers: [String: String]? = nil,
isBase64Encoded: Bool = false,
cookies: [String]? = nil
Expand Down
Loading

0 comments on commit 3d5faf8

Please sign in to comment.