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

swagger-codegen から眺める Swift4

2594ac7ce91fd7d9a3ce71ca7cc2d0c0?s=47 d_date
November 21, 2017

swagger-codegen から眺める Swift4

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

2594ac7ce91fd7d9a3ce71ca7cc2d0c0?s=128

d_date

November 21, 2017
Tweet

Transcript

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

  2. Daiki Matsudate iOS Mobile App Engineer @d_date

  3. None
  4. None
  5. None
  6. None
  7. None
  8. ͓஌Βͤ

  9. None
  10. https://firebase.asia/

  11. ͓஌Βͤᶄ

  12. Tokyo 2018/3/1 - 3

  13. ͓஌Βͤᶅ

  14. None
  15. ͓஌Βͤᶆ

  16. None
  17. ͓஌Βͤᶇ

  18. None
  19. None
  20. ͔͜͜Βຊฤ

  21. None
  22. • APIͷ࢓༷ΛYAMLͰ؅ཧ (OpenAPI2.0) • αʔόʔαΠυ / ΫϥΠΞϯταΠυͷίʔυੜ੒͕Մೳ • Mock΋ͨͯΒΕΔ

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

  24. SwaggerͰ࢝ΊΔAPIఆٛ؅ཧͱ ίʔυδΣωϨʔτ ɹ iOSDC 2017 2017/09/15@ૣҴాେֶཧ޻ֶ෦੢ૣҴాΩϟϯύε 63߸ؗ Yohei Suginami (

    @susieyy ) https://speakerdeck.com/susieyy/swaggerdeshi-meruapiding-yi-guan-li-tokodozienereto
  25. swagger-api / swagger-codegen swagger.yaml swagger-codegen -l swift4 -i swagger.yaml

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

  27. 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
  28. 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
  29. Swift4ʹઈࢍରԠத

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

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

    enumΛStringͰσίʔυ͠Α͏ͱͯ͠ίϯύΠϧΤϥʔ • CodableΛҾ਺ʹͱΔϝιουʹAnyΛ౉ͦ͏ͱͯ͠ίϯύΠϧΤϥʔ
  32. ࣗ࡞ͷmastachςϯϓϨʔτΛ࡞Δ

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

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

  35. None
  36. PRΛૹΖ͏ʂʂ

  37. ࣗྗͰ௚͢

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

    enumΛStringͰσίʔυ͠Α͏ͱͯ͠ίϯύΠϧΤϥʔ • CodableΛҾ਺ʹͱΔϝιουʹAnyΛ౉ͦ͏ͱͯ͠ίϯύΠϧΤϥʔ
  39. RxSwift in Swift 4 • ʮRxSwift4Λνϥݟ͢Δʯ@tarunon https://www.icloud.com/keynote/ 0lglRaoECYvXtcY3bkhRC1C7Q#OverviewRxSwift4

  40. on(.next())ͰίϯύΠϧΤϥʔʁ ObserverʹVoidͷΠϕϯτΛ౉ͦ͏ͱ͢Δɻ open class func updatePet(body: Pet) -> Observable<Void> {

    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() } }
  41. on(.next())ͰίϯύΠϧΤϥʔʁ ObserverʹVoidͷΠϕϯτΛ౉ͦ͏ͱ͢Δɻ open class func updatePet(body: Pet) -> Observable<Void> {

    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
  42. on(.next())ͰίϯύΠϧΤϥʔʁ ObserverʹVoidͷΠϕϯτΛ౉ͦ͏ͱ͢Δɻ open class func updatePet(body: Pet) -> Observable<Void> {

    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() } }
  43. None
  44. Swift4Ͱλϓϧͱɺෳ਺Ҿ਺ؔ਺Λ۠ผ͢ΔΑ͏ʹͳͬͨ f ͸ f(()) ͱॻ͘

  45. 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ݺΜͰͳ͍
  46. 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) } }
  47. ͦ΋ͦ΋ɺͳΜͰܧঝؔ܎͕ൃੜ ͢ΔΜͰ͔͢ʁ

  48. 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? }
  49. ΫϥεΛܧঝͨ͠ΒͲ͏͔ʁ encode / decode Λ࣮૷ ܧঝ͸ͻͱͭͷΫϥε·Ͱ superclass ͷ ม਺͸ॻ͔ͳͯ͘ྑ͍

  50. ΫϥεΛܧঝͨ͠ΒͲ͏͔ʁ encode / decode Λ࣮૷ → ίʔυੜ੒ͰͰ͖Δ ܧঝ͸ͻͱͭͷΫϥε·Ͱ superclass ͷ

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

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

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

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

  55. Swift4ʹઈࢍରԠத • Swift4 ͳͷʹ RxSwift ͷόʔδϣϯ͕ 3.x ͰίϯύΠϧΤϥʔ • allOf

    λάʹهࡌͨ͠ ref ͕ܧঝؔ܎ʹͳΔͷʹinitͰsuper.init(coder:)Λݺ ΜͰ͍ͳ͍ͷͰίϯύΠϧΤϥʔ • ۭͷλϓϧͷมߋʹ௥ै͍ͯ͠ͳ͍ͷͰɺonNext()ͰίϯύΠϧΤϥʔ • enumΛStringͰσίʔυ͠Α͏ͱͯ͠ίϯύΠϧΤϥʔ • CodableΛҾ਺ʹͱΔϝιουʹAnyΛ౉ͦ͏ͱͯ͠ίϯύΠϧΤϥʔ
  56. (ݩ͔Β) sample΋յΕͯΔ͔Β௚͠ͱ͍ͯ

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

  58. Ώͬ͘ΓٳΜͰ…

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

    ۭͷλϓϧͷมߋʹ௥ै͍ͯ͠ͳ͍ͷͰɺonNext()ͰίϯύΠϧΤϥʔ • enumΛStringͰσίʔυ͠Α͏ͱͯ͠ίϯύΠϧΤϥʔ • CodableΛҾ਺ʹͱΔϝιουʹAnyΛ౉ͦ͏ͱͯ͠ίϯύΠϧΤϥʔ શ෦௚͠·ͨ͠
  60. https://github.com/swagger-api/ swagger-codegen/pull/6921

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

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

  63. Recap • swagger-codegen ͷ Swift4 ରԠ͠·ͨ͠ • खऔΓૣ͘૲Λੜ΍͍ͨ͠ਓʹΦεεϝ • Codable

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