Upgrade to Pro — share decks privately, control downloads, hide ads and more …

protocol_buffers.pdf

4d40d82cc3c676e8a67ffc2a473bf423?s=47 Kyohei Ito
November 20, 2016
5.5k

 protocol_buffers.pdf

4d40d82cc3c676e8a67ffc2a473bf423?s=128

Kyohei Ito

November 20, 2016
Tweet

Transcript

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

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

  3. protobufʢུʣ

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

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

  6. "I guess Apple decided that JSON is a relic of

    the past along with Objective-C" - @cjwirth
  7. Apple͞ΜͷτϨϯυʹ৐͔ͬΓ·͠ΐ͏ʂ

  8. ֓ཁ

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

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

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

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

  13. .protoͰఆٛͰ͖Δجຊͷܕ3 int32 sint32 sfixed32 uint32 fixed32 int64 sint64 sfixed64 uint64

    fixed64 bool float double string bytes 3 apple/swift-protobufͷAPI OverviewΑΓൈਮ
  14. ಋೖ

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

  16. 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
  17. ࡞੒ͨ͠ϓϥάΠϯΛ࢖͑ΔΑ͏ʹ͢Δ ඞཁʹԠͯ͡೚ҙͷ৔ॴʹஔ͍ͯPATHΛ௨͢ $ mkdir ~/.protoc $ cp .build/debug/protoc-gen-swift ~/.protoc/protoc-gen-swift $

    echo 'export PATH=$PATH:$HOME/.protoc' >> ~/.bash_profile $ source ~/.bash_profile
  18. .protoͷ࡞੒

  19. 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
  20. syntax syntax = "proto3"; • proto3ͱهड़͠ͳ͍ͱproto2ͱΈͳ͞ΕΔ

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

  22. Field Types int64 id = 1; string title = 2;

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

    author = 3; • message಺ͰҰҙͷ਺஋ΛׂΓ౰ͯΔ • ॱং͸ߟྀ͠ͳͯ͘΋ྑ͍
  24. Field Rules repeated BookInfo books = 3; • repeatedͰ഑ྻΛఆٛͰ͖Δ

  25. ͦͷଞ • package • map • enum • etc.. ৄ͘͠͸Language

    GuideΛࢀর͍ͯͩ͘͠͞ɻ
  26. ΋͏Ұ౓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; }
  27. .protoͷίϯύΠϧ

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

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

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

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

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

  33. ࢖͍ํ

  34. ϥΠϒϥϦͷ௥Ճ • 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ͱΠϯετʔϧόʔδϣϯΛ߹ Θ͍ͤͯͩ͘͞ɻ
  35. Binary serializable γϦΞϥΠζ let library = MyLibrary() let data =

    try library.serializeProtobuf() σγϦΞϥΠζ let library = try MyLibrary(protobuf: data)
  36. 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)
  37. JSONͱͷڞଘ

  38. JSON request let url = URL(string: "http://localhost:8080/")! var request =

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

    URLRequest(url: url) let value = "application/protobuf" request.setValue(value, forHTTPHeaderField: "Accept")
  40. 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()) }
  41. Tips

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

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

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

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

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

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

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

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

  50. Thanks!