Skip to content

Commit

Permalink
Merge pull request #6 from markbattistella/2024.08.03
Browse files Browse the repository at this point in the history
2024-08-03 - Updated to Swift 6 concurrency
  • Loading branch information
markbattistella authored Aug 2, 2024
2 parents 93c53c5 + 9490732 commit bd11bf8
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 11 deletions.
7 changes: 5 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.8
// swift-tools-version: 5.10

import PackageDescription

Expand All @@ -20,7 +20,10 @@ let package = Package(
.target(
name: "ZodiacKit",
dependencies: [],
path: "Sources"
path: "Sources",
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
]
),
.testTarget(
name: "ZodiacKitTests",
Expand Down
1 change: 1 addition & 0 deletions Sources/ZodiacKit/Defaults/DefaultZodiacs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Foundation

/// Contains the default set of Western Zodiac signs and their associated date ranges.
/// These defaults are used by `ZodiacService` if no custom zodiacs are provided.
@ZodiacActor
public let defaultZodiacs: [WesternZodiac] = [
WesternZodiac(
sign: .aquarius,
Expand Down
39 changes: 39 additions & 0 deletions Sources/ZodiacKit/Helper/ZodiacActor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Project:
// Author: Mark Battistella
// Website: https://markbattistella.com
//

import Foundation

/// A global actor to manage concurrency for shared resources.
///
/// The `GlobalActor` is used to ensure that certain shared resources are accessed
/// in a thread-safe manner. By isolating code to this actor, we prevent data races
/// and ensure safe access to globally accessible variables.
///
/// Usage:
/// ```swift
/// @ZodiacActor
/// public var sharedResource: SomeType = ...
///
/// @ZodiacActor
/// public func performThreadSafeOperation() {
/// // Code that interacts with sharedResource
/// }
/// ```
///
/// You can also use `await` to call functions isolated to this actor:
/// ```swift
/// await GlobalActor.run {
/// performThreadSafeOperation()
/// }
/// ```
///
/// - Note: Make sure to use the `@ZodiacActor` attribute to isolate global variables or functions.
@globalActor
public actor ZodiacActor {

/// The shared instance of the global actor.
public static let shared = ZodiacActor()
}
4 changes: 2 additions & 2 deletions Sources/ZodiacKit/Model/WesternZodiac.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

/// Represents a zodiac sign with its date range
public struct WesternZodiac: Codable {
public struct WesternZodiac: Codable, Sendable {

/// The zodiac sign
public let sign: WesternZodiacSign
Expand All @@ -19,7 +19,7 @@ public struct WesternZodiac: Codable {
public let endDate: DateRange

/// Represents a date with day and month only
public struct DateRange: Codable {
public struct DateRange: Codable, Sendable{

/// The day of the date range
public let day: Int
Expand Down
2 changes: 1 addition & 1 deletion Sources/ZodiacKit/Model/WesternZodiacSign.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public typealias Colour = NSColor
#endif

/// Represents Western zodiac signs
public enum WesternZodiacSign: String, ZodiacSign {
public enum WesternZodiacSign: String, ZodiacSign, Sendable {

/// Available list of Western zodiac signs
case aquarius, aries, cancer, capricorn, gemini, leo, libra,
Expand Down
13 changes: 7 additions & 6 deletions Sources/ZodiacKit/Services/ZodiacService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import Foundation

/// A service for determining a person's Western and Chinese zodiac signs based on their birthdate.
@ZodiacActor
public struct ZodiacService {

/// A collection of WesternZodiac objects representing the zodiac signs.
Expand All @@ -26,12 +27,12 @@ public struct ZodiacService {
/// - Parameter zodiacs: An array of WesternZodiac objects. If not provided,
/// a default set is used.
/// - Throws: An error if the provided zodiacs are invalid.
public init(zodiacs: [WesternZodiac] = defaultZodiacs) throws {
try validator.validate(zodiacs: zodiacs)
self.zodiacs = zodiacs
self.dayOfYearToZodiac = ZodiacService.mapZodiacsToDaysOfYear(zodiacs: zodiacs,
validator: validator)
}
public init(zodiacs: [WesternZodiac] = defaultZodiacs) throws {
try validator.validate(zodiacs: zodiacs)
self.zodiacs = zodiacs
self.dayOfYearToZodiac = ZodiacService.mapZodiacsToDaysOfYear(zodiacs: zodiacs,
validator: validator)
}

/// Maps zodiac periods to days of the year.
///
Expand Down
3 changes: 3 additions & 0 deletions Tests/ZodiacKitTests/Dates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extension ZodiacKitTests {

/// Tests that the service correctly identifies the Western Zodiac sign based on the
/// start date of each sign's period.
@ZodiacActor
func testStartDateReturnsCorrectSign() {
let testCases: [(day: Int, month: Int, expectedSign: WesternZodiacSign)] = [
// Each tuple represents the boundary start date for a zodiac sign and the expected sign.
Expand All @@ -37,6 +38,7 @@ extension ZodiacKitTests {

/// Tests that the service correctly identifies the Western Zodiac sign based on the
/// end date of each sign's period.
@ZodiacActor
func testEndDateReturnsCorrectSign() {
let testCases: [(day: Int, month: Int, expectedSign: WesternZodiacSign)] = [
// Each tuple represents the boundary end date for a zodiac sign and the expected sign.
Expand All @@ -63,6 +65,7 @@ extension ZodiacKitTests {

/// Tests that the service correctly identifies the Western Zodiac sign for dates falling
/// in the middle of each sign's period.
@ZodiacActor
func testDateInMiddleReturnsCorrectSign() {
let orderedSigns: [WesternZodiacSign] = [
.aquarius, .pisces, .aries, .taurus, .gemini,
Expand Down
1 change: 1 addition & 0 deletions Tests/ZodiacKitTests/Setup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ZodiacKitTests: XCTestCase {

/// Sets up necessary instances before each test is run. This includes initializing the
/// `validator` and `service` variables.
@ZodiacActor
override func setUp() {
super.setUp()
self.validator = DateValidator()
Expand Down

0 comments on commit bd11bf8

Please sign in to comment.