From 3838216c484aa9dad68fa55a4b7399182f9c1417 Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Sat, 21 Mar 2020 14:58:22 +0100 Subject: [PATCH] Created cli tool `JSONTestSuiteCLI` --- Package.resolved | 25 ++++++++++++++++++ Package.swift | 11 +++++++- Sources/JSONTestSuiteCLI/main.swift | 39 +++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 Package.resolved create mode 100644 Sources/JSONTestSuiteCLI/main.swift diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..612a145 --- /dev/null +++ b/Package.resolved @@ -0,0 +1,25 @@ +{ + "object": { + "pins": [ + { + "package": "swift-argument-parser", + "repositoryURL": "https://github.com/apple/swift-argument-parser.git", + "state": { + "branch": null, + "revision": "35b76bf577d3cc74820f8991894ce3bcdf024ddc", + "version": "0.0.2" + } + }, + { + "package": "swift-nio", + "repositoryURL": "https://github.com/apple/swift-nio", + "state": { + "branch": null, + "revision": "a27a07719ca785bcaca019a5b9fe1814b981b4a2", + "version": "2.15.0" + } + } + ] + }, + "version": 1 +} diff --git a/Package.swift b/Package.swift index ec0bb82..b7bab68 100644 --- a/Package.swift +++ b/Package.swift @@ -14,9 +14,18 @@ var package = Package( targets: ["PureSwiftJSONParsing"]), ], dependencies: [ - + // these are only used for testing + .package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMajor(from: "0.0.1")), + .package(url: "https://github.com/apple/swift-nio", .upToNextMajor(from: "2.13.0")) ], targets: [ + .target( + name: "JSONTestSuiteCLI", + dependencies: [ + .product(name: "ArgumentParser", package: "swift-argument-parser"), + .product(name: "NIO", package: "swift-nio"), + ] + ), .target( name: "PureSwiftJSONCoding", dependencies: ["PureSwiftJSONParsing"]), diff --git a/Sources/JSONTestSuiteCLI/main.swift b/Sources/JSONTestSuiteCLI/main.swift new file mode 100644 index 0000000..93db471 --- /dev/null +++ b/Sources/JSONTestSuiteCLI/main.swift @@ -0,0 +1,39 @@ +import ArgumentParser +import NIO + +struct JSONTestSuiteCLI: ParsableCommand { + + @Argument(help: "the json file to parse") var jsonFile: String + + func run() throws { + // here lives the logic + + let bytes = try self.read(file: jsonFile) + + + } + + func read(file: String) throws -> [UInt8] { + + let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) + defer { + try! group.syncShutdownGracefully() + } + let loop = group.next() + let threadPool = NIOThreadPool(numberOfThreads: 1) + threadPool.start() + defer { + try! threadPool.syncShutdownGracefully() + } + let fileIO = NonBlockingFileIO(threadPool: threadPool) + + let (handle, region) = try fileIO.openFile(path: file, eventLoop: loop).wait() + let allocator = ByteBufferAllocator() + var buffer = try fileIO.read(fileRegion: region, allocator: allocator, eventLoop: loop).wait() + + return buffer.readBytes(length: buffer.readableBytes)! + } + +} + +JSONTestSuiteCLI.main()