Skip to content
This repository has been archived by the owner on Feb 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #365 from hkellaway/chore/consistent-data-syntax
Browse files Browse the repository at this point in the history
Improve usage of Data
  • Loading branch information
hkellaway authored Sep 1, 2020
2 parents d4839fb + ec17660 commit 69f605b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
31 changes: 30 additions & 1 deletion Sources/Gloss/ExtensionArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,35 @@ public extension Array where Element: JSONDecodable & Decodable {
return models
}

/**
Returns array of new objects created from provided data.
If creation of JSON or any decodings fail, nil is returned.

- parameter data: Raw JSON data.
- parameter jsonDecoder: A `Swift.JSONDecoder`.
- parameter serializer: Serializer to use when creating JSON from data.
- parameter ooptions: Options for reading the JSON data.
- parameter logger: Logs issues with `Swift.Decodable`.

- returns: Object or nil.
*/
static func from(decodableData data: Data, jsonDecoder: JSONDecoder = JSONDecoder(), serializer: JSONSerializer = GlossJSONSerializer(), options: JSONSerialization.ReadingOptions = .mutableContainers, logger: GlossLogger = GlossLogger()) -> [Element]? {
do {
let jsonArray = try jsonDecoder.decode([Element].self, from: data)
return jsonArray
} catch {
logger.log(message: "Swift.Decodable error: \(error)")

guard
let jsonArray = serializer.jsonArray(from: data, options: options),
let models = [Element].from(jsonArray: jsonArray) else {
return nil
}

return models
}
}

}

public extension Array where Element: JSONEncodable & Encodable {
Expand Down Expand Up @@ -138,7 +167,7 @@ public extension Array where Element: JSONDecodable {
*/
static func from(data: Data, serializer: JSONSerializer = GlossJSONSerializer(), options: JSONSerialization.ReadingOptions = .mutableContainers) -> [Element]? {
guard
let jsonArray = (try? JSONSerialization.jsonObject(with: data, options: options)) as? [JSON],
let jsonArray = serializer.jsonArray(from: data, options: options),
let models = [Element].from(jsonArray: jsonArray) else {
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/GlossTests/GlossTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class GlossTests: XCTestCase {
}

func testModelsFromJSONArrayProducesValidModels() {
let result = [TestModel].from(decodableJSONArray: testJSONArray!)
let result: [TestModel]? = .from(decodableJSONArray: testJSONArray!)
let model1: TestModel = result![0]
let model2: TestModel = result![1]

Expand Down Expand Up @@ -206,7 +206,7 @@ class GlossTests: XCTestCase {
func testModelsFromJSONArrayReturnsNilIfDecodingFails() {
testJSONArray![0].removeValue(forKey: "bool")

let result = [TestModel].from(decodableJSONArray: testJSONArray!)
let result: [TestModel]? = .from(decodableJSONArray: testJSONArray!)

XCTAssertNil(result, "Model array from JSON array should be nil is any decoding fails.")
}
Expand Down Expand Up @@ -303,7 +303,7 @@ class GlossTests: XCTestCase {
invalidJSON.removeValue(forKey: "bool")
var jsonArray = testJSONArray!
jsonArray.append(invalidJSON)
let result = [TestModel].from(decodableJSONArray: jsonArray)
let result: [TestModel]? = .from(decodableJSONArray: jsonArray)

XCTAssertNil(result, "JSON array from model array should be nil is any encoding fails.")
}
Expand All @@ -325,14 +325,14 @@ class GlossTests: XCTestCase {

func testModelFromJSONRawData() {
let data = try! JSONSerialization.data(withJSONObject: testModelsJSON!, options: [])
let model = TestModel(data: data)
let model: TestModel? = .from(decodableData: data)

XCTAssertNotNil(model, "Model from Data should not be nil.")
}

func testModelArrayFromJSONArrayRawData() {
let data = try! JSONSerialization.data(withJSONObject: testJSONArray!, options: [])
let modelArray = [TestModel].from(data: data)
let modelArray: [TestModel]? = .from(decodableData: data)

XCTAssertNotNil(modelArray, "Model array from Data should not be nil.")
}
Expand Down

0 comments on commit 69f605b

Please sign in to comment.