Skip to content
Open
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
6 changes: 3 additions & 3 deletions Spine/DeserializeOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class DeserializeOperation: Operation {
let linkURL: URL? = linkData["links"]?["self"].url

if let linkage = linkData["data"]?.array {
let mappedLinkage = linkage.map { ResourceIdentifier(type: $0["type"].stringValue, id: $0["id"].stringValue) }
let mappedLinkage = linkage.map { ResourceIdentifier(type: $0["type"].stringValue, id: $0["id"].stringValue, meta: $0["meta"].dictionaryObject) }
resourceCollection = LinkedResourceCollection(resourcesURL: resourcesURL, linkURL: linkURL, linkage: mappedLinkage)
} else {
resourceCollection = LinkedResourceCollection(resourcesURL: resourcesURL, linkURL: linkURL, linkage: nil)
Expand All @@ -321,10 +321,10 @@ class DeserializeOperation: Operation {
let data: [ResourceIdentifier]?

if let toOne = linkData["data"].dictionary {
data = [ResourceIdentifier(type: toOne["type"]!.stringValue, id: toOne["id"]!.stringValue)]
data = [ResourceIdentifier(type: toOne["type"]!.stringValue, id: toOne["id"]!.stringValue, meta: toOne["meta"]?.dictionaryObject)]
} else if let toMany = linkData["data"].array {
data = toMany.map { JSON -> ResourceIdentifier in
return ResourceIdentifier(type: JSON["type"].stringValue, id: JSON["id"].stringValue)
return ResourceIdentifier(type: JSON["type"].stringValue, id: JSON["id"].stringValue, meta: JSON["meta"].dictionaryObject)
}
} else {
data = nil
Expand Down
15 changes: 12 additions & 3 deletions Spine/Resource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,31 @@ public struct ResourceIdentifier: Equatable {
/// The resource ID.
public private(set) var id: String

/// Constructs a new ResourceIdentifier instance with given `type` and `id`.
init(type: ResourceType, id: String) {
/// Optional non-standard meta-information.
public private(set) var meta: [String: Any]?

/// Constructs a new ResourceIdentifier instance with given `type`, `id` and `meta`.
init(type: ResourceType, id: String, meta: [String: Any]?) {
self.type = type
self.id = id
self.meta = meta
}

/// Constructs a new ResourceIdentifier instance from the given dictionary.
/// The dictionary must contain values for the "type" and "id" keys.
init(dictionary: NSDictionary) {
type = dictionary["type"] as! ResourceType
id = dictionary["id"] as! String
meta = dictionary["meta"] as? [String: Any]
}

/// Returns a dictionary with "type" and "id" keys containing the type and id.
public func toDictionary() -> NSDictionary {
return ["type": type, "id": id]
var dictionary: [String: Any] = ["type": type, "id": id]
if let meta = meta {
dictionary["meta"] = meta
}
return dictionary as NSDictionary
}
}

Expand Down
2 changes: 1 addition & 1 deletion Spine/SerializeOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class SerializeOperation: Operation {

if let resources = linkedResources?.resources {
resourceIdentifiers = resources.filter { $0.id != nil }.map { resource in
return ResourceIdentifier(type: resource.resourceType, id: resource.id!)
return ResourceIdentifier(type: resource.resourceType, id: resource.id!, meta: nil)
}
}

Expand Down
8 changes: 4 additions & 4 deletions SpineTests/ResourceCollectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class LinkedResourceCollectionTests: XCTestCase {
func testInitWithResourcesURLAndURLAndLinkage() {
let resourcesURL = URL(string: "http://example.com/foos")!
let linkURL = URL(string: "http://example.com/bars/1/link/foos")!
let linkage = [ResourceIdentifier(type: "foos", id: "1"), ResourceIdentifier(type: "bars", id: "2")]
let linkage = [ResourceIdentifier(type: "foos", id: "1", meta: ["foo": "bar"]), ResourceIdentifier(type: "bars", id: "2", meta: ["baz": "buz"])]
let collection = LinkedResourceCollection(resourcesURL: resourcesURL, linkURL: linkURL, linkage: linkage)

XCTAssertNotNil(collection.resourcesURL, "Expected resources URL to be not nil.")
Expand All @@ -76,7 +76,7 @@ class LinkedResourceCollectionTests: XCTestCase {
func testInitWithResourcesURLAndURLAndHomogenousTypeAndLinkage() {
let resourcesURL = URL(string: "http://example.com/foos")!
let linkURL = URL(string: "http://example.com/bars/1/link/foos")!
let collection = LinkedResourceCollection(resourcesURL: resourcesURL, linkURL: linkURL, linkage: ["1", "2"].map { ResourceIdentifier(type: "foos", id: $0) })
let collection = LinkedResourceCollection(resourcesURL: resourcesURL, linkURL: linkURL, linkage: ["1", "2"].map { ResourceIdentifier(type: "foos", id: $0, meta: ["foo": "bar"]) })

XCTAssertNotNil(collection.resourcesURL, "Expected resources URL to be not nil.")
XCTAssertEqual(collection.resourcesURL!, resourcesURL, "Expected resources URL to be equal.")
Expand All @@ -85,8 +85,8 @@ class LinkedResourceCollectionTests: XCTestCase {
XCTAssertEqual(collection.linkURL!, linkURL, "Expected link URL to be equal.")

XCTAssert(collection.linkage != nil, "Expected linkage to be not nil.")
XCTAssertEqual(collection.linkage![0], ResourceIdentifier(type: "foos", id: "1"), "Expected first linkage item to be equal.")
XCTAssertEqual(collection.linkage![1], ResourceIdentifier(type: "foos", id: "2"), "Expected second linkage item to be equal.")
XCTAssertEqual(collection.linkage![0], ResourceIdentifier(type: "foos", id: "1", meta: ["foo": "bar"]), "Expected first linkage item to be equal.")
XCTAssertEqual(collection.linkage![1], ResourceIdentifier(type: "foos", id: "2", meta: ["foo": "bar"]), "Expected second linkage item to be equal.")
}

func testAppendResource() {
Expand Down