Skip to content

feat: Static encoders/decoder access #259

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

Merged
merged 2 commits into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.0.1...main)
* _Contributing to this repo? Add info about your change here to be included in the next release_

__Improvements__
- Add static methods for accessing encoders/decoder so developers do not have to create instances to access ([#259](https://github.com/parse-community/Parse-Swift/pull/259)), thanks to [Corey Baker](https://github.com/cbaker6).

### 2.0.1
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.0.0...2.0.1)

Expand Down
26 changes: 23 additions & 3 deletions Sources/ParseSwift/Coding/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,42 @@ internal extension Date {

// MARK: Coding
public extension ParseObject {

/// The Parse encoder is used to JSON encode all `ParseObject`s and
/// types in a way meaninful for a Parse Server to consume.
func getEncoder() -> ParseEncoder {
static func getEncoder() -> ParseEncoder {
return ParseCoding.parseEncoder()
}

/// The Parse encoder is used to JSON encode all `ParseObject`s and
/// types in a way meaninful for a Parse Server to consume.
func getEncoder() -> ParseEncoder {
return Self.getEncoder()
}

/// The JSON encoder setup with the correct `dateEncodingStrategy`
/// strategy to send data to a Parse Server.
func getJSONEncoder() -> JSONEncoder {
static func getJSONEncoder() -> JSONEncoder {
return ParseCoding.jsonEncoder()
}

/// The JSON encoder setup with the correct `dateEncodingStrategy`
/// strategy to send data to a Parse Server.
func getJSONEncoder() -> JSONEncoder {
return Self.getJSONEncoder()
}

/// The JSON decoder setup with the correct `dateDecodingStrategy`
/// strategy to decode data from a Parse Server. This encoder is used to decode all data received
/// from the server.
func getDecoder() -> JSONDecoder {
static func getDecoder() -> JSONDecoder {
ParseCoding.jsonDecoder()
}

/// The JSON decoder setup with the correct `dateDecodingStrategy`
/// strategy to decode data from a Parse Server. This encoder is used to decode all data received
/// from the server.
func getDecoder() -> JSONDecoder {
Self.getDecoder()
}
}
6 changes: 3 additions & 3 deletions Tests/ParseSwiftTests/ParseObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class ParseObjectTests: XCTestCase { // swiftlint:disable:this type_body_length
do {
encoded = try ParseCoding.jsonEncoder().encode(scoreOnServer)
//Get dates in correct format from ParseDecoding strategy
scoreOnServer = try scoreOnServer.getDecoder().decode(GameScore.self, from: encoded)
scoreOnServer = try GameScore.getDecoder().decode(GameScore.self, from: encoded)
} catch {
XCTFail("Should encode/decode. Error \(error)")
return
Expand Down Expand Up @@ -992,7 +992,7 @@ class ParseObjectTests: XCTestCase { // swiftlint:disable:this type_body_length
scoreOnServer.ACL = nil
let encoded: Data!
do {
encoded = try scoreOnServer.getEncoder().encode(scoreOnServer, skipKeys: .none)
encoded = try GameScore.getEncoder().encode(scoreOnServer, skipKeys: .none)
//Get dates in correct format from ParseDecoding strategy
scoreOnServer = try scoreOnServer.getDecoder().decode(GameScore.self, from: encoded)
} catch {
Expand Down Expand Up @@ -1196,7 +1196,7 @@ class ParseObjectTests: XCTestCase { // swiftlint:disable:this type_body_length

let encoded: Data!
do {
encoded = try scoreOnServer.getJSONEncoder().encode(response)
encoded = try GameScore.getJSONEncoder().encode(response)
//Get dates in correct format from ParseDecoding strategy
let encodedScoreOnServer = try scoreOnServer.getEncoder().encode(scoreOnServer)
scoreOnServer = try scoreOnServer.getDecoder().decode(GameScore.self, from: encodedScoreOnServer)
Expand Down