Skip to content
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

Add support to decode links in Resource #21

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions Vox/Core/Class/Context_Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ extension Context {
} else if data is NSNull {
value = nil
}
} else if let linkDocumentData = resource.object?.value(forKeyPath: "links.\(key)") {
if let stringObject = linkDocumentData as? String, let urlObject = URL(string: stringObject) {
value = urlObject // assume simple string url
} else if let dictionary = linkDocumentData as? [String: Any] {
// need to parse { href: "", meta : {..}}
value = dictionary
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions Vox/Core/Class/Resource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ open class Resource: BaseResource {

return _relationships
}

public var links: NSMutableDictionary? {
var _links: NSMutableDictionary?

context?.queue.sync {
_links = object?["links"] as? NSMutableDictionary
}

return _links
}

public required init(context: Context? = nil) {
super.init()
Expand Down Expand Up @@ -77,6 +87,7 @@ open class Resource: BaseResource {
public func documentDictionary() throws -> [String: Any] {
let attributes = self.attributes
let relationships = self.relationships
let links = self.links

var dictionary: [String: Any] = [
"type": self.type
Expand All @@ -95,6 +106,11 @@ open class Resource: BaseResource {
relationships.count > 0 {
dictionary["relationships"] = relationships
}

if let links = links,
links.count > 0 {
dictionary["links"] = links
}

return ["data": dictionary]
}
Expand Down Expand Up @@ -131,6 +147,7 @@ extension Array where Element: Resource {

let attributes = resource.attributes
let relationships = resource.relationships
let links = resource.links

var dictionary: [String: Any] = [
"id": id,
Expand All @@ -146,6 +163,11 @@ extension Array where Element: Resource {
relationships.count > 0 {
dictionary["relationships"] = relationships
}

if let links = links,
links.count > 0 {
dictionary["links"] = links
}

return dictionary
}
Expand Down
12 changes: 11 additions & 1 deletion VoxTests/Assets/Articles.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@
"type": "persons2"
}
}
}
},
"links": {
"here": "www.example.com",
"there": "www.example2.com",
"related": {
"href": "http://example.com/articles/1/comments",
"meta": {
"count": 10
}
}
}
}],
"included": [
{
Expand Down
7 changes: 6 additions & 1 deletion VoxTests/Deserializer/DeserializerCollectionSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ fileprivate class Article2: Resource {
@objc dynamic var author: Person2?
@objc dynamic var hint: String?
@objc dynamic var customObject: [String: Any]?
@objc dynamic var here: URL?
@objc dynamic var there: URL?
@objc dynamic var related: [String: Any]?
}


Expand Down Expand Up @@ -61,7 +64,9 @@ class DeserializerCollectionSpec: QuickSpec {
expect(article?.descriptionText).to(equal("Desc"))
expect(article?.keywords).notTo(beNil())
expect(article?.customObject).notTo(beNil())

expect(article?.here).to(equal(URL(string: "www.example.com")!))
expect(article?.related).notTo(beNil())
expect(article?.links).notTo(beNil())
let coauthors = article?.coauthors
let author = article?.author

Expand Down