-
Notifications
You must be signed in to change notification settings - Fork 721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SQLite persistent normalized cache #79
Changes from 1 commit
ddee83a
55e8b8d
647884e
3a094a1
6384fb4
d4349f5
e34dd89
9f8b5fb
135ac3f
76cdf91
9680d22
8cba5f5
829157d
deeab16
6f5192b
43b2cb5
1ae3e37
6d9ef48
abaa650
589096f
20fa038
26a2625
3e7a0ee
5245bf8
da1f9e7
b802c7b
ae5b4c0
35fed37
acbddf2
82990f0
1c95de2
8409057
4a4457d
1d195f7
2a6ec2d
91af7ce
adbfc27
a8d94b2
0a92c5d
c2a25c2
b18b616
859936b
cd130e5
25a72bb
ded68d7
17f19db
4dca165
ef49021
1c30841
4853f4d
4a517a7
41b02d0
9931574
d536c17
f14048c
4a07f40
aa12af9
69d124d
ca6209d
ec80581
5118469
97b6ce5
92fb642
31ad83d
e5dac42
1f344e0
eb67621
6ee21ba
9f9d17f
c029a48
28feedf
76de545
cab7b64
95cd1af
5e4e3b5
998c1d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,60 +83,56 @@ public final class SQLiteNormalizedCache: NormalizedCache { | |
throw SQLiteNormalizedCacheError.invalidRecordEncoding(record: record) | ||
} | ||
|
||
let recordJSON = try SQLiteSerialization.deserialize(data: recordData) | ||
return Record(key: row[key], recordJSON) | ||
let fields = try SQLiteSerialization.deserialize(data: recordData) | ||
return Record(key: row[key], fields) | ||
} | ||
} | ||
|
||
private let serializedReferenceKey = "reference" | ||
|
||
final class SQLiteSerialization { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, I think this should probably be private. |
||
static func serialize(fields: JSONObject) throws -> Data { | ||
static func serialize(fields: Record.Fields) throws -> Data { | ||
var objectToSerialize = JSONObject() | ||
for (key, value) in fields { | ||
objectToSerialize[key] = try serialize(value: value) | ||
} | ||
return try JSONSerializationFormat.serialize(value: objectToSerialize) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The cleanup in this commit is great! Perhaps |
||
return try JSONSerialization.data(withJSONObject: objectToSerialize, options: []) | ||
} | ||
|
||
private static func serialize(value: Record.Value) throws -> JSONValue { | ||
switch value { | ||
case let reference as Reference: | ||
return [serializedReferenceKey: reference.key] | ||
case let array as [Record.Value]: | ||
return try array.map { try serialize(value: $0) } | ||
default: | ||
return value | ||
} | ||
} | ||
|
||
static func deserialize(data: Data) throws -> JSONObject { | ||
let object = try JSONSerializationFormat.deserialize(data: data) | ||
static func deserialize(data: Data) throws -> Record.Fields { | ||
let object = try JSONSerialization.jsonObject(with: data, options: []) | ||
guard let jsonObject = object as? JSONObject else { | ||
throw SQLiteNormalizedCacheError.invalidRecordShape(object: object) | ||
} | ||
var deserializedObject = JSONObject() | ||
var fields = Record.Fields() | ||
for (key, value) in jsonObject { | ||
deserializedObject[key] = try deserialize(valueJSON: value) | ||
fields[key] = try deserialize(jsonValue: value) | ||
} | ||
return deserializedObject | ||
return fields | ||
} | ||
|
||
private static func deserialize(valueJSON: Any) throws -> Any { | ||
switch valueJSON { | ||
private static func deserialize(jsonValue: JSONValue) throws -> Record.Value { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about this for the naming of the
|
||
switch jsonValue { | ||
case let dictionary as JSONObject: | ||
guard let reference = dictionary[serializedReferenceKey] as? String else { | ||
throw SQLiteNormalizedCacheError.invalidRecordValue(value: valueJSON) | ||
throw SQLiteNormalizedCacheError.invalidRecordValue(value: jsonValue) | ||
} | ||
return Reference(key: reference) | ||
case let array as NSArray: | ||
return try array.map { try deserialize(valueJSON: $0) } | ||
case let array as [JSONValue]: | ||
return try array.map { try deserialize(jsonValue: $0) } | ||
default: | ||
return valueJSON | ||
} | ||
} | ||
|
||
private static func serialize(value: Any) throws -> Any { | ||
switch value { | ||
case let reference as Reference: | ||
return [serializedReferenceKey: reference.key] | ||
case let array as NSArray: | ||
return try array.map { try serialize(value: $0) } | ||
case let string as NSString: | ||
return string as String | ||
case let number as NSNumber: | ||
return number.doubleValue | ||
default: | ||
return value | ||
return jsonValue | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these be removed?