Skip to content

Commit 7b7116d

Browse files
authored
refactor: Create method for skipping ParseObject keys (#144)
* refactor: Create method for skipping Keys * test additiional types * fix linux test with NSNumber * remove float test from linux
1 parent e54ac22 commit 7b7116d

File tree

3 files changed

+85
-21
lines changed

3 files changed

+85
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!-- markdownlint-disable -->
22
![parse-swift](https://user-images.githubusercontent.com/8621344/204069535-e1882bb0-bbcb-4178-87e6-58fd1bed96d1.png)
33

4-
<h3 align="center">iOS · macOS · watchOS · tvOS · Linux · Android · Windows</h3>
4+
<h3 align="center">iOS · macOS · watchOS · tvOS · visionOS ·Linux · Android · Windows</h3>
55

66
<h4 align="center">
77
⭐️ ParseSwift was highlighted in <a href="https://iosdevweekly.com/issues/560#start" target="_blank"> issue #560</a> of <a href="https://iosdevweekly.com" target="_blank"> iOS Dev Weekly</a> and discussed in <a href="https://blog.swiftpackageindex.com/posts/swift-package-indexing-episode-5/" target="_blank"> episode 5</a> of <a href="https://swiftpackageindexing.transistor.fm" target="_blank">Swift Package Index Twitter Spaces</a> </h3>

Sources/ParseSwift/Coding/ParseEncoder.swift

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -435,75 +435,76 @@ private struct _ParseEncoderKeyedEncodingContainer<Key: CodingKey>: KeyedEncodin
435435

436436
// MARK: - KeyedEncodingContainerProtocol Methods
437437
mutating func encodeNil(forKey key: Key) throws {
438-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
438+
guard !shouldSkipKey(key) else { return }
439439
container[key.stringValue] = NSNull()
440440
}
441441
mutating func encode(_ value: Bool, forKey key: Key) throws {
442-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
442+
guard !shouldSkipKey(key) else { return }
443443
self.container[key.stringValue] = self.encoder.box(value)
444444
}
445445
mutating func encode(_ value: Int, forKey key: Key) throws {
446-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
446+
guard !shouldSkipKey(key) else { return }
447447
self.container[key.stringValue] = self.encoder.box(value)
448448
}
449449
mutating func encode(_ value: Int8, forKey key: Key) throws {
450-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
450+
guard !shouldSkipKey(key) else { return }
451451
self.container[key.stringValue] = self.encoder.box(value)
452452
}
453453
mutating func encode(_ value: Int16, forKey key: Key) throws {
454-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
454+
guard !shouldSkipKey(key) else { return }
455455
self.container[key.stringValue] = self.encoder.box(value)
456456
}
457457
mutating func encode(_ value: Int32, forKey key: Key) throws {
458-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
458+
guard !shouldSkipKey(key) else { return }
459459
self.container[key.stringValue] = self.encoder.box(value)
460460
}
461461
mutating func encode(_ value: Int64, forKey key: Key) throws {
462-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
462+
guard !shouldSkipKey(key) else { return }
463463
self.container[key.stringValue] = self.encoder.box(value)
464464
}
465465
mutating func encode(_ value: UInt, forKey key: Key) throws {
466-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
466+
guard !shouldSkipKey(key) else { return }
467467
self.container[key.stringValue] = self.encoder.box(value)
468468
}
469469
mutating func encode(_ value: UInt8, forKey key: Key) throws {
470-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
470+
guard !shouldSkipKey(key) else { return }
471471
self.container[key.stringValue] = self.encoder.box(value)
472472
}
473473
mutating func encode(_ value: UInt16, forKey key: Key) throws {
474-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
474+
guard !shouldSkipKey(key) else { return }
475475
self.container[key.stringValue] = self.encoder.box(value)
476476
}
477477
mutating func encode(_ value: UInt32, forKey key: Key) throws {
478-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
478+
guard !shouldSkipKey(key) else { return }
479479
self.container[key.stringValue] = self.encoder.box(value)
480480
}
481481
mutating func encode(_ value: UInt64, forKey key: Key) throws {
482-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
482+
guard !shouldSkipKey(key) else { return }
483483
self.container[key.stringValue] = self.encoder.box(value)
484484
}
485485
mutating func encode(_ value: String, forKey key: Key) throws {
486-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
486+
guard !shouldSkipKey(key) else { return }
487487
self.container[key.stringValue] = self.encoder.box(value)
488488
}
489489
mutating func encode(_ value: Float, forKey key: Key) throws {
490490
// Since the float may be invalid and throw, the coding path needs to contain this key.
491-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
491+
guard !shouldSkipKey(key) else { return }
492492
self.encoder.codingPath.append(key)
493493
defer { self.encoder.codingPath.removeLast() }
494494
self.container[key.stringValue] = try self.encoder.box(value)
495495
}
496496

497497
mutating func encode(_ value: Double, forKey key: Key) throws {
498498
// Since the double may be invalid and throw, the coding path needs to contain this key.
499-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
499+
guard !shouldSkipKey(key) else { return }
500500
self.encoder.codingPath.append(key)
501501
defer { self.encoder.codingPath.removeLast() }
502502
self.container[key.stringValue] = try self.encoder.box(value)
503503
}
504504

505505
mutating func encode<T>(_ value: T, forKey key: Key) throws where T: Encodable {
506-
if self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys { return }
506+
507+
guard !shouldSkipKey(key) else { return }
507508

508509
var valueToEncode: Encodable = value
509510
if ((value as? Objectable) != nil)
@@ -542,6 +543,13 @@ private struct _ParseEncoderKeyedEncodingContainer<Key: CodingKey>: KeyedEncodin
542543
self.container[key.stringValue] = try self.encoder.box(valueToEncode)
543544
}
544545

546+
func shouldSkipKey(_ key: Key) -> Bool {
547+
guard self.encoder.skippedKeys.contains(key.stringValue) && !self.encoder.ignoreSkipKeys else {
548+
return false
549+
}
550+
return true
551+
}
552+
545553
mutating func nestedContainer<NestedKey>(
546554
keyedBy keyType: NestedKey.Type,
547555
forKey key: Key

Tests/ParseSwiftTests/ParseEncoderTests/ParseEncoderExtraTests.swift

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ import XCTest
1111

1212
class ParseEncoderTests: XCTestCase {
1313
struct GameScore: ParseObject, ParseQueryScorable {
14-
//: These are required by ParseObject
14+
// These are required by ParseObject
1515
var objectId: String?
1616
var createdAt: Date?
1717
var updatedAt: Date?
1818
var ACL: ParseACL?
1919
var score: Double?
2020
var originalData: Data?
2121

22-
//: ParseUser property
22+
// ParseUser property
2323
var emailVerified: Bool?
2424

25-
//: Your own properties
25+
// Your own properties
2626
var points: Int
2727

28-
//: a custom initializer
28+
// a custom initializer
2929
init() {
3030
self.points = 5
3131
}
@@ -34,6 +34,43 @@ class ParseEncoderTests: XCTestCase {
3434
}
3535
}
3636

37+
struct AdditionalTypes: ParseObject {
38+
// These are required by ParseObject
39+
var objectId: String?
40+
var createdAt: Date?
41+
var updatedAt: Date?
42+
var ACL: ParseACL?
43+
var score: Double?
44+
var originalData: Data?
45+
46+
// Your own properties
47+
var int8: Int8
48+
var int16: Int16
49+
var int32: Int32
50+
var int64: Int64
51+
var uint: UInt
52+
var uint8: UInt8
53+
var uint16: UInt16
54+
var uint32: UInt32
55+
var uint64: UInt64
56+
var float: Float
57+
58+
// a custom initializer
59+
init() {
60+
self.int8 = 1
61+
self.int16 = 2
62+
self.int32 = 3
63+
self.int64 = 4
64+
self.uint = 5
65+
self.uint8 = 1
66+
self.uint16 = 2
67+
self.uint32 = 3
68+
self.uint64 = 4
69+
self.float = 1.1
70+
}
71+
72+
}
73+
3774
struct Address: Codable {
3875
let street: String
3976
let city: String
@@ -80,6 +117,25 @@ class ParseEncoderTests: XCTestCase {
80117
XCTAssertEqual(parseDecoded["*"]?["read"], true)
81118
}
82119

120+
func testEncodingAdditionalTypes() throws {
121+
let object = AdditionalTypes()
122+
123+
let encodedJSON = try ParseCoding.parseEncoder().encode(object, skipKeys: .object)
124+
let decodedJSON = try ParseCoding.jsonDecoder().decode([String: AnyCodable].self, from: encodedJSON)
125+
XCTAssertEqual(decodedJSON["int8"]?.value as? NSNumber, NSNumber(value: object.int8))
126+
XCTAssertEqual(decodedJSON["int16"]?.value as? NSNumber, NSNumber(value: object.int16))
127+
XCTAssertEqual(decodedJSON["int32"]?.value as? NSNumber, NSNumber(value: object.int32))
128+
XCTAssertEqual(decodedJSON["int64"]?.value as? NSNumber, NSNumber(value: object.int64))
129+
XCTAssertEqual(decodedJSON["uint"]?.value as? NSNumber, NSNumber(value: object.uint))
130+
XCTAssertEqual(decodedJSON["uint8"]?.value as? NSNumber, NSNumber(value: object.uint8))
131+
XCTAssertEqual(decodedJSON["uint16"]?.value as? NSNumber, NSNumber(value: object.uint16))
132+
XCTAssertEqual(decodedJSON["uint32"]?.value as? NSNumber, NSNumber(value: object.uint32))
133+
XCTAssertEqual(decodedJSON["uint64"]?.value as? NSNumber, NSNumber(value: object.uint64))
134+
#if !os(Linux) && !os(Android) && !os(Windows)
135+
XCTAssertEqual(decodedJSON["float"]?.value as? Double, Double(object.float))
136+
#endif
137+
}
138+
83139
func testSkipKeysDefaultCodingKeys() throws {
84140
var score = GameScore(points: 10)
85141
score.objectId = "yarr"

0 commit comments

Comments
 (0)