File tree Expand file tree Collapse file tree 1 file changed +9
-6
lines changed
Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,13 @@ struct ErrorMessage: LocalizedError {
1818/// A struct that parses a JSON file containing a "tags" array, extracting the "name" field from each element.
1919struct PackageTargetsParser {
2020
21+ struct Root : Decodable {
22+ struct Tag : Decodable {
23+ let name : String
24+ }
25+ let tags : [ Tag ]
26+ }
27+
2128 /// Parses the provided JSON file and extracts the "name" values from the "tags" array.
2229 ///
2330 /// - Parameter path: The path to the JSON file to be parsed.
@@ -42,12 +49,8 @@ struct PackageTargetsParser {
4249 /// ```
4350 func parse( from path: String ) throws -> [ String ] {
4451 let data = try Data ( contentsOf: URL ( fileURLWithPath: path) )
45- let json = try JSONSerialization . jsonObject ( with: data, options: [ ] )
46- if let dict = json as? [ String : Any ] , let tags = dict [ " tags " ] as? [ [ String : Any ] ] {
47- return tags. compactMap { $0 [ " name " ] as? String }
48- } else {
49- throw ErrorMessage ( message: " Properties not found. " )
50- }
52+ let root = try JSONDecoder ( ) . decode ( Root . self, from: data)
53+ return root. tags. map ( \. name)
5154 }
5255}
5356
You can’t perform that action at this time.
0 commit comments