Skip to content

Commit

Permalink
Fix linter issues
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Piotrowski <piotr@synadia.com>
  • Loading branch information
piotrpio authored and Jarema committed Feb 2, 2024
1 parent f6e01ec commit 67e12c6
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 101 deletions.
8 changes: 4 additions & 4 deletions Sources/NatsSwift/Extensions/Data+Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ extension Data {
let serverOp = try ServerOp.parse(from: lineData)

// if it's a message, get the full payload and add to returned data
if case .Message(var msg) = serverOp {
if case .message(var msg) = serverOp {
if msg.length == 0 {
serverOps.append(serverOp)
} else {
Expand All @@ -113,11 +113,11 @@ extension Data {
self.index(
payloadEndIndex, offsetBy: Data.crlf.count, limitedBy: self.endIndex)
?? self.endIndex
serverOps.append(.Message(msg))
serverOps.append(.message(msg))
continue
}
//TODO(jrm): Add HMSG handling here too.
} else if case .HMessage(var msg) = serverOp {
} else if case .hMessage(var msg) = serverOp {
if msg.length == 0 {
serverOps.append(serverOp)
} else {
Expand Down Expand Up @@ -167,7 +167,7 @@ extension Data {
self.index(
payloadEndIndex, offsetBy: Data.crlf.count, limitedBy: self.endIndex)
?? self.endIndex
serverOps.append(.HMessage(msg))
serverOps.append(.hMessage(msg))
continue
}

Expand Down
10 changes: 5 additions & 5 deletions Sources/NatsSwift/NatsClient/NatsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ var logger = Logger(label: "NatsSwift")

/// Client connection states
public enum NatsState {
case Pending
case Connected
case Disconnected
case Closed
case pending
case connected
case disconnected
case closed
}

public struct Auth {
Expand Down Expand Up @@ -96,7 +96,7 @@ extension Client {
throw NSError(
domain: "nats_swift", code: 1, userInfo: ["message": "empty connection handler"])
}
try connectionHandler.write(operation: ClientOp.Publish((subject, reply, payload, headers)))
try connectionHandler.write(operation: ClientOp.publish((subject, reply, payload, headers)))
}

public func flush() async throws {
Expand Down
4 changes: 2 additions & 2 deletions Sources/NatsSwift/NatsClient/NatsClientOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ClientOptions {
return self
}

public func username_and_password(_ username: String, _ password: String) -> ClientOptions {
public func usernameAndPassword(_ username: String, _ password: String) -> ClientOptions {
if self.auth == nil {
self.auth = Auth(user: username, password: password)
} else {
Expand All @@ -61,7 +61,7 @@ public class ClientOptions {
return self
}

public func credentials_file(_ credentials: URL) -> ClientOptions {
public func credentialsFile(_ credentials: URL) -> ClientOptions {
if self.auth == nil {
self.auth = Auth.fromCredentials(credentials)
} else {
Expand Down
44 changes: 22 additions & 22 deletions Sources/NatsSwift/NatsConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ConnectionHandler: ChannelInboundHandler {
internal let pingInterval: TimeInterval

typealias InboundIn = ByteBuffer
internal var state: NatsState = .Pending
internal var state: NatsState = .pending
internal var subscriptions: [UInt64: Subscription]
internal var subscriptionCounter = ManagedAtomic<UInt64>(0)
internal var serverInfo: ServerInfo?
Expand Down Expand Up @@ -66,9 +66,9 @@ class ConnectionHandler: ChannelInboundHandler {
self.serverInfoContinuation = nil
logger.debug("server info")
switch op {
case let .Error(err):
case .error(let err):
continuation.resume(throwing: err)
case let .Info(info):
case .info(let info):
continuation.resume(returning: info)
default:
// ignore until we get either error or server info
Expand All @@ -81,7 +81,7 @@ class ConnectionHandler: ChannelInboundHandler {
self.connectionEstablishedContinuation = nil
logger.debug("conn established")
switch op {
case let .Error(err):
case .error(let err):
continuation.resume(throwing: err)
default:
continuation.resume()
Expand All @@ -90,19 +90,19 @@ class ConnectionHandler: ChannelInboundHandler {
}

switch op {
case .Ping:
case .ping:
logger.debug("ping")
do {
try self.write(operation: .Pong)
try self.write(operation: .pong)
} catch {
// TODO(pp): handle async error
logger.error("error sending pong: \(error)")
continue
}
case .Pong:
case .pong:
logger.debug("pong")
self.outstandingPings.store(0, ordering: AtomicStoreOrdering.relaxed)
case let .Error(err):
case .error(let err):
logger.debug("error \(err)")

let normalizedError = err.normalizedError
Expand All @@ -114,11 +114,11 @@ class ConnectionHandler: ChannelInboundHandler {
context.fireErrorCaught(err)
}
// TODO(pp): handle auth errors here
case let .Message(msg):
case .message(let msg):
self.handleIncomingMessage(msg)
case let .HMessage(msg):
case .hMessage(let msg):
self.handleIncomingHMessage(msg)
case let .Info(serverInfo):
case .info(let serverInfo):
logger.debug("info \(op)")
self.serverInfo = serverInfo
default:
Expand Down Expand Up @@ -221,15 +221,15 @@ class ConnectionHandler: ChannelInboundHandler {
self.connectionEstablishedContinuation = continuation
Task.detached {
do {
try self.write(operation: ClientOp.Connect(connect))
try self.write(operation: ClientOp.Ping)
try self.write(operation: ClientOp.connect(connect))
try self.write(operation: ClientOp.ping)
self.channel?.flush()
} catch {
continuation.resume(throwing: error)
}
}
}
self.state = .Connected
self.state = .pending
guard let channel = self.channel else {
throw NSError(domain: "nats_swift", code: 1, userInfo: ["message": "empty channel"])
}
Expand All @@ -244,7 +244,7 @@ class ConnectionHandler: ChannelInboundHandler {
}

func close() async throws {
self.state = .Closed
self.state = .closed
try await disconnect()
try await self.group.shutdownGracefully()
}
Expand All @@ -261,7 +261,7 @@ class ConnectionHandler: ChannelInboundHandler {
handleDisconnect()
return
}
let ping = ClientOp.Ping
let ping = ClientOp.ping
do {
try self.write(operation: ping)
logger.debug("sent ping: \(pingsOut)")
Expand All @@ -280,7 +280,7 @@ class ConnectionHandler: ChannelInboundHandler {
func channelInactive(context: ChannelHandlerContext) {
logger.debug("TCP channel inactive")

if self.state == .Connected {
if self.state == .pending {
handleDisconnect()
}
}
Expand All @@ -289,15 +289,15 @@ class ConnectionHandler: ChannelInboundHandler {
// TODO(pp): implement Close() on the connection and call it here
logger.debug("Encountered error on the channel: \(error)")
context.close(promise: nil)
if self.state == .Connected {
if self.state == .pending {
handleDisconnect()
} else if self.state == .Disconnected {
} else if self.state == .disconnected {
handleReconnect()
}
}

func handleDisconnect() {
self.state = .Disconnected
self.state = .disconnected
if let channel = self.channel {
let promise = channel.eventLoop.makePromise(of: Void.self)
Task {
Expand Down Expand Up @@ -337,7 +337,7 @@ class ConnectionHandler: ChannelInboundHandler {
break
}
for (sid, sub) in self.subscriptions {
try write(operation: ClientOp.Subscribe((sid, sub.subject, nil)))
try write(operation: ClientOp.subscribe((sid, sub.subject, nil)))
}
}
}
Expand Down Expand Up @@ -378,7 +378,7 @@ class ConnectionHandler: ChannelInboundHandler {
func subscribe(_ subject: String) async throws -> Subscription {
let sid = self.subscriptionCounter.wrappingIncrementThenLoad(
ordering: AtomicUpdateOrdering.relaxed)
try write(operation: ClientOp.Subscribe((sid, subject, nil)))
try write(operation: ClientOp.subscribe((sid, subject, nil)))
let sub = Subscription(subject: subject)
self.subscriptions[sid] = sub
return sub
Expand Down
66 changes: 33 additions & 33 deletions Sources/NatsSwift/NatsProto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,39 @@ internal enum NatsOperation: String {
}

enum ServerOp {
case Ok
case Info(ServerInfo)
case Ping
case Pong
case Error(NatsConnectionError)
case Message(MessageInbound)
case HMessage(HMessageInbound)
case ok
case info(ServerInfo)
case ping
case pong
case error(NatsConnectionError)
case message(MessageInbound)
case hMessage(HMessageInbound)

static func parse(from message: Data) throws -> ServerOp {
guard message.count > 2 else {
static func parse(from msg: Data) throws -> ServerOp {
guard msg.count > 2 else {
throw NSError(
domain: "nats_swift", code: 1,
userInfo: ["message": "unable to parse inbound message: \(message)"])
}
let msgType = message.getMessageType()
let msgType = msg.getMessageType()
switch msgType {
case .message:
return try Message(MessageInbound.parse(data: message))
return try message(MessageInbound.parse(data: msg))
case .hmessage:
return try HMessage(HMessageInbound.parse(data: message))
return try hMessage(HMessageInbound.parse(data: msg))
case .info:
return try Info(ServerInfo.parse(data: message))
return try info(ServerInfo.parse(data: msg))
case .ok:
return Ok
return ok
case .error:
if let errMsg = message.removePrefix(Data(NatsOperation.error.rawBytes)).toString() {
return Error(NatsConnectionError(errMsg))
if let errMsg = msg.removePrefix(Data(NatsOperation.error.rawBytes)).toString() {
return error(NatsConnectionError(errMsg))
}
return Error(NatsConnectionError("unexpected error"))
return error(NatsConnectionError("unexpected error"))
case .ping:
return Ping
return ping
case .pong:
return Pong
return pong
default:
throw NSError(
domain: "nats_swift", code: 1,
Expand All @@ -90,7 +90,7 @@ internal struct HMessageInbound: Equatable {
internal static func parse(data: Data) throws -> HMessageInbound {
let protoComponents =
data
.dropFirst(NatsOperation.hmessage.rawValue.count) // Assuming the message starts with "HMSG "
.dropFirst(NatsOperation.hmessage.rawValue.count) // Assuming msg starts with "HMSG "
.split(separator: space)
.filter { !$0.isEmpty }

Expand Down Expand Up @@ -146,7 +146,7 @@ internal struct MessageInbound: Equatable {
internal static func parse(data: Data) throws -> MessageInbound {
let protoComponents =
data
.dropFirst(NatsOperation.message.rawValue.count) // Assuming the message starts with "MSG "
.dropFirst(NatsOperation.message.rawValue.count) // Assuming msg starts with "MSG "
.split(separator: space)
.filter { !$0.isEmpty }

Expand Down Expand Up @@ -246,17 +246,17 @@ struct ServerInfo: Codable, Equatable {
}

enum ClientOp {
case Publish((subject: String, reply: String?, payload: Data?, headers: HeaderMap?))
case Subscribe((sid: UInt64, subject: String, queue: String?))
case Unsubscribe((sid: UInt64, max: UInt64?))
case Connect(ConnectInfo)
case Ping
case Pong
case publish((subject: String, reply: String?, payload: Data?, headers: HeaderMap?))
case subscribe((sid: UInt64, subject: String, queue: String?))
case unsubscribe((sid: UInt64, max: UInt64?))
case connect(ConnectInfo)
case ping
case pong

internal func asBytes(using allocator: ByteBufferAllocator) throws -> ByteBuffer {
var buffer: ByteBuffer
switch self {
case let .Publish((subject, reply, payload, headers)):
case .publish((let subject, let reply, let payload, let headers)):
if let payload = payload {
buffer = allocator.buffer(
capacity: payload.count + subject.utf8.count
Expand Down Expand Up @@ -295,7 +295,7 @@ enum ClientOp {
buffer.writeString("0\r\n")
}

case let .Subscribe((sid, subject, queue)):
case .subscribe((let sid, let subject, let queue)):
buffer = allocator.buffer(capacity: 0)
if let queue {
buffer.writeString(
Expand All @@ -304,24 +304,24 @@ enum ClientOp {
buffer.writeString("\(NatsOperation.subscribe.rawValue) \(subject) \(sid)\r\n")
}

case let .Unsubscribe((sid, max)):
case .unsubscribe((let sid, let max)):
buffer = allocator.buffer(capacity: 0)
if let max {
buffer.writeString("\(NatsOperation.unsubscribe.rawValue) \(sid) \(max)\r\n")
} else {
buffer.writeString("\(NatsOperation.unsubscribe.rawValue) \(sid)\r\n")
}
case let .Connect(info):
case .connect(let info):
let json = try JSONEncoder().encode(info)
buffer = allocator.buffer(capacity: json.count + 5)
buffer.writeString("\(NatsOperation.connect.rawValue) ")
buffer.writeData(json)
buffer.writeString("\r\n")
return buffer
case .Ping:
case .ping:
buffer = allocator.buffer(capacity: 8)
buffer.writeString("\(NatsOperation.ping.rawValue)\r\n")
case .Pong:
case .pong:
buffer = allocator.buffer(capacity: 8)
buffer.writeString("\(NatsOperation.pong.rawValue)\r\n")
}
Expand Down
Loading

0 comments on commit 67e12c6

Please sign in to comment.