Skip to content

Commit

Permalink
style: improve playground examples (#339)
Browse files Browse the repository at this point in the history
* fix: allow ParseRole to save when using custom objectId's

* codecov

* style: improve playground examples
  • Loading branch information
cbaker6 authored Jan 30, 2022
1 parent 51e16d0 commit 5307210
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct GameScore: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameScore {

init(points: Int) {
Expand Down Expand Up @@ -95,7 +95,7 @@ struct GameData: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameData {

init (bytes: ParseBytes?, polygon: ParsePolygon) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,37 @@ struct Hello: ParseCloud {
//: These are required by `ParseCloud`, you can set the default value to make it easier
//: to use.
var functionJobName: String = "hello"

//: If your cloud function takes arguments, they can be passed by creating properties:
//var argument1: [String: Int] = ["test": 5]
}

//: Create another `ParseCloud` type.
struct TestCloudCode: ParseCloud {

//: Return type of your Cloud Function
typealias ReturnType = String
typealias ReturnType = [String: Int]

//: These are required by `ParseCloud`, you can set the default value to make it easier
//: to use.
var functionJobName: String = "testCloudCode"

//: If your cloud function takes arguments, they can be passed by creating properties:
//var argument1: [String: Int] = ["test": 5]
var argument1: [String: Int]
}

//: Create another `ParseCloud` type.
struct TestCloudCodeError: ParseCloud {

//: Return type of your Cloud Function
typealias ReturnType = String

//: These are required by `ParseCloud`, you can set the default value to make it easier
//: to use.
var functionJobName: String = "testCloudCodeError"
}

/*: Assuming you have the Cloud Function named "hello" on your parse-server:
// main.js
Parse.Cloud.define('hello', async () => {
Parse.Cloud.define('hello', async (request) => {
console.log('From client: ' + JSON.stringify(request));
return 'Hello world!';
});
*/
Expand All @@ -61,13 +70,33 @@ hello.runFunction { result in
/*: Assuming you have the Cloud Function named "testCloudCode" on your parse-server.
You can catch custom errors created in Cloud Code:
// main.js
Parse.Cloud.define("testCloudCode", async() => {
throw new Parse.Error(3000, "cloud has an error on purpose.");
Parse.Cloud.define("testCloudCode", async(request) => {
console.log('From client: ' + JSON.stringify(request));
return request.params.argument1;
});
*/
let testCloudCode = TestCloudCode()
let testCloudCode = TestCloudCode(argument1: ["test": 5])

testCloudCode.runFunction { result in
switch result {
case .success(let response):
print("Response from cloud function: \(response)")
case .failure(let error):
assertionFailure("Error: \(error.localizedDescription)")
}
}

/*: Assuming you have the Cloud Function named "testCloudCode" on your parse-server.
You can catch custom errors created in Cloud Code:
// main.js
Parse.Cloud.define("testCloudCodeError", async(request) => {
console.log('From client: ' + JSON.stringify(request));
throw new Parse.Error(3000, "cloud has an error on purpose.");
});
*/
let testCloudCodeError = TestCloudCodeError()

testCloudCodeError.runFunction { result in
switch result {
case .success:
assertionFailure("Should have thrown a custom error")
Expand Down Expand Up @@ -98,7 +127,11 @@ testCloudCode.runFunction { result in

//: Jobs can be run the same way by using the method `startJob()`.

//: Saving objects with context for beforeSave, afterSave, etc.
/*: Saving objects with context for beforeSave, afterSave, etc.
Parse.Cloud.beforeSave("GameScore", async(request) => {
console.log('From client context: ' + JSON.stringify(request.context));
});
*/
//: Create your own value typed `ParseObject`.
struct GameScore: ParseObject {
//: These are required by ParseObject
Expand All @@ -123,7 +156,7 @@ struct GameScore: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameScore {
//: Custom initializer.
init(points: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct GameScore: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameScore {
//: Custom initializer.
init(name: String, points: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct GameScore: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameScore {

init(points: Int) {
Expand All @@ -108,32 +108,33 @@ extension GameScore {
var savedRole: Role<User>?

//: Now we will create the Role.
if let currentUser = User.current {

//: Every Role requires an ACL that can't be changed after saving.
var acl = ParseACL()
acl.setReadAccess(user: currentUser, value: true)
acl.setWriteAccess(user: currentUser, value: true)

do {
//: Create the actual Role with a name and ACL.
let adminRole = try Role<User>(name: "Administrator", acl: acl)
adminRole.save { result in
switch result {
case .success(let saved):
print("The role saved successfully: \(saved)")
print("Check your \"Role\" class in Parse Dashboard.")

//: Store the saved role so we can use it later...
savedRole = saved

case .failure(let error):
print("Error saving role: \(error)")
}
guard let currentUser = User.current else {
fatalError("User currently isn't signed in")
}

//: Every Role requires an ACL that can't be changed after saving.
var acl = ParseACL()
acl.setReadAccess(user: currentUser, value: true)
acl.setWriteAccess(user: currentUser, value: true)

do {
//: Create the actual Role with a name and ACL.
let adminRole = try Role<User>(name: "Administrator", acl: acl)
adminRole.save { result in
switch result {
case .success(let saved):
print("The role saved successfully: \(saved)")
print("Check your \"Role\" class in Parse Dashboard.")

//: Store the saved role so we can use it later...
savedRole = saved

case .failure(let error):
print("Error saving role: \(error)")
}
} catch {
print("Error: \(error)")
}
} catch {
print("Error: \(error)")
}

//: Lets check to see if our Role has saved.
Expand Down Expand Up @@ -200,11 +201,6 @@ do {
//: This variable will store the saved role.
var savedRoleModerator: Role<User>?

//: We need another ACL.
var acl = ParseACL()
acl.setReadAccess(user: User.current!, value: true)
acl.setWriteAccess(user: User.current!, value: false)

do {
//: Create the actual Role with a name and ACL.
let memberRole = try Role<User>(name: "Member", acl: acl)
Expand Down Expand Up @@ -352,27 +348,25 @@ do {

//: Now we will see how to use the stored `ParseRelation on` property in User to create query
//: all of the relations to `scores`.
var currentUser: User?
var updatedCurrentUser: User
do {
//: Fetch the updated user since the previous relations were created on the server.
currentUser = try User.current?.fetch()
print("Updated current user with relation: \(String(describing: currentUser))")
updatedCurrentUser = try User.current!.fetch()
print("Updated current user with relation: \(updatedCurrentUser)")
} catch {
updatedCurrentUser = User.current!
print("\(error.localizedDescription)")
}

do {
if let usableStoredRelation = try currentUser?.relation(currentUser?.scores, key: "scores") {
try (usableStoredRelation.query() as Query<GameScore>).find { result in
switch result {
case .success(let scores):
print("Found related scores from stored ParseRelation: \(scores)")
case .failure(let error):
print("Error finding scores from stored ParseRelation: \(error)")
}
let usableStoredRelation = try updatedCurrentUser.relation(updatedCurrentUser.scores, key: "scores")
try (usableStoredRelation.query() as Query<GameScore>).find { result in
switch result {
case .success(let scores):
print("Found related scores from stored ParseRelation: \(scores)")
case .failure(let error):
print("Error finding scores from stored ParseRelation: \(error)")
}
} else {
print("Error: should unwrapped relation")
}
} catch {
print("\(error.localizedDescription)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct GameScore: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameScore {
//: Custom initializer.
init(points: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct GameScore: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameScore {
//: Custom initializer.
init(objectId: String, points: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct GameScore: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameScore {
//: Custom initializer.
init(name: String, points: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct GameScore: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameScore {
//: Custom initializer.
init(name: String, points: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct GameScore: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameScore {
//: Custom initializer.
init(name: String, points: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct User: ParseUser {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension User {
//: Custom init for signup.
init(username: String, password: String, email: String) {
Expand Down Expand Up @@ -92,7 +92,7 @@ struct GameScore: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameScore {
//: Custom initializer.
init(points: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct GameScore: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameScore {
//: Custom initializer.
init(points: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct GameScore: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameScore {
//: Custom initializer.
init(points: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct Book: ParseObject, ParseQueryScorable {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension Book {

init(title: String) {
Expand Down Expand Up @@ -84,7 +84,7 @@ struct Author: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension Author {
init(name: String, book: Book) {
self.name = name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct GameScore: ParseObject {
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
//: to preserve the memberwise initializer.
extension GameScore {
//: Custom initializer.
init(points: Int) {
Expand Down

0 comments on commit 5307210

Please sign in to comment.