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
layerx_20241129.pdf
kyoheig3
2
330
flutterkaigi_2024.pdf
kyoheig3
0
580
flutter_kaigi_2021.pdf
kyoheig3
0
970
flutter_kmm_1.pdf
kyoheig3
1
1k
ca.swift_10.pdf
kyoheig3
0
620
iosdc_2018.pdf
kyoheig3
2
2.7k
orecon_vol1.pdf
kyoheig3
4
1.6k
iosdc_2017.pdf
kyoheig3
4
840
ca.swift_2.pdf
kyoheig3
9
1.3k
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
5
200
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Building Adaptive Systems
keathley
38
2.4k
YesSQL, Process and Tooling at Scale
rocio
170
14k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Faster Mobile Websites
deanohume
305
30k
The World Runs on Bad Software
bkeepers
PRO
66
11k
Adopting Sorbet at Scale
ufuk
74
9.2k
How to Ace a Technical Interview
jacobian
276
23k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7.1k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
240
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!