Skip to content

Commit 3547627

Browse files
committed
add PrimitiveObjectStore and friends
1 parent 00f4622 commit 3547627

File tree

9 files changed

+199
-48
lines changed

9 files changed

+199
-48
lines changed

ParseSwift.xcodeproj/project.pbxproj

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
4A82B7F41F254CCE0063D731 /* File.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A82B7F01F254B820063D731 /* File.swift */; };
1919
4A82B7F51F254CCE0063D731 /* GeoPoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A82B7ED1F254B820063D731 /* GeoPoint.swift */; };
2020
4A82B7F61F254CCE0063D731 /* Parse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A82B7EE1F254B820063D731 /* Parse.swift */; };
21-
4A82B7F71F254CCE0063D731 /* Miscellaneous.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A82B7EC1F254B820063D731 /* Miscellaneous.swift */; };
2221
4A82B7F81F254CCE0063D731 /* Pointer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A82B7F11F254B820063D731 /* Pointer.swift */; };
2322
4A82B7FF1F256A8F0063D731 /* Query.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A82B7FE1F256A8F0063D731 /* Query.swift */; };
2423
4A99A4691F2650CA00D72A59 /* ParseMutationContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A99A4681F2650CA00D72A59 /* ParseMutationContainer.swift */; };
@@ -52,7 +51,6 @@
5251
4AF85BDD1F783B9800665264 /* API+Commands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AF85BDC1F783B9800665264 /* API+Commands.swift */; };
5352
4AF85BDE1F783BB400665264 /* API+Commands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AF85BDC1F783B9800665264 /* API+Commands.swift */; };
5453
4AFDA72A1F26DAE1002AE4FC /* Parse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A82B7EE1F254B820063D731 /* Parse.swift */; };
55-
4AFDA72B1F26DAE1002AE4FC /* Miscellaneous.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A82B7EC1F254B820063D731 /* Miscellaneous.swift */; };
5654
4AFDA72C1F26DAE1002AE4FC /* ParseUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AEBA54E1F265A0D00628B17 /* ParseUser.swift */; };
5755
4AFDA72D1F26DAE1002AE4FC /* ParseMutationContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A99A4681F2650CA00D72A59 /* ParseMutationContainer.swift */; };
5856
4AFDA72E1F26DAE1002AE4FC /* AddOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AEBA5481F26519B00628B17 /* AddOperation.swift */; };
@@ -83,6 +81,16 @@
8381
F92B578B24C3F09600A43E8D /* Queryable.swift in Sources */ = {isa = PBXBuildFile; fileRef = F92B578924C3F09600A43E8D /* Queryable.swift */; };
8482
F92B578D24C3F0F300A43E8D /* NoBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = F92B578C24C3F0F300A43E8D /* NoBody.swift */; };
8583
F92B578E24C3F0F300A43E8D /* NoBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = F92B578C24C3F0F300A43E8D /* NoBody.swift */; };
84+
F980C99424C52DD900262E4F /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F980C99224C52DD800262E4F /* Extensions.swift */; };
85+
F980C99524C52DD900262E4F /* ParseCoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = F980C99324C52DD900262E4F /* ParseCoding.swift */; };
86+
F980C99624C52DE100262E4F /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F980C99224C52DD800262E4F /* Extensions.swift */; };
87+
F980C99724C52DE100262E4F /* ParseCoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = F980C99324C52DD900262E4F /* ParseCoding.swift */; };
88+
F980C99924C52E0200262E4F /* BaseParseUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = F980C99824C52E0200262E4F /* BaseParseUser.swift */; };
89+
F980C99C24C52E1900262E4F /* ParseStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = F980C99A24C52E1900262E4F /* ParseStorage.swift */; };
90+
F980C99D24C52E1900262E4F /* PrimitiveObjectStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = F980C99B24C52E1900262E4F /* PrimitiveObjectStore.swift */; };
91+
F980C99E24C52E1D00262E4F /* PrimitiveObjectStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = F980C99B24C52E1900262E4F /* PrimitiveObjectStore.swift */; };
92+
F980C99F24C52E1D00262E4F /* ParseStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = F980C99A24C52E1900262E4F /* ParseStorage.swift */; };
93+
F980C9A024C5395400262E4F /* BaseParseUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = F980C99824C52E0200262E4F /* BaseParseUser.swift */; };
8694
/* End PBXBuildFile section */
8795

8896
/* Begin PBXContainerItemProxy section */
@@ -107,7 +115,6 @@
107115
4A2D8C801F48B3A900EE1FCC /* ParseEncoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParseEncoder.swift; sourceTree = "<group>"; };
108116
4A2F14901F4A41B900A7A7EF /* Asynchronous.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Asynchronous.swift; sourceTree = "<group>"; };
109117
4A2F14951F4A5F2900A7A7EF /* Responses.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Responses.swift; sourceTree = "<group>"; };
110-
4A82B7EC1F254B820063D731 /* Miscellaneous.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Miscellaneous.swift; sourceTree = "<group>"; };
111118
4A82B7ED1F254B820063D731 /* GeoPoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeoPoint.swift; sourceTree = "<group>"; };
112119
4A82B7EE1F254B820063D731 /* Parse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Parse.swift; sourceTree = "<group>"; };
113120
4A82B7F01F254B820063D731 /* File.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = File.swift; sourceTree = "<group>"; };
@@ -155,6 +162,11 @@
155162
F92B578624C3F02D00A43E8D /* Saveable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Saveable.swift; sourceTree = "<group>"; };
156163
F92B578924C3F09600A43E8D /* Queryable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Queryable.swift; sourceTree = "<group>"; };
157164
F92B578C24C3F0F300A43E8D /* NoBody.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoBody.swift; sourceTree = "<group>"; };
165+
F980C99224C52DD800262E4F /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
166+
F980C99324C52DD900262E4F /* ParseCoding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParseCoding.swift; sourceTree = "<group>"; };
167+
F980C99824C52E0200262E4F /* BaseParseUser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseParseUser.swift; sourceTree = "<group>"; };
168+
F980C99A24C52E1900262E4F /* ParseStorage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParseStorage.swift; sourceTree = "<group>"; };
169+
F980C99B24C52E1900262E4F /* PrimitiveObjectStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrimitiveObjectStore.swift; sourceTree = "<group>"; };
158170
/* End PBXFileReference section */
159171

160172
/* Begin PBXFrameworksBuildPhase section */
@@ -219,6 +231,7 @@
219231
isa = PBXGroup;
220232
children = (
221233
4AC3976F1F48778900DEA9D3 /* ACL.swift */,
234+
F980C99824C52E0200262E4F /* BaseParseUser.swift */,
222235
4AF85BD21F78011100665264 /* ParseError.swift */,
223236
4A82B7F01F254B820063D731 /* File.swift */,
224237
4A82B7ED1F254B820063D731 /* GeoPoint.swift */,
@@ -246,8 +259,10 @@
246259
4AA807491F7930D0008CD551 /* Storage */ = {
247260
isa = PBXGroup;
248261
children = (
249-
4AA8074A1F7930E9008CD551 /* SecureStorage.swift */,
250262
4AA8074D1F7931B7008CD551 /* KeychainStore.swift */,
263+
F980C99A24C52E1900262E4F /* ParseStorage.swift */,
264+
F980C99B24C52E1900262E4F /* PrimitiveObjectStore.swift */,
265+
4AA8074A1F7930E9008CD551 /* SecureStorage.swift */,
251266
);
252267
path = Storage;
253268
sourceTree = "<group>";
@@ -353,7 +368,8 @@
353368
709075C424B9117400B95310 /* AnyCodable.swift */,
354369
709075C524B9117500B95310 /* AnyDecodable.swift */,
355370
709075C624B9117500B95310 /* AnyEncodable.swift */,
356-
4A82B7EC1F254B820063D731 /* Miscellaneous.swift */,
371+
F980C99224C52DD800262E4F /* Extensions.swift */,
372+
F980C99324C52DD900262E4F /* ParseCoding.swift */,
357373
4A2D8C801F48B3A900EE1FCC /* ParseEncoder.swift */,
358374
);
359375
path = Coding;
@@ -597,7 +613,6 @@
597613
4A2F14961F4A5F2900A7A7EF /* Responses.swift in Sources */,
598614
709075C724B9117500B95310 /* AnyCodable.swift in Sources */,
599615
4AC397741F488FF900DEA9D3 /* API.swift in Sources */,
600-
4A82B7F71F254CCE0063D731 /* Miscellaneous.swift in Sources */,
601616
4A82B7F51F254CCE0063D731 /* GeoPoint.swift in Sources */,
602617
4AA8074B1F7930E9008CD551 /* SecureStorage.swift in Sources */,
603618
4A82B7F61F254CCE0063D731 /* Parse.swift in Sources */,
@@ -622,9 +637,14 @@
622637
4AEBA54D1F26523800628B17 /* DeleteOperation.swift in Sources */,
623638
4AF85BDD1F783B9800665264 /* API+Commands.swift in Sources */,
624639
4A82B7F81F254CCE0063D731 /* Pointer.swift in Sources */,
640+
F980C99C24C52E1900262E4F /* ParseStorage.swift in Sources */,
625641
709075C924B9117500B95310 /* AnyDecodable.swift in Sources */,
642+
F980C99524C52DD900262E4F /* ParseCoding.swift in Sources */,
643+
F980C99D24C52E1900262E4F /* PrimitiveObjectStore.swift in Sources */,
644+
F980C99924C52E0200262E4F /* BaseParseUser.swift in Sources */,
626645
4AEBA5491F26519B00628B17 /* AddOperation.swift in Sources */,
627646
4A99A46C1F2650FF00D72A59 /* AddUniqueOperation.swift in Sources */,
647+
F980C99424C52DD900262E4F /* Extensions.swift in Sources */,
628648
4AEBA54B1F2651D900628B17 /* RemoveOperation.swift in Sources */,
629649
);
630650
runOnlyForDeploymentPostprocessing = 0;
@@ -645,9 +665,10 @@
645665
isa = PBXSourcesBuildPhase;
646666
buildActionMask = 2147483647;
647667
files = (
648-
4AFDA72B1F26DAE1002AE4FC /* Miscellaneous.swift in Sources */,
668+
F980C99E24C52E1D00262E4F /* PrimitiveObjectStore.swift in Sources */,
649669
709075C824B9117500B95310 /* AnyCodable.swift in Sources */,
650670
4AFDA7301F26DAE1002AE4FC /* DeleteOperation.swift in Sources */,
671+
F980C9A024C5395400262E4F /* BaseParseUser.swift in Sources */,
651672
4AFDA7341F26DAE1002AE4FC /* Pointer.swift in Sources */,
652673
4AA8074C1F7930E9008CD551 /* SecureStorage.swift in Sources */,
653674
4A6511501F48E400005237DF /* BatchUtils.swift in Sources */,
@@ -672,10 +693,13 @@
672693
F92B578B24C3F09600A43E8D /* Queryable.swift in Sources */,
673694
4AFDA7321F26DAE1002AE4FC /* IncrementOperation.swift in Sources */,
674695
4AF85BDE1F783BB400665264 /* API+Commands.swift in Sources */,
696+
F980C99F24C52E1D00262E4F /* ParseStorage.swift in Sources */,
675697
4AFDA72D1F26DAE1002AE4FC /* ParseMutationContainer.swift in Sources */,
676698
709075CA24B9117500B95310 /* AnyDecodable.swift in Sources */,
699+
F980C99724C52DE100262E4F /* ParseCoding.swift in Sources */,
677700
4AFDA72C1F26DAE1002AE4FC /* ParseUser.swift in Sources */,
678701
4A6511511F48E406005237DF /* ACL.swift in Sources */,
702+
F980C99624C52DE100262E4F /* Extensions.swift in Sources */,
679703
4AFDA72F1F26DAE1002AE4FC /* RemoveOperation.swift in Sources */,
680704
);
681705
runOnlyForDeploymentPostprocessing = 0;

Sources/ParseSwift/API/API.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public struct API {
7575
headers["X-Parse-Client-Key"] = clientKey
7676
}
7777

78-
if let token = CurrentUserInfo.currentSessionToken {
78+
if let token = BaseParseUser.currentUserContainer?.sessionToken {
7979
headers["X-Parse-Session-Token"] = token
8080
}
8181

Sources/ParseSwift/Coding/Extensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal extension Date {
1212
func parseFormatted() -> String {
1313
return ParseCoding.dateFormatter.string(from: self)
1414
}
15-
15+
1616
var parseRepresentation: [String: String] {
1717
return ["__type": "Date", "iso": parseFormatted()]
1818
}
@@ -24,7 +24,7 @@ extension JSONEncoder {
2424
guard let string = String(data: try encode(value), encoding: .utf8) else {
2525
throw ParseError(code: .unknownError, message: "Unable to encode object...")
2626
}
27-
27+
2828
return string
2929
}
3030
}

Sources/ParseSwift/Coding/ParseCoding.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ internal enum ParseCoding {}
1414
// MARK: Coders
1515
extension ParseCoding {
1616
private static let forbiddenKeys = Set(["createdAt", "updatedAt", "objectId", "className"])
17-
17+
1818
static func jsonEncoder() -> JSONEncoder {
1919
let encoder = JSONEncoder()
2020
encoder.dateEncodingStrategy = jsonDateEncodingStrategy
2121
return encoder
2222
}
23-
23+
2424
static func jsonDecoder() -> JSONDecoder {
2525
let encoder = JSONDecoder()
2626
encoder.dateDecodingStrategy = jsonDateDecodingStrategy
@@ -37,7 +37,7 @@ extension ParseCoding {
3737
}
3838
return true
3939
}
40-
40+
4141
return encoder
4242
}
4343
}
@@ -48,7 +48,7 @@ extension ParseCoding {
4848
case iso
4949
case type = "__type"
5050
}
51-
51+
5252
static let dateFormatter: DateFormatter = {
5353
var dateFormatter = DateFormatter()
5454
dateFormatter.locale = Locale(identifier: "")
@@ -62,7 +62,7 @@ extension ParseCoding {
6262
let dateString = dateFormatter.string(from: date)
6363
try container.encode(dateString, forKey: .iso)
6464
})
65-
65+
6666
static let parseDateEncodingStrategy: ParseEncoder.DateEncodingStrategy = .custom({ (date, enc) in
6767
var container = enc.container(keyedBy: DateEncodingKeys.self)
6868
try container.encode("Date", forKey: .type)
@@ -81,7 +81,7 @@ extension ParseCoding {
8181
return dateFormatter.date(from: decoded)!
8282
}
8383
}
84-
84+
8585
throw ParseError(code: .unknownError, message: "unable to decode")
8686
})
8787
}

Sources/ParseSwift/MutationOperations/ParseMutationContainer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
public struct ParseMutationContainer<T>: Encodable where T: ParseObject {
1212
typealias ObjectType = T
13-
13+
1414
var target: T
1515
private var operations = [String: Encodable]()
1616

Sources/ParseSwift/Objects/ParseUser.swift

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import Foundation
22

3-
// MARK: CurrentUserInfo
4-
internal struct CurrentUserInfo {
5-
static var currentUser: Any?
6-
static var currentSessionToken: String?
3+
// MARK: CurrentUserContainer
4+
struct CurrentUserContainer<T: ParseUser>: Codable {
5+
var currentUser: T?
6+
var sessionToken: String?
77
}
88

99
// MARK: ParseUser
@@ -13,34 +13,37 @@ public protocol ParseUser: ParseObject {
1313
var password: String? { get set }
1414
}
1515

16-
// MARK: Convenience
16+
// MARK: Default Implementations
1717
public extension ParseUser {
1818
static var className: String {
1919
return "_User"
2020
}
21-
22-
static var current: Self? {
23-
return CurrentUserInfo.currentUser as? Self
21+
}
22+
23+
// MARK: Current User Support
24+
extension ParseUser {
25+
static var currentUserContainer: CurrentUserContainer<Self>? {
26+
get { try? ParseStorage.shared.get(valueFor: ParseStorage.Keys.currentUser) }
27+
set { try? ParseStorage.shared.set(newValue, for: ParseStorage.Keys.currentUser) }
2428
}
25-
26-
var sessionToken: String? {
27-
if let currentUser = CurrentUserInfo.currentUser as? Self,
28-
currentUser.objectId != nil && self.objectId != nil &&
29-
currentUser.objectId == self.objectId {
30-
return CurrentUserInfo.currentSessionToken
31-
}
3229

33-
return nil
30+
public static var current: Self? {
31+
get { Self.currentUserContainer?.currentUser }
32+
set { Self.currentUserContainer?.currentUser = newValue }
33+
}
34+
35+
public var sessionToken: String? {
36+
Self.currentUserContainer?.sessionToken
3437
}
3538
}
3639

3740
// MARK: Logging In
3841
extension ParseUser {
3942
public static func login(username: String,
40-
password: String) throws -> Self {
43+
password: String) throws -> Self {
4144
return try loginCommand(username: username, password: password).execute(options: [])
4245
}
43-
46+
4447
private static func loginCommand(username: String,
4548
password: String) -> API.Command<NoBody, Self> {
4649
let params = [
@@ -53,8 +56,12 @@ extension ParseUser {
5356
params: params) { (data) -> Self in
5457
let user = try ParseCoding.jsonDecoder().decode(Self.self, from: data)
5558
let response = try ParseCoding.jsonDecoder().decode(LoginSignupResponse.self, from: data)
56-
CurrentUserInfo.currentUser = user
57-
CurrentUserInfo.currentSessionToken = response.sessionToken
59+
60+
Self.currentUserContainer = .init(
61+
currentUser: user,
62+
sessionToken: response.sessionToken
63+
)
64+
5865
return user
5966
}
6067
}
@@ -65,22 +72,21 @@ extension ParseUser {
6572
public static func logout() throws {
6673
_ = try logoutCommand().execute(options: [])
6774
}
68-
75+
6976
private static func logoutCommand() -> API.Command<NoBody, Void> {
7077
return API.Command(method: .POST, path: .logout) { (_) -> Void in
71-
CurrentUserInfo.currentUser = nil
72-
CurrentUserInfo.currentSessionToken = nil
78+
currentUserContainer = nil
7379
}
7480
}
7581
}
7682

7783
// MARK: Signing Up
7884
extension ParseUser {
7985
public static func signup(username: String,
80-
password: String) throws -> Self {
86+
password: String) throws -> Self {
8187
return try signupCommand(username: username, password: password).execute(options: [])
8288
}
83-
89+
8490
public func signup() throws -> Self {
8591
return try signupCommand().execute(options: [])
8692
}
@@ -96,9 +102,11 @@ extension ParseUser {
96102
user.password = password
97103
user.updatedAt = response.updatedAt ?? response.createdAt
98104

99-
// Set the current user
100-
CurrentUserInfo.currentUser = user
101-
CurrentUserInfo.currentSessionToken = response.sessionToken
105+
currentUserContainer = .init(
106+
currentUser: user,
107+
sessionToken: response.sessionToken
108+
)
109+
102110
return user
103111
}
104112
}
@@ -109,9 +117,12 @@ extension ParseUser {
109117
let response = try ParseCoding.jsonDecoder().decode(LoginSignupResponse.self, from: data)
110118
user.updatedAt = response.updatedAt ?? response.createdAt
111119
user.createdAt = response.createdAt
112-
// Set the current user
113-
CurrentUserInfo.currentUser = user
114-
CurrentUserInfo.currentSessionToken = response.sessionToken
120+
121+
Self.currentUserContainer = .init(
122+
currentUser: user,
123+
sessionToken: response.sessionToken
124+
)
125+
115126
return user
116127
}
117128
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// File 2.swift
3+
//
4+
//
5+
// Created by Pranjal Satija on 7/19/20.
6+
//
7+
8+
import Foundation
9+
10+
/// Used internally to form a concrete type representing `ParseUser`.
11+
/// `ParseUser` itself has associatedtype requirements, so it's often awkard to use it directly.
12+
struct BaseParseUser: ParseUser {
13+
var username: String?
14+
var email: String?
15+
var password: String?
16+
var objectId: String?
17+
var createdAt: Date?
18+
var updatedAt: Date?
19+
var ACL: ACL?
20+
}

0 commit comments

Comments
 (0)