Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit e968236

Browse files
committed
Conversion of argument names
1 parent 634dc01 commit e968236

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,62 @@ data class Dst(...) {
160160
val mapper: KRowMapper<Dst> = KRowMapper(Dst::class)
161161
```
162162

163+
### Conversion of argument names
164+
By default, `KRowMapper` looks for the column corresponding to the argument name.
165+
166+
```kotlin
167+
data class Dst(
168+
fooFoo: String,
169+
barBar: String,
170+
bazBaz: Int?
171+
)
172+
173+
// required arguments: fooFoo, barBar, bazBaz
174+
val mapper: KRowMapper<Dst> = KRowMapper(::Dst)
175+
176+
// the behavior is equivalent to the following
177+
val rowMapper: RowMapper<Dst> = { rs, _ ->
178+
Dst(
179+
rs.getString("fooFoo"),
180+
rs.getString("barBar"),
181+
rs.getInt("bazBaz"),
182+
)
183+
}
184+
```
185+
186+
On the other hand, if the argument naming convention is a `camelCase` and the DB column naming convention is a `snake_case` You will not be able to see the match in this case.
187+
In this situation, you need to pass the naming transformation function at the initialization of `KRowMapper`.
188+
189+
```kotlin
190+
val mapper: KRowMapper<Dst> = KRowMapper(::Dst) { fieldName: String ->
191+
/* some naming transformation process */
192+
}
193+
```
194+
195+
#### The actual conversion process
196+
Since `KRowMapper` does not provide the naming transformation process, the naming transformation process requires an external library.
197+
198+
As an example, sample code that passes the conversion process from `camelCase` to `snake_case` is shown for two libraries, `Jackson` and `Guava`.
199+
These libraries are often used by `Spring framework` and other libraries.
200+
201+
##### Jackson
202+
```kotlin
203+
import com.fasterxml.jackson.databind.PropertyNamingStrategy
204+
205+
val parameterNameConverter: (String) -> String = PropertyNamingStrategy.SnakeCaseStrategy()::translate
206+
val mapper: KRowMapper<Dst> = KRowMapper(::Dst, parameterNameConverter)
207+
```
208+
209+
##### Guava
210+
```kotlin
211+
import com.google.common.base.CaseFormat
212+
213+
val parameterNameConverter: (String) -> String = { fieldName: String ->
214+
CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, fieldName)
215+
}
216+
val mapper: KRowMapper<Dst> = KRowMapper(::Dst, parameterNameConverter)
217+
```
218+
163219
## Usage
164220
### Deserialize column
165221
`KRowMapper` provides a deserialization function for the acquisition results of three patterns.

0 commit comments

Comments
 (0)