From a9d687ebf237b50d468505bd0f0c105df518af41 Mon Sep 17 00:00:00 2001 From: AKosylo Date: Fri, 20 Oct 2023 17:10:15 +0200 Subject: [PATCH] swiftlint fixes. warning fixes add isTools flag for CLI utils --- .../xcshareddata/xcschemes/Tools.xcscheme | 8 ++++++-- DXFeedFramework/Events/Market/OptionSale.swift | 4 ++-- .../Native/Events/Markets/OptionSaleMapper.swift | 2 +- DXFeedFramework/Native/Graal/ThreadManager.swift | 2 +- .../Live/NativeInstrumentProfileConnection.swift | 3 ++- .../Native/Schedule/NativeSchedule.swift | 6 +++--- DXFeedFramework/Utils/String+Range.swift | 2 ++ .../Listeners/TestEndpoointStateListener.swift | 2 +- DXFeedFrameworkTests/PublisherTest.swift | 16 ++++++++++++---- Samples/PerfTestCL/Arguments.swift | 6 ++++++ Samples/PerfTestCL/ConnectCommand.swift | 3 ++- Samples/PerfTestCL/DumpCommand.swift | 8 ++------ Samples/PerfTestCL/HelpCommand.swift | 1 + Samples/PerfTestCL/IpfConnectCommand.swift | 1 + Samples/PerfTestCL/LatencyTestCommand.swift | 3 ++- Samples/PerfTestCL/LiveIpfCommand.swift | 3 ++- Samples/PerfTestCL/PerfTestCommand.swift | 4 ++-- Samples/PerfTestCL/ScheduleCommand.swift | 6 +++++- Samples/PerfTestCL/SubscriptionUtils.swift | 8 ++++---- Samples/PerfTestCL/ToolsCommand.swift | 1 + Samples/PerfTestCL/main.swift | 10 +++++++--- 21 files changed, 65 insertions(+), 34 deletions(-) diff --git a/DXFeedFramework.xcodeproj/xcshareddata/xcschemes/Tools.xcscheme b/DXFeedFramework.xcodeproj/xcshareddata/xcschemes/Tools.xcscheme index b414c38f5..d1b9b2a23 100644 --- a/DXFeedFramework.xcodeproj/xcshareddata/xcschemes/Tools.xcscheme +++ b/DXFeedFramework.xcodeproj/xcshareddata/xcschemes/Tools.xcscheme @@ -55,6 +55,10 @@ argument = "PerfTest localhost:6666 TimeAndSale YQKNT" isEnabled = "NO"> + + @@ -76,12 +80,12 @@ isEnabled = "NO"> + isEnabled = "NO"> .allocate(capacity: 1) - let value = graal_attach_thread(Isolate.shared.isolate.pointee, threadPointer) + _ = graal_attach_thread(Isolate.shared.isolate.pointee, threadPointer) _ = pthread_setspecific(ThreadManager.key.pointee, threadPointer) return threadPointer } diff --git a/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnection.swift b/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnection.swift index ecf976320..7dfe9866c 100644 --- a/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnection.swift +++ b/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnection.swift @@ -33,7 +33,8 @@ class NativeInstrumentProfileConnection { } } - private static let listenerCallback: dxfg_ipf_connection_state_change_listener_func = {_, oldState, newState, context in + private static let listenerCallback: dxfg_ipf_connection_state_change_listener_func + = {_, oldState, newState, context in if let context = context { let endpoint: AnyObject = bridge(ptr: context) if let listener = endpoint as? WeakListener { diff --git a/DXFeedFramework/Native/Schedule/NativeSchedule.swift b/DXFeedFramework/Native/Schedule/NativeSchedule.swift index 687dc1ee7..beaebabfa 100644 --- a/DXFeedFramework/Native/Schedule/NativeSchedule.swift +++ b/DXFeedFramework/Native/Schedule/NativeSchedule.swift @@ -248,9 +248,9 @@ class NativeSchedule { public static func setDefaults(_ data: Data) throws { let thread = currentThread() _ = try data.withUnsafeBytes({ pointer in - let result = try ErrorCheck.nativeCall(thread, dxfg_Schedule_setDefaults(thread, - pointer.baseAddress, - Int32(data.count))) + _ = try ErrorCheck.nativeCall(thread, dxfg_Schedule_setDefaults(thread, + pointer.baseAddress, + Int32(data.count))) }) } diff --git a/DXFeedFramework/Utils/String+Range.swift b/DXFeedFramework/Utils/String+Range.swift index 934ce220a..1036e6793 100644 --- a/DXFeedFramework/Utils/String+Range.swift +++ b/DXFeedFramework/Utils/String+Range.swift @@ -99,9 +99,11 @@ extension String.Index { } extension String { + // swiftlint:disable identifier_name public func slice(from: String, to: String) -> String? { guard let rangeFrom = range(of: from)?.upperBound else { return nil } guard let rangeTo = self[rangeFrom...].range(of: to)?.lowerBound else { return nil } return String(self[rangeFrom.. Bool { diff --git a/DXFeedFrameworkTests/PublisherTest.swift b/DXFeedFrameworkTests/PublisherTest.swift index 07a8511ff..b81099542 100644 --- a/DXFeedFrameworkTests/PublisherTest.swift +++ b/DXFeedFrameworkTests/PublisherTest.swift @@ -24,17 +24,25 @@ final class PublisherTest: XCTestCase { func execute() throws { do { - let endpoint: DXEndpoint? = try DXEndpoint.builder().withRole(.publisher).withProperty("test", "value").build() + let endpoint: DXEndpoint? = try DXEndpoint + .builder() + .withRole(.publisher) + .withProperty("test", "value") + .build() try endpoint?.connect(":7400") let testQuote = Quote("AAPL") testQuote.bidSize = 100 testQuote.askPrice = 666 try? testQuote.setSequence(10) - let feedEndpoint = try DXEndpoint.builder().withRole(.feed).withProperty("test", "value").build() - var publisher = endpoint?.getPublisher() + let feedEndpoint = try DXEndpoint + .builder() + .withRole(.feed) + .withProperty("test", "value") + .build() + let publisher = endpoint?.getPublisher() let connectedExpectation = expectation(description: "Connected") - var stateListener: TestEndpoointStateListener? = TestEndpoointStateListener { listener in + let stateListener: TestEndpoointStateListener? = TestEndpoointStateListener { listener in listener.callback = { state in if state == .connected { connectedExpectation.fulfill() diff --git a/Samples/PerfTestCL/Arguments.swift b/Samples/PerfTestCL/Arguments.swift index c17f24ff3..75e173e84 100644 --- a/Samples/PerfTestCL/Arguments.swift +++ b/Samples/PerfTestCL/Arguments.swift @@ -73,6 +73,12 @@ Cmd \(cmd) contains not enough \(cmd.count - 1) arguments. Expected \(requiredNu arguments.count } + public func parseTypes(at index: Int) -> [EventCode] { + return arguments[2].split(separator: ",").compactMap { str in + return EventCode(string: String(str)) + } + } + public func parseSymbols(at index: Int) -> [Symbol] { let symbols = arguments[index] if symbols.lowercased() == "all" { diff --git a/Samples/PerfTestCL/ConnectCommand.swift b/Samples/PerfTestCL/ConnectCommand.swift index 1094616af..bd3450ca7 100644 --- a/Samples/PerfTestCL/ConnectCommand.swift +++ b/Samples/PerfTestCL/ConnectCommand.swift @@ -9,6 +9,7 @@ import Foundation import DXFeedFramework class ConnectCommand: ToolsCommand { + var isTools: Bool = true var cmd = "Connect" var shortDescription = "Connects to specified address(es)." @@ -55,7 +56,7 @@ Where: let listener = ConnectEventListener() subscription.createSubscription(address: arguments[1], symbols: arguments.parseSymbols(at: 3), - types: arguments[2], + types: arguments.parseTypes(at: 2), listener: listener, properties: arguments.properties, time: arguments.time) diff --git a/Samples/PerfTestCL/DumpCommand.swift b/Samples/PerfTestCL/DumpCommand.swift index 4c0e1da0d..30fa821c5 100644 --- a/Samples/PerfTestCL/DumpCommand.swift +++ b/Samples/PerfTestCL/DumpCommand.swift @@ -9,6 +9,7 @@ import Foundation import DXFeedFramework class DumpCommand: ToolsCommand { + var isTools: Bool = true var cmd: String = "Dump" var shortDescription = "Dumps all events received from address." @@ -41,10 +42,8 @@ class DumpCommand: ToolsCommand { } }() - func execute() { let address = arguments[1] - let types = arguments[2] let symbols = arguments.parseSymbols(at: 3) isQuite = arguments.isQuite @@ -58,9 +57,7 @@ class DumpCommand: ToolsCommand { .withName("DumpTool") .build() - let eventTypes = types.split(separator: ",").compactMap { str in - return EventCode(string: String(str)) - } + let eventTypes = arguments.parseTypes(at: 2) let subscription = try inputEndpoint.getFeed()?.createSubscription(eventTypes) var outputEndpoint: DXEndpoint? @@ -76,7 +73,6 @@ class DumpCommand: ToolsCommand { } try subscription?.add(observer: self) - try subscription?.addSymbols(symbols) try inputEndpoint.connect(address) diff --git a/Samples/PerfTestCL/HelpCommand.swift b/Samples/PerfTestCL/HelpCommand.swift index 09adfe4a4..925e4c9fd 100644 --- a/Samples/PerfTestCL/HelpCommand.swift +++ b/Samples/PerfTestCL/HelpCommand.swift @@ -8,6 +8,7 @@ import Foundation class HelpCommand: ToolsCommand { + var isTools: Bool = true var cmd = "Help" var shortDescription = "Help tool." diff --git a/Samples/PerfTestCL/IpfConnectCommand.swift b/Samples/PerfTestCL/IpfConnectCommand.swift index 194d51ce1..e76285bc7 100644 --- a/Samples/PerfTestCL/IpfConnectCommand.swift +++ b/Samples/PerfTestCL/IpfConnectCommand.swift @@ -9,6 +9,7 @@ import Foundation import DXFeedFramework class IpfConnectCommand: ToolsCommand { + var isTools: Bool = false lazy var name = { stringReference(self) }() diff --git a/Samples/PerfTestCL/LatencyTestCommand.swift b/Samples/PerfTestCL/LatencyTestCommand.swift index 9af5894da..b705def4a 100644 --- a/Samples/PerfTestCL/LatencyTestCommand.swift +++ b/Samples/PerfTestCL/LatencyTestCommand.swift @@ -8,6 +8,7 @@ import Foundation class LatencyTestCommand: ToolsCommand { + var isTools: Bool = true var cmd = "LatencyTest" var shortDescription = "Connects to the specified address(es) and calculates latency." @@ -42,7 +43,7 @@ class LatencyTestCommand: ToolsCommand { func execute() { let address = arguments[1] - let types = arguments[2] + let types = arguments.parseTypes(at: 2) let listener = LatencyEventListener() diff --git a/Samples/PerfTestCL/LiveIpfCommand.swift b/Samples/PerfTestCL/LiveIpfCommand.swift index 28904793f..9b62756b2 100644 --- a/Samples/PerfTestCL/LiveIpfCommand.swift +++ b/Samples/PerfTestCL/LiveIpfCommand.swift @@ -9,6 +9,7 @@ import Foundation import DXFeedFramework class LiveIpfCommand: ToolsCommand { + var isTools: Bool = false lazy var name = { stringReference(self) }() @@ -51,7 +52,7 @@ class LiveIpfCommand: ToolsCommand { func execute() { do { collector = try DXInstrumentProfileCollector() - connection = try DXInstrumentProfileConnection(arguments[1], collector!) + connection = try DXInstrumentProfileConnection(arguments.count > 1 ? arguments[1] : LiveIpfCommand.defaultIpfUrl, collector!) // Update period can be used to re-read IPF files, not needed for services supporting IPF "live-update" try connection?.setUpdatePeriod(60000) connection?.add(observer: self) diff --git a/Samples/PerfTestCL/PerfTestCommand.swift b/Samples/PerfTestCL/PerfTestCommand.swift index 7b2a94a0e..cb6c4cca1 100644 --- a/Samples/PerfTestCL/PerfTestCommand.swift +++ b/Samples/PerfTestCL/PerfTestCommand.swift @@ -9,6 +9,7 @@ import Foundation import DXFeedFramework class PerfTestCommand: ToolsCommand { + var isTools: Bool = true var cmd: String = "PerfTest" var shortDescription: String = "Connects to specified address and calculates performance counters." var fullDescription: String = @@ -39,13 +40,12 @@ class PerfTestCommand: ToolsCommand { var subscription = Subscription() func execute() { let address = arguments[1] - let types = arguments[2] let listener = PerfTestEventListener() subscription.createSubscription(address: address, symbols: arguments.parseSymbols(at: 3), - types: types, + types: arguments.parseTypes(at: 2), listener: listener, properties: arguments.properties, time: nil) diff --git a/Samples/PerfTestCL/ScheduleCommand.swift b/Samples/PerfTestCL/ScheduleCommand.swift index c9598cb1a..389286c0d 100644 --- a/Samples/PerfTestCL/ScheduleCommand.swift +++ b/Samples/PerfTestCL/ScheduleCommand.swift @@ -9,6 +9,7 @@ import Foundation import DXFeedFramework class ScheduleCommand: ToolsCommand { + var isTools: Bool = false var cmd = "ScheduleSample" var shortDescription = "A sample program that demonstrates different use cases of Schedule API." @@ -94,7 +95,10 @@ class ScheduleCommand: ToolsCommand { } } - print("Checked \(profiles.count) instrument profiles: \(successes) successes, \((profiles.count - successes)) failures") + print( +""" +Checked \(profiles.count) instrument profiles: \(successes) successes, \((profiles.count - successes)) failures +""") } } diff --git a/Samples/PerfTestCL/SubscriptionUtils.swift b/Samples/PerfTestCL/SubscriptionUtils.swift index 94f237235..7cdfe314e 100644 --- a/Samples/PerfTestCL/SubscriptionUtils.swift +++ b/Samples/PerfTestCL/SubscriptionUtils.swift @@ -14,13 +14,13 @@ class Subscription { func createSubscription(address: String, symbols: [Symbol], - types: String, + types: [EventCode], listener: O, properties: [String: String], time: String?) where O: DXEventListener, O: Hashable { print(""" -Create subscription to \(address) for \(types):\(symbols) with properties:\(properties) and time \(time) +Create subscription to \(address) for \(types):\(symbols) with properties:\(properties) and time \(time ?? "---") """) endpoint = try? DXEndpoint .builder() @@ -28,8 +28,8 @@ Create subscription to \(address) for \(types):\(symbols) with properties:\(prop .withProperties(properties) .build() _ = try? endpoint?.connect(address) - types.split(separator: ",").forEach { str in - let subscription = try? endpoint?.getFeed()?.createSubscription(EventCode(string: String(str))) + types.forEach { str in + let subscription = try? endpoint?.getFeed()?.createSubscription(str) try? subscription?.add(observer: listener) if time != nil { guard let date = TimeUtil.parse(time!) else { diff --git a/Samples/PerfTestCL/ToolsCommand.swift b/Samples/PerfTestCL/ToolsCommand.swift index 2acc82caa..c6ecf07a9 100644 --- a/Samples/PerfTestCL/ToolsCommand.swift +++ b/Samples/PerfTestCL/ToolsCommand.swift @@ -8,6 +8,7 @@ import Foundation protocol ToolsCommand { + var isTools: Bool { get } var cmd: String { get } var shortDescription: String { get } var fullDescription: String { get } diff --git a/Samples/PerfTestCL/main.swift b/Samples/PerfTestCL/main.swift index 34da5779f..efe4b61dc 100644 --- a/Samples/PerfTestCL/main.swift +++ b/Samples/PerfTestCL/main.swift @@ -26,9 +26,13 @@ func getCommandsDecription() -> String { let maxSize = (commands.max { cmd1, cmd2 in cmd1.cmd.count < cmd2.cmd.count }?.cmd.count ?? 0) + 4 - let descriptions = commands.map { cmd in - let spaces = maxSize - cmd.cmd.count - return " \(cmd.cmd + String(repeating: " ", count: spaces)): \(cmd.shortDescription)" + let descriptions = commands.compactMap { cmd in + if cmd.isTools { + let spaces = maxSize - cmd.cmd.count + return " \(cmd.cmd + String(repeating: " ", count: spaces)): \(cmd.shortDescription)" + } else { + return nil + } }.joined(separator: "\n") return descriptions