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

Kotlinでサーバサイドを始めよう!

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

 Kotlinでサーバサイドを始めよう!

Avatar for Taro Nagasawa

Taro Nagasawa

May 22, 2019
Tweet

More Decks by Taro Nagasawa

Other Decks in Programming

Transcript

  1. データクラス、プロパティ、コンストラクタ // Java public final class User { private final

    Long id; private final String name; public Person(Long id, String name) { this.id = id; this.name = name; } public Long getId() { return id; } public String getName() { return name; } @Override public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final User user = (User) o; if (!id.equals(user.id)) return false; return name.equals(user.name); } @Override public int hashCode() { int result = id.hashCode(); result = 31 * result + name.hashCode(); return result; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } public User withId(Long id) { return new User(id, name); } public User withName(String name) { return new User(id, name); } public static Builder { private Long id; private String name; public User build() { return new User(id, name); } public Builder id(Long id) { this.id = id; return this; } public Builder name(String name) { this.name = name; return this; } } } data class User( val id: Long, val name: String ) Java Kotlin
  2. Null安全 val a: Int = null // コンパイルエラー val b:

    Int? = null // OK b.toString() // コンパイルエラー s?.length if (s != null) s.length else null 同じ if-elseは式 (評価されて値になる) 厳しいけど、扱いやすい
  3. マルチプラットフォーム・プロジェクト Common Platform (JVM) Platform (JS) Regular (JVM) Regular (JS)

    矢印は依存の方向 // Common module expect fun hello() // JVM module actual fun hello() { System.out.println("Hello!") } // JS module actual fun hello() { console.log("Hello!") }
  4. アノテーションベースのいつものSpring @SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) }

    @Service class HelloWorldService { fun helloWorld(): String = "Hello, world!" } @RestController class HelloWorldController(val helloWorldService: HelloWorldService) { @GetMapping("/hello-world") fun helloWorld(): String = helloWorldService.helloWorld() }
  5. KotlinDSLによるDIとルーティング設定も fun main(args: Array<String>) { SpringApplicationBuilder() .sources(DemoApplication::class.java) .initializers(beans { bean

    { HelloWorldService() } bean { HelloWorldController(ref()) } bean { router { GET("/hello-world") { ref<HelloWorldController>().helloWorld() } } } }) .run(*args) } Bean 登録 routing 設定
  6. Kotlin x Spring Boot x GraphQL type Query { drugs(yjCode:

    String!) : [Drug!] } type Drug { yjCode: String! name: String! } @Component class DrugQueryResolver( val drugService: DrugService ): GraphQLQueryResolver { fun drugs(yjCode: String): List<Drug> { return drugService.getDrugs(yjCode) } } しらじさんによるサンプルコード https://github.com/ubie-inc/kotlin-graphql-sample
  7. Kotlin向けWebアプリフレームワーク Ktor • https://ktor.io • JetBrainsにより開発 • いわゆるマイクロ・フレームワーク • Kotlin

    DSLによるルーティング設定 • ノンブロッキング、コルーチン対応 fun main(args: Array<String>) { embeddedServer(Netty, 8080) { routing { get("/") { call.respondText("Hello, world!", ContentType.Text.Html) } } }.start(wait = true) }
  8. JUnit5 class FooTest { @Nested inner class fooMethod { @Test

    fun `should throw exception`() { assertThrows<MyException>() { Foo().foo() } } } }
  9. class FooTest { @Nested inner class fooMethod { @Test fun

    `should throw exception`() { assertThrows<MyException>() { Foo().foo() } } } } JUnit5 メソッドをグルーピングしてテストの見通しを良く
  10. JUnit5 class FooTest { @Nested inner class fooMethod { @Test

    fun `should throw exception`() { assertThrows<MyException>() { Foo().foo() } } } }
  11. MockK interface UserService { fun createUser(id: Long): User suspend fun

    findUser(id: Long): User? } val userService = mockk<UserService>() every { userService.createUser(1) } returns user coEvery { userService.findUser(1) } returns user
  12. モック生成は一度、都度リセット class DesignControllerTest { private val repo: DesignRepository = mockk()

    private val client: DesignClient = mockk() private val controller: DesignController(repo, client) @BeforeEach fun init() { clearMocks(repo, client) } } 参考 https://www.youtube.com/watch?v=RX_g65J14H0
  13. まとめ • Kotlin 書きやすく読みやすい! • カバーしている環境が多岐に渡る • 強力な後ろ盾があり、コミュニティもよさそう • Kotlinでも普通にSpringが使える

    • GraphQLも簡単にできそう • データクラスの内容をテストするときはデータクラスと比較する とレポートが読みやすい • モックライブラリはMockKがよさそう   サーバーサイドKotlin よい