Skip to content

Commit 2b2fe00

Browse files
unnameddthomaspaulmann
authored andcommitted
Implement init decoder to RaycastData model
to support future Metadata
1 parent 3671760 commit 2b2fe00

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

Tools/Toolkit/Sources/ToolkitLibrary/Models/RaycastData.swift

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,52 @@
66
import Foundation
77

88
struct RaycastData: Codable {
9-
var groups = Groups()
10-
var updatedAt = Date()
11-
var totalScriptCommands = 0
9+
var groups: Groups
10+
var updatedAt: Date
11+
var totalScriptCommands: Int
12+
var metadata: [Metadata]
13+
14+
var isEmpty: Bool {
15+
groups.isEmpty
16+
&& totalScriptCommands == 0
17+
&& metadata.isEmpty
18+
}
19+
20+
private enum CodingKeys: String, CodingKey {
21+
case groups
22+
case updatedAt
23+
case totalScriptCommands
24+
case metadata
25+
}
26+
27+
init() {
28+
self.groups = .init()
29+
self.updatedAt = Date()
30+
self.totalScriptCommands = 0
31+
self.metadata = []
32+
}
33+
34+
init(from decoder: Decoder) throws {
35+
let container = try decoder.container(keyedBy: CodingKeys.self)
36+
37+
groups = try container.decode(Groups.self, forKey: .groups)
38+
totalScriptCommands = try container.decode(Int.self, forKey: .totalScriptCommands)
39+
40+
if let value = try container.decodeIfPresent(String.self, forKey: .updatedAt) {
41+
let dateFormatter = DateFormatter()
42+
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
43+
44+
self.updatedAt = dateFormatter.date(from: value) ?? Date()
45+
}
46+
else {
47+
self.updatedAt = Date()
48+
}
49+
50+
if let metadata = try container.decodeIfPresent([Metadata].self, forKey: .metadata) {
51+
self.metadata = metadata
52+
}
53+
else {
54+
self.metadata = []
55+
}
56+
}
1257
}

0 commit comments

Comments
 (0)