Skip to content

Commit

Permalink
Change Codable to Decodable and add guard
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwell-legrand committed May 14, 2024
1 parent 80cfc13 commit 4040883
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions Sources/xcresultparser/CoberturaCoverageConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ enum JSONParseError: Error {
}

// Subrange information struct
struct Subrange: Codable {
struct Subrange: Decodable {
let column: Int
let executionCount: Int
let length: Int
}

// LineDetail information struct
struct LineDetail: Codable {
struct LineDetail: Decodable {
let isExecutable: Bool
let line: Int
let executionCount: Int?
let subranges: [Subrange]?
}

// FileCoverage information struct
struct FileCoverage: Codable {
struct FileCoverage: Decodable {
let files: [String: [LineDetail]]

// Custom initializer to handle the top-level dictionary
Expand All @@ -67,17 +67,6 @@ struct FileCoverage: Codable {
self.files = filesDict
}

// Custom encoding method to handle the top-level dictionary
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
for (key, value) in files {
guard let codingKey = CodingKeys(stringValue: key) else {
continue
}
try container.encode(value, forKey: codingKey)
}
}

private struct CodingKeys: CodingKey {
var stringValue: String
var intValue: Int?
Expand Down Expand Up @@ -151,15 +140,15 @@ public class CoberturaCoverageConverter: CoverageConverter, XmlSerializable {

for lineData in value {
let lineNum = lineData.line
var covered = lineData.executionCount
if covered != nil {
// If the line coverage count is a MAX_INT, just set it to 1
if covered == Int.max {
covered = 1
}
let line = LineInfo(lineNumber: String(lineNum), coverage: covered!)
fileLines.append(line)
guard var covered = lineData.executionCount else {
continue
}
// If the line coverage count is a MAX_INT, just set it to 1
if covered == Int.max {
covered = 1
}
let line = LineInfo(lineNumber: String(lineNum), coverage: covered)
fileLines.append(line)
}

let fileInfoInst = FileInfo(path: self.relativePath(for: fileName, relativeTo: self.projectRoot), lines: fileLines)
Expand Down

0 comments on commit 4040883

Please sign in to comment.