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

Commit 4788715

Browse files
committed
Deserialization from multiple arguments
1 parent c945d51 commit 4788715

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,60 @@ data class Dst(
355355
)
356356
```
357357

358+
#### Deserialization from multiple arguments
359+
If `InnerDst` requires multiple arguments, you can not map `Dst` with `KRwoMapper` as it is.
360+
You can deserialize such a class which requires multiple arguments by using the `KParameterFlatten` annotation.
361+
362+
```kotlin
363+
data class InnerDst(val fooFoo: Int, val barBar: String)
364+
data class Dst(val bazBaz: InnerDst, val quxQux: LocalDateTime)
365+
```
366+
367+
If the column name of `DB` is `snake_case` and you want to specify the argument name as a prefix, the following annotation is added.
368+
The class specified with `KParameterFlatten` is initialized from the function specified with the aforementioned `KConstructor` annotation or the `primary constructor`.
369+
370+
```kotlin
371+
data class InnerDst(val fooFoo: Int, val barBar: String)
372+
data class Dst(
373+
@KParameterFlatten(nameJoiner = NameJoiner.Snake::class)
374+
val bazBaz: InnerDst,
375+
val quxQux: LocalDateTime
376+
)
377+
378+
// required 3 arguments that baz_baz_foo_foo, baz_baz_bar_bar, qux_qux
379+
val mapper: KRowMapper<Dst> = KRowMapper(::Dst) { /* some naming transformation process */ }
380+
```
381+
382+
##### Annotation options of KParameterFlatten
383+
The `KParameterFlatten` annotation has two options for handling argument names.
384+
385+
###### fieldNameToPrefix
386+
By default, the `KParameterFlatten` annotation tries to find a match by prefixing the name of the argument with the name of the prefix.
387+
If you don't want to prefix the argument names, you can set the `fieldNameToPrefix` option to `false`.
388+
389+
```kotlin
390+
data class InnerDst(val fooFoo: Int, val barBar: String)
391+
data class Dst(
392+
@KParameterFlatten(fieldNameToPrefix = false)
393+
val bazBaz: InnerDst,
394+
val quxQux: LocalDateTime
395+
)
396+
397+
// required 3 arguments that foo_foo, bar_bar, qux_qux
398+
val mapper: KRowMapper<Dst> = KRowMapper(::Dst) { /* some naming transformation process */ }
399+
```
400+
401+
If `fieldNameToPrefix = false` is specified, the `nameJoiner` option is ignored.
402+
403+
##### nameJoiner
404+
The `nameJoiner` specifies how to join argument names to argument names.
405+
By default, `camelCase` is specified, and `snake_case` and `kebab-case` are also supported.
406+
You can also write your own by creating `object` which extends the `NameJoiner` class.
407+
408+
##### Use with other deserialization methods
409+
The `KParameterFlatten` annotation also works with all the deserialization methods we have introduced so far.
410+
Also, you can use further `KParameterFlatten` annotations in `InnerDst`.
411+
358412
### Use default arguments
359413
`KRowMapper` supports `default arguments`.
360414
`Default arguments` are available in the following situations:

0 commit comments

Comments
 (0)