From be980ce6d723d8104068a2a29c7c1f20e42fef12 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Fri, 17 Jun 2016 11:44:23 +0200 Subject: [PATCH] Throwing parsers (as opposed to trapping) --- Podfile | 2 +- Podfile.lock | 10 ++-- XcodeServerSDK.xcodeproj/project.pbxproj | 30 +++++------ .../API Routes/XcodeServer+Bot.swift | 22 +++++--- .../API Routes/XcodeServer+Device.swift | 6 ++- .../API Routes/XcodeServer+Integration.swift | 38 ++++++++------ .../API Routes/XcodeServer+LiveUpdates.swift | 11 ++-- .../API Routes/XcodeServer+Platform.swift | 6 ++- .../API Routes/XcodeServer+Repository.swift | 16 ++++-- .../API Routes/XcodeServer+Toolchain.swift | 6 ++- XcodeServerSDK/Server Entities/Bot.swift | 8 +-- .../Server Entities/BotConfiguration.swift | 22 ++++---- .../Server Entities/BotSchedule.swift | 10 ++-- XcodeServerSDK/Server Entities/Commit.swift | 14 +++--- .../Server Entities/Contributor.swift | 10 ++-- XcodeServerSDK/Server Entities/Device.swift | 35 ++++++------- .../Server Entities/DeviceSpecification.swift | 24 ++++----- .../Server Entities/EmailConfiguration.swift | 12 ++--- XcodeServerSDK/Server Entities/File.swift | 8 +-- .../Server Entities/Integration.swift | 50 +++++++++---------- .../Server Entities/IntegrationCommits.swift | 18 +++---- .../Server Entities/IntegrationIssue.swift | 16 +++--- .../Server Entities/IntegrationIssues.swift | 42 ++++++++++++---- .../Server Entities/LiveUpdateMessage.swift | 4 +- .../Server Entities/Repository.swift | 14 +++--- .../SourceControlBlueprint.swift | 26 +++++----- .../Server Entities/TestHierarchy.swift | 4 +- .../Server Entities/Toolchain.swift | 11 ++-- XcodeServerSDK/Server Entities/Trigger.swift | 20 ++++---- .../Server Entities/TriggerConditions.swift | 16 +++--- XcodeServerSDK/XcodeServer.swift | 1 + XcodeServerSDK/XcodeServerEntity.swift | 17 +++---- XcodeServerSDKTests/BotParsingTests.swift | 4 +- XcodeServerSDKTests/ContributorTests.swift | 4 +- XcodeServerSDKTests/DevicesTests.swift | 2 +- XcodeServerSDKTests/FileTests.swift | 10 ++-- XcodeServerSDKTests/IssueTests.swift | 4 +- XcodeServerSDKTests/LiveUpdatesTests.swift | 8 +-- XcodeServerSDKTests/RepositoryTests.swift | 4 +- XcodeServerSDKTests/TestUtils.swift | 12 ++--- 40 files changed, 320 insertions(+), 257 deletions(-) diff --git a/Podfile b/Podfile index b637aa4..746f136 100644 --- a/Podfile +++ b/Podfile @@ -2,7 +2,7 @@ use_frameworks! def utils - pod 'BuildaUtils', '~> 0.2.3' + pod 'BuildaUtils', '~> 0.3.2' end def tests diff --git a/Podfile.lock b/Podfile.lock index ac05629..fcd8d8d 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -1,12 +1,12 @@ PODS: - - BuildaUtils (0.2.8): + - BuildaUtils (0.3.2): - SwiftSafe (~> 0.1) - DVR (0.0.4-czechboy0) - Nimble (3.1.0) - SwiftSafe (0.1) DEPENDENCIES: - - BuildaUtils (~> 0.2.3) + - BuildaUtils (~> 0.3.2) - DVR (from `https://github.com/czechboy0/DVR.git`, tag `v0.0.5-czechboy0`) - Nimble (from `https://github.com/Quick/Nimble.git`, commit `b9256b0bdecc4ef1f659b7663dcd3aab6f43fb5f`) @@ -27,11 +27,11 @@ CHECKOUT OPTIONS: :git: https://github.com/Quick/Nimble.git SPEC CHECKSUMS: - BuildaUtils: 0974c128ace30916b9b9429e8585c1c6402593dd + BuildaUtils: 1c6bf3a28948c3aae242171abf2474cd4746a2d1 DVR: 386f347071f55f3f9105239db6764483009ec875 Nimble: eb2a9b164b9a3f16df6581c692a0bfced7d072a4 SwiftSafe: 77ffd12b02678790bec1ef56a2d14ec5036f1fd6 -PODFILE CHECKSUM: 67beeffd0d8e791afcfda6d3008101471799f0d9 +PODFILE CHECKSUM: 2d0c8056afb549d3590407decab7568949826dc2 -COCOAPODS: 1.0.0.beta.3 +COCOAPODS: 1.0.1 diff --git a/XcodeServerSDK.xcodeproj/project.pbxproj b/XcodeServerSDK.xcodeproj/project.pbxproj index 93d0827..806413c 100644 --- a/XcodeServerSDK.xcodeproj/project.pbxproj +++ b/XcodeServerSDK.xcodeproj/project.pbxproj @@ -419,12 +419,12 @@ isa = PBXNativeTarget; buildConfigurationList = 3A7B48D01B2A5AC40077ABEA /* Build configuration list for PBXNativeTarget "XcodeServerSDK" */; buildPhases = ( - 608915A9B6E46B97563824D6 /* Check Pods Manifest.lock */, + 608915A9B6E46B97563824D6 /* [CP] Check Pods Manifest.lock */, 3A7B48B51B2A5AC40077ABEA /* Sources */, 3A7B48B61B2A5AC40077ABEA /* Frameworks */, 3A7B48B71B2A5AC40077ABEA /* Headers */, 3A7B48B81B2A5AC40077ABEA /* Resources */, - BEE968EB4C2D07460226F3D4 /* Copy Pods Resources */, + BEE968EB4C2D07460226F3D4 /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -439,12 +439,12 @@ isa = PBXNativeTarget; buildConfigurationList = 3A7B48D31B2A5AC40077ABEA /* Build configuration list for PBXNativeTarget "XcodeServerSDKTests" */; buildPhases = ( - 729BDC3F596341C91349E495 /* Check Pods Manifest.lock */, + 729BDC3F596341C91349E495 /* [CP] Check Pods Manifest.lock */, 3A7B48C11B2A5AC40077ABEA /* Sources */, 3A7B48C21B2A5AC40077ABEA /* Frameworks */, 3A7B48C31B2A5AC40077ABEA /* Resources */, - 0C8961B6A622E40751509352 /* Embed Pods Frameworks */, - 3B64028920C09D09701D0715 /* Copy Pods Resources */, + 0C8961B6A622E40751509352 /* [CP] Embed Pods Frameworks */, + 3B64028920C09D09701D0715 /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -528,14 +528,14 @@ /* End PBXResourcesBuildPhase section */ /* Begin PBXShellScriptBuildPhase section */ - 0C8961B6A622E40751509352 /* Embed Pods Frameworks */ = { + 0C8961B6A622E40751509352 /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( ); inputPaths = ( ); - name = "Embed Pods Frameworks"; + name = "[CP] Embed Pods Frameworks"; outputPaths = ( ); runOnlyForDeploymentPostprocessing = 0; @@ -543,14 +543,14 @@ shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-XcodeServerSDKTests/Pods-XcodeServerSDKTests-frameworks.sh\"\n"; showEnvVarsInLog = 0; }; - 3B64028920C09D09701D0715 /* Copy Pods Resources */ = { + 3B64028920C09D09701D0715 /* [CP] Copy Pods Resources */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( ); inputPaths = ( ); - name = "Copy Pods Resources"; + name = "[CP] Copy Pods Resources"; outputPaths = ( ); runOnlyForDeploymentPostprocessing = 0; @@ -558,14 +558,14 @@ shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-XcodeServerSDKTests/Pods-XcodeServerSDKTests-resources.sh\"\n"; showEnvVarsInLog = 0; }; - 608915A9B6E46B97563824D6 /* Check Pods Manifest.lock */ = { + 608915A9B6E46B97563824D6 /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( ); inputPaths = ( ); - name = "Check Pods Manifest.lock"; + name = "[CP] Check Pods Manifest.lock"; outputPaths = ( ); runOnlyForDeploymentPostprocessing = 0; @@ -573,14 +573,14 @@ shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n"; showEnvVarsInLog = 0; }; - 729BDC3F596341C91349E495 /* Check Pods Manifest.lock */ = { + 729BDC3F596341C91349E495 /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( ); inputPaths = ( ); - name = "Check Pods Manifest.lock"; + name = "[CP] Check Pods Manifest.lock"; outputPaths = ( ); runOnlyForDeploymentPostprocessing = 0; @@ -588,14 +588,14 @@ shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n"; showEnvVarsInLog = 0; }; - BEE968EB4C2D07460226F3D4 /* Copy Pods Resources */ = { + BEE968EB4C2D07460226F3D4 /* [CP] Copy Pods Resources */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( ); inputPaths = ( ); - name = "Copy Pods Resources"; + name = "[CP] Copy Pods Resources"; outputPaths = ( ); runOnlyForDeploymentPostprocessing = 0; diff --git a/XcodeServerSDK/API Routes/XcodeServer+Bot.swift b/XcodeServerSDK/API Routes/XcodeServer+Bot.swift index 10e2d90..2eb17a0 100644 --- a/XcodeServerSDK/API Routes/XcodeServer+Bot.swift +++ b/XcodeServerSDK/API Routes/XcodeServer+Bot.swift @@ -80,8 +80,10 @@ extension XcodeServer { } if let body = (body as? NSDictionary)?["results"] as? NSArray { - let bots: [Bot] = XcodeServerArray(body) - completion(bots: bots, error: nil) + let (result, error): ([Bot]?, NSError?) = unthrow { + return try XcodeServerArray(body) + } + completion(bots: result, error: error) } else { completion(bots: nil, error: Error.withInfo("Wrong data returned: \(body)")) } @@ -109,8 +111,10 @@ extension XcodeServer { } if let body = body as? NSDictionary { - let bot = Bot(json: body) - completion(bot: bot, error: nil) + let (result, error): (Bot?, NSError?) = unthrow { + return try Bot(json: body) + } + completion(bot: result, error: error) } else { completion(bot: nil, error: Error.withInfo("Wrong body \(body)")) } @@ -205,8 +209,14 @@ extension XcodeServer { return } - let bot = Bot(json: dictBody) - completion(response: XcodeServer.CreateBotResponse.Success(bot: bot)) + let (result, error): (Bot?, NSError?) = unthrow { + return try Bot(json: dictBody) + } + if let err = error { + completion(response: XcodeServer.CreateBotResponse.Error(error: err)) + } else { + completion(response: XcodeServer.CreateBotResponse.Success(bot: result!)) + } } } diff --git a/XcodeServerSDK/API Routes/XcodeServer+Device.swift b/XcodeServerSDK/API Routes/XcodeServer+Device.swift index 745ee05..6f40048 100644 --- a/XcodeServerSDK/API Routes/XcodeServer+Device.swift +++ b/XcodeServerSDK/API Routes/XcodeServer+Device.swift @@ -28,8 +28,10 @@ extension XcodeServer { } if let array = (body as? NSDictionary)?["results"] as? NSArray { - let devices: [Device] = XcodeServerArray(array) - completion(devices: devices, error: error) + let (result, error): ([Device]?, NSError?) = unthrow { + return try XcodeServerArray(array) + } + completion(devices: result, error: error) } else { completion(devices: nil, error: Error.withInfo("Wrong body \(body)")) } diff --git a/XcodeServerSDK/API Routes/XcodeServer+Integration.swift b/XcodeServerSDK/API Routes/XcodeServer+Integration.swift index b5b8ce5..d0bbbe5 100644 --- a/XcodeServerSDK/API Routes/XcodeServer+Integration.swift +++ b/XcodeServerSDK/API Routes/XcodeServer+Integration.swift @@ -40,8 +40,10 @@ extension XcodeServer { } if let body = (body as? NSDictionary)?["results"] as? NSArray { - let integrations: [Integration] = XcodeServerArray(body) - completion(integrations: integrations, error: nil) + let (result, error): ([Integration]?, NSError?) = unthrow { + return try XcodeServerArray(body) + } + completion(integrations: result, error: error) } else { completion(integrations: nil, error: Error.withInfo("Wrong body \(body)")) } @@ -69,8 +71,10 @@ extension XcodeServer { } if let body = body as? NSDictionary { - let integration = Integration(json: body) - completion(integration: integration, error: nil) + let (result, error): (Integration?, NSError?) = unthrow { + return try Integration(json: body) + } + completion(integration: result, error: error) } else { completion(integration: nil, error: Error.withInfo("Wrong body \(body)")) } @@ -100,8 +104,10 @@ extension XcodeServer { return } - let integrations: [Integration] = XcodeServerArray(integrationsBody) - completion(integrations: integrations, error: nil) + let (result, error): ([Integration]?, NSError?) = unthrow { + return try XcodeServerArray(integrationsBody) + } + completion(integrations: result, error: error) } } @@ -132,8 +138,10 @@ extension XcodeServer { return } - let integration = Integration(json: integrationBody) - completion(integration: integration, error: nil) + let (result, error): (Integration?, NSError?) = unthrow { + return try Integration(json: integrationBody) + } + completion(integration: result, error: error) } } @@ -186,9 +194,10 @@ extension XcodeServer { return } - let integrationCommits = IntegrationCommits(json: integrationCommitsBody[0] as! NSDictionary) - completion(integrationCommits: integrationCommits, error: nil) - + let (result, error): (IntegrationCommits?, NSError?) = unthrow { + return try IntegrationCommits(json: integrationCommitsBody[0] as! NSDictionary) + } + completion(integrationCommits: result, error: error) } } @@ -218,9 +227,10 @@ extension XcodeServer { return } - let integrationIssues = IntegrationIssues(json: integrationIssuesBody) - completion(integrationIssues: integrationIssues, error: nil) - + let (result, error): (IntegrationIssues?, NSError?) = unthrow { + return try IntegrationIssues(json: integrationIssuesBody) + } + completion(integrationIssues: result, error: error) } } diff --git a/XcodeServerSDK/API Routes/XcodeServer+LiveUpdates.swift b/XcodeServerSDK/API Routes/XcodeServer+LiveUpdates.swift index cdc41f1..16013f6 100644 --- a/XcodeServerSDK/API Routes/XcodeServer+LiveUpdates.swift +++ b/XcodeServerSDK/API Routes/XcodeServer+LiveUpdates.swift @@ -107,11 +107,16 @@ extension XcodeServer { self.sendRequest(state, params: params) { [weak self] (message) -> () in let packets = SocketIOHelper.parsePackets(message) - self?.handlePackets(packets, state: state) + + do { + try self?.handlePackets(packets, state: state) + } catch { + state.error(error) + } } } - private func handlePackets(packets: [SocketIOPacket], state: LiveUpdateState) { + private func handlePackets(packets: [SocketIOPacket], state: LiveUpdateState) throws { //check for errors if let lastPacket = packets.last where lastPacket.type == .Error { @@ -129,7 +134,7 @@ extension XcodeServer { //we good? let events = packets.filter { $0.type == .Event } let validEvents = events.filter { $0.jsonPayload != nil } - let messages = validEvents.map { LiveUpdateMessage(json: $0.jsonPayload!) } + let messages = try validEvents.map { try LiveUpdateMessage(json: $0.jsonPayload!) } if messages.count > 0 { state.messageHandler?(messages: messages) } diff --git a/XcodeServerSDK/API Routes/XcodeServer+Platform.swift b/XcodeServerSDK/API Routes/XcodeServer+Platform.swift index b5863f1..213118d 100644 --- a/XcodeServerSDK/API Routes/XcodeServer+Platform.swift +++ b/XcodeServerSDK/API Routes/XcodeServer+Platform.swift @@ -28,8 +28,10 @@ extension XcodeServer { } if let array = (body as? NSDictionary)?["results"] as? NSArray { - let platforms: [DevicePlatform] = XcodeServerArray(array) - completion(platforms: platforms, error: error) + let (result, error): ([DevicePlatform]?, NSError?) = unthrow { + return try XcodeServerArray(array) + } + completion(platforms: result, error: error) } else { completion(platforms: nil, error: Error.withInfo("Wrong body \(body)")) } diff --git a/XcodeServerSDK/API Routes/XcodeServer+Repository.swift b/XcodeServerSDK/API Routes/XcodeServer+Repository.swift index 29096fc..bfb204c 100644 --- a/XcodeServerSDK/API Routes/XcodeServer+Repository.swift +++ b/XcodeServerSDK/API Routes/XcodeServer+Repository.swift @@ -31,8 +31,10 @@ extension XcodeServer { return } - let repos: [Repository] = XcodeServerArray(repositoriesBody) - completion(repositories: repos, error: nil) + let (result, error): ([Repository]?, NSError?) = unthrow { + return try XcodeServerArray(repositoriesBody) + } + completion(repositories: result, error: error) } } @@ -89,8 +91,14 @@ extension XcodeServer { return } - let repository = Repository(json: repositoryBody) - completion(response: XcodeServer.CreateRepositoryResponse.Success(repository)) + let (result, error): (Repository?, NSError?) = unthrow { + return try Repository(json: repositoryBody) + } + if let error = error { + completion(response: .Error(error)) + } else { + completion(response: .Success(result!)) + } } } diff --git a/XcodeServerSDK/API Routes/XcodeServer+Toolchain.swift b/XcodeServerSDK/API Routes/XcodeServer+Toolchain.swift index 4c466c6..883b63c 100644 --- a/XcodeServerSDK/API Routes/XcodeServer+Toolchain.swift +++ b/XcodeServerSDK/API Routes/XcodeServer+Toolchain.swift @@ -26,8 +26,10 @@ extension XcodeServer { } if let body = (body as? NSDictionary)?["results"] as? NSArray { - let toolchains: [Toolchain] = XcodeServerArray(body) - completion(toolchains: toolchains, error: nil) + let (result, error): ([Toolchain]?, NSError?) = unthrow { _ in + return try XcodeServerArray(body) + } + completion(toolchains: result, error: error) } else { completion(toolchains: nil, error: Error.withInfo("Wrong body \(body)")) } diff --git a/XcodeServerSDK/Server Entities/Bot.swift b/XcodeServerSDK/Server Entities/Bot.swift index 64755b6..f17b6d7 100644 --- a/XcodeServerSDK/Server Entities/Bot.swift +++ b/XcodeServerSDK/Server Entities/Bot.swift @@ -14,13 +14,13 @@ public class Bot : XcodeServerEntity { public let configuration: BotConfiguration public let integrationsCount: Int - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { - self.name = json.stringForKey("name") - self.configuration = BotConfiguration(json: json.dictionaryForKey("configuration")) + self.name = try json.stringForKey("name") + self.configuration = try BotConfiguration(json: try json.dictionaryForKey("configuration")) self.integrationsCount = json.optionalIntForKey("integration_counter") ?? 0 - super.init(json: json) + try super.init(json: json) } /** diff --git a/XcodeServerSDK/Server Entities/BotConfiguration.swift b/XcodeServerSDK/Server Entities/BotConfiguration.swift index 0a9de7c..46c131a 100644 --- a/XcodeServerSDK/Server Entities/BotConfiguration.swift +++ b/XcodeServerSDK/Server Entities/BotConfiguration.swift @@ -111,9 +111,9 @@ public class BotConfiguration : XcodeServerEntity { public let deviceSpecification: DeviceSpecification public let sourceControlBlueprint: SourceControlBlueprint - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { - self.builtFromClean = CleaningPolicy(rawValue: json.intForKey("builtFromClean")) ?? .Never + self.builtFromClean = CleaningPolicy(rawValue: try json.intForKey("builtFromClean")) ?? .Never self.codeCoveragePreference = CodeCoveragePreference(rawValue: json.optionalIntForKey("codeCoveragePreference") ?? 0) ?? .UseSchemeSetting if let buildConfigOverride = json.optionalStringForKey("buildConfiguration") { @@ -121,18 +121,18 @@ public class BotConfiguration : XcodeServerEntity { } else { self.buildConfiguration = .UseSchemeSetting } - self.analyze = json.boolForKey("performsAnalyzeAction") - self.archive = json.boolForKey("performsArchiveAction") + self.analyze = try json.boolForKey("performsAnalyzeAction") + self.archive = try json.boolForKey("performsArchiveAction") self.exportsProductFromArchive = json.optionalBoolForKey("exportsProductFromArchive") ?? false - self.test = json.boolForKey("performsTestAction") - self.schemeName = json.stringForKey("schemeName") - self.schedule = BotSchedule(json: json) - self.triggers = XcodeServerArray(json.arrayForKey("triggers")) - self.sourceControlBlueprint = SourceControlBlueprint(json: json.dictionaryForKey("sourceControlBlueprint")) + self.test = try json.boolForKey("performsTestAction") + self.schemeName = try json.stringForKey("schemeName") + self.schedule = try BotSchedule(json: json) + self.triggers = try XcodeServerArray(try json.arrayForKey("triggers")) + self.sourceControlBlueprint = try SourceControlBlueprint(json: try json.dictionaryForKey("sourceControlBlueprint")) //old bots (xcode 6) only have testingDeviceIds, try to parse those into the new format of DeviceSpecification (xcode 7) if let deviceSpecJSON = json.optionalDictionaryForKey("deviceSpecification") { - self.deviceSpecification = DeviceSpecification(json: deviceSpecJSON) + self.deviceSpecification = try DeviceSpecification(json: deviceSpecJSON) } else { if let testingDeviceIds = json.optionalArrayForKey("testingDeviceIDs") as? [String] { self.deviceSpecification = DeviceSpecification(testingDeviceIDs: testingDeviceIds) @@ -141,7 +141,7 @@ public class BotConfiguration : XcodeServerEntity { } } - super.init(json: json) + try super.init(json: json) } public init( diff --git a/XcodeServerSDK/Server Entities/BotSchedule.swift b/XcodeServerSDK/Server Entities/BotSchedule.swift index 6053994..e25bbc7 100644 --- a/XcodeServerSDK/Server Entities/BotSchedule.swift +++ b/XcodeServerSDK/Server Entities/BotSchedule.swift @@ -52,14 +52,14 @@ public class BotSchedule : XcodeServerEntity { public let hours: Int! public let minutes: Int! - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { - let schedule = Schedule(rawValue: json.intForKey("scheduleType"))! + let schedule = Schedule(rawValue: try json.intForKey("scheduleType"))! self.schedule = schedule if schedule == .Periodical { - let period = Period(rawValue: json.intForKey("periodicScheduleInterval"))! + let period = Period(rawValue: try json.intForKey("periodicScheduleInterval"))! self.period = period let minutes = json.optionalIntForKey("minutesAfterHourToIntegrate") @@ -77,7 +77,7 @@ public class BotSchedule : XcodeServerEntity { case .Weekly: self.minutes = minutes! self.hours = hours! - self.day = Day(rawValue: json.intForKey("weeklyScheduleDay")) + self.day = Day(rawValue: try json.intForKey("weeklyScheduleDay")) } } else { self.period = nil @@ -86,7 +86,7 @@ public class BotSchedule : XcodeServerEntity { self.day = nil } - super.init(json: json) + try super.init(json: json) } private init(schedule: Schedule, period: Period?, day: Day?, hours: Int?, minutes: Int?) { diff --git a/XcodeServerSDK/Server Entities/Commit.swift b/XcodeServerSDK/Server Entities/Commit.swift index 4d4bb51..525cf1a 100644 --- a/XcodeServerSDK/Server Entities/Commit.swift +++ b/XcodeServerSDK/Server Entities/Commit.swift @@ -18,15 +18,15 @@ public class Commit: XcodeServerEntity { public let contributor: Contributor // MARK: Initializers - public required init(json: NSDictionary) { - self.hash = json.stringForKey("XCSCommitHash") - self.filePaths = json.arrayForKey("XCSCommitCommitChangeFilePaths").map { File(json: $0) } + public required init(json: NSDictionary) throws { + self.hash = try json.stringForKey("XCSCommitHash") + self.filePaths = try json.arrayForKey("XCSCommitCommitChangeFilePaths").map { try File(json: $0) } self.message = json.optionalStringForKey("XCSCommitMessage") - self.date = json.dateForKey("XCSCommitTimestamp") - self.repositoryID = json.stringForKey("XCSBlueprintRepositoryID") - self.contributor = Contributor(json: json.dictionaryForKey("XCSCommitContributor")) + self.date = try json.dateForKey("XCSCommitTimestamp") + self.repositoryID = try json.stringForKey("XCSBlueprintRepositoryID") + self.contributor = try Contributor(json: try json.dictionaryForKey("XCSCommitContributor")) - super.init(json: json) + try super.init(json: json) } } \ No newline at end of file diff --git a/XcodeServerSDK/Server Entities/Contributor.swift b/XcodeServerSDK/Server Entities/Contributor.swift index 2f83d14..1f8f88e 100644 --- a/XcodeServerSDK/Server Entities/Contributor.swift +++ b/XcodeServerSDK/Server Entities/Contributor.swift @@ -19,12 +19,12 @@ public class Contributor: XcodeServerEntity { public let displayName: String public let emails: [String] - public required init(json: NSDictionary) { - self.name = json.stringForKey(kContributorName) - self.displayName = json.stringForKey(kContributorDisplayName) - self.emails = json.arrayForKey(kContributorEmails) + public required init(json: NSDictionary) throws { + self.name = try json.stringForKey(kContributorName) + self.displayName = try json.stringForKey(kContributorDisplayName) + self.emails = try json.arrayForKey(kContributorEmails) - super.init(json: json) + try super.init(json: json) } public override func dictionarify() -> NSDictionary { diff --git a/XcodeServerSDK/Server Entities/Device.swift b/XcodeServerSDK/Server Entities/Device.swift index b4072e1..9bdb6f7 100644 --- a/XcodeServerSDK/Server Entities/Device.swift +++ b/XcodeServerSDK/Server Entities/Device.swift @@ -7,6 +7,7 @@ // import Foundation +import BuildaUtils public class Device : XcodeServerEntity { @@ -31,37 +32,37 @@ public class Device : XcodeServerEntity { public let isServer: Bool public let retina: Bool - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { - self.osVersion = json.stringForKey("osVersion") - self.connected = json.boolForKey("connected") - self.simulator = json.boolForKey("simulator") - self.modelCode = json.stringForKey("modelCode") - self.deviceType = json.stringForKey("deviceType") - self.modelName = json.stringForKey("modelName") + self.connected = try json.boolForKey("connected") + self.osVersion = try json.stringForKey("osVersion") + self.simulator = try json.boolForKey("simulator") + self.modelCode = try json.stringForKey("modelCode") + self.deviceType = try json.stringForKey("deviceType") + self.modelName = try json.stringForKey("modelName") self.deviceECID = json.optionalStringForKey("deviceECID") - self.modelUTI = json.stringForKey("modelUTI") + self.modelUTI = try json.stringForKey("modelUTI") if let proxyDevice = json.optionalDictionaryForKey("activeProxiedDevice") { - self.activeProxiedDevice = Device(json: proxyDevice) + self.activeProxiedDevice = try Device(json: proxyDevice) } else { self.activeProxiedDevice = nil } self.trusted = json.optionalBoolForKey("trusted") ?? false - self.name = json.stringForKey("name") - self.supported = json.boolForKey("supported") + self.name = try json.stringForKey("name") + self.supported = try json.boolForKey("supported") self.processor = json.optionalStringForKey("processor") - self.identifier = json.stringForKey("identifier") - self.enabledForDevelopment = json.boolForKey("enabledForDevelopment") + self.identifier = try json.stringForKey("identifier") + self.enabledForDevelopment = try json.boolForKey("enabledForDevelopment") self.serialNumber = json.optionalStringForKey("serialNumber") - self.platform = DevicePlatform.PlatformType(rawValue: json.stringForKey("platformIdentifier")) ?? .Unknown - self.architecture = json.stringForKey("architecture") + self.platform = DevicePlatform.PlatformType(rawValue: try json.stringForKey("platformIdentifier")) ?? .Unknown + self.architecture = try json.stringForKey("architecture") //for some reason which is not yet clear to me (probably old/new XcS versions), sometimes //the key is "server" and sometimes "isServer". this just picks up the present one. self.isServer = json.optionalBoolForKey("server") ?? json.optionalBoolForKey("isServer") ?? false - self.retina = json.boolForKey("retina") + self.retina = try json.boolForKey("retina") - super.init(json: json) + try super.init(json: json) } public override func dictionarify() -> NSDictionary { diff --git a/XcodeServerSDK/Server Entities/DeviceSpecification.swift b/XcodeServerSDK/Server Entities/DeviceSpecification.swift index d120c4e..f3493b5 100644 --- a/XcodeServerSDK/Server Entities/DeviceSpecification.swift +++ b/XcodeServerSDK/Server Entities/DeviceSpecification.swift @@ -33,14 +33,14 @@ public class DevicePlatform : XcodeServerEntity { public let type: PlatformType public let simulatorType: SimulatorType? - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { - self.displayName = json.stringForKey("displayName") - self.version = json.stringForKey("version") + self.displayName = try json.stringForKey("displayName") + self.version = try json.stringForKey("version") self.type = PlatformType(rawValue: json.optionalStringForKey("identifier") ?? "") ?? .Unknown self.simulatorType = SimulatorType(rawValue: json.optionalStringForKey("simulatorIdentifier") ?? "") - super.init(json: json) + try super.init(json: json) } //for just informing the intention - iOS or WatchOS or OS X - and we'll fetch the real ones and replace this placeholder with a fetched one. @@ -150,13 +150,13 @@ public class DeviceFilter : XcodeServerEntity { public let architectureType: ArchitectureType //TODO: ditto, find out more. - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { - self.platform = DevicePlatform(json: json.dictionaryForKey("platform")) - self.filterType = FilterType(rawValue: json.intForKey("filterType")) ?? .AllAvailableDevicesAndSimulators + self.platform = try DevicePlatform(json: try json.dictionaryForKey("platform")) + self.filterType = FilterType(rawValue: try json.intForKey("filterType")) ?? .AllAvailableDevicesAndSimulators self.architectureType = ArchitectureType(rawValue: json.optionalIntForKey("architectureType") ?? -1) ?? .Unknown - super.init(json: json) + try super.init(json: json) } public init(platform: DevicePlatform, filterType: FilterType, architectureType: ArchitectureType) { @@ -182,12 +182,12 @@ public class DeviceSpecification : XcodeServerEntity { public let deviceIdentifiers: [String] public let filters: [DeviceFilter] - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { - self.deviceIdentifiers = json.arrayForKey("deviceIdentifiers") - self.filters = XcodeServerArray(json.arrayForKey("filters")) + self.deviceIdentifiers = try json.arrayForKey("deviceIdentifiers") + self.filters = try XcodeServerArray(try json.arrayForKey("filters")) - super.init(json: json) + try super.init(json: json) } public init(filters: [DeviceFilter], deviceIdentifiers: [String]) { diff --git a/XcodeServerSDK/Server Entities/EmailConfiguration.swift b/XcodeServerSDK/Server Entities/EmailConfiguration.swift index 47641e1..c152149 100644 --- a/XcodeServerSDK/Server Entities/EmailConfiguration.swift +++ b/XcodeServerSDK/Server Entities/EmailConfiguration.swift @@ -37,13 +37,13 @@ public class EmailConfiguration : XcodeServerEntity { return dict } - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { - self.emailCommitters = json.boolForKey("emailCommitters") - self.includeCommitMessages = json.boolForKey("includeCommitMessages") - self.includeIssueDetails = json.boolForKey("includeIssueDetails") - self.additionalRecipients = json.arrayForKey("additionalRecipients") + self.emailCommitters = try json.boolForKey("emailCommitters") + self.includeCommitMessages = try json.boolForKey("includeCommitMessages") + self.includeIssueDetails = try json.boolForKey("includeIssueDetails") + self.additionalRecipients = try json.arrayForKey("additionalRecipients") - super.init(json: json) + try super.init(json: json) } } \ No newline at end of file diff --git a/XcodeServerSDK/Server Entities/File.swift b/XcodeServerSDK/Server Entities/File.swift index aec6d27..607db8c 100644 --- a/XcodeServerSDK/Server Entities/File.swift +++ b/XcodeServerSDK/Server Entities/File.swift @@ -20,11 +20,11 @@ public class File: XcodeServerEntity { super.init() } - public required init(json: NSDictionary) { - self.filePath = json.stringForKey("filePath") - self.status = FileStatus(rawValue: json.intForKey("status")) ?? .Other + public required init(json: NSDictionary) throws { + self.filePath = try json.stringForKey("filePath") + self.status = FileStatus(rawValue: try json.intForKey("status")) ?? .Other - super.init(json: json) + try super.init(json: json) } public override func dictionarify() -> NSDictionary { diff --git a/XcodeServerSDK/Server Entities/Integration.swift b/XcodeServerSDK/Server Entities/Integration.swift index d843b68..5982690 100644 --- a/XcodeServerSDK/Server Entities/Integration.swift +++ b/XcodeServerSDK/Server Entities/Integration.swift @@ -63,16 +63,16 @@ public class Integration : XcodeServerEntity { case TriggerError = "trigger-error" } - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { - self.queuedDate = json.dateForKey("queuedDate") + self.queuedDate = try json.dateForKey("queuedDate") self.startedDate = json.optionalDateForKey("startedTime") self.endedTime = json.optionalDateForKey("endedTime") self.duration = json.optionalDoubleForKey("duration") - self.shouldClean = json.boolForKey("shouldClean") - self.currentStep = Step(rawValue: json.stringForKey("currentStep")) ?? .Unknown - self.number = json.intForKey("number") - self.successStreak = json.intForKey("success_streak") + self.shouldClean = try json.boolForKey("shouldClean") + self.currentStep = Step(rawValue: try json.stringForKey("currentStep")) ?? .Unknown + self.number = try json.intForKey("number") + self.successStreak = try json.intForKey("success_streak") self.expectedCompletionDate = json.optionalDateForKey("expectedCompletionDate") if let raw = json.optionalStringForKey("result") { @@ -82,19 +82,19 @@ public class Integration : XcodeServerEntity { } if let raw = json.optionalDictionaryForKey("buildResultSummary") { - self.buildResultSummary = BuildResultSummary(json: raw) + self.buildResultSummary = try BuildResultSummary(json: raw) } else { self.buildResultSummary = nil } if let testedDevices = json.optionalArrayForKey("testedDevices") { - self.testedDevices = XcodeServerArray(testedDevices) + self.testedDevices = try XcodeServerArray(testedDevices) } else { self.testedDevices = nil } if let testHierarchy = json.optionalDictionaryForKey("testHierarchy") where testHierarchy.count > 0 { - self.testHierarchy = TestHierarchy(json: testHierarchy) + self.testHierarchy = try TestHierarchy(json: testHierarchy) } else { self.testHierarchy = nil } @@ -102,12 +102,12 @@ public class Integration : XcodeServerEntity { self.assets = json.optionalDictionaryForKey("assets") if let blueprint = json.optionalDictionaryForKey("revisionBlueprint") { - self.blueprint = SourceControlBlueprint(json: blueprint) + self.blueprint = try SourceControlBlueprint(json: blueprint) } else { self.blueprint = nil } - super.init(json: json) + try super.init(json: json) } } @@ -128,24 +128,24 @@ public class BuildResultSummary : XcodeServerEntity { public let codeCoveragePercentage: Int public let codeCoveragePercentageDelta: Int - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { - self.analyzerWarningCount = json.intForKey("analyzerWarningCount") - self.testFailureCount = json.intForKey("testFailureCount") - self.testsChange = json.intForKey("testsChange") - self.errorCount = json.intForKey("errorCount") - self.testsCount = json.intForKey("testsCount") - self.testFailureChange = json.intForKey("testFailureChange") - self.warningChange = json.intForKey("warningChange") - self.regressedPerfTestCount = json.intForKey("regressedPerfTestCount") - self.warningCount = json.intForKey("warningCount") - self.errorChange = json.intForKey("errorChange") - self.improvedPerfTestCount = json.intForKey("improvedPerfTestCount") - self.analyzerWarningChange = json.intForKey("analyzerWarningChange") + self.analyzerWarningCount = try json.intForKey("analyzerWarningCount") + self.testFailureCount = try json.intForKey("testFailureCount") + self.testsChange = try json.intForKey("testsChange") + self.errorCount = try json.intForKey("errorCount") + self.testsCount = try json.intForKey("testsCount") + self.testFailureChange = try json.intForKey("testFailureChange") + self.warningChange = try json.intForKey("warningChange") + self.regressedPerfTestCount = try json.intForKey("regressedPerfTestCount") + self.warningCount = try json.intForKey("warningCount") + self.errorChange = try json.intForKey("errorChange") + self.improvedPerfTestCount = try json.intForKey("improvedPerfTestCount") + self.analyzerWarningChange = try json.intForKey("analyzerWarningChange") self.codeCoveragePercentage = json.optionalIntForKey("codeCoveragePercentage") ?? 0 self.codeCoveragePercentageDelta = json.optionalIntForKey("codeCoveragePercentageDelta") ?? 0 - super.init(json: json) + try super.init(json: json) } } diff --git a/XcodeServerSDK/Server Entities/IntegrationCommits.swift b/XcodeServerSDK/Server Entities/IntegrationCommits.swift index 169877b..f4e5704 100644 --- a/XcodeServerSDK/Server Entities/IntegrationCommits.swift +++ b/XcodeServerSDK/Server Entities/IntegrationCommits.swift @@ -17,14 +17,14 @@ public class IntegrationCommits: XcodeServerEntity { public let commits: [String: [Commit]] public let endedTimeDate: NSDate? - public required init(json: NSDictionary) { - self.integration = json.stringForKey("integration") - self.botTinyID = json.stringForKey("botTinyID") - self.botID = json.stringForKey("botID") - self.commits = IntegrationCommits.populateCommits(json.dictionaryForKey("commits")) - self.endedTimeDate = IntegrationCommits.parseDate(json.arrayForKey("endedTimeDate")) + public required init(json: NSDictionary) throws { + self.integration = try json.stringForKey("integration") + self.botTinyID = try json.stringForKey("botTinyID") + self.botID = try json.stringForKey("botID") + self.commits = try IntegrationCommits.populateCommits(try json.dictionaryForKey("commits")) + self.endedTimeDate = IntegrationCommits.parseDate(try json.arrayForKey("endedTimeDate")) - super.init(json: json) + try super.init(json: json) } /** @@ -34,7 +34,7 @@ public class IntegrationCommits: XcodeServerEntity { - returns: Dictionary of parsed Commit objects. */ - class func populateCommits(json: NSDictionary) -> [String: [Commit]] { + class func populateCommits(json: NSDictionary) throws -> [String: [Commit]] { var resultsDictionary: [String: [Commit]] = Dictionary() for (key, value) in json { @@ -43,7 +43,7 @@ public class IntegrationCommits: XcodeServerEntity { continue } - resultsDictionary[blueprintID] = commitsArray.map { Commit(json: $0) } + resultsDictionary[blueprintID] = try commitsArray.map { try Commit(json: $0) } } return resultsDictionary diff --git a/XcodeServerSDK/Server Entities/IntegrationIssue.swift b/XcodeServerSDK/Server Entities/IntegrationIssue.swift index a38197e..48e67eb 100644 --- a/XcodeServerSDK/Server Entities/IntegrationIssue.swift +++ b/XcodeServerSDK/Server Entities/IntegrationIssue.swift @@ -39,18 +39,18 @@ public class IntegrationIssue: XcodeServerEntity { public let status: IssueStatus // MARK: Initialization - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { self.payload = json.copy() as? NSDictionary ?? NSDictionary() self.message = json.optionalStringForKey("message") - self.type = IssueType(rawValue: json.stringForKey("type"))! - self.issueType = json.stringForKey("issueType") - self.commits = json.arrayForKey("commits").map { Commit(json: $0) } - self.integrationID = json.stringForKey("integrationID") - self.age = json.intForKey("age") - self.status = IssueStatus(rawValue: json.intForKey("status"))! + self.type = try IssueType(rawValue: json.stringForKey("type"))! + self.issueType = try json.stringForKey("issueType") + self.commits = try json.arrayForKey("commits").map { try Commit(json: $0) } + self.integrationID = try json.stringForKey("integrationID") + self.age = try json.intForKey("age") + self.status = IssueStatus(rawValue: try json.intForKey("status"))! - super.init(json: json) + try super.init(json: json) } } \ No newline at end of file diff --git a/XcodeServerSDK/Server Entities/IntegrationIssues.swift b/XcodeServerSDK/Server Entities/IntegrationIssues.swift index a5d8da1..5bb7b2d 100644 --- a/XcodeServerSDK/Server Entities/IntegrationIssues.swift +++ b/XcodeServerSDK/Server Entities/IntegrationIssues.swift @@ -21,18 +21,42 @@ public class IntegrationIssues: XcodeServerEntity { // MARK: Initialization - public required init(json: NSDictionary) { - self.buildServiceErrors = json.arrayForKey("buildServiceErrors").map { IntegrationIssue(json: $0) } - self.buildServiceWarnings = json.arrayForKey("buildServiceWarnings").map { IntegrationIssue(json: $0) } - self.triggerErrors = json.arrayForKey("triggerErrors").map { IntegrationIssue(json: $0) } + public required init(json: NSDictionary) throws { + self.buildServiceErrors = try json.arrayForKey("buildServiceErrors").map { try IntegrationIssue(json: $0) } + self.buildServiceWarnings = try json.arrayForKey("buildServiceWarnings").map { try IntegrationIssue(json: $0) } + self.triggerErrors = try json.arrayForKey("triggerErrors").map { try IntegrationIssue(json: $0) } // Nested issues - self.errors = json.dictionaryForKey("errors").allValues.filter { $0.count != 0 }.flatMap { ($0 as! NSArray).map { IntegrationIssue(json: $0 as! NSDictionary) } } - self.warnings = json.dictionaryForKey("warnings").allValues.filter { $0.count != 0 }.flatMap { ($0 as! NSArray).map { IntegrationIssue(json: $0 as! NSDictionary) } } - self.testFailures = json.dictionaryForKey("testFailures").allValues.filter { $0.count != 0 }.flatMap { ($0 as! NSArray).map { IntegrationIssue(json: $0 as! NSDictionary) } } - self.analyzerWarnings = json.dictionaryForKey("analyzerWarnings").allValues.filter { $0.count != 0 }.flatMap { ($0 as! NSArray).map { IntegrationIssue(json: $0 as! NSDictionary) } } + self.errors = try json + .dictionaryForKey("errors") + .allValues + .filter { $0.count != 0 } + .flatMap { + try ($0 as! NSArray).map { try IntegrationIssue(json: $0 as! NSDictionary) } + } + self.warnings = try json + .dictionaryForKey("warnings") + .allValues + .filter { $0.count != 0 } + .flatMap { + try ($0 as! NSArray).map { try IntegrationIssue(json: $0 as! NSDictionary) } + } + self.testFailures = try json + .dictionaryForKey("testFailures") + .allValues + .filter { $0.count != 0 } + .flatMap { + try ($0 as! NSArray).map { try IntegrationIssue(json: $0 as! NSDictionary) } + } + self.analyzerWarnings = try json + .dictionaryForKey("analyzerWarnings") + .allValues + .filter { $0.count != 0 } + .flatMap { + try ($0 as! NSArray).map { try IntegrationIssue(json: $0 as! NSDictionary) } + } - super.init(json: json) + try super.init(json: json) } } \ No newline at end of file diff --git a/XcodeServerSDK/Server Entities/LiveUpdateMessage.swift b/XcodeServerSDK/Server Entities/LiveUpdateMessage.swift index ac13292..679782d 100644 --- a/XcodeServerSDK/Server Entities/LiveUpdateMessage.swift +++ b/XcodeServerSDK/Server Entities/LiveUpdateMessage.swift @@ -51,7 +51,7 @@ public class LiveUpdateMessage: XcodeServerEntity { public let result: Integration.Result? public let currentStep: Integration.Step? - required public init(json: NSDictionary) { + required public init(json: NSDictionary) throws { let typeString = json.optionalStringForKey("name") ?? "" @@ -79,7 +79,7 @@ public class LiveUpdateMessage: XcodeServerEntity { self.currentStep = nil } - super.init(json: json) + try super.init(json: json) } } diff --git a/XcodeServerSDK/Server Entities/Repository.swift b/XcodeServerSDK/Server Entities/Repository.swift index 189ae13..baa88f1 100644 --- a/XcodeServerSDK/Server Entities/Repository.swift +++ b/XcodeServerSDK/Server Entities/Repository.swift @@ -122,16 +122,16 @@ public class Repository: XcodeServerEntity { - returns: Initialized repository struct. */ - public required init(json: NSDictionary) { - self.name = json.stringForKey("name") + public required init(json: NSDictionary) throws { + self.name = try json.stringForKey("name") - self.httpAccess = HTTPAccessType(rawValue: json.intForKey("httpAccessType"))! - self.sshAccess = SSHAccessType(rawValue: json.intForKey("posixPermissions"))! + self.httpAccess = HTTPAccessType(rawValue: try json.intForKey("httpAccessType"))! + self.sshAccess = SSHAccessType(rawValue: try json.intForKey("posixPermissions"))! - self.writeAccessExternalIds = json.arrayForKey("writeAccessExternalIDs") - self.readAccessExternalIds = json.arrayForKey("readAccessExternalIDs") + self.writeAccessExternalIds = try json.arrayForKey("writeAccessExternalIDs") + self.readAccessExternalIds = try json.arrayForKey("readAccessExternalIDs") - super.init(json: json) + try super.init(json: json) } /** diff --git a/XcodeServerSDK/Server Entities/SourceControlBlueprint.swift b/XcodeServerSDK/Server Entities/SourceControlBlueprint.swift index 9cae6e4..772ec13 100644 --- a/XcodeServerSDK/Server Entities/SourceControlBlueprint.swift +++ b/XcodeServerSDK/Server Entities/SourceControlBlueprint.swift @@ -30,30 +30,30 @@ public class SourceControlBlueprint : XcodeServerEntity { public let sshPassphrase: String? public var certificateFingerprint: String? = nil - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { - self.wCCName = json.stringForKey(XcodeBlueprintNameKey) + self.wCCName = try json.stringForKey(XcodeBlueprintNameKey) - let primaryRepoId = json.stringForKey(XcodeBlueprintPrimaryRemoteRepositoryKey) + let primaryRepoId = try json.stringForKey(XcodeBlueprintPrimaryRemoteRepositoryKey) self.projectWCCIdentifier = primaryRepoId - let workingCopyPaths = json.dictionaryForKey(XcodeBlueprintWorkingCopyPathsKey) - self.projectName = workingCopyPaths.stringForKey(primaryRepoId) + let workingCopyPaths = try json.dictionaryForKey(XcodeBlueprintWorkingCopyPathsKey) + self.projectName = try workingCopyPaths.stringForKey(primaryRepoId) - let repos: [NSDictionary] = json.arrayForKey(XcodeBlueprintRemoteRepositoriesKey) - let primarys: [NSDictionary] = repos.filter { + let repos: [NSDictionary] = try json.arrayForKey(XcodeBlueprintRemoteRepositoriesKey) + let primarys: [NSDictionary] = try repos.filter { (item: NSDictionary) -> Bool in - return item.stringForKey(XcodeBlueprintRemoteRepositoryIdentifierKey) == primaryRepoId + return try item.stringForKey(XcodeBlueprintRemoteRepositoryIdentifierKey) == primaryRepoId } - self.projectPath = json.stringForKey(XcodeBlueprintRelativePathToProjectKey) + self.projectPath = try json.stringForKey(XcodeBlueprintRelativePathToProjectKey) let repo = primarys.first! - self.projectURL = repo.stringForKey(XcodeBlueprintRemoteRepositoryURLKey) + self.projectURL = try repo.stringForKey(XcodeBlueprintRemoteRepositoryURLKey) self.certificateFingerprint = repo.optionalStringForKey(XcodeBlueprintRemoteRepositoryCertFingerprintKey) - let locations = json.dictionaryForKey(XcodeBlueprintLocationsKey) - let location = locations.dictionaryForKey(primaryRepoId) + let locations = try json.dictionaryForKey(XcodeBlueprintLocationsKey) + let location = try locations.dictionaryForKey(primaryRepoId) self.branch = location.optionalStringForKey(XcodeBranchIdentifierKey) ?? "" self.commitSHA = location.optionalStringForKey(XcodeLocationRevisionKey) @@ -63,7 +63,7 @@ public class SourceControlBlueprint : XcodeServerEntity { self.publicSSHKey = authenticationStrategy?.optionalStringForKey(XcodeRepoPublicKeyDataKey) self.sshPassphrase = authenticationStrategy?.optionalStringForKey(XcodeRepoPasswordKey) - super.init(json: json) + try super.init(json: json) } public init(branch: String, projectWCCIdentifier: String, wCCName: String, projectName: String, diff --git a/XcodeServerSDK/Server Entities/TestHierarchy.swift b/XcodeServerSDK/Server Entities/TestHierarchy.swift index a216b1d..e42f10c 100644 --- a/XcodeServerSDK/Server Entities/TestHierarchy.swift +++ b/XcodeServerSDK/Server Entities/TestHierarchy.swift @@ -66,14 +66,14 @@ public class TestHierarchy : XcodeServerEntity { which is the aggregated status, so that you don't have to iterate through all tests to figure it out yourself. 1 if all are 1, 0 otherwise. */ - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { //TODO: come up with useful things to parse //TODO: add search capabilities, aggregate generation etc self.testData = TestHierarchy.pullData(json) - super.init(json: json) + try super.init(json: json) } class func pullData(json: NSDictionary) -> TestData { diff --git a/XcodeServerSDK/Server Entities/Toolchain.swift b/XcodeServerSDK/Server Entities/Toolchain.swift index 5de0922..1853c71 100644 --- a/XcodeServerSDK/Server Entities/Toolchain.swift +++ b/XcodeServerSDK/Server Entities/Toolchain.swift @@ -14,13 +14,12 @@ public class Toolchain: XcodeServerEntity { public let path: String public let signatureVerified: Bool - - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { - self.displayName = json.stringForKey("displayName") - self.path = json.stringForKey("path") - self.signatureVerified = json.boolForKey("signatureVerified") + self.displayName = try json.stringForKey("displayName") + self.path = try json.stringForKey("path") + self.signatureVerified = try json.boolForKey("signatureVerified") - super.init(json: json) + try super.init(json: json) } } \ No newline at end of file diff --git a/XcodeServerSDK/Server Entities/Trigger.swift b/XcodeServerSDK/Server Entities/Trigger.swift index e42fd1d..9d32842 100644 --- a/XcodeServerSDK/Server Entities/Trigger.swift +++ b/XcodeServerSDK/Server Entities/Trigger.swift @@ -85,28 +85,28 @@ public struct TriggerConfig: XcodeRead, XcodeWrite { } } - public init(json: NSDictionary) { + public init(json: NSDictionary) throws { - let phase = Phase(rawValue: json.intForKey("phase"))! + let phase = Phase(rawValue: try json.intForKey("phase"))! self.phase = phase if let conditionsJSON = json.optionalDictionaryForKey("conditions") where phase == .Postbuild { //also parse conditions - self.conditions = TriggerConditions(json: conditionsJSON) + self.conditions = try TriggerConditions(json: conditionsJSON) } else { self.conditions = nil } - let kind = Kind(rawValue: json.intForKey("type"))! + let kind = Kind(rawValue: try json.intForKey("type"))! self.kind = kind if let configurationJSON = json.optionalDictionaryForKey("emailConfiguration") where kind == .EmailNotification { //also parse email config - self.emailConfiguration = EmailConfiguration(json: configurationJSON) + self.emailConfiguration = try EmailConfiguration(json: configurationJSON) } else { self.emailConfiguration = nil } - self.name = json.stringForKey("name") - self.scriptBody = json.stringForKey("scriptBody") + self.name = try json.stringForKey("name") + self.scriptBody = try json.stringForKey("scriptBody") self.id = json.optionalStringForKey("id") ?? Ref.new() } @@ -136,10 +136,10 @@ public class Trigger : XcodeServerEntity { super.init() } - required public init(json: NSDictionary) { + required public init(json: NSDictionary) throws { - self.config = TriggerConfig(json: json) - super.init(json: json) + self.config = try TriggerConfig(json: json) + try super.init(json: json) } public override func dictionarify() -> NSDictionary { diff --git a/XcodeServerSDK/Server Entities/TriggerConditions.swift b/XcodeServerSDK/Server Entities/TriggerConditions.swift index 1c88cdc..925406c 100644 --- a/XcodeServerSDK/Server Entities/TriggerConditions.swift +++ b/XcodeServerSDK/Server Entities/TriggerConditions.swift @@ -46,16 +46,16 @@ public class TriggerConditions : XcodeServerEntity { return dict } - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { self.status = json.optionalIntForKey("status") ?? 2 - self.onAnalyzerWarnings = json.boolForKey("onAnalyzerWarnings") - self.onBuildErrors = json.boolForKey("onBuildErrors") - self.onFailingTests = json.boolForKey("onFailingTests") - self.onInternalErrors = json.boolForKey("onInternalErrors") - self.onSuccess = json.boolForKey("onSuccess") - self.onWarnings = json.boolForKey("onWarnings") + self.onAnalyzerWarnings = try json.boolForKey("onAnalyzerWarnings") + self.onBuildErrors = try json.boolForKey("onBuildErrors") + self.onFailingTests = try json.boolForKey("onFailingTests") + self.onInternalErrors = try json.boolForKey("onInternalErrors") + self.onSuccess = try json.boolForKey("onSuccess") + self.onWarnings = try json.boolForKey("onWarnings") - super.init(json: json) + try super.init(json: json) } } \ No newline at end of file diff --git a/XcodeServerSDK/XcodeServer.swift b/XcodeServerSDK/XcodeServer.swift index a590f25..d0e577c 100644 --- a/XcodeServerSDK/XcodeServer.swift +++ b/XcodeServerSDK/XcodeServer.swift @@ -154,3 +154,4 @@ public extension XcodeServer { } } + diff --git a/XcodeServerSDK/XcodeServerEntity.swift b/XcodeServerSDK/XcodeServerEntity.swift index 006a45a..7e839c0 100644 --- a/XcodeServerSDK/XcodeServerEntity.swift +++ b/XcodeServerSDK/XcodeServerEntity.swift @@ -9,7 +9,7 @@ import Foundation public protocol XcodeRead { - init(json: NSDictionary) + init(json: NSDictionary) throws } public protocol XcodeWrite { @@ -27,7 +27,7 @@ public class XcodeServerEntity : XcodeRead, XcodeWrite { public let originalJSON: NSDictionary? //initializer which takes a dictionary and fills in values for recognized keys - public required init(json: NSDictionary) { + public required init(json: NSDictionary) throws { self.id = json.optionalStringForKey("_id") self.rev = json.optionalStringForKey("_rev") @@ -49,21 +49,20 @@ public class XcodeServerEntity : XcodeRead, XcodeWrite { return NSDictionary() } - public class func optional(json: NSDictionary?) -> T? { + public class func optional(json: NSDictionary?) throws -> T? { if let json = json { - return T(json: json) + return try T(json: json) } return nil } } //parse an array of dictionaries into an array of parsed entities -public func XcodeServerArray(jsonArray: NSArray!) -> [T] { +public func XcodeServerArray(jsonArray: NSArray) throws -> [T] { - let array = jsonArray as! [NSDictionary]! - let parsed = array.map { - (json: NSDictionary) -> (T) in - return T(json: json) + let array = jsonArray as! [NSDictionary] + let parsed = try array.map { (json: NSDictionary) -> (T) in + return try T(json: json) } return parsed } diff --git a/XcodeServerSDKTests/BotParsingTests.swift b/XcodeServerSDKTests/BotParsingTests.swift index 445576e..da3d076 100644 --- a/XcodeServerSDKTests/BotParsingTests.swift +++ b/XcodeServerSDKTests/BotParsingTests.swift @@ -26,9 +26,9 @@ class BotParsingTests: XCTestCase { self.waitForExpectationsWithTimeout(10, handler: nil) } - func testShared() { + func testShared() throws { - let bot = self.botInCassetteWithName("osx_bot") + let bot = try self.botInCassetteWithName("osx_bot") XCTAssertEqual(bot.id, "963bc95f1c1a56f69f3392b4aa03302f") XCTAssertEqual(bot.rev, "10-117b73f201ea103229a2e8cd26a01845") diff --git a/XcodeServerSDKTests/ContributorTests.swift b/XcodeServerSDKTests/ContributorTests.swift index 7e0d2a0..9775201 100644 --- a/XcodeServerSDKTests/ContributorTests.swift +++ b/XcodeServerSDKTests/ContributorTests.swift @@ -34,8 +34,8 @@ class ContributorTests: XCTestCase { override func setUp() { super.setUp() - singleEmail = Contributor(json: singleEmailContributor) - multiEmail = Contributor(json: multiEmailContributor) + singleEmail = try! Contributor(json: singleEmailContributor) + multiEmail = try! Contributor(json: multiEmailContributor) } // MARK: Test cases diff --git a/XcodeServerSDKTests/DevicesTests.swift b/XcodeServerSDKTests/DevicesTests.swift index 1b9f20b..91a5abd 100644 --- a/XcodeServerSDKTests/DevicesTests.swift +++ b/XcodeServerSDKTests/DevicesTests.swift @@ -11,7 +11,7 @@ import XcodeServerSDK class DevicesTests: XCTestCase { - let macMini = Device(json: [ + let macMini = try! Device(json: [ "osVersion" : "10.11", "connected" : true, "simulator" : false, diff --git a/XcodeServerSDKTests/FileTests.swift b/XcodeServerSDKTests/FileTests.swift index a8fa320..d83fe88 100644 --- a/XcodeServerSDKTests/FileTests.swift +++ b/XcodeServerSDKTests/FileTests.swift @@ -22,13 +22,13 @@ class FileTests: XCTestCase { ] // MARK: Initialization - func testDictionaryInit() { - var file = File(json: sampleAdded) + func testDictionaryInit() throws { + var file = try File(json: sampleAdded) XCTAssertEqual(file.filePath, "File1.swift") XCTAssertEqual(file.status, FileStatus.Added) - file = File(json: sampleOther) + file = try File(json: sampleOther) XCTAssertEqual(file.filePath, "File2.swift") XCTAssertEqual(file.status, FileStatus.Other) @@ -42,8 +42,8 @@ class FileTests: XCTestCase { } // MARK: Dictioninarifying - func testDictionarify() { - let file = File(json: sampleAdded) + func testDictionarify() throws { + let file = try File(json: sampleAdded) XCTAssertEqual(file.dictionarify(), sampleAdded) } diff --git a/XcodeServerSDKTests/IssueTests.swift b/XcodeServerSDKTests/IssueTests.swift index 07fc8a2..9063b23 100644 --- a/XcodeServerSDKTests/IssueTests.swift +++ b/XcodeServerSDKTests/IssueTests.swift @@ -11,7 +11,7 @@ import XCTest class IssueTests: XCTestCase { - let buildServiceError = IntegrationIssue(json: [ + let buildServiceError = try! IntegrationIssue(json: [ "_id": "99e84a22b20df344df2217d5e4186094", "_rev": "3-0cf0b25e995f1a050617d3c5824007f7", "message": "This integration was canceled.", @@ -23,7 +23,7 @@ class IssueTests: XCTestCase { "status": 0 ]) - let errorWithCommits = IntegrationIssue(json: [ + let errorWithCommits = try! IntegrationIssue(json: [ "_id": "99e84a22b20df344df2217d5e413f43e", "_rev": "3-7dfc0aa57a8119c025cd2d86143589f2", "message": "Expected declaration", diff --git a/XcodeServerSDKTests/LiveUpdatesTests.swift b/XcodeServerSDKTests/LiveUpdatesTests.swift index 5d6cfb5..f7ef0f9 100644 --- a/XcodeServerSDKTests/LiveUpdatesTests.swift +++ b/XcodeServerSDKTests/LiveUpdatesTests.swift @@ -35,14 +35,14 @@ class LiveUpdatesTests: XCTestCase { expect(advice) == SocketIOPacket.ErrorAdvice.Reconnect } - func testParsing_SingleEventMessage() { + func testParsing_SingleEventMessage() throws { let message = "5:::{\"name\":\"advisoryIntegrationStatus\",\"args\":[{\"message\":\"BuildaKit : Linking\",\"_id\":\"07a63fae4ff2d5a37eee830be556d143\",\"percentage\":0.7578125,\"botId\":\"07a63fae4ff2d5a37eee830be50c502a\"},null]}" let packets: [SocketIOPacket] = SocketIOHelper.parsePackets(message) expect(packets.count) == 1 let packet = packets.first! expect(packet.jsonPayload).toNot(beNil()) - let msg = LiveUpdateMessage(json: packet.jsonPayload!) + let msg = try LiveUpdateMessage(json: packet.jsonPayload!) expect(msg.type) == LiveUpdateMessage.MessageType.AdvisoryIntegrationStatus expect(msg.message) == "BuildaKit : Linking" expect(msg.integrationId) == "07a63fae4ff2d5a37eee830be556d143" @@ -50,13 +50,13 @@ class LiveUpdatesTests: XCTestCase { expect(msg.botId) == "07a63fae4ff2d5a37eee830be50c502a" } - func testParsing_MultipleEventMessages() { + func testParsing_MultipleEventMessages() throws { let message = "�205�5:::{\"name\":\"advisoryIntegrationStatus\",\"args\":[{\"message\":\"Buildasaur : Linking\",\"_id\":\"07a63fae4ff2d5a37eee830be556d143\",\"percentage\":0.8392857360839844,\"botId\":\"07a63fae4ff2d5a37eee830be50c502a\"},null]}�218�5:::{\"name\":\"advisoryIntegrationStatus\",\"args\":[{\"message\":\"Buildasaur : Copying 1 of 3 files\",\"_id\":\"07a63fae4ff2d5a37eee830be556d143\",\"percentage\":0.8571428680419921,\"botId\":\"07a63fae4ff2d5a37eee830be50c502a\"},null]}�218�5:::{\"name\":\"advisoryIntegrationStatus\",\"args\":[{\"message\":\"Buildasaur : Copying 2 of 3 files\",\"_id\":\"07a63fae4ff2d5a37eee830be556d143\",\"percentage\":0.8607142639160156,\"botId\":\"07a63fae4ff2d5a37eee830be50c502a\"},null]}�228�5:::{\"name\":\"advisoryIntegrationStatus\",\"args\":[{\"message\":\"BuildaUtils : Compiling Swift source files\",\"_id\":\"07a63fae4ff2d5a37eee830be556d143\",\"percentage\":0.05511363506317139,\"botId\":\"07a63fae4ff2d5a37eee830be50c502a\"},null]}" let packets: [SocketIOPacket] = SocketIOHelper.parsePackets(message) expect(packets.count) == 4 for packet in packets { expect(packet.jsonPayload).toNot(beNil()) - let msg = LiveUpdateMessage(json: packet.jsonPayload!) + let msg = try LiveUpdateMessage(json: packet.jsonPayload!) expect(msg.type) == LiveUpdateMessage.MessageType.AdvisoryIntegrationStatus } } diff --git a/XcodeServerSDKTests/RepositoryTests.swift b/XcodeServerSDKTests/RepositoryTests.swift index 13d789a..900936c 100644 --- a/XcodeServerSDKTests/RepositoryTests.swift +++ b/XcodeServerSDKTests/RepositoryTests.swift @@ -23,8 +23,8 @@ class RepositoryTests: XCTestCase { ] // MARK: Initialization - func testInit() { - let repo = Repository(json: json) + func testInit() throws { + let repo = try Repository(json: json) XCTAssertEqual(repo.name, "Test") XCTAssertEqual(repo.httpAccess, Repository.HTTPAccessType.LoggedIn) diff --git a/XcodeServerSDKTests/TestUtils.swift b/XcodeServerSDKTests/TestUtils.swift index dac80bd..52b83f5 100644 --- a/XcodeServerSDKTests/TestUtils.swift +++ b/XcodeServerSDKTests/TestUtils.swift @@ -89,20 +89,20 @@ extension XCTestCase { return NSDictionary() } - func botInCassetteWithName(name: String) -> Bot { + func botInCassetteWithName(name: String) throws -> Bot { let json = self.loadJSONResponseFromCassetteWithName(name) - let bot = Bot(json: json) + let bot = try Bot(json: json) return bot } - func botInFileWithName(name: String) -> Bot { + func botInFileWithName(name: String) throws -> Bot { let json = self.loadJSONWithName(name) - let bot = Bot(json: json) + let bot = try Bot(json: json) return bot } - func configurationFromBotWithName(name: String) -> BotConfiguration { - let bot = self.botInFileWithName(name) + func configurationFromBotWithName(name: String) throws -> BotConfiguration { + let bot = try self.botInFileWithName(name) let configuration = bot.configuration return configuration }