Skip to content

Commit

Permalink
Improve decoding logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Feb 11, 2024
1 parent d4555d0 commit 511434f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
11 changes: 6 additions & 5 deletions Sources/Angle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion Sources/Mesh.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/Polygon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
20 changes: 10 additions & 10 deletions Sources/Vertex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,23 @@ 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,
debugDescription: "Cannot decode vertex"
)
}
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)
)
}
}

Expand Down

0 comments on commit 511434f

Please sign in to comment.