From 511434fe4dc814f8cc9296cf681c03cea7249ed9 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Sun, 11 Feb 2024 12:10:16 +0000 Subject: [PATCH] Improve decoding logic --- Sources/Angle.swift | 11 ++++++----- Sources/Mesh.swift | 3 ++- Sources/Path.swift | 3 ++- Sources/Polygon.swift | 3 ++- Sources/Vertex.swift | 20 ++++++++++---------- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Sources/Angle.swift b/Sources/Angle.swift index d452d4e5..6da5f497 100644 --- a/Sources/Angle.swift +++ b/Sources/Angle.swift @@ -53,15 +53,16 @@ extension Angle: Codable { /// Creates a new angle by decoding from the given decoder. /// - Parameter decoder: The decoder to read data from. public init(from decoder: Decoder) throws { - guard let container = try? decoder.container(keyedBy: CodingKeys.self) else { - try self.init(radians: Double(from: decoder)) + if let radians = try? decoder.singleValueContainer().decode(Double.self) { + self.init(radians: radians) return } - if let radians = try container.decodeIfPresent(Double.self, forKey: .radians) { - self.init(radians: radians) + let container = try decoder.container(keyedBy: CodingKeys.self) + if let degrees = try container.decodeIfPresent(Double.self, forKey: .degrees) { + self.init(degrees: degrees) return } - self = try .degrees(container.decode(Double.self, forKey: .degrees)) + try self.init(radians: container.decode(Double.self, forKey: .radians)) } /// Encodes this angle into the given encoder. diff --git a/Sources/Mesh.swift b/Sources/Mesh.swift index a7451de1..f220e4df 100644 --- a/Sources/Mesh.swift +++ b/Sources/Mesh.swift @@ -67,7 +67,8 @@ extension Mesh: Codable { submeshes: nil ) } else { - let polygons = try [Polygon](from: decoder) + let container = try decoder.singleValueContainer() + let polygons = try container.decode([Polygon].self) self.init(polygons) } } diff --git a/Sources/Path.swift b/Sources/Path.swift index 774a943c..637b33f8 100644 --- a/Sources/Path.swift +++ b/Sources/Path.swift @@ -72,7 +72,8 @@ extension Path: Codable { self.init(points ?? []) } } else { - let points = try [PathPoint](from: decoder) + let container = try decoder.singleValueContainer() + let points = try container.decode([PathPoint].self) self.init(points) } } diff --git a/Sources/Polygon.swift b/Sources/Polygon.swift index de1baa97..9550bb34 100644 --- a/Sources/Polygon.swift +++ b/Sources/Polygon.swift @@ -70,7 +70,8 @@ extension Polygon: Codable { plane = try container.decode(Plane.self) material = try container.decodeIfPresent(CodableMaterial.self)?.value } else { - vertices = try [Vertex](from: decoder) + let container = try decoder.singleValueContainer() + vertices = try container.decode([Vertex].self) } guard !verticesAreDegenerate(vertices) else { throw DecodingError.dataCorruptedError( diff --git a/Sources/Vertex.swift b/Sources/Vertex.swift index 05caa2de..f8719297 100644 --- a/Sources/Vertex.swift +++ b/Sources/Vertex.swift @@ -75,16 +75,8 @@ extension Vertex: Codable { /// Creates a new vertex by decoding from the given decoder. /// - Parameter decoder: The decoder to read data from. public init(from decoder: Decoder) throws { - if let container = try? decoder.container(keyedBy: CodingKeys.self) { - try self.init( - container.decode(Vector.self, forKey: .position), - container.decodeIfPresent(Vector.self, forKey: .normal), - container.decodeIfPresent(Vector.self, forKey: .texcoord), - container.decodeIfPresent(Color.self, forKey: .color) - ) - } else { - let container = try decoder.singleValueContainer() - let values = try container.decode([Double].self) + let container = try decoder.singleValueContainer() + if let values = try? container.decode([Double].self) { guard let vertex = Vertex(values) else { throw DecodingError.dataCorruptedError( in: container, @@ -92,6 +84,14 @@ extension Vertex: Codable { ) } self = vertex + } else { + let container = try decoder.container(keyedBy: CodingKeys.self) + try self.init( + container.decode(Vector.self, forKey: .position), + container.decodeIfPresent(Vector.self, forKey: .normal), + container.decodeIfPresent(Vector.self, forKey: .texcoord), + container.decodeIfPresent(Color.self, forKey: .color) + ) } }