Skip to content
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

feat: add set operation based on KeyPath #403

Merged
merged 9 commits into from
Sep 5, 2022

Conversation

cbaker6
Copy link
Contributor

@cbaker6 cbaker6 commented Sep 4, 2022

New Pull Request Checklist

Issue Description

Using the set ParseOperation

The set operation currently requires using a tuple of the key String and KeyPath. This redundancy is because currently there is no way to get the string value of a KeyPath reliably without the developer supplying the correct key String. Errors can be reduced when using the set operation if only the KeyPath is used to set the value instead of the key String.

Unwrapping optional ParseObject properties

Safely unwrapping properties in Swift is easy, but can be cumbersome and require extra code. Since all properties of a ParseObject are required to be optional for encoding/decoding and Parse Pointer purposes developers usually have to write additional code to unwrap values. It would be nice if a helper method was provided that can do this.

Related issue: #n/a

Approach

Using the set ParseOperation

Since the ParseOperation has the ParseObject as a target, add a set operation method that only needs the KeyPath and the value to set. This only works for set as the rest of the operations require additional information modified on the target and sent to the server to complete the operation. In addition, it doesn't work for setting nil values as this requires the SDK to encode a NSNull value instead of nil.

Usage of the new set method can only be used when using multiple calls of the same method (different KeyPath). The SDK will throw an error if the developer attempts to combine other types of operations such as: add, increment, forceSet, etc. This is because the other operations methods work entirely different than the new method. The new method sets changes directly on the target and uses the targets save/update methods as opposed to the others which save operations into a dictionary to encode to the server.

/*
Assume you have a ParseObject named GameScore that has
properties named points and levels.
*/ 
var score = GameScore()
score.points = 10
let savedScore = try await score.save() // Create ParseObject on server
/* 
Now you want to update the saved object via operations.
Multiple operations can be chained together.
*/
var operations = try savedScore.operation
      .set(\.points, value: 15)
      .set(\.levels, value: [1])
let updatedScore = try await operations.save()
print(updatedScore) // Updated score

Unwrapping optional ParseObject properties

Adds the get() throws method to allow developers to get the unwrapped property of any ParseObject based on its KeyPath. This will save developers the hassle of having to write code to safely unwrap properties of a ParseObject. If the property is nil, an error will be thrown. Usage:

/*
Assume you have a ParseObject named GameScore that has
a property named points.
*/ 
var score = GameScore()
score.points = 10
let points = try score.get(\.points)
print(points) // 10

// score has not been saved, so it does not have an objectId
do {
  try score.get(\.objectId)
} catch {
  print(error) // Had an issue unwrapping since the value is nil
}

TODOs before merging

  • Add tests
  • Refactor SDK throwing general errors
  • Add entry to changelog
  • Add changes to documentation (guides, repository pages, in-code descriptions)

@parse-github-assistant
Copy link

parse-github-assistant bot commented Sep 4, 2022

Thanks for opening this pull request!

  • 🎉 We are excited about your hands-on contribution!

@codecov
Copy link

codecov bot commented Sep 4, 2022

Codecov Report

Merging #403 (121e4fa) into main (9dac227) will increase coverage by 0.30%.
The diff coverage is 72.09%.

@@            Coverage Diff             @@
##             main     #403      +/-   ##
==========================================
+ Coverage   89.66%   89.97%   +0.30%     
==========================================
  Files         158      159       +1     
  Lines       15113    15130      +17     
==========================================
+ Hits        13551    13613      +62     
+ Misses       1562     1517      -45     
Impacted Files Coverage Δ
...Authentication/Protocols/ParseAuthentication.swift 77.83% <0.00%> (+0.79%) ⬆️
Sources/ParseSwift/Extensions/URLSession.swift 80.78% <0.00%> (+0.31%) ⬆️
Sources/ParseSwift/Parse.swift 99.01% <ø> (ø)
Sources/ParseSwift/Types/ParseACL.swift 93.58% <ø> (ø)
Sources/ParseSwift/Types/ParseConfig.swift 100.00% <ø> (ø)
Sources/ParseSwift/Types/ParseConfiguration.swift 53.19% <ø> (ø)
Sources/ParseSwift/Types/ParseVersion.swift 95.83% <ø> (ø)
Sources/ParseSwift/Objects/ParseInstallation.swift 85.87% <32.00%> (+0.59%) ⬆️
Sources/ParseSwift/Objects/ParseUser.swift 87.45% <38.70%> (+1.11%) ⬆️
Sources/ParseSwift/Types/ParseFile.swift 88.73% <50.00%> (+1.19%) ⬆️
... and 8 more

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

@cbaker6 cbaker6 merged commit d94dcad into parse-community:main Sep 5, 2022
@cbaker6 cbaker6 deleted the keyPathOperations branch September 5, 2022 03:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant