Skip to content

Commit

Permalink
Update swiftformat (#151)
Browse files Browse the repository at this point in the history
* Update to swiftformat 0.52.10

This is the latest version of swiftformat.

The .swiftformat config is modified to target Swift 5.7, which is the
oldest Swift currently supported by mqtt-nio.

The validate.sh script is updated to resolve some shellcheck issues.

Signed-off-by: Peter Grayson <[email protected]>

* Formatting fixes for swiftformat 0.52.10

Signed-off-by: Peter Grayson <[email protected]>

---------

Signed-off-by: Peter Grayson <[email protected]>
  • Loading branch information
jpgrayson authored Nov 28, 2023
1 parent a370d9e commit 267b83a
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ jobs:
- name: Install Dependencies
run: |
brew install mint
mint install nicklockwood/swiftformat@0.48.17 --no-link
mint install nicklockwood/swiftformat@0.52.10 --no-link
- name: run script
run: ./scripts/validate.sh
4 changes: 2 additions & 2 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Minimum swiftformat version
--minversion 0.47.4
--minversion 0.52.0

# Swift version
--swiftversion 5.1
--swiftversion 5.7

# file options
--exclude .build
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ docker-compose run test

### Formatting

We use Nick Lockwood's SwiftFormat for formatting code. PRs will not be accepted if they haven't be formatted. The current version of SwiftFormat we are using is v0.48.17.
We use Nick Lockwood's SwiftFormat for formatting code. PRs will not be accepted if they haven't be formatted. The current version of SwiftFormat we are using is v0.52.10.
2 changes: 1 addition & 1 deletion Sources/MQTTNIO/AsyncAwaitSupport/MQTTClient+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension MQTTClient {
public func shutdown(queue: DispatchQueue = .global()) async throws {
return try await withUnsafeThrowingContinuation { cont in
self.shutdown(queue: queue) { error in
if let error = error {
if let error {
cont.resume(throwing: error)
} else {
cont.resume()
Expand Down
2 changes: 1 addition & 1 deletion Sources/MQTTNIO/ChannelHandlers/WebSocketHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ final class WebSocketHandler: ChannelDuplexHandler {

/// Send ping and setup task to check for pong and send new ping
private func sendPingAndWait(context: ChannelHandlerContext) {
guard context.channel.isActive, let pingInterval = pingInterval else {
guard context.channel.isActive, let pingInterval else {
return
}
if self.waitingOnPong {
Expand Down
14 changes: 7 additions & 7 deletions Sources/MQTTNIO/MQTTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public final class MQTTClient {
return self.host
}

internal let globalPacketId = ManagedAtomic<UInt16>(1)
let globalPacketId = ManagedAtomic<UInt16>(1)
/// default logger that logs nothing
private static let loggingDisabled = Logger(label: "MQTT-do-not-log", factory: { _ in SwiftLogNoOpLogHandler() })
/// inflight messages
Expand All @@ -101,7 +101,7 @@ public final class MQTTClient {
configuration: Configuration = Configuration()
) {
self.host = host
if let port = port {
if let port {
self.port = port
} else {
switch (configuration.useSSL, configuration.useWebSockets) {
Expand Down Expand Up @@ -190,7 +190,7 @@ public final class MQTTClient {
var errorStorage: Error?
let continuation = DispatchWorkItem {}
self.shutdown(queue: DispatchQueue(label: "mqtt-client.shutdown")) { error in
if let error = error {
if let error {
errorStorageLock.withLock {
errorStorage = error
}
Expand Down Expand Up @@ -334,7 +334,7 @@ public final class MQTTClient {
will: publish
)

return self.connect(packet: packet).map { $0.sessionPresent }
return self.connect(packet: packet).map(\.sessionPresent)
}

/// Publish message to topic
Expand Down Expand Up @@ -441,7 +441,7 @@ public final class MQTTClient {
self.shutdownListeners.removeListener(named: name)
}

internal func updatePacketId() -> UInt16 {
func updatePacketId() -> UInt16 {
let id = self.globalPacketId.wrappingIncrementThenLoad(by: 1, ordering: .relaxed)

// packet id must be non-zero
Expand All @@ -468,7 +468,7 @@ public final class MQTTClient {
private var lock = NIOLock()
}

internal extension MQTTClient {
extension MQTTClient {
/// connect to broker
func connect(
packet: MQTTConnectPacket,
Expand Down Expand Up @@ -513,7 +513,7 @@ internal extension MQTTClient {
}
case let auth as MQTTAuthPacket:
// auth messages require an auth workflow closure
guard let authWorkflow = authWorkflow else { return eventLoop.makeFailedFuture(MQTTError.authWorkflowRequired) }
guard let authWorkflow else { return eventLoop.makeFailedFuture(MQTTError.authWorkflowRequired) }
return self.processAuth(auth, authWorkflow: authWorkflow, on: eventLoop)
.flatMapThrowing { result -> MQTTConnAckPacket in
// once auth workflow is finished we should receive a connack
Expand Down
2 changes: 1 addition & 1 deletion Sources/MQTTNIO/MQTTClientV5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ extension MQTTClient {
if auth.reason == .success {
return eventLoop.makeSucceededFuture(auth)
}
guard let authWorkflow = authWorkflow else { return eventLoop.makeFailedFuture(MQTTError.authWorkflowRequired) }
guard let authWorkflow else { return eventLoop.makeFailedFuture(MQTTError.authWorkflowRequired) }
return self.client.processAuth(authPacket, authWorkflow: authWorkflow, on: eventLoop)
}
.flatMapThrowing { response -> MQTTAuthV5 in
Expand Down
6 changes: 3 additions & 3 deletions Sources/MQTTNIO/MQTTInflight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ struct MQTTInflight {
/// add packet
mutating func add(packet: MQTTPacket) {
self.lock.withLock {
packets.append(packet)
self.packets.append(packet)
}
}

/// remove packert
mutating func remove(id: UInt16) {
self.lock.withLock {
guard let first = packets.firstIndex(where: { $0.packetId == id }) else { return }
packets.remove(at: first)
self.packets.remove(at: first)
}
}

/// remove all packets
mutating func clear() {
self.lock.withLock {
packets = []
self.packets = []
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/MQTTNIO/MQTTListeners.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ final class MQTTListeners<ReturnType> {

func addListener(named name: String, listener: @escaping Listener) {
self.lock.withLock {
listeners[name] = listener
self.listeners[name] = listener
}
}

func removeListener(named name: String) {
self.lock.withLock {
listeners[name] = nil
self.listeners[name] = nil
}
}

Expand Down
18 changes: 9 additions & 9 deletions Sources/MQTTNIO/MQTTPacket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import NIOCore

internal enum InternalError: Swift.Error {
enum InternalError: Swift.Error {
case incompletePacket
case notImplemented
}
Expand Down Expand Up @@ -99,7 +99,7 @@ struct MQTTConnectPacket: MQTTPacket {
byteBuffer.writeInteger(version.versionByte)
// connect flags
var flags = self.cleanSession ? ConnectFlags.cleanSession : 0
if let will = will {
if let will {
flags |= ConnectFlags.willFlag
flags |= will.retain ? ConnectFlags.willRetain : 0
flags |= will.qos.rawValue << ConnectFlags.willQoSShift
Expand All @@ -116,17 +116,17 @@ struct MQTTConnectPacket: MQTTPacket {

// payload
try MQTTSerializer.writeString(self.clientIdentifier, to: &byteBuffer)
if let will = will {
if let will {
if version == .v5_0 {
try will.properties.write(to: &byteBuffer)
}
try MQTTSerializer.writeString(will.topicName, to: &byteBuffer)
try MQTTSerializer.writeBuffer(will.payload, to: &byteBuffer)
}
if let userName = userName {
if let userName {
try MQTTSerializer.writeString(userName, to: &byteBuffer)
}
if let password = password {
if let password {
try MQTTSerializer.writeString(password, to: &byteBuffer)
}
}
Expand All @@ -149,7 +149,7 @@ struct MQTTConnectPacket: MQTTPacket {
// client identifier
size += self.clientIdentifier.utf8.count + 2
// will publish
if let will = will {
if let will {
// properties
if version == .v5_0 {
let propertiesPacketSize = will.properties.packetSize
Expand All @@ -161,11 +161,11 @@ struct MQTTConnectPacket: MQTTPacket {
size += will.payload.readableBytes + 2
}
// user name
if let userName = userName {
if let userName {
size += userName.utf8.count + 2
}
// password
if let password = password {
if let password {
size += password.utf8.count + 2
}
return size
Expand Down Expand Up @@ -370,7 +370,7 @@ struct MQTTPubAckPacket: MQTTPacket {
let reason: MQTTReasonCode
let properties: MQTTProperties

internal init(
init(
type: MQTTPacketType,
packetId: UInt16,
reason: MQTTReasonCode = .success,
Expand Down
2 changes: 1 addition & 1 deletion Sources/MQTTNIO/MQTTTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class MQTTTask {
let promise = eventLoop.makePromise(of: MQTTPacket.self)
self.promise = promise
self.checkInbound = checkInbound
if let timeout = timeout {
if let timeout {
self.timeoutTask = eventLoop.scheduleTask(in: timeout) {
promise.fail(MQTTError.timeout)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/MQTTNIO/TSTLSConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public struct TSTLSConfiguration {
public static func pem(_ filename: String) throws -> Self {
let certificates = try NIOSSLCertificate.fromPEMFile(filename)
let secCertificates = try certificates.map { certificate -> SecCertificate in
guard let certificate = SecCertificateCreateWithData(nil, Data(try certificate.toDERBytes()) as CFData) else { throw TSTLSConfiguration.Error.invalidData }
guard let certificate = try SecCertificateCreateWithData(nil, Data(certificate.toDERBytes()) as CFData) else { throw TSTLSConfiguration.Error.invalidData }
return certificate
}
return .init(certificates: secCertificates)
Expand Down Expand Up @@ -254,7 +254,7 @@ extension TSTLSConfiguration {
}
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) {
SecTrustEvaluateAsyncWithError(trust, Self.tlsDispatchQueue) { _, result, error in
if let error = error {
if let error {
print("Trust failed: \(error.localizedDescription)")
}
sec_protocol_verify_complete(result)
Expand Down
13 changes: 7 additions & 6 deletions scripts/validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@
##
##===----------------------------------------------------------------------===##

SWIFT_VERSION=5.3
SWIFT_FORMAT_VERSION=0.48.17
SWIFT_FORMAT_VERSION=0.52.10

set -eu
here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

which swiftformat > /dev/null 2>&1 || (echo "swiftformat not installed. You can install it using 'mint install nicklockwood/swiftformat'" ; exit -1)
command -v swiftformat >/dev/null || {
echo "swiftformat not installed. You can install it using 'mint install nicklockwood/swiftformat'"
exit 1
}

printf "=> Checking format... "
FIRST_OUT="$(git status --porcelain)"
if [[ -n "${CI-""}" ]]; then
printf "(using v$(mint run NickLockwood/SwiftFormat@$SWIFT_FORMAT_VERSION --version)) "
printf "(using v%s) " "$(mint run NickLockwood/SwiftFormat@$SWIFT_FORMAT_VERSION --version)"
mint run NickLockwood/SwiftFormat@$SWIFT_FORMAT_VERSION . > /dev/null 2>&1
else
printf "(using v$(swiftformat --version)) "
printf "(using v%s) " "$(swiftformat --version)"
swiftformat . > /dev/null 2>&1
fi
SECOND_OUT="$(git status --porcelain)"
Expand Down

0 comments on commit 267b83a

Please sign in to comment.