-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5390747
commit 12e3c41
Showing
6 changed files
with
316 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,35 @@ | ||
//: Playground - noun: a place where people can play | ||
|
||
import Kakapo | ||
@testable import Kakapo | ||
|
||
struct Dog: JSONAPIEntity { | ||
let id: String | ||
let name: String | ||
let cat: Cat? | ||
} | ||
|
||
struct Cat: JSONAPIEntity, JSONAPILinkedEntity { | ||
let id: String | ||
let name: String | ||
let topLinks: [String : JSONAPILink]? = ["one": JSONAPILink.Simple(value: "hello top"), "two": JSONAPILink.Simple(value: "world")] | ||
let links: [String : JSONAPILink]? = ["one": JSONAPILink.Simple(value: "hello"), "two": JSONAPILink.Simple(value: "world")] | ||
} | ||
|
||
struct User: JSONAPIEntity, JSONAPILinkedEntity { | ||
let id: String | ||
let name: String | ||
let dog: Dog | ||
let cats: [Cat] | ||
let links: [String : JSONAPILink]? = ["one": JSONAPILink.Simple(value: "hello"), "two": JSONAPILink.Object(href: "hello", meta: Meta())] | ||
} | ||
|
||
struct Meta: Serializable { | ||
let copyright = "Copyright 2015 Example Corp." | ||
let authors = ["Yehuda Katz", "Steve Klabnik","Dan Gebhardt","Tyler Kellen"] | ||
} | ||
|
||
let cats = [Cat(id: "33", name: "Stancho"), Cat(id: "44", name: "Hez")] | ||
let dog = Dog(id: "22", name: "Joan", cat: cats[0]) | ||
let user = User(id: "11", name: "Alex", dog: dog, cats: cats) | ||
|
||
print(JSONAPISerializer(user).serialize()!) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// JSONAPILinks.swift | ||
// Kakapo | ||
// | ||
// Created by Joan Romano on 19/06/16. | ||
// Copyright © 2016 devlucky. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum JSONAPILink: CustomSerializable { | ||
case Simple(value: String) | ||
case Object(href: String, meta: Serializable) | ||
|
||
public func customSerialize() -> AnyObject? { | ||
switch self { | ||
case let Object(href, meta): | ||
var serializedObject = [String: AnyObject](dictionaryLiteral: ("href", href)) | ||
serializedObject["meta"] = meta.serialize() | ||
|
||
return serializedObject | ||
case let Simple(value): | ||
return value | ||
} | ||
} | ||
} | ||
|
||
public protocol JSONAPILinkedEntity { | ||
var links: [String : JSONAPILink]? { get } | ||
var topLinks: [String : JSONAPILink]? { get } | ||
} | ||
|
||
extension JSONAPILinkedEntity { | ||
public var links: [String : JSONAPILink]? { return nil } | ||
public var topLinks: [String : JSONAPILink]? { return nil } | ||
} | ||
|
||
extension Array: JSONAPILinkedEntity { | ||
public var topLinks: [String : JSONAPILink]? { | ||
var returnLinks = [String : JSONAPILink]() | ||
|
||
for linkedEntity in self { | ||
guard let linkedEntity = linkedEntity as? JSONAPILinkedEntity, | ||
links = linkedEntity.topLinks else { break } | ||
returnLinks += links | ||
} | ||
|
||
return !returnLinks.isEmpty ? returnLinks : nil | ||
} | ||
} | ||
|
||
private func += <K, V> (inout left: [K:V], right: [K:V]) { | ||
for (k, v) in right { | ||
left.updateValue(v, forKey: k) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.