Skip to content
This repository has been archived by the owner on Jan 25, 2019. It is now read-only.

Update meta #203

Open
wants to merge 15 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
4 changes: 2 additions & 2 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "antitypical/Result" "3.0.0"
github "antitypical/Result" "3.2.4"
github "SwiftyJSON/SwiftyJSON" "3.1.4"
github "Thomvis/BrightFutures" "v5.0.1"
github "Thomvis/BrightFutures" "5.2.0"
5 changes: 5 additions & 0 deletions Spine/DeserializeOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ class DeserializeOperation: Operation {
var resource: Resource? = nil

if let linkData = serializedData["relationships"][key].dictionary {

guard linkData["data"] != nil, linkData["data"]!.type != SwiftyJSON.Type.null else {
return nil
}

let type = linkData["data"]?["type"].string ?? linkedType

if let id = linkData["data"]?["id"].string {
Expand Down
78 changes: 45 additions & 33 deletions Spine/SerializeOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class SerializeOperation: Operation {

// Serialize type
serializedData["type"] = resource.resourceType as AnyObject?

// Serialize meta
if let meta = resource.meta {
serializedData["meta"] = meta
}

// Serialize fields
addAttributes(from: resource, to: &serializedData )
Expand Down Expand Up @@ -135,20 +140,23 @@ class SerializeOperation: Operation {
serializedId = NSNull()
}

let serializedRelationship = [
"data": [
"type": type,
"id": serializedId
]
]

if serializedData["relationships"] == nil {
serializedData["relationships"] = [key: serializedRelationship]
} else {
var relationships = serializedData["relationships"] as! [String: Any]
relationships[key] = serializedRelationship
serializedData["relationships"] = relationships
}
if !options.contains(.OmitNullValues) || (serializedId as? String) != nil {

let serializedRelationship = [
"data": [
"type": type,
"id": serializedId
]
]

if serializedData["relationships"] == nil {
serializedData["relationships"] = [key: serializedRelationship]
} else {
var relationships = serializedData["relationships"] as! [String: Any]
relationships[key] = serializedRelationship
serializedData["relationships"] = relationships
}
}
}

/// Adds the given resources as a to to-many relationship to the serialized data.
Expand All @@ -158,24 +166,28 @@ class SerializeOperation: Operation {
/// - parameter key: The key to add to the serialized data.
/// - parameter type: The resource type of the linked resource as defined on the parent resource.
fileprivate func addToManyRelationship(_ linkedResources: ResourceCollection?, to serializedData: inout [String: Any], key: String, type: ResourceType) {
var resourceIdentifiers: [ResourceIdentifier] = []

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

let serializedRelationship = [
"data": resourceIdentifiers.map { $0.toDictionary() }
]

if serializedData["relationships"] == nil {
serializedData["relationships"] = [key: serializedRelationship]
} else {
var relationships = serializedData["relationships"] as! [String: Any]
relationships[key] = serializedRelationship
serializedData["relationships"] = relationships
}

if !options.contains(.OmitNullValues) || linkedResources != nil {

var resourceIdentifiers: [ResourceIdentifier] = []

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

let serializedRelationship = [
"data": resourceIdentifiers.map { $0.toDictionary() }
]

if serializedData["relationships"] == nil {
serializedData["relationships"] = [key: serializedRelationship]
} else {
var relationships = serializedData["relationships"] as! [String: Any]
relationships[key] = serializedRelationship
serializedData["relationships"] = relationships
}
}
}
}
1 change: 0 additions & 1 deletion Spine/Serializing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public class Serializer {
let document = JSONAPIDocument(data: resources, included: nil, errors: nil, meta: nil, links: nil, jsonapi: nil)
return try serializeDocument(document, options: options)
}


/// Converts the given resource to link data, and serializes it into NSData.
/// `{"data": { "type": "people", "id": "12" }}`
Expand Down