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
290
flutterkaigi_2024.pdf
kyoheig3
0
500
flutter_kaigi_2021.pdf
kyoheig3
0
960
flutter_kmm_1.pdf
kyoheig3
1
1k
ca.swift_10.pdf
kyoheig3
0
620
iosdc_2018.pdf
kyoheig3
2
2.6k
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
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Designing Experiences People Love
moore
138
23k
Why Our Code Smells
bkeepers
PRO
335
57k
Fireside Chat
paigeccino
34
3.1k
Code Reviewing Like a Champion
maltzj
520
39k
BBQ
matthewcrist
85
9.4k
Code Review Best Practice
trishagee
65
17k
Building Your Own Lightsaber
phodgson
103
6.1k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
How to train your dragon (web standard)
notwaldorf
88
5.7k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
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!