SlideShare a Scribd company logo
1 of 88
Download to read offline
Codable

The key to Fullstack Swift
Swift Cloud Workshop 3

February 23rd, 2018
Chris Bailey

(@Chris__Bailey)
Codable
struct Profile {
var name: String
var photo: Data
var dateOfBirth: Date
}
let profile = Profile(name: “Chris”, photo: image, dateOfBirth: Date(“06-06-1980”))
struct Profile : Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
let profile = Profile(name: “Chris”, photo: image, dateOfBirth: Date(“06-06-1980”))
struct Profile : Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
let profile = Profile(name: “Chris”, photo: image, dateOfBirth: Date(“06-06-1980”))
let encoder = JSONEncoder()
let jsonData = try encoder.encode(profile)
struct Profile : Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
let profile = Profile(name: “Chris”, photo: image, dateOfBirth: Date(“06-06-1980”))
let encoder = JSONEncoder()
let jsonData = try encoder.encode(profile)
let decoder = JSONDecoder()
let person = try decoder.decode(Profile.self, from: jsonData)
Fullstack

Development
{
"name": "",
"photo": "",
"dateOfBirth": ""
}
{
"name": "",
"photo": "",
"dateOfBirth": {
"year":
"month":
"day":
}
}
struct Profile {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift
var profile: [String : Any]
Swift
KITURA
let profile = Profile(name: name, photo: photo)let json = JSON(data: data)

guard json["name"].exists() else {
response.status(.unprocessableEntity)
response.send(json: JSON([ "error": "Missing reqired property `name`" ]))
next()
}
guard let photo = Data(base64Encoded: photoString) else {
response.status(.unprocessableEntity)
response.send(json: JSON([ "error": "Type mismatch, `photo` expected to be Base64 encoded data" ]))
next()
}
guard let name = json["name"].string else {
response.status(.unprocessableEntity)
response.send(json: JSON([ "error": "Type mismatch, `name` expected to be a String" ]))
next()
}
guard json["photo"].exists() else {
response.status(.unprocessableEntity)
response.send(json: JSON([ "error": "Missing reqired property photo`" ]))
next()
}
guard let photoString = json[“photo"].string else {
response.status(.unprocessableEntity)
response.send(json: JSON([ "error": "Type mismatch, `photo` expected to be a String" ]))
next()
}
struct Profile {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift
struct Profile {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
KITURA
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
KITURA
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
KITURA
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
let encoder = JSONEncoder()
let data = try encoder.encode(profile)
KITURA
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
let decoder = JSONDecoder()
let person = try decoder.decode(Person.self, from: jsonData)
KITURA
{
"name": "",
"photo": "",
"dateOfBirth": ""
}
{
"name": "",
"photo": "",
"dateOfBirth": ""
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
KITURA
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
KITURA
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
KITURA
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
KITURA
Under the Hood
struct Profile {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"

case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}



struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"

case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}



extension Profile : Decodable {
init(from decoder: Decoder) throws {
}
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"

case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}



extension Profile : Decodable {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)

}
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"

case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}



extension Profile : Decodable {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)

name = try values.decode(String.self, forKey: .name)
}
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"

case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}



extension Profile : Decodable {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)

name = try values.decode(String.self, forKey: .name)
photo = try values.decode(Data.self, forKey: .photo)
}
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"

case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}



extension Profile : Decodable {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)

name = try values.decode(String.self, forKey: .name)
photo = try values.decode(Data.self, forKey: .photo)
dateOfBirth = try values.decode(Date.self, forKey: .dateOfBirth)
}
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"

case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}



extension Profile : Encodable {
func encode(to encoder: Encoder) throws {
}
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"

case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}



extension Profile : Encodable {
func encode(to encoder: Encoder) throws {
var container = try encoder.container(keyedBy: CodingKeys.self)

}
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"

case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}



extension Profile : Encodable {
func encode(to encoder: Encoder) throws {
var container = try encoder.container(keyedBy: CodingKeys.self)

try container.encode(name, forKey: .name)
try container.encode(photo, forKey: .photo)
try container.encode(dateOfBirth, forKey: .dateOfBirth)
}
}
Body Data
{
"name": "John Doe",
"photo": "jbbkj362",
"dateOfBirth": "06-06-1980“
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(request: RouterRequest, response: RouterResponse, next: () -> Void) -> Void
{
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(request: RouterRequest, response: RouterResponse, next: () -> Void) -> Void
{
var profile = request.read(as: Profile.Type)
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
...
respondWith(profiles, nil)
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
router.post("/profile", handler: addProfile)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
...
respondWith(profiles, nil)
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
router.post("/profile", handler: addProfile)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
...
respondWith(profiles, nil)
}
func addProfile(profile: Profile, respondWith: @escaping (Profile?, Error?) -> Void) -> Void
{
...
respondWith(profile, nil)
}
guard let backend = KituraKit(baseURL: "http://localhost:8080") else {
print("Error creating KituraKit client")
return
}
backend.get("/profile") { (profiles: [Profile]?, error: RequestError?) in
...
}
KITURAKIT
Query Parameters
GET: /profile?name=“John Doe”#
{
"name": "John Doe",
"photo": "jbbkj362",
"dateOfBirth": "06-06-1980“
}
{"name": "John Doe","photo": "jbbkj362","dateOfBirth": "06-06-1980"}
{"name": "John Doe" }
{"name": "John Doe"}
{“name"= "John Doe"}
{ name = "John Doe"}
? name = "John Doe"}
? name = "John Doe"#
?name="John Doe"#
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
...
respondWith(profiles, nil)
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Query: QueryParams {
var name: String
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
...
respondWith(profiles, nil)
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Query: QueryParams {
var name: String
}
router.get("/profile", handler: getProfiles)
func getProfiles( , respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
...
respondWith(profiles, nil)
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Query: QueryParams {
var name: String
}
router.get("/profile", handler: getProfiles)
func getProfiles(query: Query, respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
...
respondWith(profiles, nil)
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Query: QueryParams {
var name: String
}
router.get("/profile", handler: getProfiles)
func getProfiles(query: Query, respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
...
respondWith(profiles , nil)
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Query: QueryParams {
var name: String
}
router.get("/profile", handler: getProfiles)
func getProfiles(query: Query, respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
...
respondWith(profiles.filter{ ($0.name == query.name), nil)
}
What Else?
SELECT * from Profiles
name,, photo, dateOfBirth,
"John Doe", "jbbkj362", "06-06-1980",
name,, photo, dateOfBirth
"John Doe", "jbbkj362", "06-06-1980"
name:”John Doe”,photo:”jbbkj362”,dateOfBirth:”06-06-1980"
{name:”John Doe”,photo:”jbbkj362”,dateOfBirth:”06-06-1980”}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
...
respondWith(profiles, nil)
}
struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
...
respondWith(profiles, nil)
}
struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll()
respondWith(profiles, nil)
}
struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(respondWith)
respondWith(profiles, nil)
}
struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(respondWith)
}
struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(completion: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(completion)
}
SELECT * from Profiles
SELECT * from Profiles
SELECT * from Profiles WHERE name = “John Doe”
struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(completion: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(completion)
}
struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Query: QueryParams {
var name: String
}
router.get("/profile", handler: getProfiles)
func getProfiles(completion: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(completion)
}
struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Query: QueryParams {
var name: String
}
router.get("/profile", handler: getProfiles)
func getProfiles(query: Query, completion: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(completion)
}
struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Query: QueryParams {
var name: String
}
router.get("/profile", handler: getProfiles)
func getProfiles(query: Query, completion: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll( completion)
}
struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Query: QueryParams {
var name: String
}
router.get("/profile", handler: getProfiles)
func getProfiles(query: Query, completion: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(matching: query, completion)
}
Codable
Dynamic Swift
Questions

More Related Content

Similar to Swift Cloud Workshop - Codable, the key to Fullstack Swift

Code to copy Person.java .pdf
Code to copy Person.java .pdfCode to copy Person.java .pdf
Code to copy Person.java .pdf
anokhijew
 
main-cpp file #include -iostream- #include -vector- #include -Date-h.docx
main-cpp file #include -iostream- #include -vector-   #include -Date-h.docxmain-cpp file #include -iostream- #include -vector-   #include -Date-h.docx
main-cpp file #include -iostream- #include -vector- #include -Date-h.docx
EvandWYAlland
 

Similar to Swift Cloud Workshop - Codable, the key to Fullstack Swift (11)

662305 09
662305 09662305 09
662305 09
 
Windows store app development using javascript
Windows store app development using javascriptWindows store app development using javascript
Windows store app development using javascript
 
Code to copy Person.java .pdf
Code to copy Person.java .pdfCode to copy Person.java .pdf
Code to copy Person.java .pdf
 
Kotlin Data Model
Kotlin Data ModelKotlin Data Model
Kotlin Data Model
 
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
 
JSON and Swift, Still A Better Love Story Than Twilight
JSON and Swift, Still A Better Love Story Than TwilightJSON and Swift, Still A Better Love Story Than Twilight
JSON and Swift, Still A Better Love Story Than Twilight
 
GraphQL & Relay - 串起前後端世界的橋樑
GraphQL & Relay - 串起前後端世界的橋樑GraphQL & Relay - 串起前後端世界的橋樑
GraphQL & Relay - 串起前後端世界的橋樑
 
Tools and Projects Dec 2018 Edition
Tools and Projects Dec 2018 EditionTools and Projects Dec 2018 Edition
Tools and Projects Dec 2018 Edition
 
Avro, la puissance du binaire, la souplesse du JSON
Avro, la puissance du binaire, la souplesse du JSONAvro, la puissance du binaire, la souplesse du JSON
Avro, la puissance du binaire, la souplesse du JSON
 
Snapshot Testing @ CocoaheadsNL
Snapshot Testing @ CocoaheadsNLSnapshot Testing @ CocoaheadsNL
Snapshot Testing @ CocoaheadsNL
 
main-cpp file #include -iostream- #include -vector- #include -Date-h.docx
main-cpp file #include -iostream- #include -vector-   #include -Date-h.docxmain-cpp file #include -iostream- #include -vector-   #include -Date-h.docx
main-cpp file #include -iostream- #include -vector- #include -Date-h.docx
 

More from Chris Bailey

More from Chris Bailey (20)

NodeJS Interactive 2019: FaaS meets Frameworks
NodeJS Interactive 2019:  FaaS meets FrameworksNodeJS Interactive 2019:  FaaS meets Frameworks
NodeJS Interactive 2019: FaaS meets Frameworks
 
Voxxed Micro-services: Serverless JakartaEE - JAX-RS comes to FaaS
Voxxed Micro-services: Serverless JakartaEE - JAX-RS comes to FaaSVoxxed Micro-services: Serverless JakartaEE - JAX-RS comes to FaaS
Voxxed Micro-services: Serverless JakartaEE - JAX-RS comes to FaaS
 
Silicon Valley Code Camp 2019 - Reaching the Cloud Native World
Silicon Valley Code Camp 2019 - Reaching the Cloud Native WorldSilicon Valley Code Camp 2019 - Reaching the Cloud Native World
Silicon Valley Code Camp 2019 - Reaching the Cloud Native World
 
FaaS Meets Java EE: Developing Cloud Native Applications at Speed
FaaS Meets Java EE: Developing Cloud Native Applications at SpeedFaaS Meets Java EE: Developing Cloud Native Applications at Speed
FaaS Meets Java EE: Developing Cloud Native Applications at Speed
 
AltConf 2019: Server-Side Swift State of the Union
AltConf 2019:  Server-Side Swift State of the UnionAltConf 2019:  Server-Side Swift State of the Union
AltConf 2019: Server-Side Swift State of the Union
 
Server-side Swift with Swagger
Server-side Swift with SwaggerServer-side Swift with Swagger
Server-side Swift with Swagger
 
Node Summit 2018: Cloud Native Node.js
Node Summit 2018: Cloud Native Node.jsNode Summit 2018: Cloud Native Node.js
Node Summit 2018: Cloud Native Node.js
 
Index - BFFs vs GraphQL
Index - BFFs vs GraphQLIndex - BFFs vs GraphQL
Index - BFFs vs GraphQL
 
Swift Cloud Workshop - Swift Microservices
Swift Cloud Workshop - Swift MicroservicesSwift Cloud Workshop - Swift Microservices
Swift Cloud Workshop - Swift Microservices
 
Swift Summit 2017: Server Swift State of the Union
Swift Summit 2017: Server Swift State of the UnionSwift Summit 2017: Server Swift State of the Union
Swift Summit 2017: Server Swift State of the Union
 
IBM Cloud University: Build, Deploy and Scale Node.js Microservices
IBM Cloud University: Build, Deploy and Scale Node.js MicroservicesIBM Cloud University: Build, Deploy and Scale Node.js Microservices
IBM Cloud University: Build, Deploy and Scale Node.js Microservices
 
IBM Cloud University: Java, Node.js and Swift
IBM Cloud University: Java, Node.js and SwiftIBM Cloud University: Java, Node.js and Swift
IBM Cloud University: Java, Node.js and Swift
 
Node Interactive: Node.js Performance and Highly Scalable Micro-Services
Node Interactive: Node.js Performance and Highly Scalable Micro-ServicesNode Interactive: Node.js Performance and Highly Scalable Micro-Services
Node Interactive: Node.js Performance and Highly Scalable Micro-Services
 
FrenchKit 2017: Server(less) Swift
FrenchKit 2017: Server(less) SwiftFrenchKit 2017: Server(less) Swift
FrenchKit 2017: Server(less) Swift
 
AltConf 2017: Full Stack Swift in 30 Minutes
AltConf 2017: Full Stack Swift in 30 MinutesAltConf 2017: Full Stack Swift in 30 Minutes
AltConf 2017: Full Stack Swift in 30 Minutes
 
InterConnect: Server Side Swift for Java Developers
InterConnect:  Server Side Swift for Java DevelopersInterConnect:  Server Side Swift for Java Developers
InterConnect: Server Side Swift for Java Developers
 
InterConnect: Java, Node.js and Swift - Which, Why and When
InterConnect: Java, Node.js and Swift - Which, Why and WhenInterConnect: Java, Node.js and Swift - Which, Why and When
InterConnect: Java, Node.js and Swift - Which, Why and When
 
Playgrounds: Mobile + Swift = BFF
Playgrounds: Mobile + Swift = BFFPlaygrounds: Mobile + Swift = BFF
Playgrounds: Mobile + Swift = BFF
 
Swift Summit: Pushing the boundaries of Swift to the Server
Swift Summit: Pushing the boundaries of Swift to the ServerSwift Summit: Pushing the boundaries of Swift to the Server
Swift Summit: Pushing the boundaries of Swift to the Server
 
O'Reilly Software Architecture Conf: Cloud Economics
O'Reilly Software Architecture Conf: Cloud EconomicsO'Reilly Software Architecture Conf: Cloud Economics
O'Reilly Software Architecture Conf: Cloud Economics
 

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Recently uploaded (20)

%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 

Swift Cloud Workshop - Codable, the key to Fullstack Swift

  • 1. Codable
 The key to Fullstack Swift Swift Cloud Workshop 3
 February 23rd, 2018 Chris Bailey
 (@Chris__Bailey)
  • 3. struct Profile { var name: String var photo: Data var dateOfBirth: Date } let profile = Profile(name: “Chris”, photo: image, dateOfBirth: Date(“06-06-1980”))
  • 4. struct Profile : Codable { var name: String var photo: Data var dateOfBirth: Date } let profile = Profile(name: “Chris”, photo: image, dateOfBirth: Date(“06-06-1980”))
  • 5. struct Profile : Codable { var name: String var photo: Data var dateOfBirth: Date } let profile = Profile(name: “Chris”, photo: image, dateOfBirth: Date(“06-06-1980”)) let encoder = JSONEncoder() let jsonData = try encoder.encode(profile)
  • 6. struct Profile : Codable { var name: String var photo: Data var dateOfBirth: Date } let profile = Profile(name: “Chris”, photo: image, dateOfBirth: Date(“06-06-1980”)) let encoder = JSONEncoder() let jsonData = try encoder.encode(profile) let decoder = JSONDecoder() let person = try decoder.decode(Profile.self, from: jsonData)
  • 8. { "name": "", "photo": "", "dateOfBirth": "" } { "name": "", "photo": "", "dateOfBirth": { "year": "month": "day": } } struct Profile { var name: String var photo: Data var dateOfBirth: Date } Swift var profile: [String : Any] Swift KITURA
  • 9. let profile = Profile(name: name, photo: photo)let json = JSON(data: data)
 guard json["name"].exists() else { response.status(.unprocessableEntity) response.send(json: JSON([ "error": "Missing reqired property `name`" ])) next() } guard let photo = Data(base64Encoded: photoString) else { response.status(.unprocessableEntity) response.send(json: JSON([ "error": "Type mismatch, `photo` expected to be Base64 encoded data" ])) next() } guard let name = json["name"].string else { response.status(.unprocessableEntity) response.send(json: JSON([ "error": "Type mismatch, `name` expected to be a String" ])) next() } guard json["photo"].exists() else { response.status(.unprocessableEntity) response.send(json: JSON([ "error": "Missing reqired property photo`" ])) next() } guard let photoString = json[“photo"].string else { response.status(.unprocessableEntity) response.send(json: JSON([ "error": "Type mismatch, `photo` expected to be a String" ])) next() }
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16. struct Profile { var name: String var photo: Data var dateOfBirth: Date } Swift
  • 17. struct Profile { var name: String var photo: Data var dateOfBirth: Date } Swift Swift KITURA
  • 18. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } Swift Swift KITURA
  • 19. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } Swift Swift struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } KITURA
  • 20. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } Swift Swift struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } let encoder = JSONEncoder() let data = try encoder.encode(profile) KITURA
  • 21. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } Swift Swift struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } let decoder = JSONDecoder() let person = try decoder.decode(Person.self, from: jsonData) KITURA
  • 22. { "name": "", "photo": "", "dateOfBirth": "" } { "name": "", "photo": "", "dateOfBirth": "" } struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } Swift Swift struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } KITURA
  • 23. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } Swift Swift struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } KITURA
  • 24. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } Swift Swift struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } KITURA
  • 25. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } Swift Swift struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } KITURA
  • 27. struct Profile { var name: String var photo: Data var dateOfBirth: Date }
  • 28. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date }
  • 29. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } extension Profile { enum CodingKeys: String, CodingKey { case name = "name"
 case photo = "photo" case dateOfBirth = "dateOfBirth" } }
 

  • 30. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } extension Profile { enum CodingKeys: String, CodingKey { case name = "name"
 case photo = "photo" case dateOfBirth = "dateOfBirth" } }
 
 extension Profile : Decodable { init(from decoder: Decoder) throws { } }
  • 31. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } extension Profile { enum CodingKeys: String, CodingKey { case name = "name"
 case photo = "photo" case dateOfBirth = "dateOfBirth" } }
 
 extension Profile : Decodable { init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self)
 } }
  • 32. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } extension Profile { enum CodingKeys: String, CodingKey { case name = "name"
 case photo = "photo" case dateOfBirth = "dateOfBirth" } }
 
 extension Profile : Decodable { init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self)
 name = try values.decode(String.self, forKey: .name) } }
  • 33. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } extension Profile { enum CodingKeys: String, CodingKey { case name = "name"
 case photo = "photo" case dateOfBirth = "dateOfBirth" } }
 
 extension Profile : Decodable { init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self)
 name = try values.decode(String.self, forKey: .name) photo = try values.decode(Data.self, forKey: .photo) } }
  • 34. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } extension Profile { enum CodingKeys: String, CodingKey { case name = "name"
 case photo = "photo" case dateOfBirth = "dateOfBirth" } }
 
 extension Profile : Decodable { init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self)
 name = try values.decode(String.self, forKey: .name) photo = try values.decode(Data.self, forKey: .photo) dateOfBirth = try values.decode(Date.self, forKey: .dateOfBirth) } }
  • 35. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } extension Profile { enum CodingKeys: String, CodingKey { case name = "name"
 case photo = "photo" case dateOfBirth = "dateOfBirth" } }
 
 extension Profile : Encodable { func encode(to encoder: Encoder) throws { } }
  • 36. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } extension Profile { enum CodingKeys: String, CodingKey { case name = "name"
 case photo = "photo" case dateOfBirth = "dateOfBirth" } }
 
 extension Profile : Encodable { func encode(to encoder: Encoder) throws { var container = try encoder.container(keyedBy: CodingKeys.self)
 } }
  • 37. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } extension Profile { enum CodingKeys: String, CodingKey { case name = "name"
 case photo = "photo" case dateOfBirth = "dateOfBirth" } }
 
 extension Profile : Encodable { func encode(to encoder: Encoder) throws { var container = try encoder.container(keyedBy: CodingKeys.self)
 try container.encode(name, forKey: .name) try container.encode(photo, forKey: .photo) try container.encode(dateOfBirth, forKey: .dateOfBirth) } }
  • 39. { "name": "John Doe", "photo": "jbbkj362", "dateOfBirth": "06-06-1980“ }
  • 40. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date }
  • 41. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles)
  • 42. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) func getProfiles(request: RouterRequest, response: RouterResponse, next: () -> Void) -> Void { }
  • 43. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) func getProfiles(request: RouterRequest, response: RouterResponse, next: () -> Void) -> Void { var profile = request.read(as: Profile.Type) }
  • 44. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { }
  • 45. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { ... respondWith(profiles, nil) }
  • 46. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) router.post("/profile", handler: addProfile) func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { ... respondWith(profiles, nil) }
  • 47. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) router.post("/profile", handler: addProfile) func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { ... respondWith(profiles, nil) } func addProfile(profile: Profile, respondWith: @escaping (Profile?, Error?) -> Void) -> Void { ... respondWith(profile, nil) }
  • 48. guard let backend = KituraKit(baseURL: "http://localhost:8080") else { print("Error creating KituraKit client") return } backend.get("/profile") { (profiles: [Profile]?, error: RequestError?) in ... } KITURAKIT
  • 51. { "name": "John Doe", "photo": "jbbkj362", "dateOfBirth": "06-06-1980“ }
  • 52. {"name": "John Doe","photo": "jbbkj362","dateOfBirth": "06-06-1980"}
  • 56. { name = "John Doe"}
  • 57. ? name = "John Doe"}
  • 58. ? name = "John Doe"#
  • 60. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { ... respondWith(profiles, nil) }
  • 61. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } struct Query: QueryParams { var name: String } router.get("/profile", handler: getProfiles) func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { ... respondWith(profiles, nil) }
  • 62. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } struct Query: QueryParams { var name: String } router.get("/profile", handler: getProfiles) func getProfiles( , respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { ... respondWith(profiles, nil) }
  • 63. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } struct Query: QueryParams { var name: String } router.get("/profile", handler: getProfiles) func getProfiles(query: Query, respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { ... respondWith(profiles, nil) }
  • 64. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } struct Query: QueryParams { var name: String } router.get("/profile", handler: getProfiles) func getProfiles(query: Query, respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { ... respondWith(profiles , nil) }
  • 65. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } struct Query: QueryParams { var name: String } router.get("/profile", handler: getProfiles) func getProfiles(query: Query, respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { ... respondWith(profiles.filter{ ($0.name == query.name), nil) }
  • 67. SELECT * from Profiles
  • 68. name,, photo, dateOfBirth, "John Doe", "jbbkj362", "06-06-1980",
  • 69. name,, photo, dateOfBirth "John Doe", "jbbkj362", "06-06-1980"
  • 72. struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { ... respondWith(profiles, nil) }
  • 73. struct Profile: Model { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { ... respondWith(profiles, nil) }
  • 74. struct Profile: Model { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { Profile.getAll() respondWith(profiles, nil) }
  • 75. struct Profile: Model { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { Profile.getAll(respondWith) respondWith(profiles, nil) }
  • 76. struct Profile: Model { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void { Profile.getAll(respondWith) }
  • 77. struct Profile: Model { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) func getProfiles(completion: @escaping ([Profile]?, Error?) -> Void) -> Void { Profile.getAll(completion) }
  • 78. SELECT * from Profiles
  • 79. SELECT * from Profiles
  • 80. SELECT * from Profiles WHERE name = “John Doe”
  • 81. struct Profile: Model { var name: String var photo: Data var dateOfBirth: Date } router.get("/profile", handler: getProfiles) func getProfiles(completion: @escaping ([Profile]?, Error?) -> Void) -> Void { Profile.getAll(completion) }
  • 82. struct Profile: Model { var name: String var photo: Data var dateOfBirth: Date } struct Query: QueryParams { var name: String } router.get("/profile", handler: getProfiles) func getProfiles(completion: @escaping ([Profile]?, Error?) -> Void) -> Void { Profile.getAll(completion) }
  • 83. struct Profile: Model { var name: String var photo: Data var dateOfBirth: Date } struct Query: QueryParams { var name: String } router.get("/profile", handler: getProfiles) func getProfiles(query: Query, completion: @escaping ([Profile]?, Error?) -> Void) -> Void { Profile.getAll(completion) }
  • 84. struct Profile: Model { var name: String var photo: Data var dateOfBirth: Date } struct Query: QueryParams { var name: String } router.get("/profile", handler: getProfiles) func getProfiles(query: Query, completion: @escaping ([Profile]?, Error?) -> Void) -> Void { Profile.getAll( completion) }
  • 85. struct Profile: Model { var name: String var photo: Data var dateOfBirth: Date } struct Query: QueryParams { var name: String } router.get("/profile", handler: getProfiles) func getProfiles(query: Query, completion: @escaping ([Profile]?, Error?) -> Void) -> Void { Profile.getAll(matching: query, completion) }