Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
protocol_buffers.pdf
Search
Kyohei Ito
November 20, 2016
7.4k
6
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
protocol_buffers.pdf
Kyohei Ito
November 20, 2016
More Decks by Kyohei Ito
See All by Kyohei Ito
flutter_kaigi_2025.pdf
kyoheig3
2
1.1k
layerx_20241129.pdf
kyoheig3
2
530
flutterkaigi_2024.pdf
kyoheig3
0
1.9k
flutter_kaigi_2021.pdf
kyoheig3
0
1.2k
flutter_kmm_1.pdf
kyoheig3
1
1.2k
ca.swift_10.pdf
kyoheig3
0
720
iosdc_2018.pdf
kyoheig3
2
3.2k
orecon_vol1.pdf
kyoheig3
4
1.8k
iosdc_2017.pdf
kyoheig3
4
940
Featured
See All Featured
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
240
Darren the Foodie - Storyboard
khoart
PRO
3
3.9k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
The SEO Collaboration Effect
kristinabergwall1
1
550
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
230
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
The Language of Interfaces
destraynor
162
27k
My Coaching Mixtape
mlcsv
0
310
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
510
Mobile First: as difficult as doing things right
swwweet
225
10k
Faster Mobile Websites
deanohume
310
32k
A Soul's Torment
seathinner
7
3.6k
Transcript
͜Ε͔Β࢝ΊΔ Protocol Buffers ಋೖ 2016/11/20 iOSΦʔϧελʔζ2
About Me ҏ౻ɹګฏ Github : KyoheiG3 Twitter : @KyoheiG3
protobufʢུʣ
protobufͷ؆୯ͳྺ࢙ • Google͕։ൃͨ͠ɺ௨৴ӬଓԽͰͷར༻Λతͱͨ͠γ ϦΞϥΠζϑΥʔϚοτ • ॳ൛20087݄7Β͍͠ʢWikipediaΑΓʣ • ࠷৽൛3.1.0
ͳͥࠓprotobufͳͷ͔ʁ • swiftͷprotobufͷϥΠϒϥϦͦͦ͜͜લ͔Β͋ͬͨ1 • appleͷgithubʹswift-protobufͷϦϙδτϦ͕ެ։͞Εͨ2 2 swift-protobuf 1 protobuf-swift
"I guess Apple decided that JSON is a relic of
the past along with Objective-C" - @cjwirth
Apple͞ΜͷτϨϯυʹ͔ͬΓ·͠ΐ͏ʂ
֓ཁ
JSONXMLͷ͓͞Β͍ • Key Value • Text • Ϛοϐϯά
protobufͷΈ • .protoఆٛ • γϦΞϥΠζ/σγϦΞϥΠζ • όΠφϦσʔλ௨৴
protobufͷϝϦοτ • σʔλྔ • ߴ • ҆શ • ڞ༗
protobufͷσϝϦοτ • ίϯύΠϧ • ಋೖखॱ • ֬ೝखॱ
.protoͰఆٛͰ͖Δجຊͷܕ3 int32 sint32 sfixed32 uint32 fixed32 int64 sint64 sfixed64 uint64
fixed64 bool float double string bytes 3 apple/swift-protobufͷAPI OverviewΑΓൈਮ
ಋೖ
Կͱ͋ΕprotobufΛೖΕΔ $ brew install protobuf
protobufͷSwiftϓϥάΠϯΛ࡞ checkout͢Δλά࠷৽ͷͷ $ git clone https://github.com/apple/swift-protobuf.git $ cd swift-protobuf $
git checkout tags/0.9.24 $ swift build > .build/debug/protoc-gen-swift
࡞ͨ͠ϓϥάΠϯΛ͑ΔΑ͏ʹ͢Δ ඞཁʹԠͯ͡ҙͷॴʹஔ͍ͯPATHΛ௨͢ $ mkdir ~/.protoc $ cp .build/debug/protoc-gen-swift ~/.protoc/protoc-gen-swift $
echo 'export PATH=$PATH:$HOME/.protoc' >> ~/.bash_profile $ source ~/.bash_profile
.protoͷ࡞
DataModel.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; } quick-example
syntax syntax = "proto3"; • proto3ͱهड़͠ͳ͍ͱproto2ͱΈͳ͞ΕΔ
message message BookInfo { } • ॴҦΫϥεఆٛ • ͜ͷதʹϑΟʔϧυΛఆ͍ٛͯ͘͠
Field Types int64 id = 1; string title = 2;
string author = 3; • ܕͱม໊Λઃఆ͢Δ
Tags int64 id = 1; string title = 2; string
author = 3; • messageͰҰҙͷΛׂΓͯΔ • ॱংߟྀ͠ͳͯ͘ྑ͍
Field Rules repeated BookInfo books = 3; • repeatedͰྻΛఆٛͰ͖Δ
ͦͷଞ • package • map • enum • etc.. ৄ͘͠Language
GuideΛࢀর͍ͯͩ͘͠͞ɻ
͏ҰDataModel.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; }
.protoͷίϯύΠϧ
PATHΛ௨͍ͯ͠Δ߹ $ protoc --swift_out=. DataModel.proto
ϓϥάΠϯΛࢦఆ࣮ͯ͠ߦ͢Δ߹ $ protoc --plugin=protoc-gen-swift=.build/debug/ protoc-gen-swift --swift_out=. DataModel.proto
! ࡞͞Εͨ.pb.swiftΛϓϩδΣΫτʹՃ͠·͠ΐ͏
ͪͳΈʹprotobuf-swiftͩͱ $ brew install protobuf-swift $ protoc --swift_out=. DataModel.proto
ྫ͑goͩͱ $ go get github.com/golang/protobuf/protoc-gen-go $ protoc --go_out=. DataModel.proto
͍ํ
ϥΠϒϥϦͷՃ • Package Manager dependencies: [ .Package(url: "https://github.com/apple/swift-protobuf.git", Version(0,9,24)) ]
• CocoaPods pod 'SwiftProtobuf', git: 'https://github.com/apple/swift-protobuf.git', :tag => '0.9.24' ※ϓϥάΠϯΛ࡞ͨ࣌͠ͷtagͱΠϯετʔϧόʔδϣϯΛ߹ Θ͍ͤͯͩ͘͞ɻ
Binary serializable γϦΞϥΠζ let library = MyLibrary() let data =
try library.serializeProtobuf() σγϦΞϥΠζ let library = try MyLibrary(protobuf: data)
JSON serializable γϦΞϥΠζ let library = MyLibrary() let json =
try library.serializeJSON() σγϦΞϥΠζ let json = String(bytes: data, encoding: String.Encoding.utf8)! let library = try MyLibrary(json: json)
JSONͱͷڞଘ
JSON request let url = URL(string: "http://localhost:8080/")! var request =
URLRequest(url: url) let value = "application/json" request.setValue(value, forHTTPHeaderField: "Accept")
protobuf request let url = URL(string: "http://localhost:8080/")! var request =
URLRequest(url: url) let value = "application/protobuf" request.setValue(value, forHTTPHeaderField: "Accept")
response let library = MyLibrary() 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()) }
Tips
protobufͷϑΟʔϧυͷมߋʹ͍ͭͯ • ϑΟʔϧυͷՃՄೳ • ϑΟʔϧυ໊ͷมߋ͚ͩͳΒಈ͘ • ϑΟʔϧυܕ·ͰมΘΔ߹ΤϥʔͱͳΔ
λά൪߸ʹ͍ͭͯ • λά൪߸ͰϑΟʔϧυΛࣝผ͢ΔͷͰॏෳແ͘ࢦఆ͢Δ͜ͱ • ޙͰมߋ͢ΔͷNG
Int64ͷѻ͍ʹ͍ͭͯ • jsonʹγϦΞϥΠζͨ͠߹ͷৼΔ͍ • ྫ͑goͩͱͰฦΔ͕swiftͩͱจࣈྻͰฦΔ
protobufͷόʔδϣϯʹ͍ͭͯ • ར༻͢ΔprotocϓϥάΠϯ͕αϙʔτ͢Δprotobufͷόʔδ ϣϯͱɺ.proto͔ΒίʔυΛੜ͢Δࡍͷprotobufͷόʔδ ϣϯΛಉ͡ʹ͢Δ͜ͱ ࢀߟʣHomebrewͰچόʔδϣϯͷύοέʔδΛΠϯετʔ ϧ͍ͨ͠
ͲͷϓϥάΠϯΛར༻͢Δ͔ • apple/swift-protobuf struct • alexeyxo/protobuf-swift class
ੜϑΝΠϧͷهड़ྔ apple/swift-protobuf < alexeyxo/protobuf-swift
Swift Protobuf ͷҙ • ίʔυޓੑ͕ແ͘ͳΔΑ͏ͳੵۃతͳվม͕ߦΘΕ͍ͯΔ • ϝδϟʔόʔδϣϯ·ͩ 0
σϞ https://github.com/KyoheiG3/ProtobufExample
Thanks!