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
ca.swift.pdf
Search
Kyohei Ito
December 01, 2016
1
2k
ca.swift.pdf
Kyohei Ito
December 01, 2016
Tweet
Share
More Decks by Kyohei Ito
See All by Kyohei Ito
flutter_kaigi_2021.pdf
kyoheig3
0
910
flutter_kmm_1.pdf
kyoheig3
1
990
ca.swift_10.pdf
kyoheig3
0
610
iosdc_2018.pdf
kyoheig3
2
2.6k
orecon_vol1.pdf
kyoheig3
4
1.5k
iosdc_2017.pdf
kyoheig3
4
830
ca.swift_2.pdf
kyoheig3
9
1.2k
protocol_buffers.pdf
kyoheig3
6
6.5k
abema_devcon.pdf
kyoheig3
8
7.1k
Featured
See All Featured
Building Your Own Lightsaber
phodgson
102
6k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Intergalactic Javascript Robots from Outer Space
tanoku
268
27k
4 Signs Your Business is Dying
shpigford
180
21k
Raft: Consensus for Rubyists
vanstee
136
6.6k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
228
52k
Scaling GitHub
holman
458
140k
Designing for humans not robots
tammielis
249
25k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Designing on Purpose - Digital PM Summit 2013
jponch
114
6.9k
Transcript
͜Ε͔Β࢝ΊΔ Protocol Buffers ӡ༻ 2016/12/1 CA.swift
About Me ҏ౻ɹګฏ Github : KyoheiG3 Twitter : @KyoheiG3
None
protobufΛ͍࢝ΊͨྲྀΕ • gRPCΛ͓͏ͱ͍ͯͨ͠ • Obj-CͷελςΟοΫϥΠϒϥϦͷಋೖ͕ͭΒ͔ͬͨ • AndroidͳΜ͔ͭΒͦ͏ͩͬͨ • ͳΜ͔৭ʑ͔ͭΒͬͨʢΑ֮͑ͯ͘ͳ͍
gRPCΊ͚ͨͲprotobufͦͷ··ͬͨ
protobufͷ͓͞Β͍
protobufͷಛ • ࣄલʹߏΛఆٛ͢Δ • όΠφϦσʔλΛѻ͏
protobufͷػೳ • Ϋϥεఆٛ • όΠφϦσʔλͷγϦΞϥΠζ/σγϦΞϥΠζ • JSONσʔλͷγϦΞϥΠζ/σγϦΞϥΠζ
protobufͷϝϦοτ • σʔλྔ • ߴ • ҆શ • ڞ༗
protobufͷσϝϦοτ • ίϯύΠϧ • ಋೖखॱ • ֬ೝखॱ
σϝϦοτΛϝϦοτʹ ม͍͖͑ͯ·͠ΐ͏ !
ಋೖͷ؆୯ͳྲྀΕ 1. ϓϩδΣΫτଆͰϏϧυڥΛߏங 2. .protoϑΝΠϧΛ࡞ 3. .protoϑΝΠϧΛίϯύΠϧ 4. ग़དྷ্͕ͬͨϑΝΠϧΛಋೖ
1. ϓϩδΣΫτଆͰϏϧυڥΛߏங • protocϓϥάΠϯͷબఆ • ڥ༻ҙ(Android, iOS, web, api, etc..)
• protocϓϥάΠϯͷϥΠϒϥϦΛಋೖ
1. ϓϩδΣΫτଆͰϏϧυڥΛߏங Swiftͩͱ2ͭ https://github.com/apple/swift-protobuf https://github.com/alexeyxo/protobuf-swift
2. .protoϑΝΠϧΛ࡞ .protoϑΝΠϧྫ syntax = "proto3"; message BookInfo { int64
id = 1; string title = 2; string author = 3; } message MyLibrary { int64 id = 1; string name = 2; repeated BookInfo books = 3; map<string,string> keys = 4; }
3. .protoϑΝΠϧΛίϯύΠϧ
4. ग़དྷ্͕ͬͨϑΝΠϧΛಋೖ iOSͳΒϓϩδΣΫτʹϑΝΠϧΛಥͬࠐΉ͚ͩ
ͦͷ··ΦϒδΣΫτͱͯ͠ར༻Մೳ • protobuf message MyLibrary { int64 id = 1;
string name = 2; repeated BookInfo books = 3; map<string,string> keys = 4; } • Swift public struct MyLibrary { public var id: Int64 = 0 public var name: String = "" public var books: [BookInfo] = [] public var keys: Dictionary<String,String> = [:] }
ҙ • ࡞ͬͨϑΝΠϧฤू͠ͳ͍ • ಠࣗͷmodel͕͍͍ͨ߹Ϛοϐϯά͕ඞཁ • ωʔϜεϖʔεpackageͰղܾ͢Δ
σϝϦοτΛղܾ͢Δ
JSONͳͲͱൺֱͯ͠Θ͍͠ͱࢥΘΕΔҰཡ • ࣄલఆ͕ٛඞཁ • มߋͷʹίϯύΠϧ͕ඞཁ • ελϒͱ͔࡞Δͷ໘ͳΜͰ͠ΐʁ • όΠφϦਓؒʹཧղͮ͠Β͍
͔֬ʹ...ͰJSONʹ৭ʑ͋Γͦ͏...
JSONͷख͕ܰͨ͞Β͍͢᠘
None
None
None
None
JSONͰؕΓ͕ͪͳࣦഊ • σʔλఆٛΛ࣮ͱผͷͱ͜ΖͰߦ͏ • υΩϡϝϯτͱ࣮σʔλ͕ͳΜ͔ҧ͏ • ͦͦఆ͕ٛཧ͞Ε͍ͯͳ͍ • มߋཤྺ͕͑ͳ͍ •
มߋՕॴ͕͑ͳ͍
protobufͰղܾ͠·͠ΐ͏
None
Ͱղܾ
ࣄલఆ͕ٛඞཁվΊ ࣄલఆٛͰղܾ ೝ͕ࣝζϨͳ͍
ࣄલఆ͕ٛඞཁվΊ ࣄલఆٛͰղܾ มߋΛϨϏϡʔͰ͖Δ
ࣄલఆ͕ٛඞཁվΊ ࣄલఆٛͰղܾ υΩϡϝϯτΛग़ྗͰ͖Δ
มߋͷʹίϯύΠϧ͕ඞཁվΊ มߋ͕ඞཁͳ࣌ʹίϯύΠϧͯ͠ղܾ • ϑΟʔϧυͷՃແࢹͰ͖Δ • TagมߋͳͲͷഁյతมߋ͕ͳ͚Εߋ৽ඞਢͰͳ͍ • αʔόଆͰΩʔ໊͕มߋ͞Εͯࠓ·Ͱ௨Γར༻Մೳ ※JSONͷγϦΞϥΠζͳͲͷॲཧ్͕தʹೖΔ߹ɺΩʔ໊Λ มߋ͢Δͱσʔλ͕ܽམ͠·͢
ελϒͱ͔࡞Δͷ໘ͳΜͰ͠ΐʁ JSONσγϦΞϥΠζͰղܾ • JSONͰ༻ҙͨ͠σʔλ͔ΒσγϦΞϥΠζͰ͖Δ • protobufΛฦ͢ϞοΫΛ࡞Γ͍ͨͳΒσγϦΞϥΠζσʔλ ΛߋʹprotobufʹγϦΞϥΠζ • HimotokiͬͯΔʁҰ୴JSONʹγϦΞϥΠζ͠Α͏ʢବ
όΠφϦਓؒʹཧղͮ͠Β͍ ௨৴σʔλͷಡԽͱׂΓΔ • #DEBUGͷ࣌application/jsonͰड͚औΔͳͲ • ΦϒδΣΫτී௨ʹϩάग़ྗͰ͖Δ • ੜͷJSONจࣈྻΑΓσόοά͠қ͍
ӡ༻
APIͰprotobufΛฦ͢ • EntityΛprotobufͷͷʹ͢Δ • ϦΫΤετͷAcceptϔομͰஅ
ϦΫΤετͷAcceptϔομͰஅ let library = getLibrary() let accept = request.headers["Accept"] if
accept == "application/protobuf" { response.headers["Content-Type"] = "application/protobuf" response.send(data: try library.serializeProtobuf()) } else { response.headers["Content-Type"] = "application/json; charset=UTF-8" response.send(try library.serializeJSON()) }
protobufͰϦΫΤετ͢Δ • ϦΫΤετʹAcceptϔομΛຒΊࠐΉ • ϨεϙϯεϔομΛݟͯjsonͱprotobufͷΓସ͑
ϨεϙϯεϔομΛݟͯjsonͱprotobufͷΓସ͑ let contentType = response.allHeaderFields["Content-Type"] as? String if let type
= contentType, type == "application/protobuf" { return try MyLibrary(protobuf: data) } else { let json = String(bytes: data, encoding: .utf8) return try MyLibrary(json: json!) }
stabJSONσγϦΞϥΠζ let json = String(bytes: data, encoding: .utf8) return try
MyLibrary(json: json!)
protobufΛฦ͢ϞοΫ let json = String(bytes: data, encoding: .utf8) let library
= try MyLibrary(json: json!) return try library.serializeProtobuf()
ͦͷଞΔ͜ͱ • .protoΛόʔδϣϯཧ • .protoͷυΩϡϝϯτԽ • .swiftͷframeworkԽ
.protoͷυΩϡϝϯτԽ https://github.com/estan/protoc-gen-doc $ brew install qt5 $ brew link --force
qt5 $ git clone https://github.com/estan/protoc-gen-doc.git $ cd protoc-gen-doc $ PROTOBUF_PREFIX=/usr/local/Cellar/protobuf/3.1.0 qmake $ make && make install ※PROTOBUF_PREFIXɺ༻͢Δprotobufʹ߹Θͤͯͩ͘͞ ͍ɻ
.protoͷυΩϡϝϯτԽ $ protoc --doc_out=markdown,../docs.md:. *.proto $ protoc --doc_out=html,../index.html:. *.proto
.swiftΛframeworkԽ͢Δ Pod::Spec.new do |s| s.name = "Protobuf" s.summary = "Protobuf"
s.homepage = "https://github.com/KyoheiG3/ProtobufExample" s.version = "0.0.1" s.author = { "Kyohei Ito" => "
[email protected]
" } s.ios.deployment_target = '8.0' s.source = { :path => '.' } s.source_files = "**/*.swift" s.requires_arc = true s.dependency 'SwiftProtobuf' end
.swiftΛframeworkԽ͢Δ pod 'Protobuf', :path => '../protos'
σϞ https://github.com/KyoheiG3/ProtobufExample
Thanks!