Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Swift-idl
Search
matuyuji
July 21, 2015
Programming
0
540
Swift-idl
Swift source code generator from Swift
matuyuji
July 21, 2015
Tweet
Share
More Decks by matuyuji
See All by matuyuji
Emacs × Touch Bar
matuyuji
2
1.7k
ARKit + SceneKitでMinesweeperを作ってみた
matuyuji
1
760
Go + QtでiOS アプリ開発
matuyuji
0
370
@_specialized なお話し
matuyuji
0
460
Xcode Souce Code Extensionを使ってみた
matuyuji
0
330
Codebeatを 試してみた
matuyuji
0
720
React Nativeで UIコンポーネントをつくる
matuyuji
0
980
React Nativeを使ってみた
matuyuji
0
1.2k
SwiftでLens
matuyuji
1
930
Other Decks in Programming
See All in Programming
qmuntal/stateless のススメ
sgash708
0
120
デプロイを任されたので、教わった通りにデプロイしたら障害になった件 ~俺のやらかしを越えてゆけ~
techouse
52
32k
カラム追加で増えるActiveRecordのメモリサイズ イメージできますか?
asayamakk
4
1.6k
GCCのプラグインを作る / I Made a GCC Plugin
shouth
1
150
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
150
Googleのテストサイズを活用したテスト環境の構築
toms74209200
0
270
Hotwire or React? ~Reactの録画機能をHotwireに置き換えて得られた知見~ / hotwire_or_react
harunatsujita
9
4.1k
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
16
4.1k
go.mod、DockerfileやCI設定に分散しがちなGoのバージョンをまとめて管理する / Go Connect #3
arthur1
10
2.4k
EventSourcingの理想と現実
wenas
6
2.1k
gopls を改造したら開発生産性が高まった
satorunooshie
8
240
Kubernetes for Data Engineers: Building Scalable, Reliable Data Pipelines
sucitw
1
200
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
19
1.1k
Code Reviewing Like a Champion
maltzj
519
39k
Scaling GitHub
holman
458
140k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
167
49k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
7.9k
Six Lessons from altMBA
skipperchong
26
3.5k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.2k
Facilitating Awesome Meetings
lara
49
6k
Building Applications with DynamoDB
mza
90
6.1k
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Transcript
swift-idl @matuyuji ؔϞόΠϧΞϓϦݚڀձ #4 2015.7.21
@matuyuji safx-dev.blogspot.jp
JSON Libraries • Himotoki • Decodable • Argo • JSONHelper
• ObjectMapper
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", ) } }
• DRYత͡Όͳ͍ • JSONϥΠϒϥϦ͕ཉ͍͠Θ͚͡Όͳ͍
Golang type FileEntry struct { FileName string `json:"file-name"` Size int
`json:"size"` }
ཉ͍͠ͷ • REST APIΫϥΠΞϯτΛָʹߏங͍ͨ͠ • DRYతͳͷ
safx/swift-idl struct FileEntry: JSONDecodable, JSONEncodable, Printable { let fileName: String
// json:"file-name" let size : Int } python swift-idl.py MyProj.xcodeproj
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") }
Protocols protocol Printable {} protocol NSCoding {} protocol JSONEncodable {}
protocol JSONDecodable {} protocol URLRequestHelper {} protocol ClassInit {} protocol EnumStaticInit {}
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?) }
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 } } }
Install & Setting • brew install sourcekitten • github clone
https://github.com/safx/swift-idl.git TypetalkKit swift-2.0 Xcode 6.4 Xcode 7