Swift-idl

 Swift-idl

Swift source code generator from Swift

53e469a19bcb4584c87789d237128ca0?s=128

matuyuji

July 21, 2015
Tweet

Transcript

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

  2. @matuyuji safx-dev.blogspot.jp

  3. JSON Libraries • Himotoki • Decodable • Argo • JSONHelper

    • ObjectMapper
  4. 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", ) } }
  5. ໰୊ • DRYత͡Όͳ͍ • JSONϥΠϒϥϦ͕ཉ͍͠Θ͚͡Όͳ͍

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

    `json:"size"` }
  7. ཉ͍͠΋ͷ • REST APIΫϥΠΞϯτΛָʹߏங͍ͨ͠ • DRYతͳ΋ͷ

  8. safx/swift-idl struct FileEntry: JSONDecodable, JSONEncodable, Printable { let fileName: String

    // json:"file-name" let size : Int } python swift-idl.py MyProj.xcodeproj
  9. 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") }
  10. Protocols protocol Printable {} protocol NSCoding {} protocol JSONEncodable {}

    protocol JSONDecodable {} protocol URLRequestHelper {} protocol ClassInit {} protocol EnumStaticInit {}
  11. 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?) }
  12. 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 } } }
  13. Install & Setting • brew install sourcekitten • github clone

    https://github.com/safx/swift-idl.git TypetalkKit swift-2.0 Xcode 6.4 Xcode 7