Slide 1

Slide 1 text

͜Ε͔Β࢝ΊΔ Protocol Buffers ಋೖ 2016/11/20 iOSΦʔϧελʔζ2

Slide 2

Slide 2 text

About Me ҏ౻ɹګฏ Github : KyoheiG3 Twitter : @KyoheiG3

Slide 3

Slide 3 text

protobufʢུʣ

Slide 4

Slide 4 text

protobufͷ؆୯ͳྺ࢙ • Google͕։ൃͨ͠ɺ௨৴΍ӬଓԽͰͷར༻Λ໨తͱͨ͠γ ϦΞϥΠζϑΥʔϚοτ • ॳ൛͸2008೥7݄7೔Β͍͠ʢWikipediaΑΓʣ • ࠷৽൛͸3.1.0

Slide 5

Slide 5 text

ͳͥࠓprotobufͳͷ͔ʁ • swiftͷprotobufͷϥΠϒϥϦ͸ͦͦ͜͜લ͔Β͋ͬͨ1 • appleͷgithubʹswift-protobufͷϦϙδτϦ͕ެ։͞Εͨ2 2 swift-protobuf 1 protobuf-swift

Slide 6

Slide 6 text

"I guess Apple decided that JSON is a relic of the past along with Objective-C" - @cjwirth

Slide 7

Slide 7 text

Apple͞ΜͷτϨϯυʹ৐͔ͬΓ·͠ΐ͏ʂ

Slide 8

Slide 8 text

֓ཁ

Slide 9

Slide 9 text

JSON΍XMLͷ͓͞Β͍ • Key Value • Text • Ϛοϐϯά

Slide 10

Slide 10 text

protobufͷ࢓૊Έ • .protoఆٛ • γϦΞϥΠζ/σγϦΞϥΠζ • όΠφϦσʔλ௨৴

Slide 11

Slide 11 text

protobufͷϝϦοτ • σʔλྔ • ߴ଎ • ҆શ • ڞ༗

Slide 12

Slide 12 text

protobufͷσϝϦοτ • ίϯύΠϧ • ಋೖखॱ • ֬ೝखॱ

Slide 13

Slide 13 text

.protoͰఆٛͰ͖Δجຊͷܕ3 int32 sint32 sfixed32 uint32 fixed32 int64 sint64 sfixed64 uint64 fixed64 bool float double string bytes 3 apple/swift-protobufͷAPI OverviewΑΓൈਮ

Slide 14

Slide 14 text

ಋೖ

Slide 15

Slide 15 text

Կ͸ͱ΋͋ΕprotobufΛೖΕΔ $ brew install protobuf

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

࡞੒ͨ͠ϓϥάΠϯΛ࢖͑ΔΑ͏ʹ͢Δ ඞཁʹԠͯ͡೚ҙͷ৔ॴʹஔ͍ͯPATHΛ௨͢ $ mkdir ~/.protoc $ cp .build/debug/protoc-gen-swift ~/.protoc/protoc-gen-swift $ echo 'export PATH=$PATH:$HOME/.protoc' >> ~/.bash_profile $ source ~/.bash_profile

Slide 18

Slide 18 text

.protoͷ࡞੒

Slide 19

Slide 19 text

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 keys = 4; } quick-example

Slide 20

Slide 20 text

syntax syntax = "proto3"; • proto3ͱهड़͠ͳ͍ͱproto2ͱΈͳ͞ΕΔ

Slide 21

Slide 21 text

message message BookInfo { } • ॴҦΫϥεఆٛ • ͜ͷதʹϑΟʔϧυΛఆ͍ٛͯ͘͠

Slide 22

Slide 22 text

Field Types int64 id = 1; string title = 2; string author = 3; • ܕͱม਺໊Λઃఆ͢Δ

Slide 23

Slide 23 text

Tags int64 id = 1; string title = 2; string author = 3; • message಺ͰҰҙͷ਺஋ΛׂΓ౰ͯΔ • ॱং͸ߟྀ͠ͳͯ͘΋ྑ͍

Slide 24

Slide 24 text

Field Rules repeated BookInfo books = 3; • repeatedͰ഑ྻΛఆٛͰ͖Δ

Slide 25

Slide 25 text

ͦͷଞ • package • map • enum • etc.. ৄ͘͠͸Language GuideΛࢀর͍ͯͩ͘͠͞ɻ

Slide 26

Slide 26 text

΋͏Ұ౓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 keys = 4; }

Slide 27

Slide 27 text

.protoͷίϯύΠϧ

Slide 28

Slide 28 text

PATHΛ௨͍ͯ͠Δ৔߹ $ protoc --swift_out=. DataModel.proto

Slide 29

Slide 29 text

ϓϥάΠϯΛࢦఆ࣮ͯ͠ߦ͢Δ৔߹ $ protoc --plugin=protoc-gen-swift=.build/debug/ protoc-gen-swift --swift_out=. DataModel.proto

Slide 30

Slide 30 text

׬੒ ! ࡞੒͞Εͨ.pb.swiftΛϓϩδΣΫτʹ௥Ճ͠·͠ΐ͏

Slide 31

Slide 31 text

ͪͳΈʹprotobuf-swiftͩͱ $ brew install protobuf-swift $ protoc --swift_out=. DataModel.proto

Slide 32

Slide 32 text

ྫ͑͹goͩͱ $ go get github.com/golang/protobuf/protoc-gen-go $ protoc --go_out=. DataModel.proto

Slide 33

Slide 33 text

࢖͍ํ

Slide 34

Slide 34 text

ϥΠϒϥϦͷ௥Ճ • 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ͱΠϯετʔϧόʔδϣϯΛ߹ Θ͍ͤͯͩ͘͞ɻ

Slide 35

Slide 35 text

Binary serializable γϦΞϥΠζ let library = MyLibrary() let data = try library.serializeProtobuf() σγϦΞϥΠζ let library = try MyLibrary(protobuf: data)

Slide 36

Slide 36 text

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)

Slide 37

Slide 37 text

JSONͱͷڞଘ

Slide 38

Slide 38 text

JSON request let url = URL(string: "http://localhost:8080/")! var request = URLRequest(url: url) let value = "application/json" request.setValue(value, forHTTPHeaderField: "Accept")

Slide 39

Slide 39 text

protobuf request let url = URL(string: "http://localhost:8080/")! var request = URLRequest(url: url) let value = "application/protobuf" request.setValue(value, forHTTPHeaderField: "Accept")

Slide 40

Slide 40 text

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()) }

Slide 41

Slide 41 text

Tips

Slide 42

Slide 42 text

protobufͷϑΟʔϧυͷมߋʹ͍ͭͯ • ϑΟʔϧυͷ௥Ճ͸Մೳ • ϑΟʔϧυ໊ͷมߋ͚ͩͳΒಈ͘ • ϑΟʔϧυܕ·ͰมΘΔ৔߹͸ΤϥʔͱͳΔ

Slide 43

Slide 43 text

λά൪߸ʹ͍ͭͯ • λά൪߸ͰϑΟʔϧυΛࣝผ͢ΔͷͰॏෳແ͘ࢦఆ͢Δ͜ͱ • ޙͰมߋ͢Δͷ͸NG

Slide 44

Slide 44 text

Int64ͷѻ͍ʹ͍ͭͯ • jsonʹγϦΞϥΠζͨ͠৔߹ͷৼΔ෣͍ • ྫ͑͹goͩͱ਺஋ͰฦΔ͕swiftͩͱจࣈྻͰฦΔ

Slide 45

Slide 45 text

protobufͷόʔδϣϯʹ͍ͭͯ • ར༻͢ΔprotocϓϥάΠϯ͕αϙʔτ͢Δprotobufͷόʔδ ϣϯͱɺ.proto͔ΒίʔυΛੜ੒͢Δࡍͷprotobufͷόʔδ ϣϯΛಉ͡ʹ͢Δ͜ͱ ࢀߟʣHomebrewͰچόʔδϣϯͷύοέʔδΛΠϯετʔ ϧ͍ͨ͠

Slide 46

Slide 46 text

ͲͷϓϥάΠϯΛར༻͢Δ͔ • apple/swift-protobuf ͸ struct • alexeyxo/protobuf-swift ͸ class

Slide 47

Slide 47 text

ੜ੒ϑΝΠϧͷهड़ྔ apple/swift-protobuf < alexeyxo/protobuf-swift

Slide 48

Slide 48 text

Swift Protobuf ͷ஫ҙ఺ • ίʔυޓ׵ੑ͕ແ͘ͳΔΑ͏ͳੵۃతͳվม͕ߦΘΕ͍ͯΔ • ϝδϟʔόʔδϣϯ͸·ͩ 0

Slide 49

Slide 49 text

σϞ https://github.com/KyoheiG3/ProtobufExample

Slide 50

Slide 50 text

Thanks!