From 4040883d8899465fe9c6e5e778f24736b050102d Mon Sep 17 00:00:00 2001 From: Maxwell Legrand Date: Tue, 14 May 2024 16:49:48 -0400 Subject: [PATCH] Change Codable to Decodable and add guard --- .../CoberturaCoverageConverter.swift | 33 +++++++------------ 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/Sources/xcresultparser/CoberturaCoverageConverter.swift b/Sources/xcresultparser/CoberturaCoverageConverter.swift index 93980b5..c5be2cb 100644 --- a/Sources/xcresultparser/CoberturaCoverageConverter.swift +++ b/Sources/xcresultparser/CoberturaCoverageConverter.swift @@ -36,14 +36,14 @@ 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? @@ -51,7 +51,7 @@ struct LineDetail: Codable { } // FileCoverage information struct -struct FileCoverage: Codable { +struct FileCoverage: Decodable { let files: [String: [LineDetail]] // Custom initializer to handle the top-level dictionary @@ -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? @@ -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)