-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary+Extensions.swift
More file actions
123 lines (103 loc) · 3.42 KB
/
Dictionary+Extensions.swift
File metadata and controls
123 lines (103 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright (c) 2023 European Commission
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Foundation
import SwiftyJSON
public func + (lhs: [String: String], rhs: [String: String]) -> [String: String] {
return lhs.merging(rhs) { (current, new) in new }
}
public extension Dictionary where Key == String, Value == Any {
func toThrowingJSONData() throws -> Data {
return try JSONSerialization.data(withJSONObject: self, options: [])
}
// Creates a dictionary from a JSON file in the specified bundle
static func from(bundle name: String) -> Result<Self, JSONParseError> {
let fileType = "json"
guard let path = Bundle.module.path(forResource: name, ofType: fileType) else {
return .failure(.fileNotFound(filename: name))
}
return from(JSONfile: URL(fileURLWithPath: path))
}
func getValue<T: Codable>(
for key: String,
error: LocalizedError
) throws -> T {
guard let value = self[key] as? T else {
throw error
}
return value
}
var jsonData: Data? {
return try? JSONSerialization.data(withJSONObject: self, options: [.prettyPrinted])
}
func toJSONString() -> String? {
if let jsonData = jsonData {
let jsonString = String(data: jsonData, encoding: .utf8)
return jsonString
}
return nil
}
static func from(JSONfile url: URL) -> Result<Self, JSONParseError> {
let data: Data
do {
data = try Data(contentsOf: url)
} catch let error {
return .failure(.dataInitialisation(error))
}
let jsonObject: Any
do {
jsonObject = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
} catch let error {
return .failure(.jsonSerialization(error))
}
guard let jsonResult = jsonObject as? Self else {
return .failure(.mappingFail(
value: String(describing: jsonObject),
toType: String(describing: Self.Type.self)
))
}
return .success(jsonResult)
}
func convertToDictionaryOfStrings() -> [Key: String] {
var stringDictionary: [Key: String] = [:]
for (key, value) in self {
if let stringValue = value as? String {
stringDictionary[key] = stringValue
} else {
stringDictionary[key] = JSON(value).stringValue
}
}
return stringDictionary
}
func convertToDictionaryOfStrings(excludingKeys: [String]) -> [Key: String] {
var stringDictionary: [Key: String] = [:]
for (key, value) in self {
if excludingKeys.contains(where: { $0 == key }) {
continue
}
if let stringValue = value as? String {
stringDictionary[key] = stringValue
} else {
stringDictionary[key] = JSON(value).stringValue
}
}
return stringDictionary
}
func containsAll(_ keys: [Key]) -> Bool {
let keySet = Set(keys)
let dictionaryKeySet = Set(self.keys)
return keySet.isSubset(of: dictionaryKeySet)
}
}