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

ca.swift.pdf

Kyohei Ito
December 01, 2016
1.9k

 ca.swift.pdf

Kyohei Ito

December 01, 2016
Tweet

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  5. protobufͷ͓͞Β͍

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  13. 1. ϓϩδΣΫτଆͰϏϧυ؀ڥΛߏங
    Swiftͩͱ2ͭ
    https://github.com/apple/swift-protobuf
    https://github.com/alexeyxo/protobuf-swift

    View full-size slide

  14. 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 keys = 4;
    }

    View full-size slide

  15. 3. .protoϑΝΠϧΛίϯύΠϧ

    View full-size slide

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

    View full-size slide

  17. ͦͷ··ΦϒδΣΫτͱͯ͠ར༻Մೳ
    • protobuf
    message MyLibrary {
    int64 id = 1;
    string name = 2;
    repeated BookInfo books = 3;
    map keys = 4;
    }
    • Swift
    public struct MyLibrary {
    public var id: Int64 = 0
    public var name: String = ""
    public var books: [BookInfo] = []
    public var keys: Dictionary = [:]
    }

    View full-size slide

  18. ஫ҙ఺
    • ࡞ͬͨϑΝΠϧ͸ฤू͠ͳ͍
    • ಠࣗͷmodel͕࢖͍͍ͨ৔߹͸Ϛοϐϯά͕ඞཁ
    • ωʔϜεϖʔε͸packageͰղܾ͢Δ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  24. protobufͰղܾ͠·͠ΐ͏

    View full-size slide

  25. ໰୊Ͱ໰୊ղܾ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  33. ϦΫΤετͷ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())
    }

    View full-size slide

  34. protobufͰϦΫΤετ͢Δ
    • ϦΫΤετʹAcceptϔομΛຒΊࠐΉ
    • ϨεϙϯεϔομΛݟͯjsonͱprotobufͷ੾Γସ͑

    View full-size slide

  35. ϨεϙϯεϔομΛݟͯ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!)
    }

    View full-size slide

  36. stab͸JSONσγϦΞϥΠζ
    let json = String(bytes: data, encoding: .utf8)
    return try MyLibrary(json: json!)

    View full-size slide

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

    View full-size slide

  38. ͦͷଞ΍Δ͜ͱ
    • .protoΛόʔδϣϯ؅ཧ
    • .protoͷυΩϡϝϯτԽ
    • .swiftͷframeworkԽ

    View full-size slide

  39. .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ʹ߹Θͤͯͩ͘͞
    ͍ɻ

    View full-size slide

  40. .protoͷυΩϡϝϯτԽ
    $ protoc --doc_out=markdown,../docs.md:. *.proto
    $ protoc --doc_out=html,../index.html:. *.proto

    View full-size slide

  41. .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

    View full-size slide

  42. .swiftΛframeworkԽ͢Δ
    pod 'Protobuf', :path => '../protos'

    View full-size slide

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

    View full-size slide