Skip to content
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

Optimise the Swift version of Vexilla Client #42

Merged
merged 8 commits into from Mar 17, 2024
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Expand Up @@ -226,7 +226,6 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: |
cd ./clients/swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (blocking): I would prefer not to move the Package.swift file to the top level. With plans for 14 SDKs, the root level of the project could become unwieldy if all clients SDKs did this.

Does having the Package.swift in a nested folder break something for SwiftPM?

swift build
swift test
env:
Expand Down
6 changes: 4 additions & 2 deletions clients/swift/Package.swift → Package.swift
Expand Up @@ -21,11 +21,13 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "VexillaClient",
dependencies: []
dependencies: [],
path: "clients/swift/Sources/VexillaClient"
),
.testTarget(
name: "VexillaClientTests",
dependencies: ["VexillaClient"]
dependencies: ["VexillaClient"],
path: "clients/swift/Tests/VexillaClientTests"
),
]
)
5 changes: 1 addition & 4 deletions clients/swift/.vscode/extensions.json
@@ -1,8 +1,5 @@
{
"recommendations": [
"DEVSENSE.composer-php-vscode",
"DEVSENSE.intelli-php-vscode",
"DEVSENSE.phptools-vscode",
"DEVSENSE.profiler-php-vscode"
"sswg.swift-lang"
]
}
7 changes: 3 additions & 4 deletions clients/swift/Sources/VexillaClient/Hashing.swift
@@ -1,10 +1,9 @@
import Foundation

func hashString(stringToHash: String, seed: Float64) -> Float64 {
let chars = Array(stringToHash)

let total = chars.reduce(0) { a, b in
a + Int(b.asciiValue ?? 0)
var total = 0
for char in stringToHash.utf8 {
total += Int(char)
}

var calculated = Float64(total) * seed * 42.0
Expand Down
6 changes: 3 additions & 3 deletions clients/swift/Sources/VexillaClient/Scheduling.swift
Expand Up @@ -12,7 +12,7 @@ func isScheduleActiveWithNow(schedule: Schedule, scheduleType: ScheduleType, now
let nowSeconds = now.timeIntervalSince1970
var calendar = Calendar(identifier: Calendar.Identifier.iso8601)
guard let utc = TimeZone(identifier: "UTC") else {
throw "Could not create UTC calendar"
throw VexillaSchedulingError.couldNotCreateUTCTimezone
}
calendar.timeZone = utc

Expand All @@ -26,11 +26,11 @@ func isScheduleActiveWithNow(schedule: Schedule, scheduleType: ScheduleType, now

let endDate = Date(timeIntervalSince1970: TimeInterval(schedule.end / 1000))
guard let dayAfterEndDate = calendar.date(byAdding: .day, value: 1, to: endDate) else {
throw "could not add day to endDate"
throw VexillaSchedulingError.couldNotAddDayToEndDate
}
let endOfEndDate = calendar.startOfDay(for: dayAfterEndDate)

if startOfStartDate > now || endOfEndDate < now {
guard startOfStartDate <= now && endOfEndDate >= now else {
return false
}

Expand Down