Skip to content

Commit 3184328

Browse files
committed
feat(swift): saveObjectsModified
1 parent b820b09 commit 3184328

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Foundation
2+
#if os(Linux) // For linux interop
3+
import FoundationNetworking
4+
#endif
5+
6+
import Core
7+
{{> snippets/import}}
8+
9+
func saveObjectsModified() async throws {
10+
do {
11+
{{> snippets/init}}
12+
13+
let path = URL(string: #file)!.deletingLastPathComponent()
14+
.appendingPathComponent("products.json")
15+
let data = try Data(contentsOf: URL(fileURLWithPath: path.absoluteString))
16+
let products = try JSONDecoder().decode([[String: AnyCodable]].self, from: data)
17+
18+
let records = products.map { (product) -> [String: AnyCodable] in
19+
var reference = product["product_reference"]?.value as! String
20+
var suffixes: [String] = []
21+
22+
while reference.count > 1 {
23+
reference = String(reference.dropFirst())
24+
suffixes.append(reference)
25+
}
26+
var record: [String: AnyCodable] = product
27+
record["product_reference_suffixes"] = AnyCodable(suffixes)
28+
return record
29+
}
30+
31+
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}}
32+
} catch {
33+
print(error)
34+
}
35+
}

0 commit comments

Comments
 (0)