Skip to content

Commit

Permalink
Verify type of object before mapping them
Browse files Browse the repository at this point in the history
  • Loading branch information
felginep committed Oct 4, 2019
1 parent afb327a commit 8f5b0da
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 17 deletions.
6 changes: 4 additions & 2 deletions Sources/XCResultKit/ActionTestActivitySummary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public struct ActionTestActivitySummary: XCResultObject {
uuid = try xcRequired(element: "uuid", from: json)
start = xcOptional(element: "start", from: json)
finish = xcOptional(element: "finish", from: json)
attachments = xcArray(element: "attachments", from: json).compactMap { ActionTestAttachment($0) }
subactivities = xcArray(element: "subactivities", from: json).compactMap { ActionTestActivitySummary($0) }
attachments = xcArray(element: "attachments", from: json)
.ofType(ActionTestAttachment.self)
subactivities = xcArray(element: "subactivities", from: json)
.ofType(ActionTestActivitySummary.self)
} catch {
debug("Error parsing ActionTestActivitySummary: \(error.localizedDescription)")
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public struct ActionTestPerformanceMetricSummary: XCResultObject {
do {
displayName = try xcRequired(element: "displayName", from: json)
unitOfMeasurement = try xcRequired(element: "unitOfMeasurement", from: json)
measurements = xcArray(element: "measurements", from: json).compactMap { Double($0) }
measurements = xcArray(element: "measurements", from: json).ofType(Double.self)
identifier = xcOptional(element: "identifier", from: json)
baselineName = xcOptional(element: "baselineName", from: json)
baselineAverage = xcOptional(element: "baselineAverage", from: json)
Expand Down
2 changes: 1 addition & 1 deletion Sources/XCResultKit/ActionTestPlanRunSummaries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public struct ActionTestPlanRunSummaries: XCResultObject {
public let summaries: [ActionTestPlanRunSummary]

public init?(_ json: [String : AnyObject]) {
summaries = xcArray(element: "summaries", from: json).compactMap { ActionTestPlanRunSummary($0) }
summaries = xcArray(element: "summaries", from: json).ofType(ActionTestPlanRunSummary.self)
}
}
2 changes: 1 addition & 1 deletion Sources/XCResultKit/ActionTestPlanRunSummary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct ActionTestPlanRunSummary: XCResultObject {
public init?(_ json: [String : AnyObject]) {
do {
name = try xcRequired(element: "name", from: json)
testableSummaries = xcArray(element: "testableSummaries", from: json).compactMap { ActionTestableSummary($0) }
testableSummaries = xcArray(element: "testableSummaries", from: json).ofType(ActionTestableSummary.self)
} catch {
debug("Error parsing ActionTestPlanRunSummary: \(error.localizedDescription)")
return nil
Expand Down
9 changes: 6 additions & 3 deletions Sources/XCResultKit/ActionTestSummary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ public struct ActionTestSummary: XCResultObject {
identifier = try xcRequired(element: "identifier", from: json)
testStatus = try xcRequired(element: "testStatus", from: json)
duration = try xcRequired(element: "duration", from: json)
performanceMetrics = xcArray(element: "performanceMetrics", from: json).compactMap { ActionTestPerformanceMetricSummary($0) }
failureSummaries = xcArray(element: "failureSummaries", from: json).compactMap { ActionTestFailureSummary($0) }
activitySummaries = xcArray(element: "activitySummaries", from: json).compactMap { ActionTestActivitySummary($0) }
performanceMetrics = xcArray(element: "performanceMetrics", from: json)
.ofType(ActionTestPerformanceMetricSummary.self)
failureSummaries = xcArray(element: "failureSummaries", from: json)
.ofType(ActionTestFailureSummary.self)
activitySummaries = xcArray(element: "activitySummaries", from: json)
.ofType(ActionTestActivitySummary.self)
} catch {
debug("Error parsing ActionTestSummary: \(error.localizedDescription)")
return nil
Expand Down
6 changes: 4 additions & 2 deletions Sources/XCResultKit/ActionTestSummaryGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public struct ActionTestSummaryGroup: XCResultObject {
name = try xcRequired(element: "name", from: json)
identifier = try xcRequired(element: "identifier", from: json)
duration = try xcRequired(element: "duration", from: json)
subtestGroups = xcArray(element: "subtests", from: json).compactMap { ActionTestSummaryGroup($0) }
subtests = xcArray(element: "subtests", from: json).compactMap { ActionTestMetadata($0) }
subtestGroups = xcArray(element: "subtests", from: json)
.ofType(ActionTestSummaryGroup.self)
subtests = xcArray(element: "subtests", from: json)
.ofType(ActionTestMetadata.self)
} catch {
debug("Error parsing ActionTestSummaryGroup: \(error.localizedDescription)")
return nil
Expand Down
4 changes: 2 additions & 2 deletions Sources/XCResultKit/ActionTestableSummary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public struct ActionTestableSummary: XCResultObject {
projectRelativePath = xcOptional(element: "projectRelativePath", from: json)
targetName = xcOptional(element: "targetName", from: json)
testKind = xcOptional(element: "testKind", from: json)
tests = xcArray(element: "tests", from: json).compactMap { ActionTestSummaryGroup($0) }
tests = xcArray(element: "tests", from: json).ofType(ActionTestSummaryGroup.self)
diagnosticsDirectoryName = xcOptional(element: "diagnosticsDirectoryName", from: json)
failureSummaries = xcArray(element: "failureSummaries", from: json).compactMap { ActionTestFailureSummary($0) }
failureSummaries = xcArray(element: "failureSummaries", from: json).ofType(ActionTestFailureSummary.self)
testLanguage = xcOptional(element: "testLanguage", from: json)
testRegion = xcOptional(element: "testRegion", from: json)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/XCResultKit/ActionsInvocationRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public struct ActionsInvocationRecord: XCResultObject {
issues = try xcRequired(element: "issues", from: json)
metadataRef = xcOptional(element: "metadataRef", from: json)
archive = xcOptional(element: "archive", from: json)
actions = xcArray(element: "actions", from: json).compactMap { ActionRecord($0) }
actions = xcArray(element: "actions", from: json).ofType(ActionRecord.self)
} catch {
debug("Error parsing ActionsInvocationRecord: \(error.localizedDescription)")
return nil
Expand Down
8 changes: 4 additions & 4 deletions Sources/XCResultKit/ResultIssueSummaries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public struct ResultIssueSummaries: XCResultObject {
public let warningSummaries: [IssueSummary]

public init?(_ json: [String: AnyObject]) {
analyzerWarningSummaries = xcArray(element: "analyzerWarningSummaries", from: json).compactMap { IssueSummary($0) }
errorSummaries = xcArray(element: "errorSummaries", from: json).compactMap { IssueSummary($0) }
testFailureSummaries = xcArray(element: "testFailureSummaries", from: json).compactMap { TestFailureIssueSummary($0) }
warningSummaries = xcArray(element: "warningSummaries", from: json).compactMap { IssueSummary($0) }
analyzerWarningSummaries = xcArray(element: "analyzerWarningSummaries", from: json).ofType(IssueSummary.self)
errorSummaries = xcArray(element: "errorSummaries", from: json).ofType(IssueSummary.self)
testFailureSummaries = xcArray(element: "testFailureSummaries", from: json).ofType(TestFailureIssueSummary.self)
warningSummaries = xcArray(element: "warningSummaries", from: json).ofType(IssueSummary.self)
}
}
15 changes: 15 additions & 0 deletions Sources/XCResultKit/XCResultObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,18 @@ func xcArray(element: String, from json: [String: AnyObject]) -> [[String: AnyOb
return []
}
}

extension Array where Element == [String: AnyObject] {

func ofType(_ type: String) -> [Element] {
return filter { json -> Bool in
guard let typeJson = json["_type"] as? [String: AnyObject] else { return false }
guard let typeNameJson = typeJson["_name"] as? String else { return false }
return type == typeNameJson
}
}

func ofType<T>(_ type: T.Type) -> [T] where T: XCResultObject {
return ofType(String(describing: type)).compactMap(T.init)
}
}

0 comments on commit 8f5b0da

Please sign in to comment.