parse-community / Parse-Swift

The Swift SDK for Parse Platform (iOS, macOS, watchOS, tvOS, Linux, Android, Windows)
https://parseplatform.org
MIT License
308 stars 69 forks source link

feat: add revertKeyPath() and revertObject() methods to ParseObject #402

Closed cbaker6 closed 2 years ago

cbaker6 commented 2 years ago

New Pull Request Checklist

Issue Description

After a developer begins mutating/updating a ParseObject that already has an objectId via mergeable or some other method that allows the developer to save the ParseObject using the Parse Servers quirky patch. The only way to revert a specific KeyPath back to its original value is by setting it based on a local stored version or remembering what that value was.

Related issue: #n/a

Approach

Leverage the originalData property to allow developers to revert specific KeyPath's or the whole ParseObject.

Example usage:

/* 
Assuming `score` is a GameScore `ParseObject` which was already saved and has the following properties:
- objectId = "yolo"
- name = "Frazier"
- points = 19
- ...
*/

// Reverting the whole object
var mutableScore = score.mergeable
mutableScore.points = 50
mutableScore.player = "Ali"         
mutableScore = try mutableScore.revertObject()
print(mutableScore == score) // true

// Reverting a specific KeyPath
var mutableScore2 = score.mergeable
mutableScore2.points = 50
mutableScore2.player = "Ali"         
mutableScore2 = try mutableScore2.revertKeyPath(\.player)
print(mutableScore2 == score) // false
print(mutableScore2 == mutableScore) // false
print(mutableScore2.player == score.player) // true
print(mutableScore2.player == mutableScore.player) // true

TODOs before merging

parse-github-assistant[bot] commented 2 years ago

Thanks for opening this pull request!

codecov[bot] commented 2 years ago

Codecov Report

Merging #402 (b9552e1) into main (106f97d) will increase coverage by 0.00%. The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #402   +/-   ##
=======================================
  Coverage   89.64%   89.65%           
=======================================
  Files         158      158           
  Lines       15081    15113   +32     
=======================================
+ Hits        13519    13549   +30     
- Misses       1562     1564    +2     
Impacted Files Coverage Δ
Sources/ParseSwift/Objects/ParseObject.swift 87.73% <100.00%> (+0.28%) :arrow_up:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.