Skip to content

Commit edaa1d5

Browse files
feature: Decode each element in array separately (#81)
1 parent 1ea6948 commit edaa1d5

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Foundation
2+
3+
extension KeyedDecodingContainer {
4+
5+
private struct EmptyStructData: Codable {}
6+
7+
func decodeArrayElements<T: Decodable>(
8+
forKey key: KeyedDecodingContainer<K>.Key
9+
) throws -> [T] where T: Decodable {
10+
11+
var arrayElements: [T] = []
12+
13+
let container = try nestedUnkeyedContainer(forKey: key)
14+
var containerCopy = container
15+
while !containerCopy.isAtEnd {
16+
if let element = try? containerCopy.decode(T.self) {
17+
arrayElements.append(element)
18+
}
19+
else {
20+
// @TODO: add error handling for object decoding failed
21+
_ = try containerCopy.decode(EmptyStructData.self)
22+
}
23+
}
24+
25+
return arrayElements
26+
}
27+
}

Diff for: Sources/FeaturevisorTypes/Types.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -793,9 +793,9 @@ public struct DatafileContent: Decodable {
793793

794794
schemaVersion = try values.decode(String.self, forKey: .schemaVersion)
795795
revision = try values.decode(String.self, forKey: .revision)
796-
attributes = try values.decode([Attribute].self, forKey: .attributes)
797-
segments = try values.decode([Segment].self, forKey: .segments)
798-
features = try values.decode([Feature].self, forKey: .features)
796+
attributes = try values.decodeArrayElements(forKey: .attributes)
797+
segments = try values.decodeArrayElements(forKey: .segments)
798+
features = try values.decodeArrayElements(forKey: .features)
799799
}
800800

801801
enum CodingKeys: String, CodingKey {

0 commit comments

Comments
 (0)