@@ -34,6 +34,24 @@ public var _forceRefresh: () -> Void = {}
34
34
/// Metadata embedded by Swift Bundler if present. Loaded at app start up.
35
35
private var swiftBundlerAppMetadata : AppMetadata ?
36
36
37
+ /// An error encountered when parsing Swift Bundler metadata.
38
+ private enum SwiftBundlerMetadataError : LocalizedError {
39
+ case jsonNotDictionary( Any )
40
+ case missingAppIdentifier
41
+ case missingAppVersion
42
+
43
+ var errorDescription : String ? {
44
+ switch self {
45
+ case . jsonNotDictionary:
46
+ " Root metadata JSON value wasn't an object "
47
+ case . missingAppIdentifier:
48
+ " Missing 'appIdentifier' (of type String) "
49
+ case . missingAppVersion:
50
+ " Missing 'appVersion' (of type String) "
51
+ }
52
+ }
53
+ }
54
+
37
55
extension App {
38
56
/// Metadata loaded at app start up.
39
57
public static var metadata : AppMetadata ? {
@@ -78,9 +96,26 @@ extension App {
78
96
let jsonData = Data ( bytes [ jsonStart..< lengthStart] )
79
97
80
98
do {
81
- return try JSONDecoder ( ) . decode (
82
- AppMetadata . self,
83
- from: jsonData
99
+ // Manually parsed due to the `additionalMetadata` field (which would
100
+ // require a lot of boilerplate code to parse with Codable).
101
+ let jsonValue = try JSONSerialization . jsonObject ( with: jsonData)
102
+ guard let json = jsonValue as? [ String : Any ] else {
103
+ throw SwiftBundlerMetadataError . jsonNotDictionary ( jsonValue)
104
+ }
105
+ guard let identifier = json [ " appIdentifier " ] as? String else {
106
+ throw SwiftBundlerMetadataError . missingAppIdentifier
107
+ }
108
+ guard let version = json [ " appVersion " ] as? String else {
109
+ throw SwiftBundlerMetadataError . missingAppVersion
110
+ }
111
+ let additionalMetadata =
112
+ json [ " additionalMetadata " ] . map { value in
113
+ value as? [ String : Any ] ?? [ : ]
114
+ } ?? [ : ]
115
+ return AppMetadata (
116
+ identifier: identifier,
117
+ version: version,
118
+ additionalMetadata: additionalMetadata
84
119
)
85
120
} catch {
86
121
print ( " warning: Swift Bundler metadata present but couldn't be parsed " )
0 commit comments