Skip to content

addopt strict concurrency #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 28, 2024
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ let package = Package(
],
targets: [
.target(name: "AWSLambdaEvents",
dependencies: [.product(name: "HTTPTypes", package: "swift-http-types")]),
dependencies: [.product(name: "HTTPTypes", package: "swift-http-types")],
swiftSettings: [.enableExperimentalFeature("StrictConcurrency=complete")]),
.testTarget(name: "AWSLambdaEventsTests",
dependencies: ["AWSLambdaEvents"]),
dependencies: ["AWSLambdaEvents"],
swiftSettings: [.enableExperimentalFeature("StrictConcurrency=complete")]),
]
)
2 changes: 1 addition & 1 deletion Sources/AWSLambdaEvents/AWSRegion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct AWSRegion: RawRepresentable, Equatable {
self.rawValue = rawValue
}

static var all: [AWSRegion] = [
static let all: [AWSRegion] = [
Self.af_south_1,
Self.ap_northeast_1,
Self.ap_northeast_2,
Expand Down
19 changes: 7 additions & 12 deletions Sources/AWSLambdaEvents/Cloudwatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import struct Foundation.Date
/// EventBridge has the same events/notification types as CloudWatch
public typealias EventBridgeEvent = CloudwatchEvent

public protocol CloudwatchDetail: Decodable {
public protocol CloudwatchDetail: Decodable, Sendable {
static var name: String { get }
}

Expand All @@ -37,7 +37,8 @@ extension CloudwatchDetail {
/// https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents.html
/// https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html
/// https://docs.aws.amazon.com/eventbridge/latest/userguide/event-types.html
public struct CloudwatchEvent<Detail: CloudwatchDetail>: Decodable {
/// https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-events-structure.html
public struct CloudwatchEvent<Detail: CloudwatchDetail>: Decodable, Sendable {
public let id: String
public let source: String
public let accountId: String
Expand Down Expand Up @@ -89,11 +90,11 @@ public enum CloudwatchDetails {
public static let name = "Scheduled Event"
}

public enum EC2 {
public enum EC2: Sendable {
public struct InstanceStateChangeNotification: CloudwatchDetail {
public static let name = "EC2 Instance State-change Notification"

public enum State: String, Codable {
public enum State: String, Codable, Sendable {
case running
case shuttingDown = "shutting-down"
case stopped
Expand All @@ -113,7 +114,7 @@ public enum CloudwatchDetails {
public struct SpotInstanceInterruptionNotice: CloudwatchDetail {
public static let name = "EC2 Spot Instance Interruption Warning"

public enum Action: String, Codable {
public enum Action: String, Codable, Sendable {
case hibernate
case stop
case terminate
Expand All @@ -131,12 +132,6 @@ public enum CloudwatchDetails {

struct TypeMismatch: Error {
let name: String
let type: Any
let type: any CloudwatchDetail.Type
}
}

#if swift(>=5.6)
extension CloudwatchEvent: Sendable where Detail: Sendable {}
extension CloudwatchDetails.EC2: Sendable {}
extension CloudwatchDetails.Scheduled: Sendable {}
#endif
11 changes: 3 additions & 8 deletions Sources/AWSLambdaEvents/Utils/DateWrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public struct ISO8601Coding: Decodable {
self.wrappedValue = date
}

private static let dateFormatter: DateFormatter = Self.createDateFormatter()

private static func createDateFormatter() -> DateFormatter {
private static var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
Expand All @@ -68,9 +66,7 @@ public struct ISO8601WithFractionalSecondsCoding: Decodable {
self.wrappedValue = date
}

private static let dateFormatter: DateFormatter = Self.createDateFormatter()

private static func createDateFormatter() -> DateFormatter {
private static var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
Expand Down Expand Up @@ -105,8 +101,7 @@ public struct RFC5322DateTimeCoding: Decodable {
"Expected date to be in RFC5322 date-time format, but `\(string)` is not in the correct format")
}

private static let dateFormatters: [DateFormatter] = Self.createDateFormatters()
private static func createDateFormatters() -> [DateFormatter] {
private static var dateFormatters: [DateFormatter] {
// rfc5322 dates received in SES mails sometimes do not include the day, so need two dateformatters
// one with a day and one without
let formatterWithDay = DateFormatter()
Expand Down