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

ca.swift.pdf

4d40d82cc3c676e8a67ffc2a473bf423?s=47 Kyohei Ito
December 01, 2016
1.8k

 ca.swift.pdf

4d40d82cc3c676e8a67ffc2a473bf423?s=128

Kyohei Ito

December 01, 2016
Tweet

Transcript

  1. ͜Ε͔Β࢝ΊΔ Protocol Buffers ӡ༻ 2016/12/1 CA.swift

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

  3. None
  4. protobufΛ࢖͍࢝ΊͨྲྀΕ • gRPCΛ࢖͓͏ͱ͍ͯͨ͠ • Obj-CͷελςΟοΫϥΠϒϥϦͷಋೖ͕ͭΒ͔ͬͨ • Android΋ͳΜ͔ͭΒͦ͏ͩͬͨ • ͳΜ͔৭ʑ͔ͭΒͬͨʢΑ֮͑ͯ͘ͳ͍

  5. gRPC͸΍Ί͚ͨͲprotobuf͸ͦͷ··࢖ͬͨ

  6. protobufͷ͓͞Β͍

  7. protobufͷಛ௃ • ࣄલʹߏ଄Λఆٛ͢Δ • όΠφϦσʔλΛ௚઀ѻ͏

  8. protobufͷػೳ • Ϋϥεఆٛ • όΠφϦσʔλͷγϦΞϥΠζ/σγϦΞϥΠζ • JSONσʔλͷγϦΞϥΠζ/σγϦΞϥΠζ

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

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

  11. σϝϦοτΛϝϦοτʹ ม͍͖͑ͯ·͠ΐ͏ !

  12. ಋೖͷ؆୯ͳྲྀΕ 1. ϓϩδΣΫτଆͰϏϧυ؀ڥΛߏங 2. .protoϑΝΠϧΛ࡞੒ 3. .protoϑΝΠϧΛίϯύΠϧ 4. ग़དྷ্͕ͬͨϑΝΠϧΛಋೖ

  13. 1. ϓϩδΣΫτଆͰϏϧυ؀ڥΛߏங • protocϓϥάΠϯͷબఆ • ؀ڥ෼༻ҙ(Android, iOS, web, api, etc..)

    • protocϓϥάΠϯͷϥΠϒϥϦΛಋೖ
  14. 1. ϓϩδΣΫτଆͰϏϧυ؀ڥΛߏங Swiftͩͱ2ͭ https://github.com/apple/swift-protobuf https://github.com/alexeyxo/protobuf-swift

  15. 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; }
  16. 3. .protoϑΝΠϧΛίϯύΠϧ

  17. 4. ग़དྷ্͕ͬͨϑΝΠϧΛಋೖ iOSͳΒϓϩδΣΫτʹϑΝΠϧΛಥͬࠐΉ͚ͩ

  18. ͦͷ··ΦϒδΣΫτͱͯ͠ར༻Մೳ • 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> = [:] }
  19. ஫ҙ఺ • ࡞ͬͨϑΝΠϧ͸ฤू͠ͳ͍ • ಠࣗͷmodel͕࢖͍͍ͨ৔߹͸Ϛοϐϯά͕ඞཁ • ωʔϜεϖʔε͸packageͰղܾ͢Δ

  20. σϝϦοτΛղܾ͢Δ

  21. JSONͳͲͱൺֱͯ͠൥Θ͍͠ͱࢥΘΕΔ໰୊఺Ұཡ • ࣄલఆ͕ٛඞཁ໰୊ • มߋͷ౓ʹίϯύΠϧ͕ඞཁ໰୊ • ελϒͱ͔࡞Δͷ໘౗ͳΜͰ͠ΐʁ໰୊ • όΠφϦ͸ਓؒʹ͸ཧղͮ͠Β͍໰୊

  22. ͔֬ʹ...Ͱ΋JSONʹ΋໰୊͸৭ʑ͋Γͦ͏...

  23. JSONͷख͕ܰ͞΋ͨΒ͢؁͍᠘

  24. None
  25. None
  26. None
  27. None
  28. JSONͰؕΓ͕ͪͳࣦഊ • σʔλఆٛΛ࣮૷ͱ͸ผͷͱ͜ΖͰߦ͏ • υΩϡϝϯτͱ࣮σʔλ͕ͳΜ͔ҧ͏ • ͦ΋ͦ΋ఆ͕ٛ؅ཧ͞Ε͍ͯͳ͍ • มߋཤྺ͕௥͑ͳ͍ •

    มߋՕॴ͕௥͑ͳ͍
  29. protobufͰղܾ͠·͠ΐ͏

  30. None
  31. ໰୊Ͱ໰୊ղܾ

  32. ࣄલఆ͕ٛඞཁ໰୊վΊ ࣄલఆٛͰ໰୊ղܾ ೝ͕ࣝζϨͳ͍

  33. ࣄલఆ͕ٛඞཁ໰୊վΊ ࣄલఆٛͰ໰୊ղܾ มߋΛϨϏϡʔͰ͖Δ

  34. ࣄલఆ͕ٛඞཁ໰୊վΊ ࣄલఆٛͰ໰୊ղܾ υΩϡϝϯτΛग़ྗͰ͖Δ

  35. มߋͷ౓ʹίϯύΠϧ͕ඞཁ໰୊վΊ มߋ͕ඞཁͳ࣌ʹίϯύΠϧͯ͠໰୊ղܾ • ϑΟʔϧυͷ௥Ճ͸ແࢹͰ͖Δ • TagมߋͳͲͷഁյతมߋ͕ͳ͚Ε͹ߋ৽ඞਢͰ͸ͳ͍ • αʔόଆͰΩʔ໊͕มߋ͞Εͯ΋ࠓ·Ͱ௨Γར༻Մೳ ※JSONͷγϦΞϥΠζͳͲͷॲཧ్͕தʹೖΔ৔߹ɺΩʔ໊Λ มߋ͢Δͱσʔλ͕ܽམ͠·͢

  36. ελϒͱ͔࡞Δͷ໘౗ͳΜͰ͠ΐʁ໰୊͸ JSONσγϦΞϥΠζͰղܾ • JSONͰ༻ҙͨ͠σʔλ͔ΒσγϦΞϥΠζͰ͖Δ • protobufΛฦ͢ϞοΫΛ࡞Γ͍ͨͳΒσγϦΞϥΠζσʔλ ΛߋʹprotobufʹγϦΞϥΠζ • Himotoki࢖ͬͯΔʁҰ୴JSONʹγϦΞϥΠζ͠Α͏ʢବ

  37. όΠφϦ͸ਓؒʹ͸ཧղͮ͠Β͍໰୊͸ ௨৴σʔλͷ೉ಡԽͱׂΓ੾Δ • #DEBUGͷ࣌͸application/jsonͰड͚औΔͳͲ • ΦϒδΣΫτ͸ී௨ʹϩάग़ྗͰ͖Δ • ੜͷJSONจࣈྻΑΓσόοά͠қ͍

  38. ӡ༻

  39. APIͰprotobufΛฦ͢ • EntityΛprotobufͷ΋ͷʹ͢Δ • ϦΫΤετͷAcceptϔομͰ൑அ

  40. ϦΫΤετͷ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()) }
  41. protobufͰϦΫΤετ͢Δ • ϦΫΤετʹAcceptϔομΛຒΊࠐΉ • ϨεϙϯεϔομΛݟͯjsonͱprotobufͷ੾Γସ͑

  42. ϨεϙϯεϔομΛݟͯ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!) }
  43. stab͸JSONσγϦΞϥΠζ let json = String(bytes: data, encoding: .utf8) return try

    MyLibrary(json: json!)
  44. protobufΛฦ͢ϞοΫ let json = String(bytes: data, encoding: .utf8) let library

    = try MyLibrary(json: json!) return try library.serializeProtobuf()
  45. ͦͷଞ΍Δ͜ͱ • .protoΛόʔδϣϯ؅ཧ • .protoͷυΩϡϝϯτԽ • .swiftͷframeworkԽ

  46. .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ʹ߹Θͤͯͩ͘͞ ͍ɻ
  47. .protoͷυΩϡϝϯτԽ $ protoc --doc_out=markdown,../docs.md:. *.proto $ protoc --doc_out=html,../index.html:. *.proto

  48. .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" => "je.suis.kyohei@gmail.com" } s.ios.deployment_target = '8.0' s.source = { :path => '.' } s.source_files = "**/*.swift" s.requires_arc = true s.dependency 'SwiftProtobuf' end
  49. .swiftΛframeworkԽ͢Δ pod 'Protobuf', :path => '../protos'

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

  51. Thanks!