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

swagger-codegen から眺める Swift4

d_date
November 21, 2017

swagger-codegen から眺める Swift4

2017/11/21(火) 関西モバイルアプリ研究会 in 関東
2017/11/28(火) potatotips #45

d_date

November 21, 2017
Tweet

More Decks by d_date

Other Decks in Programming

Transcript

  1. Swagger-codegen͔Β
    ோΊΔSwift4
    Daiki Matsudate / @d_date
    2017/11/28 potatotips

    View full-size slide

  2. Daiki Matsudate
    iOS Mobile App Engineer
    @d_date

    View full-size slide

  3. https://firebase.asia/

    View full-size slide

  4. ͓஌Βͤᶄ

    View full-size slide

  5. Tokyo
    2018/3/1 - 3

    View full-size slide

  6. ͓஌Βͤᶅ

    View full-size slide

  7. ͓஌Βͤᶆ

    View full-size slide

  8. ͓஌Βͤᶇ

    View full-size slide

  9. ͔͜͜Βຊฤ

    View full-size slide

  10. • APIͷ࢓༷ΛYAMLͰ؅ཧ (OpenAPI2.0)

    • αʔόʔαΠυ / ΫϥΠΞϯταΠυͷίʔυੜ੒͕Մೳ

    • Mock΋ͨͯΒΕΔ

    View full-size slide

  11. https://github.com/swagger-api/
    swagger-codegen

    View full-size slide

  12. SwaggerͰ࢝ΊΔAPIఆٛ؅ཧͱ
    ίʔυδΣωϨʔτ
    ɹ
    iOSDC 2017
    2017/09/15@ૣҴాେֶཧ޻ֶ෦੢ૣҴాΩϟϯύε 63߸ؗ
    Yohei Suginami ( @susieyy )
    https://speakerdeck.com/susieyy/swaggerdeshi-meruapiding-yi-guan-li-tokodozienereto

    View full-size slide

  13. swagger-api / swagger-codegen
    swagger.yaml
    swagger-codegen -l swift4 -i swagger.yaml

    View full-size slide

  14. Swagger Automation
    API.framework
    carthage build —-no-skip-current
    carthage archive

    View full-size slide

  15. Swagger Codegen Swi!ͷରԠঢ়گ
    — Swift4ʹઈࢍରԠத
    — JSONͷύʔε͸CodableͰ࣮૷
    — Optionalͷఆٛʹ΋ରԠ
    — EnumରԠ
    — API Client෦෼͸Alamofire͕ϕʔε
    — CocoaPodsͷϥΠϒϥϦͱͯ͠ग़ྗͰ͖Δ
    — RxSwiftͱ΋࿈ܞՄೳʢ Ϩεϙϯε͕ObservableʹͳΔ ʣ
    https://speakerdeck.com/susieyy/swaggerdeshi-meruapiding-yi-guan-li-tokodozienereto?slide=17

    View full-size slide

  16. Swagger Codegen Swi!ͷରԠঢ়گ
    — Swift4ʹઈࢍରԠத
    — JSONͷύʔε͸CodableͰ࣮૷
    — Optionalͷఆٛʹ΋ରԠ
    — EnumରԠ
    — API Client෦෼͸Alamofire͕ϕʔε
    — CocoaPodsͷϥΠϒϥϦͱͯ͠ग़ྗͰ͖Δ
    — RxSwiftͱ΋࿈ܞՄೳʢ Ϩεϙϯε͕ObservableʹͳΔ ʣ
    https://speakerdeck.com/susieyy/swaggerdeshi-meruapiding-yi-guan-li-tokodozienereto?slide=17

    View full-size slide

  17. Swift4ʹઈࢍରԠத

    View full-size slide

  18. Swift4ʹઈࢍରԠத
    master͕յΕ͍ͯΔ

    View full-size slide

  19. Swift4ʹઈࢍରԠத…
    • Swift4ͳͷʹRxSwiftͷόʔδϣϯ͕3.xͰίϯύΠϧΤϥʔ
    • allOf: λάʹهࡌͨ͠$ref͕ܧঝؔ܎ʹͳΔͷʹinitͰ
    super.init(coder:)ΛݺΜͰ͍ͳ͍ͷͰίϯύΠϧΤϥʔ

    • ۭͷλϓϧͷมߋʹ௥ै͍ͯ͠ͳ͍ͷͰɺon(.next())ͰίϯύΠϧΤϥʔ
    • enumΛStringͰσίʔυ͠Α͏ͱͯ͠ίϯύΠϧΤϥʔ
    • CodableΛҾ਺ʹͱΔϝιουʹAnyΛ౉ͦ͏ͱͯ͠ίϯύΠϧΤϥʔ

    View full-size slide

  20. ࣗ࡞ͷmastachςϯϓϨʔτΛ࡞Δ

    View full-size slide

  21. ΦϨΦϨYAML Parser & GeneratorΛ࡞Δ

    View full-size slide

  22. https://github.com/hiragram/
    Swako
    ΦϨΦϨYAML Parser & GeneratorΛ࡞Δ

    View full-size slide


  23. PRΛૹΖ͏ʂʂ

    View full-size slide

  24. ࣗྗͰ௚͢

    View full-size slide

  25. Swift4ʹઈࢍରԠத…
    • Swift4ͳͷʹRxSwiftͷόʔδϣϯ͕3.xͰίϯύΠϧΤϥʔ
    • allOf: λάʹهࡌͨ͠$ref͕ܧঝؔ܎ʹͳΔͷʹinitͰ
    super.init(coder:)ΛݺΜͰ͍ͳ͍ͷͰίϯύΠϧΤϥʔ

    • ۭͷλϓϧͷมߋʹ௥ै͍ͯ͠ͳ͍ͷͰɺon(.next())ͰίϯύΠϧΤϥʔ
    • enumΛStringͰσίʔυ͠Α͏ͱͯ͠ίϯύΠϧΤϥʔ
    • CodableΛҾ਺ʹͱΔϝιουʹAnyΛ౉ͦ͏ͱͯ͠ίϯύΠϧΤϥʔ

    View full-size slide

  26. RxSwift in Swift 4
    • ʮRxSwift4Λνϥݟ͢Δʯ@tarunon
    https://www.icloud.com/keynote/
    0lglRaoECYvXtcY3bkhRC1C7Q#OverviewRxSwift4

    View full-size slide

  27. on(.next())ͰίϯύΠϧΤϥʔʁ
    ObserverʹVoidͷΠϕϯτΛ౉ͦ͏ͱ͢Δɻ
    open class func updatePet(body: Pet) -> Observable {
    return Observable.create { observer -> Disposable in
    updatePet(body: body) { error in
    if let error = error {
    observer.on(.error(error))
    } else {
    observer.on(.next())
    }
    observer.on(.completed)
    }
    return Disposables.create()
    }
    }

    View full-size slide

  28. on(.next())ͰίϯύΠϧΤϥʔʁ
    ObserverʹVoidͷΠϕϯτΛ౉ͦ͏ͱ͢Δɻ
    open class func updatePet(body: Pet) -> Observable {
    return Observable.create { observer -> Disposable in
    updatePet(body: body) { error in
    if let error = error {
    observer.on(.error(error))
    } else {
    observer.on(.next())
    }
    observer.on(.completed)
    }
    return Disposables.create()
    }
    }
    Missing argument for parameter #1 in call

    View full-size slide

  29. on(.next())ͰίϯύΠϧΤϥʔʁ
    ObserverʹVoidͷΠϕϯτΛ౉ͦ͏ͱ͢Δɻ
    open class func updatePet(body: Pet) -> Observable {
    return Observable.create { observer -> Disposable in
    updatePet(body: body) { error in
    if let error = error {
    observer.on(.error(error))
    } else {
    observer.on(.next(()))
    }
    observer.on(.completed)
    }
    return Disposables.create()
    }
    }

    View full-size slide

  30. Swift4Ͱλϓϧͱɺෳ਺Ҿ਺ؔ਺Λ۠ผ͢ΔΑ͏ʹͳͬͨ
    f ͸ f(()) ͱॻ͘

    View full-size slide

  31. classܧঝͯ͠ΔͷʹɺsuperݺΜͰͳ͍ʁ
    open class Cat: Animal {
    public var declawed: Bool?
    // Decodable protocol methods
    public override required init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: String.self)
    declawed = try container.decodeIfPresent(Bool.self, forKey: "declawed")
    }
    }
    superݺΜͰͳ͍

    View full-size slide

  32. classܧঝͯ͠ΔͷʹɺsuperݺΜͰͳ͍ʁ
    open class Cat: Animal {
    public var declawed: Bool?
    // Decodable protocol methods
    public required init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: String.self)
    declawed = try container.decodeIfPresent(Bool.self, forKey: "declawed")
    try super.init(from: decoder)
    }
    }

    View full-size slide

  33. ͦ΋ͦ΋ɺͳΜͰܧঝؔ܎͕ൃੜ
    ͢ΔΜͰ͔͢ʁ

    View full-size slide

  34. Cat:
    allOf:
    - $ref: '#/definitions/Animal'
    - type: object
    properties:
    declawed:
    type: boolean
    Animal:
    type: object
    discriminator: className
    required:
    - className
    properties:
    className:
    type: string
    color:
    type: string
    default: 'red'
    = Animal + declawed
    open class Cat: Animal {
    public var declawed: Bool?
    }

    View full-size slide

  35. ΫϥεΛܧঝͨ͠ΒͲ͏͔ʁ
    encode / decode Λ࣮૷
    ܧঝ͸ͻͱͭͷΫϥε·Ͱ
    superclass ͷ ม਺͸ॻ͔ͳͯ͘ྑ͍

    View full-size slide

  36. ΫϥεΛܧঝͨ͠ΒͲ͏͔ʁ
    encode / decode Λ࣮૷ → ίʔυੜ੒ͰͰ͖Δ
    ܧঝ͸ͻͱͭͷΫϥε·Ͱ
    superclass ͷ ม਺͸ॻ͔ͳͯ͘ྑ͍

    View full-size slide

  37. protocolΛద߹ͨ͠ΒͲ͏͔ʁ
    ద߹͢Δprotocolͷม਺΋ॻ͔ͳ͚Ε͹͍͚ͳ͍
    encode / decodeॻ͔ͳͯ͘ྑ͍
    structͰॻ͚Δ

    View full-size slide

  38. ίʔυੜ੒ʹ͓͚ΔϕετϓϥΫςΟε
    Կ΋ܧঝ͠ͳ͍

    View full-size slide

  39. ίʔυੜ੒ʹ͓͚ΔϕετϓϥΫςΟε
    ͦ΋ͦ΋BMM0G಺ͷSFG͸औಘͯ͠ɺଞͷม਺ͱDPODBUͯ͘͠
    ΕΕ͹͍͍Μ͡Όͳ͍͔ͳ͊

    View full-size slide

  40. https://github.com/swagger-api/
    swagger-codegen/issues/6941
    classΛstructʹ͍ͨ͠

    View full-size slide

  41. Swift4ʹઈࢍରԠத
    • Swift4 ͳͷʹ RxSwift ͷόʔδϣϯ͕ 3.x ͰίϯύΠϧΤϥʔ
    • allOf λάʹهࡌͨ͠ ref ͕ܧঝؔ܎ʹͳΔͷʹinitͰsuper.init(coder:)Λݺ
    ΜͰ͍ͳ͍ͷͰίϯύΠϧΤϥʔ

    • ۭͷλϓϧͷมߋʹ௥ै͍ͯ͠ͳ͍ͷͰɺonNext()ͰίϯύΠϧΤϥʔ
    • enumΛStringͰσίʔυ͠Α͏ͱͯ͠ίϯύΠϧΤϥʔ
    • CodableΛҾ਺ʹͱΔϝιουʹAnyΛ౉ͦ͏ͱͯ͠ίϯύΠϧΤϥʔ

    View full-size slide

  42. (ݩ͔Β) sample΋յΕͯΔ͔Β௚͠ͱ͍ͯ

    View full-size slide

  43. (ݩ͔Β) ςετ௨Βͳ͍Μ͚ͩͲ…

    View full-size slide

  44. Ώͬ͘ΓٳΜͰ…

    View full-size slide

  45. Swift4ʹઈࢍରԠத
    • Swift4ͳͷʹRxSwift, Alamofireͷόʔδϣϯ͕3.xͰίϯύΠϧΤϥʔ
    • allOf λάʹهࡌͨ͠ref ͕ܧঝؔ܎ʹͳΔͷʹinitͰsuper.init(coder:)Λݺ
    ΜͰ͍ͳ͍ͷͰίϯύΠϧΤϥʔ

    • ۭͷλϓϧͷมߋʹ௥ै͍ͯ͠ͳ͍ͷͰɺonNext()ͰίϯύΠϧΤϥʔ
    • enumΛStringͰσίʔυ͠Α͏ͱͯ͠ίϯύΠϧΤϥʔ
    • CodableΛҾ਺ʹͱΔϝιουʹAnyΛ౉ͦ͏ͱͯ͠ίϯύΠϧΤϥʔ
    શ෦௚͠·ͨ͠

    View full-size slide

  46. https://github.com/swagger-api/
    swagger-codegen/pull/6921

    View full-size slide

  47. Merge͞Εͨ͋ͱϝʔϧ͕དྷͨ

    View full-size slide

  48. https://jknack.github.io/
    handlebars.java/
    Handling OpenAPI 3.0 with new engine

    View full-size slide

  49. Recap
    • swagger-codegen ͷ Swift4 ରԠ͠·ͨ͠

    • खऔΓૣ͘૲Λੜ΍͍ͨ͠ਓʹΦεεϝ

    • Codable ͷܧঝؔ܎ʹ͍ͭͯ͸ɺ࠙਌ձͰ

    View full-size slide