mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
Add sparse fields method to ResourceObject and test it.
This commit is contained in:
@@ -29,3 +29,12 @@ public struct SparseField<
|
||||
try resourceObject.encode(to: sparseEncoder)
|
||||
}
|
||||
}
|
||||
|
||||
public extension ResourceObject where Description.Attributes: SparsableAttributes {
|
||||
|
||||
/// Get a Sparse Fieldset of this `ResourceObject` that can be encoded
|
||||
/// as a `PrimaryResource`.
|
||||
func sparse(with fields: [Description.Attributes.CodingKeys]) -> SparseField<Description, MetaType, LinksType, EntityRawIdType> {
|
||||
return SparseField(self, fields: fields)
|
||||
}
|
||||
}
|
||||
|
||||
+38
-3
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// File.swift
|
||||
// SparseFieldEncodableTests.swift
|
||||
//
|
||||
//
|
||||
// Created by Mathew Polzin on 8/4/19.
|
||||
@@ -52,9 +52,44 @@ class SparseFieldEncoderTests: XCTestCase {
|
||||
|
||||
func test_PartialEncode() {
|
||||
let jsonEncoder = JSONEncoder()
|
||||
let sparseWithEverything = SparseField(testEverythingObject, fields: [.string, .bool, .array])
|
||||
let sparseObject = SparseField(testEverythingObject, fields: [.string, .bool, .array])
|
||||
|
||||
let encoded = try! jsonEncoder.encode(sparseWithEverything)
|
||||
let encoded = try! jsonEncoder.encode(sparseObject)
|
||||
|
||||
let deserialized = try! JSONSerialization.jsonObject(with: encoded,
|
||||
options: [])
|
||||
|
||||
let outerDict = deserialized as? [String: Any]
|
||||
let id = outerDict?["id"] as? String
|
||||
let type = outerDict?["type"] as? String
|
||||
let attributesDict = outerDict?["attributes"] as? [String: Any]
|
||||
let relationships = outerDict?["relationships"]
|
||||
|
||||
XCTAssertEqual(id, testEverythingObject.id.rawValue)
|
||||
XCTAssertEqual(type, EverythingTest.jsonType)
|
||||
XCTAssertNil(relationships)
|
||||
|
||||
XCTAssertEqual(attributesDict?.count, 3)
|
||||
XCTAssertEqual(attributesDict?["bool"] as? Bool,
|
||||
testEverythingObject[\.bool])
|
||||
XCTAssertNil(attributesDict?["int"])
|
||||
XCTAssertNil(attributesDict?["double"])
|
||||
XCTAssertEqual(attributesDict?["string"] as? String,
|
||||
testEverythingObject[\.string])
|
||||
XCTAssertNil(attributesDict?["nestedStruct"])
|
||||
XCTAssertNil(attributesDict?["nestedEnum"])
|
||||
XCTAssertEqual(attributesDict?["array"] as? [Bool],
|
||||
testEverythingObject[\.array])
|
||||
XCTAssertNil(attributesDict?["optional"])
|
||||
XCTAssertNil(attributesDict?["nullable"])
|
||||
XCTAssertNil(attributesDict?["optionalNullable"])
|
||||
}
|
||||
|
||||
func test_sparseFieldsMethod() {
|
||||
let jsonEncoder = JSONEncoder()
|
||||
let sparseObject = testEverythingObject.sparse(with: [.string, .bool, .array])
|
||||
|
||||
let encoded = try! jsonEncoder.encode(sparseObject)
|
||||
|
||||
let deserialized = try! JSONSerialization.jsonObject(with: encoded,
|
||||
options: [])
|
||||
Reference in New Issue
Block a user