Skip to content

Commit c63e5b3

Browse files
committed
Translate kotlin-support.rst
1 parent 5171b5f commit c63e5b3

File tree

1 file changed

+80
-62
lines changed

1 file changed

+80
-62
lines changed

docs/kotlin-support.rst

Lines changed: 80 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,142 @@
1-
==================
2-
Kotlin サポート
3-
==================
1+
==============
2+
Kotlin support
3+
==============
44

5-
.. contents:: 目次
5+
.. contents::
66
:depth: 3
77

8-
Doma `Kotlin <https://kotlinlang.org/>`_ 1.1.2を実験的にサポートしています。
8+
Doma supports `Kotlin <https://kotlinlang.org/>`_ 1.3.11 or above **experimentally**.
99

10-
Kotlin利用のベストプラクティス
11-
================================
10+
Best practices
11+
==============
1212

13-
クラスの定義やビルドに関する事柄について推奨する方法を記載します。
13+
We show you recommended ways to define classes and build them with Kotlin.
1414

15-
エンティティクラス
16-
-------------------
15+
Entity classes
16+
--------------
1717

18-
* Data Classで定義する
19-
* イミュータブルで定義する( `@Entity` の `immutable` 要素に `true` を設定する)
20-
* コンストラクタは1つだけ定義する
21-
* コンストラクタ以外でプロパティを定義しない
22-
* コンストラクタで定義するプロパティには `val` を使用する
18+
* Define as a data class
19+
* Specify ``true`` to the ``immutable`` element of ``@Entity``
20+
* Define only one constructor
21+
* Define properties only in the constructor
22+
* Use `val` for the property definitions
2323

2424
.. code-block:: java
2525
2626
@Entity(immutable = true)
2727
data class Person(
2828
@Id
29-
@GeneratedValue(strategy = org.seasar.doma.GenerationType.IDENTITY)
29+
@GeneratedValue(strategy = GenerationType.IDENTITY)
3030
val id: Int? = null,
3131
val name: Name,
3232
val address: Address)
3333
34-
ドメインクラス
35-
-------------------
34+
Domain classes
35+
--------------
3636
37-
* Data Classで定義する
38-
* コンストラクタは1つだけ定義する
39-
* コンストラクタで定義するプロパティの名前は `value` にする
40-
* コンストラクタで定義するプロパティには `val` を使用する
37+
* Define as a data class
38+
* Define only one constructor
39+
* Define only one property whose name is ``value`` in the constructor
40+
* Use `val` for the property definition
4141
4242
.. code-block:: java
4343
4444
@Domain(valueType = String::class)
4545
data class Name(val value: String)
4646
47-
エンベッダブルクラス
48-
----------------------
47+
Embeddable classes
48+
------------------
4949
50-
* Data Classで定義する
51-
* コンストラクタは1つだけ定義する
52-
* コンストラクタ以外でプロパティを定義しない
53-
* コンストラクタで定義するプロパティには `val` を使用する
50+
* Define as a data class
51+
* Define only one constructor
52+
* Define properties only in the constructor
53+
* Use `val` for the property definitions
5454
5555
.. code-block:: java
5656
5757
@Embeddable
5858
data class Address(val city: String, val street: String)
5959
60-
Daoインタフェース
61-
-------------------
60+
Dao interfaces
61+
--------------
6262
63-
* KotlinではなくJavaで定義する
64-
* 更新処理の戻り値の型は `org.seasar.doma.jdbc.Result` や `org.seasar.doma.jdbc.BatchResult` を使う
63+
* Specify a SQL template to ``@org.seasar.doma.experimental.Sql``
64+
* Use ``org.seasar.doma.jdbc.Result`` as the return type of ``@Delete``, ``@Insert`` and ``@Update``
65+
* Use ``org.seasar.doma.jdbc.BatchResult`` as the return type of
66+
``@BatchDelete``, ``@BatchInsert`` and ``@BatchUpdate``
6567
6668
.. code-block:: java
6769
68-
@Dao(config = AppConfig.class)
69-
public interface PersonDao {
70+
@Dao(config = AppConfig::class)
71+
interface PersonDao {
72+
@Sql("""
73+
select * from person where id = /*id*/0
74+
""")
7075
@Select
71-
Person selectById(Integer id);
76+
fun selectById(id: Int): Person
77+
7278
@Insert
7379
Result<Person> insert(Person person);
7480
}
7581
76-
* 更新処理の戻り値を扱う際は `Destructuring Declarations <https://kotlinlang.org/docs/reference/multi-declarations.html>`_ を使う
82+
* Use `Destructuring Declarations <https://kotlinlang.org/docs/reference/multi-declarations.html>`_
83+
for ``org.seasar.doma.jdbc.Result`` and ``org.seasar.doma.jdbc.BatchResult``
7784

7885
.. code-block:: java
7986
8087
val dao: PersonDao = ...
8188
val person = Person(name = Name("Jhon"), address = Address(city = "Tokyo", street = "Yaesu"))
8289
val (newPerson, count) = dao.insert(person)
8390
91+
Using kapt in Gradle
92+
--------------------
8493

85-
kaptによるビルド
86-
-------------------
87-
88-
Kotlinで記述されたクラスやインタフェースに対して注釈処理をするには `kapt <https://blog.jetbrains.com/kotlin/2016/12/kotlin-1-0-6-is-here/>`_ を実行する必要があります。
89-
kaptは実験的な位置付けにありドキュメントがありません。
90-
Gradleでビルドする際は、確実な注釈処理が行われるように常に `clean build` を実行することを推奨します。
94+
Annotation processors are supported in Kotlin with the
95+
`kapt <https://kotlinlang.org/docs/reference/kapt.html>`_ compiler plugin.
9196

92-
.. code-block:: sh
97+
Add the dependencies using the `kapt` and `implementation` configuration in your dependencies block:
9398

94-
./gradlew clean build
99+
.. code-block:: groovy
95100
96-
Eclispeを利用する場合設定を適切に行えばJavaの注釈処理は自動で行われますが、kapt(Kotlinの注釈処理)はGradleを実行しない限り行われないことに注意してください。
101+
dependencies {
102+
kapt "org.seasar.doma:doma:2.21.1-SNAPSHOT"
103+
implementation "org.seasar.doma:doma:2.21.1-SNAPSHOT"
104+
}
97105
98-
下記はbuild.gradleの抜粋です。コンパイル時にSQLファイルを参照するために下記の設定に特に注意してください。
106+
If you use resource files such as SQL files, make the kapt find them:
99107

100108
.. code-block:: groovy
101109
102-
// コンパイルより前にSQLファイルを出力先ディレクトリにコピーするために依存関係を逆転する
103-
compileJava.dependsOn processResources
104-
105-
// SQLファイルなどリソースファイルの出力先ディレクトリをkaptに伝える
106-
kapt {
107-
arguments {
108-
arg("doma.resources.dir", processResources.destinationDir)
109-
}
110-
}
110+
kapt {
111+
arguments {
112+
arg("doma.resources.dir", compileKotlin.destinationDir)
113+
}
114+
}
115+
116+
task copyDomaResources(type: Sync) {
117+
from sourceSets.main.resources.srcDirs
118+
into compileKotlin.destinationDir
119+
include 'doma.compile.config'
120+
include 'META-INF/**/*.sql'
121+
include 'META-INF/**/*.script'
122+
}
123+
124+
compileKotlin {
125+
dependsOn copyDomaResources
126+
}
127+
111128
129+
.. note::
112130

113-
JavaとKotlinの混在
114-
-------------------------
131+
Remember that you always have options as follows:
115132

116-
kaptの不確実な挙動を避けるため、Domaに関するコードの全てをJavaで書くことは検討に値します。
117-
Domaの利用において、JavaとKotlinの混在は問題ありません。
133+
- Write all codes with Kotlin
134+
- Write all codes with Java
135+
- Write codes annotated with Doma's annotations in Java and others in Kotlin
118136

119-
サンプルプロジェクト
120-
=====================
137+
The third option is worth considering, because it can avoid some troubles with the kapt.
121138

122-
サンプルコードについては下記のプロジェクトを参照ください。
139+
Sample project
140+
==============
123141

124142
* `kotlin-sample <https://github.com/domaframework/kotlin-sample>`_

0 commit comments

Comments
 (0)