Slide 1

Slide 1 text

swift-idl @matuyuji ؔ੢ϞόΠϧΞϓϦݚڀձ #4 2015.7.21

Slide 2

Slide 2 text

@matuyuji safx-dev.blogspot.jp

Slide 3

Slide 3 text

JSON Libraries • Himotoki • Decodable • Argo • JSONHelper • ObjectMapper

Slide 4

Slide 4 text

Decodable struct FileEntry { let fileName: String let size: Int } extension FileEntry: Decodable { static func decode(j: AnyObject) throws -> FileEntry { return try Repository( fileName: j => “file-name", size: j => "size", ) } }

Slide 5

Slide 5 text

໰୊ • DRYత͡Όͳ͍ • JSONϥΠϒϥϦ͕ཉ͍͠Θ͚͡Όͳ͍

Slide 6

Slide 6 text

Golang type FileEntry struct { FileName string `json:"file-name"` Size int `json:"size"` }

Slide 7

Slide 7 text

ཉ͍͠΋ͷ • REST APIΫϥΠΞϯτΛָʹߏங͍ͨ͠ • DRYతͳ΋ͷ

Slide 8

Slide 8 text

safx/swift-idl struct FileEntry: JSONDecodable, JSONEncodable, Printable { let fileName: String // json:"file-name" let size : Int } python swift-idl.py MyProj.xcodeproj

Slide 9

Slide 9 text

public struct FileEntry: JSONDecodable, JSONEncodable, CustomStringConvertible { public let fileName: String // json:"file-name" public let size : Int public static func parseJSON(data: AnyObject) throws -> FileEntry { if !(data is NSDictionary) { throw JSONDecodeError.TypeMismatch(key: "FileEntry", type: "NSDictionary") } let fileName: String if let v: AnyObject = data["file-name"] { if v is NSNull { throw JSONDecodeError.NonNullablle(key: "file-name") } else { do { fileName = try String.parseJSON(v) } catch JSONDecodeError.ValueTranslationFailed { throw JSONDecodeError.TypeMismatch(key: "file-name", type: "String") } } } else { throw JSONDecodeError.MissingKey(key: "file-name") } let size: Int if let v: AnyObject = data["size"] { if v is NSNull { throw JSONDecodeError.NonNullablle(key: "size") } else { do { size = try Int.parseJSON(v) } catch JSONDecodeError.ValueTranslationFailed { throw JSONDecodeError.TypeMismatch(key: "size", type: "Int") }

Slide 10

Slide 10 text

Protocols protocol Printable {} protocol NSCoding {} protocol JSONEncodable {} protocol JSONDecodable {} protocol URLRequestHelper {} protocol ClassInit {} protocol EnumStaticInit {}

Slide 11

Slide 11 text

URLRequestHelper enum Router: URLRequestHelper { case GetProfile // router:",profile" case GetMessages(topicId: TopicID, // router:"GET,topics/\(topicId)" count: Int?) case PostMessage(topicId: TopicID, // router:"POST,topics/\(topicId)" message: String, replyTo: Int?) }

Slide 12

Slide 12 text

public enum Router { case GetProfile case GetMessages(topicId: TopicID, count: Int?) case PostMessage(topicId: TopicID, message: String, replyTo: Int?) public var method: String { switch self { case .GetProfile: return "GET" case .GetMessages: return "GET" case .PostMessage: return "POST" } } public var path: String { switch self { case .GetProfile: return "profile" case .GetMessages(let (topicId, _)): return "topics/\(topicId)" case .PostMessage(let (topicId, _, _)): return "topics/\(topicId)" } } public var params: [String: AnyObject] { switch self { case .GetProfile: return [:] case .GetMessages(let (_, count)): var p: [String: AnyObject] = [:] count.map { p["count"] = $0.toJSON() } return p case .PostMessage(let (_, message, replyTo)): var p: [String: AnyObject] = ["message": message.toJSON()] replyTo.map { p["replyTo"] = $0.toJSON() } return p } } }

Slide 13

Slide 13 text

Install & Setting • brew install sourcekitten • github clone https://github.com/safx/swift-idl.git TypetalkKit swift-2.0 Xcode 6.4 Xcode 7