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
flutterkaigi_2024.pdf
kyoheig3
0
81
flutter_kaigi_2021.pdf
kyoheig3
0
940
flutter_kmm_1.pdf
kyoheig3
1
1k
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.3k
protocol_buffers.pdf
kyoheig3
6
6.5k
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Practical Orchestrator
shlominoach
186
10k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Fireside Chat
paigeccino
34
3k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
The Cult of Friendly URLs
andyhume
78
6k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Statistics for Hackers
jakevdp
796
220k
Building an army of robots
kneath
302
43k
Designing the Hi-DPI Web
ddemaree
280
34k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
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!